数据拉取配置

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 重置标签配置

其代码结构体定义为:

  1. // ScrapeConfig configures a scraping unit for Prometheus.
  2. type ScrapeConfig struct {
  3. // The job name to which the job label is set by default.
  4. JobName string `yaml:"job_name"`
  5. // Indicator whether the scraped metrics should remain unmodified.
  6. HonorLabels bool `yaml:"honor_labels,omitempty"`
  7. // A set of query parameters with which the target is scraped.
  8. Params url.Values `yaml:"params,omitempty"`
  9. // How frequently to scrape the targets of this scrape config.
  10. ScrapeInterval model.Duration `yaml:"scrape_interval,omitempty"`
  11. // The timeout for scraping targets of this config.
  12. ScrapeTimeout model.Duration `yaml:"scrape_timeout,omitempty"`
  13. // The HTTP resource path on which to fetch metrics from targets.
  14. MetricsPath string `yaml:"metrics_path,omitempty"`
  15. // The URL scheme with which to fetch metrics from targets.
  16. Scheme string `yaml:"scheme,omitempty"`
  17. // More than this many samples post metric-relabelling will cause the scrape to fail.
  18. SampleLimit uint `yaml:"sample_limit,omitempty"`
  19. // We cannot do proper Go type embedding below as the parser will then parse
  20. // values arbitrarily into the overflow maps of further-down types.
  21. ServiceDiscoveryConfig ServiceDiscoveryConfig `yaml:",inline"`
  22. HTTPClientConfig HTTPClientConfig `yaml:",inline"`
  23. // List of target relabel configurations.
  24. RelabelConfigs []*RelabelConfig `yaml:"relabel_configs,omitempty"`
  25. // List of metric relabel configurations.
  26. MetricRelabelConfigs []*RelabelConfig `yaml:"metric_relabel_configs,omitempty"`
  27. // Catches all undefined fields and must be empty after parsing.
  28. XXX map[string]interface{} `yaml:",inline"`
  29. }

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

  1. // ServiceDiscoveryConfig configures lists of different service discovery mechanisms.
  2. type ServiceDiscoveryConfig struct {
  3. // List of labeled target groups for this job.
  4. StaticConfigs []*TargetGroup `yaml:"static_configs,omitempty"`
  5. // List of DNS service discovery configurations.
  6. DNSSDConfigs []*DNSSDConfig `yaml:"dns_sd_configs,omitempty"`
  7. // List of file service discovery configurations.
  8. FileSDConfigs []*FileSDConfig `yaml:"file_sd_configs,omitempty"`
  9. // List of Consul service discovery configurations.
  10. ConsulSDConfigs []*ConsulSDConfig `yaml:"consul_sd_configs,omitempty"`
  11. // List of Serverset service discovery configurations.
  12. ServersetSDConfigs []*ServersetSDConfig `yaml:"serverset_sd_configs,omitempty"`
  13. // NerveSDConfigs is a list of Nerve service discovery configurations.
  14. NerveSDConfigs []*NerveSDConfig `yaml:"nerve_sd_configs,omitempty"`
  15. // MarathonSDConfigs is a list of Marathon service discovery configurations.
  16. MarathonSDConfigs []*MarathonSDConfig `yaml:"marathon_sd_configs,omitempty"`
  17. // List of Kubernetes service discovery configurations.
  18. KubernetesSDConfigs []*KubernetesSDConfig `yaml:"kubernetes_sd_configs,omitempty"`
  19. // List of GCE service discovery configurations.
  20. GCESDConfigs []*GCESDConfig `yaml:"gce_sd_configs,omitempty"`
  21. // List of EC2 service discovery configurations.
  22. EC2SDConfigs []*EC2SDConfig `yaml:"ec2_sd_configs,omitempty"`
  23. // List of OpenStack service discovery configurations.
  24. OpenstackSDConfigs []*OpenstackSDConfig `yaml:"openstack_sd_configs,omitempty"`
  25. // List of Azure service discovery configurations.
  26. AzureSDConfigs []*AzureSDConfig `yaml:"azure_sd_configs,omitempty"`
  27. // List of Triton service discovery configurations.
  28. TritonSDConfigs []*TritonSDConfig `yaml:"triton_sd_configs,omitempty"`
  29. // Catches all undefined fields and must be empty after parsing.
  30. XXX map[string]interface{} `yaml:",inline"`
  31. }

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

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

  1. # The job name assigned to scraped metrics by default.
  2. job_name: <job_name>
  3. # How frequently to scrape targets from this job.
  4. [ scrape_interval: <duration> | default = <global_config.scrape_interval> ]
  5. # Per-scrape timeout when scraping this job.
  6. [ scrape_timeout: <duration> | default = <global_config.scrape_timeout> ]
  7. # The HTTP resource path on which to fetch metrics from targets.
  8. [ metrics_path: <path> | default = /metrics ]
  9. # honor_labels controls how Prometheus handles conflicts between labels that are
  10. # already present in scraped data and labels that Prometheus would attach
  11. # server-side ("job" and "instance" labels, manually configured target
  12. # labels, and labels generated by service discovery implementations).
  13. #
  14. # If honor_labels is set to "true", label conflicts are resolved by keeping label
  15. # values from the scraped data and ignoring the conflicting server-side labels.
  16. #
  17. # If honor_labels is set to "false", label conflicts are resolved by renaming
  18. # conflicting labels in the scraped data to "exported_<original-label>" (for
  19. # example "exported_instance", "exported_job") and then attaching server-side
  20. # labels. This is useful for use cases such as federation, where all labels
  21. # specified in the target should be preserved.
  22. #
  23. # Note that any globally configured "external_labels" are unaffected by this
  24. # setting. In communication with external systems, they are always applied only
  25. # when a time series does not have a given label yet and are ignored otherwise.
  26. [ honor_labels: <boolean> | default = false ]
  27. # Configures the protocol scheme used for requests.
  28. [ scheme: <scheme> | default = http ]
  29. # Optional HTTP URL parameters.
  30. params:
  31. [ <string>: [<string>, ...] ]
  32. # Sets the `Authorization` header on every scrape request with the
  33. # configured username and password.
  34. basic_auth:
  35. [ username: <string> ]
  36. [ password: <string> ]
  37. # Sets the `Authorization` header on every scrape request with
  38. # the configured bearer token. It is mutually exclusive with `bearer_token_file`.
  39. [ bearer_token: <string> ]
  40. # Sets the `Authorization` header on every scrape request with the bearer token
  41. # read from the configured file. It is mutually exclusive with `bearer_token`.
  42. [ bearer_token_file: /path/to/bearer/token/file ]
  43. # Configures the scrape request's TLS settings.
  44. tls_config:
  45. [ <tls_config> ]
  46. # Optional proxy URL.
  47. [ proxy_url: <string> ]
  48. # List of Azure service discovery configurations.
  49. azure_sd_configs:
  50. [ - <azure_sd_config> ... ]
  51. # List of Consul service discovery configurations.
  52. consul_sd_configs:
  53. [ - <consul_sd_config> ... ]
  54. # List of DNS service discovery configurations.
  55. dns_sd_configs:
  56. [ - <dns_sd_config> ... ]
  57. # List of EC2 service discovery configurations.
  58. ec2_sd_configs:
  59. [ - <ec2_sd_config> ... ]
  60. # List of OpenStack service discovery configurations.
  61. openstack_sd_configs:
  62. [ - <openstack_sd_config> ... ]
  63. # List of file service discovery configurations.
  64. file_sd_configs:
  65. [ - <file_sd_config> ... ]
  66. # List of GCE service discovery configurations.
  67. gce_sd_configs:
  68. [ - <gce_sd_config> ... ]
  69. # List of Kubernetes service discovery configurations.
  70. kubernetes_sd_configs:
  71. [ - <kubernetes_sd_config> ... ]
  72. # List of Marathon service discovery configurations.
  73. marathon_sd_configs:
  74. [ - <marathon_sd_config> ... ]
  75. # List of AirBnB's Nerve service discovery configurations.
  76. nerve_sd_configs:
  77. [ - <nerve_sd_config> ... ]
  78. # List of Zookeeper Serverset service discovery configurations.
  79. serverset_sd_configs:
  80. [ - <serverset_sd_config> ... ]
  81. # List of Triton service discovery configurations.
  82. triton_sd_configs:
  83. [ - <triton_sd_config> ... ]
  84. # List of labeled statically configured targets for this job.
  85. static_configs:
  86. [ - <static_config> ... ]
  87. # List of target relabel configurations.
  88. relabel_configs:
  89. [ - <relabel_config> ... ]
  90. # List of metric relabel configurations.
  91. metric_relabel_configs:
  92. [ - <relabel_config> ... ]
  93. # Per-scrape limit on number of scraped samples that will be accepted.
  94. # If more than this number of samples are present after metric relabelling
  95. # the entire scrape will be treated as failed. 0 means no limit.
  96. [ sample_limit: <int> | default = 0 ]