feat: add metrics option and prometheus handler wraps (#3445)

This commit is contained in:
John Guo
2024-04-07 09:57:13 +08:00
committed by GitHub
parent db5eed17b1
commit 1b7aea0ced
47 changed files with 309 additions and 189 deletions

View File

@ -3,29 +3,28 @@ module github.com/gogf/gf/example
go 1.18
require (
github.com/gogf/gf/contrib/config/apollo/v2 v2.6.1
github.com/gogf/gf/contrib/config/consul/v2 v2.6.1
github.com/gogf/gf/contrib/config/kubecm/v2 v2.6.1
github.com/gogf/gf/contrib/config/nacos/v2 v2.6.1
github.com/gogf/gf/contrib/config/polaris/v2 v2.6.1
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.1
github.com/gogf/gf/contrib/metric/otelmetric/v2 v2.6.1
github.com/gogf/gf/contrib/nosql/redis/v2 v2.6.1
github.com/gogf/gf/contrib/registry/etcd/v2 v2.6.1
github.com/gogf/gf/contrib/registry/file/v2 v2.6.4
github.com/gogf/gf/contrib/registry/nacos/v2 v2.6.1
github.com/gogf/gf/contrib/registry/polaris/v2 v2.6.1
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.6.1
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.6.1
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.6.1
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/contrib/config/apollo/v2 v2.7.0
github.com/gogf/gf/contrib/config/consul/v2 v2.7.0
github.com/gogf/gf/contrib/config/kubecm/v2 v2.7.0
github.com/gogf/gf/contrib/config/nacos/v2 v2.7.0
github.com/gogf/gf/contrib/config/polaris/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.0
github.com/gogf/gf/contrib/metric/otelmetric/v2 v2.7.0
github.com/gogf/gf/contrib/nosql/redis/v2 v2.7.0
github.com/gogf/gf/contrib/registry/etcd/v2 v2.7.0
github.com/gogf/gf/contrib/registry/file/v2 v2.7.0
github.com/gogf/gf/contrib/registry/nacos/v2 v2.7.0
github.com/gogf/gf/contrib/registry/polaris/v2 v2.7.0
github.com/gogf/gf/contrib/rpc/grpcx/v2 v2.7.0
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 v2.7.0
github.com/gogf/gf/contrib/trace/otlphttp/v2 v2.7.0
github.com/gogf/gf/v2 v2.7.0
github.com/hashicorp/consul/api v1.24.0
github.com/hashicorp/go-cleanhttp v0.5.2
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
github.com/polarismesh/polaris-go v1.5.5
github.com/prometheus/client_golang v1.19.0
go.opentelemetry.io/otel/exporters/prometheus v0.46.0
go.opentelemetry.io/otel/sdk/metric v1.24.0
golang.org/x/time v0.5.0
google.golang.org/grpc v1.60.1
google.golang.org/protobuf v1.32.0
@ -122,6 +121,7 @@ require (
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/sdk v1.24.0 // indirect
go.opentelemetry.io/otel/sdk/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.opentelemetry.io/proto/otlp v1.1.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
@ -165,6 +165,7 @@ replace (
github.com/gogf/gf/contrib/nosql/redis/v2 => ../contrib/nosql/redis/
github.com/gogf/gf/contrib/registry/etcd/v2 => ../contrib/registry/etcd/
github.com/gogf/gf/contrib/registry/file/v2 => ../contrib/registry/file/
github.com/gogf/gf/contrib/registry/nacos/v2 => ../contrib/registry/nacos/
github.com/gogf/gf/contrib/registry/polaris/v2 => ../contrib/registry/polaris/
github.com/gogf/gf/contrib/rpc/grpcx/v2 => ../contrib/rpc/grpcx/
github.com/gogf/gf/contrib/trace/otlpgrpc/v2 => ../contrib/trace/otlpgrpc

View File

@ -304,8 +304,6 @@ github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 h1:p104kn46Q8WdvHunIJ9dAyjPVtrBPhSr3KT2yUst43I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/gogf/gf/contrib/registry/nacos/v2 v2.6.1 h1:yWmm+1yjk8JDh1CUYjAvOV7pBN400xJluw07w7U1GsI=
github.com/gogf/gf/contrib/registry/nacos/v2 v2.6.1/go.mod h1:1j1dsYPFByfzK9UHk7XBqRu5jNKEDO3hgwgwmThfa5c=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

View File

@ -9,13 +9,10 @@ package main
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"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/v2/os/gmetric"
)
@ -31,7 +28,7 @@ var (
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
gmetric.NewAttribute("const_attr_1", 1),
},
},
)
@ -41,7 +38,7 @@ var (
Help: "This is a simple demo for UpDownCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_2", 2),
gmetric.NewAttribute("const_attr_2", 2),
},
},
)
@ -51,7 +48,7 @@ var (
Help: "This is a simple demo for histogram usage",
Unit: "ms",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_3", 3),
gmetric.NewAttribute("const_attr_3", 3),
},
Buckets: []float64{0, 10, 20, 50, 100, 500, 1000, 2000, 5000, 10000},
},
@ -62,7 +59,7 @@ var (
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_4", 4),
gmetric.NewAttribute("const_attr_4", 4),
},
},
)
@ -72,7 +69,7 @@ var (
Help: "This is a simple demo for ObservableUpDownCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_5", 5),
gmetric.NewAttribute("const_attr_5", 5),
},
},
)
@ -82,7 +79,7 @@ var (
Help: "This is a simple demo for ObservableGauge usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_6", 6),
gmetric.NewAttribute("const_attr_6", 6),
},
},
)
@ -109,7 +106,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -132,8 +132,5 @@ func main() {
histogram.Record(20000)
// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -9,13 +9,10 @@ package main
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"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/v2/os/gmetric"
)
@ -31,7 +28,7 @@ var (
Help: "This is a simple demo for Counter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
gmetric.NewAttribute("const_attr_1", 1),
},
},
)
@ -42,7 +39,7 @@ var (
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_3", 3),
gmetric.NewAttribute("const_attr_3", 3),
},
Callback: func(ctx context.Context, obs gmetric.MetricObserver) error {
obs.Observe(10)
@ -65,7 +62,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -74,8 +74,5 @@ func main() {
counter.Add(ctx, 10)
// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -9,13 +9,10 @@ package main
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"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/v2/os/gmetric"
)
@ -31,7 +28,7 @@ var (
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
gmetric.NewAttribute("const_attr_1", 1),
},
},
)
@ -41,7 +38,7 @@ var (
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_4", 4),
gmetric.NewAttribute("const_attr_4", 4),
},
},
)
@ -54,7 +51,7 @@ func main() {
meter.MustRegisterCallback(func(ctx context.Context, obs gmetric.Observer) error {
obs.Observe(observableCounter, 10, gmetric.Option{
Attributes: gmetric.Attributes{
gmetric.NewAttribute("dynamic_label_1", 1),
gmetric.NewAttribute("dynamic_attr_1", 1),
},
})
return nil
@ -70,7 +67,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -78,13 +78,10 @@ func main() {
counter.Inc(ctx)
counter.Add(ctx, 10, gmetric.Option{
Attributes: gmetric.Attributes{
gmetric.NewAttribute("dynamic_label_2", 2),
gmetric.NewAttribute("dynamic_attr_2", 2),
},
})
// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -9,13 +9,10 @@ package main
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"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/v2/os/gmetric"
)
@ -36,7 +33,7 @@ var (
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
gmetric.NewAttribute("const_attr_1", 1),
},
},
)
@ -46,7 +43,7 @@ var (
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_2", 2),
gmetric.NewAttribute("const_attr_2", 2),
},
},
)
@ -56,7 +53,7 @@ func main() {
var ctx = gctx.New()
gmetric.SetGlobalAttributes(gmetric.Attributes{
gmetric.NewAttribute("g1", 1),
gmetric.NewAttribute("global_attr_1", 1),
}, gmetric.SetGlobalAttributesOption{
Instrument: instrument,
InstrumentVersion: instrumentVersion,
@ -79,7 +76,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -88,8 +88,5 @@ func main() {
counter.Add(ctx, 10)
// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -7,13 +7,11 @@
package main
import (
"go.opentelemetry.io/otel/exporters/prometheus"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gctx"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
)
func main() {
@ -29,7 +27,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -39,8 +40,5 @@ func main() {
g.Log().Infof(ctx, `content length from "%s": %d`, url, len(content))
// A simple http server for metrics export.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -7,11 +7,10 @@
package main
import (
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"time"
"go.opentelemetry.io/otel/exporters/prometheus"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
@ -31,7 +30,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -46,7 +48,7 @@ func main() {
time.Sleep(time.Second * 5)
r.Response.Write("ok")
})
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.BindHandler("/metrics", otelmetric.PrometheusHandler)
s.SetPort(8000)
s.Run()
}

View File

@ -9,13 +9,10 @@ package main
import (
"context"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.opentelemetry.io/otel/exporters/prometheus"
"go.opentelemetry.io/otel/sdk/metric"
"github.com/gogf/gf/contrib/metric/otelmetric/v2"
"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/v2/os/gmetric"
)
@ -40,7 +37,7 @@ var (
Help: "This is a simple demo for Counter usage",
Unit: "bytes",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_1", 1),
gmetric.NewAttribute("const_attr_1", 1),
},
},
)
@ -50,7 +47,7 @@ var (
Help: "This is a simple demo for ObservableCounter usage",
Unit: "%",
Attributes: gmetric.Attributes{
gmetric.NewAttribute("const_label_2", 2),
gmetric.NewAttribute("const_attr_2", 2),
},
},
)
@ -75,7 +72,10 @@ func main() {
}
// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(
otelmetric.WithReader(exporter),
otelmetric.WithBuiltInMetrics(),
)
provider.SetAsGlobal()
defer provider.Shutdown(ctx)
@ -84,8 +84,5 @@ func main() {
counter.Add(ctx, 10)
// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}

View File

@ -42,7 +42,6 @@ func main() {
// 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) {
@ -53,5 +52,6 @@ func main() {
// 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.SetPort(8000)
s.Run()
}