数据拉取配置

scrape_configs 主要用于配置拉取数据节点,每一个拉取配置主要包含以下参数:

  • job_name:任务名称

  • honor_labels: 用于解决拉取数据标签有冲突,当设置为 true, 以拉取数据为准,否则以服务配置为准

  • params:数据拉取访问时带的请求参数

  • scrape_interval: 拉取时间间隔

  • scrape_timeout: 拉取超时时间

  • metrics_path: 拉取节点的 metric 路径

  • scheme: 拉取数据访问协议

  • sample_limit: 存储的数据标签个数限制,如果超过限制,该数据将被忽略,不入存储;默认值为0,表示没有限制

  • relabel_configs: 拉取数据重置标签配置

  • metric_relabel_configs:metric 重置标签配置

其代码结构体定义为:

// ScrapeConfig configures a scraping unit for Prometheus.
type ScrapeConfig struct {
    // The job name to which the job label is set by default.
    JobName string `yaml:"job_name"`
    // Indicator whether the scraped metrics should remain unmodified.
    HonorLabels bool `yaml:"honor_labels,omitempty"`
    // A set of query parameters with which the target is scraped.
    Params url.Values `yaml:"params,omitempty"`
    // How frequently to scrape the targets of this scrape config.
    ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
    // The timeout for scraping targets of this config.
    ScrapeTimeout model.Duration `yaml:"scrape_timeout,omitempty"`
    // The HTTP resource path on which to fetch metrics from targets.
    MetricsPath string `yaml:"metrics_path,omitempty"`
    // The URL scheme with which to fetch metrics from targets.
    Scheme string `yaml:"scheme,omitempty"`
    // More than this many samples post metric-relabelling will cause the scrape to fail.
    SampleLimit uint `yaml:"sample_limit,omitempty"`

    // We cannot do proper Go type embedding below as the parser will then parse
    // values arbitrarily into the overflow maps of further-down types.

    ServiceDiscoveryConfig ServiceDiscoveryConfig `yaml:",inline"`
    HTTPClientConfig       HTTPClientConfig       `yaml:",inline"`

    // List of target relabel configurations.
    RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"`
    // List of metric relabel configurations.
    MetricRelabelConfigs []*RelabelConfig `yaml:"metric_relabel_configs,omitempty"`

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

以上配置定义中还包含 ServiceDiscoveryConfig,它的代码定义为:

// ServiceDiscoveryConfig configures lists of different service discovery mechanisms.
type ServiceDiscoveryConfig struct {
    // List of labeled target groups for this job.
    StaticConfigs []*TargetGroup `yaml:"static_configs,omitempty"`
    // List of DNS service discovery configurations.
    DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"`
    // List of file service discovery configurations.
    FileSDConfigs []*FileSDConfig `yaml:"file_sd_configs,omitempty"`
    // List of Consul service discovery configurations.
    ConsulSDConfigs []*ConsulSDConfig `yaml:"consul_sd_configs,omitempty"`
    // List of Serverset service discovery configurations.
    ServersetSDConfigs []*ServersetSDConfig `yaml:"serverset_sd_configs,omitempty"`
    // NerveSDConfigs is a list of Nerve service discovery configurations.
    NerveSDConfigs []*NerveSDConfig `yaml:"nerve_sd_configs,omitempty"`
    // MarathonSDConfigs is a list of Marathon service discovery configurations.
    MarathonSDConfigs []*MarathonSDConfig `yaml:"marathon_sd_configs,omitempty"`
    // List of Kubernetes service discovery configurations.
    KubernetesSDConfigs []*KubernetesSDConfig `yaml:"kubernetes_sd_configs,omitempty"`
    // List of GCE service discovery configurations.
    GCESDConfigs []*GCESDConfig `yaml:"gce_sd_configs,omitempty"`
    // List of EC2 service discovery configurations.
    EC2SDConfigs []*EC2SDConfig `yaml:"ec2_sd_configs,omitempty"`
    // List of OpenStack service discovery configurations.
    OpenstackSDConfigs []*OpenstackSDConfig `yaml:"openstack_sd_configs,omitempty"`
    // List of Azure service discovery configurations.
    AzureSDConfigs []*AzureSDConfig `yaml:"azure_sd_configs,omitempty"`
    // List of Triton service discovery configurations.
    TritonSDConfigs []*TritonSDConfig `yaml:"triton_sd_configs,omitempty"`

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

ServiceDiscoveryConfig 主要用于 target 发现,大体分为两类,静态配置和动态发现。

所以,一份完整的 scrape_configs 配置大致为:

# The job name assigned to scraped metrics by default.
job_name: <job_name>

# How frequently to scrape targets from this job.
[ scrape_interval: <duration> | default = <global_config.scrape_interval> ]

# Per-scrape timeout when scraping this job.
[ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]

# The HTTP resource path on which to fetch metrics from targets.
[ metrics_path: <path> | default = /metrics ]

# honor_labels controls how Prometheus handles conflicts between labels that are
# already present in scraped data and labels that Prometheus would attach
# server-side ("job" and "instance" labels, manually configured target
# labels, and labels generated by service discovery implementations).
#
# If honor_labels is set to "true", label conflicts are resolved by keeping label
# values from the scraped data and ignoring the conflicting server-side labels.
#
# If honor_labels is set to "false", label conflicts are resolved by renaming
# conflicting labels in the scraped data to "exported_<original-label>" (for
# example "exported_instance", "exported_job") and then attaching server-side
# labels. This is useful for use cases such as federation, where all labels
# specified in the target should be preserved.
#
# Note that any globally configured "external_labels" are unaffected by this
# setting. In communication with external systems, they are always applied only
# when a time series does not have a given label yet and are ignored otherwise.
[ honor_labels: <boolean> | default = false ]

# Configures the protocol scheme used for requests.
[ scheme: <scheme> | default = http ]

# Optional HTTP URL parameters.
params:
  [ <string>: [<string>, ...] ]

# Sets the `Authorization` header on every scrape request with the
# configured username and password.
basic_auth:
  [ username: <string> ]
  [ password: <string> ]

# Sets the `Authorization` header on every scrape request with
# the configured bearer token. It is mutually exclusive with `bearer_token_file`.
[ bearer_token: <string> ]

# Sets the `Authorization` header on every scrape request with the bearer token
# read from the configured file. It is mutually exclusive with `bearer_token`.
[ bearer_token_file: /path/to/bearer/token/file ]

# Configures the scrape request's TLS settings.
tls_config:
  [ <tls_config> ]

# Optional proxy URL.
[ proxy_url: <string> ]

# List of Azure service discovery configurations.
azure_sd_configs:
  [ - <azure_sd_config> ... ]

# List of Consul service discovery configurations.
consul_sd_configs:
  [ - <consul_sd_config> ... ]

# List of DNS service discovery configurations.
dns_sd_configs:
  [ - <dns_sd_config> ... ]

# List of EC2 service discovery configurations.
ec2_sd_configs:
  [ - <ec2_sd_config> ... ]

# List of OpenStack service discovery configurations.
openstack_sd_configs:
  [ - <openstack_sd_config> ... ]

# List of file service discovery configurations.
file_sd_configs:
  [ - <file_sd_config> ... ]

# List of GCE service discovery configurations.
gce_sd_configs:
  [ - <gce_sd_config> ... ]

# List of Kubernetes service discovery configurations.
kubernetes_sd_configs:
  [ - <kubernetes_sd_config> ... ]

# List of Marathon service discovery configurations.
marathon_sd_configs:
  [ - <marathon_sd_config> ... ]

# List of AirBnB's Nerve service discovery configurations.
nerve_sd_configs:
  [ - <nerve_sd_config> ... ]

# List of Zookeeper Serverset service discovery configurations.
serverset_sd_configs:
  [ - <serverset_sd_config> ... ]

# List of Triton service discovery configurations.
triton_sd_configs:
  [ - <triton_sd_config> ... ]

# List of labeled statically configured targets for this job.
static_configs:
  [ - <static_config> ... ]

# List of target relabel configurations.
relabel_configs:
  [ - <relabel_config> ... ]

# List of metric relabel configurations.
metric_relabel_configs:
  [ - <relabel_config> ... ]

# Per-scrape limit on number of scraped samples that will be accepted.
# If more than this number of samples are present after metric relabelling
# the entire scrape will be treated as failed. 0 means no limit.
[ sample_limit: <int> | default = 0 ]

Last updated