1
0
mirror of https://github.com/gogf/gf.git synced 2025-04-05 03:05:05 +08:00

56 lines
1.3 KiB
Go

// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package main
import (
"time"
"go.opentelemetry.io/otel/exporters/prometheus"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
)
func main() {
var ctx = gctx.New()
// Prometheus exporter to export metrics as Prometheus format.
exporter, err := prometheus.New(
prometheus.WithoutCounterSuffixes(),
prometheus.WithoutUnits(),
)
if err != nil {
g.Log().Fatal(ctx, err)
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Write("ok")
})
s.BindHandler("/error", func(r *ghttp.Request) {
panic("error")
})
s.BindHandler("/sleep", func(r *ghttp.Request) {
time.Sleep(time.Second * 5)
r.Response.Write("ok")
})
s.BindHandler("/metrics", otelmetric.PrometheusHandler)
s.SetPort(8000)
s.Run()
}