Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add metrics option and prometheus handler wraps #3445

Merged
merged 5 commits into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions contrib/metric/otelmetric/otelmetric.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
package otelmetric

import (
"go.opentelemetry.io/otel/sdk/metric"

"github.com/gogf/gf/v2/os/gmetric"
)

// NewProvider creates and returns a metrics provider.
func NewProvider(option ...metric.Option) (gmetric.Provider, error) {
func NewProvider(option ...Option) (gmetric.Provider, error) {
provider, err := newProvider(option...)
if err != nil {
return nil, err
Expand All @@ -24,7 +22,7 @@ func NewProvider(option ...metric.Option) (gmetric.Provider, error) {

// MustProvider creates and returns a metrics provider.
// It panics if any error occurs.
func MustProvider(option ...metric.Option) gmetric.Provider {
func MustProvider(option ...Option) gmetric.Provider {
provider, err := NewProvider(option...)
if err != nil {
panic(err)
Expand Down
108 changes: 108 additions & 0 deletions contrib/metric/otelmetric/otelmetric_option.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// 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 otelmetric

import (
"go.opentelemetry.io/otel/sdk/metric"
"go.opentelemetry.io/otel/sdk/resource"
)

// newProviderConfigByOptions returns a config configured with options.
func newProviderConfigByOptions(options []Option) providerConfig {
conf := providerConfig{}
for _, o := range options {
conf = o.apply(conf)
}
return conf
}

// Option applies a configuration option value to a MeterProvider.
type Option interface {
apply(providerConfig) providerConfig
}

// optionFunc applies a set of options to a config.
type optionFunc func(providerConfig) providerConfig

// apply returns a config with option(s) applied.
func (o optionFunc) apply(conf providerConfig) providerConfig {
return o(conf)
}

// providerConfig is the configuration for Provider.
type providerConfig struct {
viewOption metric.Option
readerOption metric.Option
resourceOption metric.Option
enabledBuiltInMetrics bool
}

// IsBuiltInMetricsEnabled returns whether the builtin metrics is enabled.
func (cfg providerConfig) IsBuiltInMetricsEnabled() bool {
return cfg.enabledBuiltInMetrics
}

// MetricOptions converts and returns the providerConfig as metrics options.
func (cfg providerConfig) MetricOptions() []metric.Option {
var metricOptions = make([]metric.Option, 0)
if cfg.viewOption != nil {
metricOptions = append(metricOptions, cfg.viewOption)
}
if cfg.readerOption != nil {
metricOptions = append(metricOptions, cfg.readerOption)
}
if cfg.resourceOption != nil {
metricOptions = append(metricOptions, cfg.resourceOption)
}
return metricOptions
}

// WithBuiltInMetrics enables builtin metrics.
func WithBuiltInMetrics() Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.enabledBuiltInMetrics = true
return cfg
})
}

// WithResource associates a Resource with a MeterProvider. This Resource
// represents the entity producing telemetry and is associated with all Meters
// the MeterProvider will create.
func WithResource(res *resource.Resource) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.resourceOption = metric.WithResource(res)
return cfg
})
}

// WithReader associates Reader r with a MeterProvider.
//
// By default, if this option is not used, the MeterProvider will perform no
// operations; no data will be exported without a Reader.
func WithReader(reader metric.Reader) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
if reader == nil {
return cfg
}
cfg.readerOption = metric.WithReader(reader)
return cfg
})
}

// WithView associates views a MeterProvider.
//
// Views are appended to existing ones in a MeterProvider if this option is
// used multiple times.
//
// By default, if this option is not used, the MeterProvider will use the
// default view.
func WithView(views ...metric.View) Option {
return optionFunc(func(cfg providerConfig) providerConfig {
cfg.viewOption = metric.WithView(views...)
return cfg
})
}
34 changes: 34 additions & 0 deletions contrib/metric/otelmetric/otelmetric_prometheus.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// 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 otelmetric

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)

// PrometheusHandler returns the http handler for prometheus metrics exporting.
func PrometheusHandler(r *ghttp.Request) {
// Remove all builtin metrics that are produced by prometheus client.
prometheus.Unregister(collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}))
prometheus.Unregister(collectors.NewGoCollector())

handler := promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})
handler.ServeHTTP(r.Response.Writer, r.Request)
}

