MCPcopy Index your code
hub / github.com/prometheus/prometheus / Load

Function Load

config/config.go:74–127  ·  view source on GitHub ↗

Load parses the YAML input s into a Config.

(s string, logger *slog.Logger)

Source from the content-addressed store, hash-verified

72
73// Load parses the YAML input s into a Config.
74func 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.

Callers 11

loadConfigurationFunction · 0.92
TestYAMLRoundtripFunction · 0.85
TestOTLPAllowUTF8Function · 0.85
TestTSDBChunkEncodingFunction · 0.85
TestEmptyConfigFunction · 0.85
TestGlobalConfigFunction · 0.85
TestExtraScrapeMetricsFunction · 0.85
LoadFileFunction · 0.85

Calls 6

AddMethod · 0.95
LabelsMethod · 0.95
NewScratchBuilderFunction · 0.92
ExpandMethod · 0.80
RangeMethod · 0.65
IsEmptyMethod · 0.45

Tested by 10

loadConfigurationFunction · 0.74
TestYAMLRoundtripFunction · 0.68
TestOTLPAllowUTF8Function · 0.68
TestTSDBChunkEncodingFunction · 0.68
TestEmptyConfigFunction · 0.68
TestGlobalConfigFunction · 0.68
TestExtraScrapeMetricsFunction · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…