Load parses the YAML input s into a Config.
(s string, logger *slog.Logger)
| 72 | |
| 73 | // Load parses the YAML input s into a Config. |
| 74 | func Load(s string, logger *slog.Logger) (*Config, error) { |
| 75 | cfg := &Config{} |
| 76 | // If the entire config body is empty the UnmarshalYAML method is |
| 77 | // never called. We thus have to set the DefaultConfig at the entry |
| 78 | // point as well. |
| 79 | *cfg = DefaultConfig |
| 80 | |
| 81 | err := yaml.UnmarshalStrict([]byte(s), cfg) |
| 82 | if err != nil { |
| 83 | return nil, err |
| 84 | } |
| 85 | |
| 86 | // When the config body is empty, UnmarshalYAML is never called, so |
| 87 | // TSDBConfig may still be nil. |
| 88 | if cfg.StorageConfig.TSDBConfig == nil { |
| 89 | retention := DefaultTSDBRetentionConfig |
| 90 | cfg.StorageConfig.TSDBConfig = &TSDBConfig{Retention: &retention} |
| 91 | } |
| 92 | |
| 93 | b := labels.NewScratchBuilder(0) |
| 94 | cfg.GlobalConfig.ExternalLabels.Range(func(v labels.Label) { |
| 95 | newV := os.Expand(v.Value, func(s string) string { |
| 96 | if s == "$" { |
| 97 | return "$" |
| 98 | } |
| 99 | if v := os.Getenv(s); v != "" { |
| 100 | return v |
| 101 | } |
| 102 | logger.Warn("Empty environment variable", "name", s) |
| 103 | return "" |
| 104 | }) |
| 105 | if newV != v.Value { |
| 106 | logger.Debug("External label replaced", "label", v.Name, "input", v.Value, "output", newV) |
| 107 | } |
| 108 | // Note newV can be blank. https://github.com/prometheus/prometheus/issues/11024 |
| 109 | b.Add(v.Name, newV) |
| 110 | }) |
| 111 | if !b.Labels().IsEmpty() { |
| 112 | cfg.GlobalConfig.ExternalLabels = b.Labels() |
| 113 | } |
| 114 | |
| 115 | switch cfg.OTLPConfig.TranslationStrategy { |
| 116 | case otlptranslator.UnderscoreEscapingWithSuffixes, otlptranslator.UnderscoreEscapingWithoutSuffixes: |
| 117 | case "": |
| 118 | case otlptranslator.NoTranslation, otlptranslator.NoUTF8EscapingWithSuffixes: |
| 119 | if cfg.GlobalConfig.MetricNameValidationScheme == model.LegacyValidation { |
| 120 | return nil, fmt.Errorf("OTLP translation strategy %q is not allowed when UTF8 is disabled", cfg.OTLPConfig.TranslationStrategy) |
| 121 | } |
| 122 | default: |
| 123 | return nil, fmt.Errorf("unsupported OTLP translation strategy %q", cfg.OTLPConfig.TranslationStrategy) |
| 124 | } |
| 125 | cfg.loaded = true |
| 126 | return cfg, nil |
| 127 | } |
| 128 | |
| 129 | // LoadFile parses and validates the given YAML file into a read-only Config. |
| 130 | // Callers should never write to or shallow copy the returned Config. |
searching dependent graphs…