// StartPrometheusMetricsServer starts running a http server for metrics exporting.
func StartPrometheusMetricsServer(port int, path string) {
s := g.Server()
s.BindHandler(path, PrometheusHandler)
s.SetPort(port)
s.Run()
}
26 changes: 16 additions & 10 deletions contrib/metric/otelmetric/otelmetric_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type localProvider struct {

// newProvider creates and returns an object that implements gmetric.Provider.
// DO NOT set this as global provider internally.
func newProvider(options ...metric.Option) (gmetric.Provider, error) {
func newProvider(options ...Option) (gmetric.Provider, error) {
// TODO global logger set for otel.
// otel.SetLogger()

Expand All @@ -36,11 +36,15 @@ func newProvider(options ...metric.Option) (gmetric.Provider, error) {
builtinViews = createViewsForBuiltInMetrics()
callbacks = gmetric.GetRegisteredCallbacks()
)
options = append(options, metric.WithView(builtinViews...))
provider := &localProvider{
// MeterProvider is the core object that can create otel metrics.
MeterProvider: metric.NewMeterProvider(options...),
}
options = append(options, WithView(builtinViews...))

var (
config = newProviderConfigByOptions(options)
provider = &localProvider{
// MeterProvider is the core object that can create otel metrics.
MeterProvider: metric.NewMeterProvider(config.MetricOptions()...),
}
)

if err = provider.initializeMetrics(metrics); err != nil {
return nil, err
Expand All @@ -51,10 +55,12 @@ func newProvider(options ...metric.Option) (gmetric.Provider, error) {
}

// builtin metrics: golang.
err = runtime.Start(
runtime.WithMinimumReadMemStatsInterval(time.Second),
runtime.WithMeterProvider(provider),
)
if config.IsBuiltInMetricsEnabled() {
err = runtime.Start(
runtime.WithMinimumReadMemStatsInterval(time.Second),
runtime.WithMeterProvider(provider),
)
}
if err != nil {
return nil, gerror.WrapCode(
gcode.CodeInternalError, err, `start built-in runtime metrics failed`,
Expand Down
3 changes: 1 addition & 2 deletions contrib/metric/otelmetric/otelmetric_z_unit_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"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"
Expand Down Expand Up @@ -54,7 +53,7 @@ func Test_HTTP_Server(t *testing.T) {
}

// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(exporter))
provider := otelmetric.MustProvider(otelmetric.WithReader(exporter))
defer provider.Shutdown(ctx)

gmetric.SetGlobalProvider(provider)
Expand Down
8 changes: 4 additions & 4 deletions contrib/metric/otelmetric/otelmetric_z_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func Test_Basic(t *testing.T) {
reader := metric.NewManualReader()

// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(reader))
provider := otelmetric.MustProvider(otelmetric.WithReader(reader))
defer provider.Shutdown(ctx)

// Counter.
Expand Down Expand Up @@ -155,7 +155,7 @@ func Test_Basic(t *testing.T) {

metricsJsonContent := gjson.MustEncodeString(rm)

t.Assert(len(rm.ScopeMetrics), 5)
t.Assert(len(rm.ScopeMetrics), 4)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.counter`), 1)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.updown_counter`), 1)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.histogram`), 1)
Expand Down Expand Up @@ -269,7 +269,7 @@ func Test_GlobalAttributes(t *testing.T) {
reader := metric.NewManualReader()

// OpenTelemetry provider.
provider := otelmetric.MustProvider(metric.WithReader(reader))
provider := otelmetric.MustProvider(otelmetric.WithReader(reader))
defer provider.Shutdown(ctx)

// Add value for counter.
Expand Down Expand Up @@ -298,7 +298,7 @@ func Test_GlobalAttributes(t *testing.T) {
t.AssertNil(err)

metricsJsonContent := gjson.MustEncodeString(rm)
t.Assert(len(rm.ScopeMetrics), 4)
t.Assert(len(rm.ScopeMetrics), 3)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.counter`), 1)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.histogram`), 1)
t.Assert(gstr.Count(metricsJsonContent, `goframe.metric.demo.observable_counter`), 1)
Expand Down
13 changes: 5 additions & 8 deletions example/metric/basic/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand All @@ -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")
}
13 changes: 5 additions & 8 deletions example/metric/callback/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand All @@ -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")
}
13 changes: 5 additions & 8 deletions example/metric/dynamic_attributes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)

Expand All @@ -83,8 +83,5 @@ func main() {
})

// HTTP Server for metrics exporting.
s := g.Server()
s.BindHandler("/metrics", ghttp.WrapH(promhttp.Handler()))
s.SetPort(8000)
s.Run()
otelmetric.StartPrometheusMetricsServer(8000, "/metrics")
}