categraf/inputs/prometheus/README.md

45 lines
1.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# prometheus
prometheus 插件的作用,就是抓取 `/metrics` 接口的数据,上报给服务端。通过,各类 exporter 会暴露 `/metrics` 接口数据,越来越多的开源组件也会内置 prometheus SDK吐出 prometheus 格式的监控数据,比如 rabbitmq 插件,其 README 中就有介绍。
这个插件 fork 自 telegraf/prometheus做了一些删减改造仍然支持通过 consul 做服务发现,管理所有的目标地址,删掉了 Kubernetes 部分Kubernetes 部分准备放到其他插件里实现。
增加了两个配置url_label_key 和 url_label_value。为了标识监控数据是从哪个 scrape url 拉取的,会为监控数据附一个标签来标识这个 url默认的标签 KEY 是用 instance当然也可以改成别的不过不建议。url_label_value 是标签值,支持 go template 语法,如果为空,就是整个 url 的内容,也可以通过模板变量只取一部分,比如 `http://localhost:9104/metrics`,只想取 IP 和端口部分,就可以写成:
```ini
url_label_value = "{{.Host}}"
```
如果 HTTP scheme 部分和 `/metrics` Path 部分都想取,可以这么写:
```ini
url_label_value = "{{.Scheme}}://{{.Host}}{{.Path}}"
```
相关变量是用这个方法生成的,供大家参考:
```go
func (ul *UrlLabel) GenerateLabel(u *url.URL) (string, string, error) {
if ul.LabelValue == "" {
return ul.LabelKey, u.String(), nil
}
dict := map[string]string{
"Scheme": u.Scheme,
"Host": u.Host,
"Hostname": u.Hostname(),
"Port": u.Port(),
"Path": u.Path,
"Query": u.RawQuery,
"Fragment": u.Fragment,
}
var buffer bytes.Buffer
err := ul.LabelValueTpl.Execute(&buffer, dict)
if err != nil {
return "", "", err
}
return ul.LabelKey, buffer.String(), nil
}
```