根据prometheus 文档中的定义,在编写导出器时,它声明以下内容:
仅当 Prometheus 抓取指标时才应从应用程序中提取指标,导出器不应根据自己的计时器执行抓取。
以下代码在技术上工作正常,并使用我的自定义指标发布适当的页面。所以它按原样解决了我的业务需求。
然而,它效率低下并且运行无限循环来不断更新值。这与上面提到的仅在 Prometheus 抓取时生成指标值的文档中的做法不符。
package main
import (
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
func main() {
var metricSystemTime = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "custom_system_time", Help: "Timestamp in unix epoch format"})
prometheus.MustRegister(metricSystemTime)
go startServer()
for {
metricSystemTime.Set(float64(time.Now().Unix()))
time.Sleep(1 * time.Second)
}
}
func startServer() {
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":19100", nil))
}
Run Code Online (Sandbox Code Playgroud)
为了仅在 prometheus 抓取导出器端点时提取指标,这意味着我需要以某种方式侦听对象上的 GET 请求http.ListenAndServe()?我怎样才能做到这一点?
在我的阅读中,我发现http.HandlerFunc()相反,http.Handler()它似乎走在正确的道路上,但我无法让它工作promhttp.Handler(),而且我在其中迷失了方向。
经过大量搜索后弄清楚了。希望这对将来的人有帮助。
这是一个工作导出器,满足仅在普罗米修斯刮擦发生时收集指标的要求
package main
import (
"log"
"net/http"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Collector struct {
// Add one of the below lines for each collector you define in the newCollector() function
systemTime *prometheus.Desc
}
// Declare your exporter metrics here. Referred to as "collectors"
func newCollector() *Collector {
return &Collector{
systemTime: prometheus.NewDesc("my_system_time",
"System timestamp in unix epoch format",
nil, nil,
),
// Add more collectors (aka metric definitions) here
}
}
func (collector *Collector) Describe(ch chan<- *prometheus.Desc) {
// Add one of these lines for each of your collectors declared above
ch <- collector.systemTime
}
// This fuction runs when Prometheus scrapes the exporter. It will set a new value for the metric(s)
// I have no idea how it works, but it does.
func (collector *Collector) Collect(ch chan<- prometheus.Metric) {
var setSystemTime float64
// Calculate the value of the metric
setSystemTime = float64(time.Now().Unix())
// Here is where you set the "new" value for the metric
// This example is a Gauge, but it can be any Prom metric type
ch <- prometheus.MustNewConstMetric(collector.systemTime, prometheus.GaugeValue, setSystemTime)
}
// Main function, spawns the collector and the web server
func main() {
collector := newCollector()
prometheus.MustRegister(collector)
http.Handle("/metrics", promhttp.Handler())
log.Fatal(http.ListenAndServe(":19100", nil))
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1841 次 |
| 最近记录: |