-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add example for exporting prometheus metrics using
ghttp.Server
(#3266
- Loading branch information
Showing
3 changed files
with
67 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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() | ||
} |