专栏
天翼云开发者社区

prometheus采集与预计算配置管理

2023-10-09 11:35:04 10阅读

Thanos-sidecar实例信息模板定义-sidecar_instance.yml

instance:
{{- if eq .EnvMap.POD_IP "113.125.219.21"}}
tags:
  province: "福建省|吉林省"
{{- else if eq .EnvMap.POD_IP "113.125.219.22"}}
tags:
  province: "江苏省"
  hash: 0
  hash_modulus: 2
{{- else if eq .EnvMap.POD_IP "113.125.219.23"}}
tags:
  province: "江苏省"
  hash: 1
  hash_modulus: 2
{{- end}}
exclude_labels:
  - hash
  - hash_modulus

通过上面的语法定义好实例信息,其中:

  1. EnvMap是环境变量集合,引用环境变量可通过 .EnvMap.xxx。比如通过k8s向sidecar设置了环境变量POD_IP,则在模板中引用方法为 .EnvMap.POD_IP

  2. 实例信息格式说明

    instance:
     # 标签信息
    tags:
       # 在这里自定义标签
       # tags中的标签将被作为标识 __identify_${tag中值} (如:__identify_province)传递给query
       # 查询是可以添加类似 __identify_province="福建省" 标签来为query定向指定后端,避免全局扫描
       # 格式:key: value 如需作为query标识则不支持嵌套
    exclude_labels:
       # 数组,这里指定哪些标签不传递给query
       # 如上面的例子:最终只有 __identify_province 会传递到query

    注意:如果在tags中定义了很多标签,那么不需要传递给query的一定要在exclude_labels设置下,避免影响query性能

 

定义prometheus采集模板-prometheus.yaml

scrape_configs:
{{- if and (.Instance.Tags.province) (ne .Instance.Tags.province "")}}
- job_name: 'nginx-vtx-exporter'
metrics_path: /vod_vts
relabel_configs:
- source_labels: [__meta_consul_service_metadata_province]
  regex: {{Instance.Tags.province}}
  action: keep
{{- if and (.Instance.Tags.hash) (.Instance.Tags.hash_modulus)}}
- source_labels: [__meta_consul_service_metadata_node]
  modulus: {{.Instance.Tags.hash_modulus}}
  target_label: __tmp_hash
  action: hashmod
- source_labels: [__tmp_hash]
  regex: ^{{.Instance.Tags.hash}}$
  action: keep
{{- end}}
....
- job_name: ''
  ....
{{- end}}

 

定义prometheus rule模板-*.tpl

groups:
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
# 如果做拆分了,就不在这里做省份粒度的预计算
{{- if or (not .Instance.Tags.hash) (not .Instance.Tags.hash_modulus)}}
- record:  record_Rate5mDomainIspProvinceBW
  expr: sum(record_Rate5mInstanceBW{node_module=~".*直播.*"})by(province,isp)
{{- end}}

 

rule模板命名一定要用.tpl后缀才会生效

 

综上所述,最后为sidecar指定命令参数:

--reloader.identify-config-file=/xxx/sidecar_instance.yml # 指定sidecar实例信息文件
--reloader.config-file=/xxx/prometheus.yaml # 指定prometheus配置模板文件,这个参数不变,里面的内容要变
--reloader.rule-dir=/xxx/rule # 指定规则目录,参数不变

/xxx/rule 目录中的内容说明:.tpl 后缀的就是模板文件,会生成对应的 .yml文件;.yml文件不经过模板化处理

同一个目录下.tpl后缀的文件和.yml后缀的文件,出去后缀后的名称不要相同,否则会互相覆盖

 

Thanos-rule预计算配置管理

实例信息模板定义-rule_instance.yml

这部分与sidecar实例信息模板定义一样

instance:
{{- if eq .EnvMap.POD_NAME "thanos-rule-0"}}
tags:
  provinces:
    - '福建省'
    - '江苏省'
{{- else if eq .EnvMap.POD_NAME "thanos-rule-1"}}
tags:
  provinces:
    - '辽宁省'
    - '广东省'
{{- end}}
exclude_labels:
  - provinces

 

rule配置模板-*.tpl

groups:
# 如果有定义 provinces 则说明需要进行省粒度预计算
{{- if and (.Instance.Tags.provinces) (gt (len .Instance.Tags.provinces) 0)}}
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
{{- range $index, $province := .Instance.Tags.provinces}}
- record: record_Rate5mDomainIspProvinceBW
  expr: sum(record_Rate5mInstanceBW{node_module=~".*直 播.*",__identify_province=~"{{$province}}"})by(host_group_level)
{{- end}}
{{- end}}

 

综上所述,thanos-rule需要新增参数

--rule-template=/xxx/rule_instance.yml

