我还没有找到一些在prometheus中使用Gauge,Counter和Histogram的好例子.对此有任何帮助.我尝试使用该文档,但我无法成功创建一个有用的应用程序.
Zst*_*ack 17
您可以在prometheus/client_golang中找到示例。首先,您只需获取软件包即可:
$ go get github.com/prometheus/client_golang/prometheus
$ go get github.com/prometheus/client_golang/prometheus/push
Run Code Online (Sandbox Code Playgroud)
您可以通过设置正确的pushgateway地址来运行以下示例,在本例中为http://localhost:9091/ :
package main
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/push"
)
func ExamplePusher_Push() {
completionTime := prometheus.NewGauge(prometheus.GaugeOpts{
Name: "db_backup_last_completion_timestamp_seconds",
Help: "The timestamp of the last successful completion of a DB backup.",
})
completionTime.SetToCurrentTime()
if err := push.New("http://localhost:9091/", "db_backup").
Collector(completionTime).
Grouping("db", "customers").
Push(); err != nil {
fmt.Println("Could not push completion time to Pushgateway:", err)
}
}
func main() {
ExamplePusher_Push()
}
Run Code Online (Sandbox Code Playgroud)
运行你的脚本:
$ go run pushExample.go
Run Code Online (Sandbox Code Playgroud)
运行代码后,您应该会在网关 ( http://localhost:9091/ )上看到指标。界面如下所示:

我找到了这个
`
package main
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
)
var (
cpuTemp = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "cpu_temperature_celsius",
Help: "Current temperature of the CPU.",
})
hdFailures = prometheus.NewCounter(prometheus.CounterOpts{
Name: "hd_errors_total",
Help: "Number of hard-disk errors.",
})
)
func init() {
prometheus.MustRegister(cpuTemp)
prometheus.MustRegister(hdFailures)
}
func main() {
cpuTemp.Set(65.3)
hdFailures.Inc()
http.Handle("/metrics", prometheus.Handler())
http.ListenAndServe(":8080", nil)
}
Run Code Online (Sandbox Code Playgroud)
`
这可能对某些人有用.
Vat*_*ine -2
Prometheus 是一个基于拉式的系统,如果您想要基于推式的监控,则需要使用某种网关。下面是一个最小的示例(没有实际执行任何有用的操作,例如启动 HTTP 侦听器,或实际对指标执行任何操作):
import (
"github.com/prometheus/client_golang/prometheus"
"net/http"
)
var responseMetric = prometheus.NewHistogram(
prometheus.HistogramOpts{
Name: "request_duration_milliseconds",
Help: "Request latency distribution",
Buckets: prometheus.ExponentialBuckets(10.0, 1.13, 40),
})
func main() {
prometheus.MustRegister(responseMetric)
http.Handle("/metrics", prometheus.Handler())
// Any other setup, then an http.ListenAndServe here
}
Run Code Online (Sandbox Code Playgroud)
然后,您需要配置 Prometheus 来抓取/metrics二进制文件提供的页面。
| 归档时间: |
|
| 查看次数: |
6608 次 |
| 最近记录: |