关于prometheus:prometheus与exemplar

20次阅读

共计 2918 个字符,预计需要花费 8 分钟才能阅读完成。

一. exemplar 是什么

exemplar 译为“样本”、“范例”。

exemplar 最早被用在 Google 的 StackDriver 中,前面成为了 OpenMetrics 规范的一部分,即: 能够为 metrics 额定减少属性。

典型利用是为 metrics 增加 trace 信息,这样 metrics 和 tracing 就能够关联起来。

OpenMetrics introduces the ability for scrape targets to add exemplars to certain metrics.
Exemplars are references to data outside of the MetricSet. A common use case are IDs of program traces.

指标对象通过 /metrics 接口裸露 metrics 和 exemplar 信息,prometheus 在 pull 时,会一起拉取并保留。

prometehus 反对对 exemplar 的采集和存储,启动时须要额定减少参数:

--enable-feature=exemplar-storage

prometheus 中 exemplar 对象的定义:

  • 跟一般的 metrics 相似,由 t /v、labels 组成;
// Exemplar is easier to use, user-facing representation of *dto.Exemplar.
type Exemplar struct {
    Value  float64
    Labels Labels
    // Optional.
    // Default value (time.Time{}) indicates its empty, which should be
    // understood as time.Now() time at the moment of creation of metric.
    Timestamp time.Time
}

二. client 侧暴漏 metrics 和 exemplar

对于 metrics,个别应用 prometheus/client_go 减少本人的指标;

prometheus/client_go 同样反对裸露本人的 exemplar,故咱们用 prometheus/client_go 编写 client。

以 application 中最常见的 http_request_duration_seconds 指标为例:

  • 定义 Histogram 类型变量:requestDurations;
  • 应用 requestDurations.ObserveWithExemplar() 更新变量的值:

    • value = time.Since(now).Seconds()
    • lables = {“dummyID”: rand.Int(100000) }
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
    Name:    "http_request_duration_seconds",
    Help:    "A histogram of the HTTP request durations in seconds.",
    Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
})

go func() {
    for {
        // Record fictional latency.
        now := time.Now()
        requestDurations.(prometheus.ExemplarObserver).ObserveWithExemplar(time.Since(now).Seconds(),                                        // value
            prometheus.Labels{"dummyID": fmt.Sprint(rand.Intn(100000))},      // labels
        )
        time.Sleep(600 * time.Millisecond)
    }
}()

http_handler 中减少 EnableOpenMetrics:true 参数:

http.Handle(
    "/metrics", promhttp.HandlerFor(
        registry,
        promhttp.HandlerOpts{EnableOpenMetrics: true,}),
)

启动 client,通过 curl 采集原始数据:

#  curl -H "Accept: application/openmetrics-text" http://localhost:8080/metrics

# HELP http_request_duration_seconds A histogram of the HTTP request durations in seconds.
# TYPE http_request_duration_seconds histogram
http_request_duration_seconds_bucket{le="0.1"} 5006 # {dummyID="80815"} 4.097e-06 1.6735926303881307e+09
http_request_duration_seconds_bucket{le="0.15000000000000002"} 5006
http_request_duration_seconds_bucket{le="0.22500000000000003"} 5006
http_request_duration_seconds_bucket{le="0.3375"} 5006
http_request_duration_seconds_bucket{le="0.5062500000000001"} 5006
http_request_duration_seconds_bucket{le="+Inf"} 5006
http_request_duration_seconds_sum 0.02248467300000002
http_request_duration_seconds_count 5006

能够看到,exemplar 的信息与一般的 metrics 用 #距离,对于:

# {dummyID="80815"} 4.097e-06 1.6735926303881307e+09

其中:

  • {dummyID=”80815″} 为 labels;
  • 4.097e-06 为 value,即 code 中的 time.Since(now).Seconds();
  • 1.6735926303881307e+09 为 timestamp;

三. prometheus 拉取 client 的数据

应用 prometheus 采集 client 的 metrics,而后就能够在 prometheus UI 上看到 exemplar 的信息

也能够将 prometheus 作为数据源,导入 grafana 查看:

参考:

1.https://vbehar.medium.com/usi…
2.client-demo: https://github.com/prometheus…
3.prometheus exemplar: https://prometheus.io/docs/pr…
4.OpenMetrics Specification: https://github.com/OpenObserv…

正文完
 0