type Config struct {
    GlobalConfig   GlobalConfig    `yaml:"global"`
    AlertingConfig AlertingConfig  `yaml:"alerting,omitempty"`
    RuleFiles      []string        `yaml:"rule_files,omitempty"`
    ScrapeConfigs  []*ScrapeConfig `yaml:"scrape_configs,omitempty"`

    RemoteWriteConfigs []*RemoteWriteConfig `yaml:"remote_write,omitempty"`
    RemoteReadConfigs  []*RemoteReadConfig  `yaml:"remote_read,omitempty"`

    // Catches all undefined fields and must be empty after parsing.
    XXX map[string]interface{} `yaml:",inline"`

    // original is the input from which the config was parsed.
    original string


// GlobalConfig configures values that are used across other configuration
// objects.
type GlobalConfig struct {
    // How frequently to scrape targets by default.
    ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
    // The default timeout when scraping targets.
    ScrapeTimeout model.Duration `yaml:"scrape_timeout,omitempty"`
    // How frequently to evaluate rules by default.
    EvaluationInterval model.Duration `yaml:"evaluation_interval,omitempty"`
    // The labels to add to any timeseries that this Prometheus instance scrapes.
    ExternalLabels model.LabelSet `yaml:"external_labels,omitempty"`

    // Catches all undefined fields and must be empty after parsing.
    XXX map[string]interface{} `yaml:",inline"`


    // DefaultGlobalConfig is the default global configuration.
DefaultGlobalConfig = GlobalConfig{
        ScrapeInterval:     model.Duration(1 * time.Minute),
        ScrapeTimeout:      model.Duration(10 * time.Second),
        EvaluationInterval: model.Duration(1 * time.Minute),


func (c *GlobalConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
    // Create a clean global config as the previous one was already populated
    // by the default due to the YAML parser behavior for empty blocks.
    gc := &GlobalConfig{}
    type plain GlobalConfig
    if err := unmarshal((*plain)(gc)); err != nil {
        return err
    if err := checkOverflow(gc.XXX, "global config"); err != nil {
        return err
    // First set the correct scrape interval, then check that the timeout
    // (inferred or explicit) is not greater than that.
    if gc.ScrapeInterval == 0 {
        gc.ScrapeInterval = DefaultGlobalConfig.ScrapeInterval
    if gc.ScrapeTimeout > gc.ScrapeInterval {
        return fmt.Errorf("global scrape timeout greater than scrape interval")
    if gc.ScrapeTimeout == 0 {
        if DefaultGlobalConfig.ScrapeTimeout > gc.ScrapeInterval {
            gc.ScrapeTimeout = gc.ScrapeInterval
        } else {
            gc.ScrapeTimeout = DefaultGlobalConfig.ScrapeTimeout
    if gc.EvaluationInterval == 0 {
        gc.EvaluationInterval = DefaultGlobalConfig.EvaluationInterval
    *c = *gc
    return nil



golang 源码阅读 页面已被访问85次