New creates a metrics WAL that remote writes its data. TODO the passed logger does not include any other context attribute Should we standarize slog and deprecate go-kit/log too?
(cfg *Config, o Overrides, tenant string, reg prometheus.Registerer, _ log.Logger)
| 58 | // TODO the passed logger does not include any other context attribute |
| 59 | // Should we standarize slog and deprecate go-kit/log too? |
| 60 | func New(cfg *Config, o Overrides, tenant string, reg prometheus.Registerer, _ log.Logger) (Storage, error) { |
| 61 | // TODO move this to the generator.go |
| 62 | |
| 63 | // Validate empty tenant to prevent WAL directory deletion only when org ID header is required |
| 64 | if tenant == "" { |
| 65 | return nil, errors.New("tenant cannot be empty") |
| 66 | } |
| 67 | |
| 68 | logger := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{ |
| 69 | AddSource: true, |
| 70 | })).With("tenant", tenant) |
| 71 | reg = prometheus.WrapRegistererWith(prometheus.Labels{"tenant": tenant}, reg) |
| 72 | |
| 73 | // Create tenant-specific WAL directory |
| 74 | walDir := filepath.Join(cfg.Path, tenant) |
| 75 | |
| 76 | // Clean the WAL before everything |
| 77 | logger.Info("clearing old WAL on start up", "dir", walDir) |
| 78 | |
| 79 | err := os.RemoveAll(walDir) |
| 80 | if err != nil { |
| 81 | logger.Warn(fmt.Sprintf("failed to remove wal on start up: %s", err.Error())) |
| 82 | } |
| 83 | |
| 84 | logger.Info("creating WAL", "dir", walDir) |
| 85 | |
| 86 | // Create complete WAL directory structure: <root>/<tenant>/wal/ |
| 87 | // This creates both <walDir>/<tenant>/ and <walDir>/<tenant>/wal/. If we don't create the wal |
| 88 | // subdirectory remote storage logs a scary error. |
| 89 | err = os.MkdirAll(filepath.Join(walDir, "wal"), 0o700) |
| 90 | if err != nil { |
| 91 | return nil, fmt.Errorf("could not create directory for metrics WAL: %w", err) |
| 92 | } |
| 93 | |
| 94 | // Set up remote storage writer |
| 95 | startTimeCallback := func() (int64, error) { |
| 96 | return int64(model.Latest), nil |
| 97 | } |
| 98 | remoteStorage := remote.NewStorage(logger.With("component", "remote"), reg, startTimeCallback, walDir, cfg.RemoteWriteFlushDeadline, &noopScrapeManager{}, false) |
| 99 | |
| 100 | headers := o.MetricsGeneratorRemoteWriteHeaders(tenant) |
| 101 | generateNativeHistograms := o.MetricsGeneratorGenerateNativeHistograms(tenant) |
| 102 | sendNativeHistograms := overrides.HasNativeHistograms(generateNativeHistograms) |
| 103 | |
| 104 | remoteStorageConfig := &prometheus_config.Config{ |
| 105 | RemoteWriteConfigs: generateTenantRemoteWriteConfigs(cfg.RemoteWrite, tenant, headers, cfg.RemoteWriteAddOrgIDHeader, logger, sendNativeHistograms), |
| 106 | } |
| 107 | |
| 108 | err = remoteStorage.ApplyConfig(remoteStorageConfig) |
| 109 | if err != nil { |
| 110 | return nil, err |
| 111 | } |
| 112 | |
| 113 | // Set up WAL |
| 114 | wal, err := agent.Open(logger.With("component", "wal"), reg, remoteStorage, walDir, cfg.Wal.toPrometheusAgentOptions()) |
| 115 | if err != nil { |
| 116 | return nil, err |
| 117 | } |