-rule-file=/xxx/rule/*.yml指定规则目录,其中,rule模板仍然是以.tpl为后缀。

  • 0
  • 0
  • 0
0 评论
0/1000
评论(0) 发表评论
李良伟

李良伟

2 篇文章 0 粉丝
关注

prometheus采集与预计算配置管理

2023-10-09 11:35:04 10阅读

Thanos-sidecar实例信息模板定义-sidecar_instance.yml

instance:
{{- if eq .EnvMap.POD_IP "113.125.219.21"}}
tags:
  province: "福建省|吉林省"
{{- else if eq .EnvMap.POD_IP "113.125.219.22"}}
tags:
  province: "江苏省"
  hash: 0
  hash_modulus: 2
{{- else if eq .EnvMap.POD_IP "113.125.219.23"}}
tags:
  province: "江苏省"
  hash: 1
  hash_modulus: 2
{{- end}}
exclude_labels:
  - hash
  - hash_modulus

通过上面的语法定义好实例信息,其中:

  1. EnvMap是环境变量集合,引用环境变量可通过 .EnvMap.xxx。比如通过k8s向sidecar设置了环境变量POD_IP,则在模板中引用方法为 .EnvMap.POD_IP

  2. 实例信息格式说明

    instance:
     # 标签信息
    tags:
       # 在这里自定义标签
       # tags中的标签将被作为标识 __identify_${tag中值} (如:__identify_province)传递给query
       # 查询是可以添加类似 __identify_province="福建省" 标签来为query定向指定后端,避免全局扫描
       # 格式:key: value 如需作为query标识则不支持嵌套
    exclude_labels:
       # 数组,这里指定哪些标签不传递给query
       # 如上面的例子:最终只有 __identify_province 会传递到query

    注意:如果在tags中定义了很多标签,那么不需要传递给query的一定要在exclude_labels设置下,避免影响query性能

 

定义prometheus采集模板-prometheus.yaml

scrape_configs:
{{- if and (.Instance.Tags.province) (ne .Instance.Tags.province "")}}
- job_name: 'nginx-vtx-exporter'
metrics_path: /vod_vts
relabel_configs:
- source_labels: [__meta_consul_service_metadata_province]
  regex: {{Instance.Tags.province}}
  action: keep
{{- if and (.Instance.Tags.hash) (.Instance.Tags.hash_modulus)}}
- source_labels: [__meta_consul_service_metadata_node]
  modulus: {{.Instance.Tags.hash_modulus}}
  target_label: __tmp_hash
  action: hashmod
- source_labels: [__tmp_hash]
  regex: ^{{.Instance.Tags.hash}}$
  action: keep
{{- end}}
....
- job_name: ''
  ....
{{- end}}

 

定义prometheus rule模板-*.tpl

groups:
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
# 如果做拆分了,就不在这里做省份粒度的预计算
{{- if or (not .Instance.Tags.hash) (not .Instance.Tags.hash_modulus)}}
- record:  record_Rate5mDomainIspProvinceBW
  expr: sum(record_Rate5mInstanceBW{node_module=~".*直播.*"})by(province,isp)
{{- end}}

 

rule模板命名一定要用.tpl后缀才会生效

 

综上所述,最后为sidecar指定命令参数:

--reloader.identify-config-file=/xxx/sidecar_instance.yml # 指定sidecar实例信息文件
--reloader.config-file=/xxx/prometheus.yaml # 指定prometheus配置模板文件,这个参数不变,里面的内容要变
--reloader.rule-dir=/xxx/rule # 指定规则目录,参数不变

/xxx/rule 目录中的内容说明:.tpl 后缀的就是模板文件,会生成对应的 .yml文件;.yml文件不经过模板化处理

同一个目录下.tpl后缀的文件和.yml后缀的文件,出去后缀后的名称不要相同,否则会互相覆盖

 

Thanos-rule预计算配置管理

实例信息模板定义-rule_instance.yml

这部分与sidecar实例信息模板定义一样

instance:
{{- if eq .EnvMap.POD_NAME "thanos-rule-0"}}
tags:
  provinces:
    - '福建省'
    - '江苏省'
{{- else if eq .EnvMap.POD_NAME "thanos-rule-1"}}
tags:
  provinces:
    - '辽宁省'
    - '广东省'
{{- end}}
exclude_labels:
  - provinces

 

rule配置模板-*.tpl

groups:
# 如果有定义 provinces 则说明需要进行省粒度预计算
{{- if and (.Instance.Tags.provinces) (gt (len .Instance.Tags.provinces) 0)}}
- name: nginx_bandwidth_all_thanos_record_record
interval: 60s
rules:
{{- range $index, $province := .Instance.Tags.provinces}}
- record: record_Rate5mDomainIspProvinceBW
  expr: sum(record_Rate5mInstanceBW{node_module=~".*直 播.*",__identify_province=~"{{$province}}"})by(host_group_level)
{{- end}}
{{- end}}

 

综上所述,thanos-rule需要新增参数

--rule-template=/xxx/rule_instance.yml

-rule-file=/xxx/rule/*.yml指定规则目录,其中,rule模板仍然是以.tpl为后缀。

文章来自专栏

监控与我

2 篇文章 1 订阅
0 评论
0/1000
评论(0) 发表评论
  • 0
    点赞
  • 0
    收藏
  • 0
    评论