mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
add example for exporting prometheus metrics using ghttp.Server (#3266)
This commit is contained in:
@ -1,6 +1,6 @@
|
||||
module github.com/gogf/gf/example
|
||||
|
||||
go 1.20
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.1
|
||||
@ -22,6 +22,7 @@ require (
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2
|
||||
github.com/nacos-group/nacos-sdk-go v1.1.4
|
||||
github.com/polarismesh/polaris-go v1.5.5
|
||||
github.com/prometheus/client_golang v1.17.0
|
||||
golang.org/x/time v0.3.0
|
||||
google.golang.org/grpc v1.59.0
|
||||
google.golang.org/protobuf v1.31.0
|
||||
@ -93,7 +94,6 @@ require (
|
||||
github.com/pelletier/go-toml v1.9.3 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/polarismesh/specification v1.4.1 // indirect
|
||||
github.com/prometheus/client_golang v1.17.0 // indirect
|
||||
github.com/prometheus/client_model v0.4.1-0.20230718164431-9a2bf3000d16 // indirect
|
||||
github.com/prometheus/common v0.44.0 // indirect
|
||||
github.com/prometheus/procfs v0.11.1 // indirect
|
||||
|
||||
51
example/metric/prometheus/prometheus.go
Normal file
51
example/metric/prometheus/prometheus.go
Normal file
@ -0,0 +1,51 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
)
|
||||
|
||||
// Demo metric type Counter
|
||||
var metricCounter = promauto.NewCounter(
|
||||
prometheus.CounterOpts{
|
||||
Name: "demo_counter",
|
||||
Help: "A demo counter.",
|
||||
},
|
||||
)
|
||||
|
||||
// Demo metric type Gauge.
|
||||
var metricGauge = promauto.NewGauge(
|
||||
prometheus.GaugeOpts{
|
||||
Name: "demo_gauge",
|
||||
Help: "A demo gauge.",
|
||||
},
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Create prometheus metric registry.
|
||||
registry := prometheus.NewRegistry()
|
||||
registry.MustRegister(
|
||||
metricCounter,
|
||||
metricGauge,
|
||||
)
|
||||
|
||||
// Start metric http server.
|
||||
s := g.Server()
|
||||
s.SetPort(8000)
|
||||
// Fake metric values.
|
||||
// http://127.0.0.1:8000/
|
||||
s.BindHandler("/", func(r *ghttp.Request) {
|
||||
metricCounter.Add(1)
|
||||
metricGauge.Set(float64(grand.N(1, 100)))
|
||||
r.Response.Write("fake ok")
|
||||
})
|
||||
// Export metric values.
|
||||
// You can view http://127.0.0.1:8000/metrics to see all metric values.
|
||||
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
|
||||
s.Run()
|
||||
}
|
||||
Reference in New Issue
Block a user