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

@ -199,16 +199,16 @@ jobs:
uses: actions/checkout@v4
- name: Start Apollo Containers
run: docker-compose -f ".github/workflows/apollo/docker-compose.yml" up -d --build
run: docker compose -f ".github/workflows/apollo/docker-compose.yml" up -d --build
- name: Start Nacos Containers
run: docker-compose -f ".github/workflows/nacos/docker-compose.yml" up -d --build
run: docker compose -f ".github/workflows/nacos/docker-compose.yml" up -d --build
- name: Start Redis Cluster Containers
run: docker-compose -f ".github/workflows/redis/docker-compose.yml" up -d --build
run: docker compose -f ".github/workflows/redis/docker-compose.yml" up -d --build
- name: Start Consul Containers
run: docker-compose -f ".github/workflows/consul/docker-compose.yml" up -d --build
run: docker compose -f ".github/workflows/consul/docker-compose.yml" up -d --build
- name: Setup Golang ${{ matrix.go-version }}
uses: actions/setup-go@v5
@ -228,16 +228,16 @@ jobs:
run: bash .github/workflows/ci-main.sh coverage
- name: Stop Redis Cluster Containers
run: docker-compose -f ".github/workflows/redis/docker-compose.yml" down
run: docker compose -f ".github/workflows/redis/docker-compose.yml" down
- name: Stop Apollo Containers
run: docker-compose -f ".github/workflows/apollo/docker-compose.yml" down
run: docker compose -f ".github/workflows/apollo/docker-compose.yml" down
- name: Stop Nacos Containers
run: docker-compose -f ".github/workflows/nacos/docker-compose.yml" down
run: docker compose -f ".github/workflows/nacos/docker-compose.yml" down
- name: Stop Consul Containers
run: docker-compose -f ".github/workflows/consul/docker-compose.yml" down
run: docker compose -f ".github/workflows/consul/docker-compose.yml" down
- name: Report Coverage
uses: codecov/codecov-action@v4

View File

@ -3,13 +3,13 @@ module github.com/gogf/gf/cmd/gf/v2
go 1.18
require (
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.7.0
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.0
github.com/gogf/gf/v2 v2.7.0
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f
github.com/olekukonko/tablewriter v0.0.5
golang.org/x/mod v0.9.0

View File

@ -38,20 +38,6 @@ github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiU
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.4 h1:ggkj93Ln7ULOyoDGIGaphkg/qV8zDDSbkFVn6EljjNQ=
github.com/gogf/gf/contrib/drivers/clickhouse/v2 v2.6.4/go.mod h1:J7CWaKo4mIYv1BqfHFpTPAjs5Sm7eZrCjA2UMAfM98E=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.4 h1:S22r+bWoSSESNJh/IT9tpbgDhmWltxztiQ5ONn75BRo=
github.com/gogf/gf/contrib/drivers/mssql/v2 v2.6.4/go.mod h1:NFhR+wl4BF1W/ddH9hKXARL+g3nP+r1qeAgxqOl75bA=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.4 h1:ScG3YcYMDEP/UrwNtwQPt2noySa5ZpoV7BxrwaeBaro=
github.com/gogf/gf/contrib/drivers/mysql/v2 v2.6.4/go.mod h1:oFFE9u1zHkxVXk7ZkNipXQR9JFyDZDiixZy243ywhfQ=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.4 h1:9bOuNKHZ/ZBNuuZT2Qc0V+ypkKI2k8ZkSwcnqH+ikEY=
github.com/gogf/gf/contrib/drivers/oracle/v2 v2.6.4/go.mod h1:tK8W1hDZdwjfWh3GlNdYoBWLs6unfey2+XW8FFnWICM=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.4 h1:a+zIohGG1mv+1/9lBb/C0coM+MbmPYOWKo2SL1tJhTU=
github.com/gogf/gf/contrib/drivers/pgsql/v2 v2.6.4/go.mod h1:I7TGm/kilpNGwT8h8Frc5vGWRLgn5Vn9ce2gIBHcfbg=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4 h1:eJYZCJIqybMV6oJY7J7clWTNqx0yzZI7CaUn31igBGc=
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4/go.mod h1:3q/YKn6NL7KkUdT6nw93eM5d1QjmX/+b+CPiIm/aLiA=
github.com/gogf/gf/v2 v2.6.4 h1:w7HXdH9mcTsn/aE13CkaDbRArmAL1KS3FuQqDi6u74Y=
github.com/gogf/gf/v2 v2.6.4/go.mod h1:x2XONYcI4hRQ/4gMNbWHmZrNzSEIg20s2NULbzom5k0=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f h1:7xfXR/BhG3JDqO1s45n65Oyx9t4E/UqDOXep6jXdLCM=
github.com/gogf/selfupdate v0.0.0-20231215043001-5c48c528462f/go.mod h1:HnYoio6S7VaFJdryKcD/r9HgX+4QzYfr00XiXUo/xz0=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/apolloconfig/agollo/v4 v4.3.1
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
)
require (

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/consul/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/hashicorp/consul/api v1.24.0
github.com/hashicorp/go-cleanhttp v0.5.2
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/kubecm/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/nacos/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/nacos-group/nacos-sdk-go/v2 v2.2.5
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/config/polaris/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/polarismesh/polaris-go v1.5.5
)

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/ClickHouse/clickhouse-go/v2 v2.0.15
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/google/uuid v1.3.0
github.com/shopspring/decimal v1.3.1
)

View File

@ -6,7 +6,7 @@ replace github.com/gogf/gf/v2 => ../../../
require (
gitee.com/chunanyong/dm v1.8.12
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
)
require (

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/denisenkom/go-mssqldb v0.12.3
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
)
require (

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
)
require (

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/oracle/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/sijms/go-ora/v2 v2.7.10
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/drivers/pgsql/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/lib/pq v1.10.9
)

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/glebarez/go-sqlite v1.21.2
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
)
require (

View File

@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/drivers/sqlitecgo/v2
go 1.18
require (
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.6.4
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/contrib/drivers/sqlite/v2 v2.7.0
github.com/gogf/gf/v2 v2.7.0
github.com/mattn/go-sqlite3 v1.14.17
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/metric/otelmetric/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.1
github.com/gogf/gf/v2 v2.7.0
github.com/prometheus/client_golang v1.19.0
go.opentelemetry.io/contrib/instrumentation/runtime v0.49.0
go.opentelemetry.io/otel v1.24.0

View File

@ -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
@ -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)

View File

@ -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
})
}

View File

@ -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()
}

View File

@ -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()
@ -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
@ -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`,

View File

@ -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"
@ -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)

View File

@ -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.
@ -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)
@ -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.
@ -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)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/nosql/redis/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/redis/go-redis/v9 v9.2.1
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.14.0

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/etcd/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
go.etcd.io/etcd/client/v3 v3.5.7
)

View File

@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/registry/file/v2
go 1.18
require github.com/gogf/gf/v2 v2.6.4
require github.com/gogf/gf/v2 v2.7.0
require (
github.com/BurntSushi/toml v1.2.0 // indirect

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/nacos/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/joy999/nacos-sdk-go v0.0.0-20231120071639-10a34b3e7288
)

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/registry/polaris/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
github.com/polarismesh/polaris-go v1.5.5
)

View File

@ -4,7 +4,7 @@ go 1.18
require (
github.com/go-zookeeper/zk v1.0.3
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
golang.org/x/sync v0.4.0
)

View File

@ -3,8 +3,8 @@ module github.com/gogf/gf/contrib/rpc/grpcx/v2
go 1.18
require (
github.com/gogf/gf/contrib/registry/file/v2 v2.6.4
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/contrib/registry/file/v2 v2.7.0
github.com/gogf/gf/v2 v2.7.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/trace v1.14.0
google.golang.org/grpc v1.57.2

View File

@ -2,7 +2,7 @@ module github.com/gogf/gf/contrib/sdk/httpclient/v2
go 1.18
require github.com/gogf/gf/v2 v2.6.4
require github.com/gogf/gf/v2 v2.7.0
require (
github.com/BurntSushi/toml v1.2.0 // indirect

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/jaeger/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.4
github.com/gogf/gf/v2 v2.7.0
go.opentelemetry.io/otel v1.14.0
go.opentelemetry.io/otel/exporters/jaeger v1.14.0
go.opentelemetry.io/otel/sdk v1.14.0

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlpgrpc/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.1
github.com/gogf/gf/v2 v2.7.0
go.opentelemetry.io/otel v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.22.0

View File

@ -3,7 +3,7 @@ module github.com/gogf/gf/contrib/trace/otlphttp/v2
go 1.18
require (
github.com/gogf/gf/v2 v2.6.1
github.com/gogf/gf/v2 v2.7.0
go.opentelemetry.io/otel v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.22.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.22.0

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()
}

View File

@ -14,9 +14,17 @@ import (
// SetGlobalAttributesOption binds the global attributes to certain instrument.
type SetGlobalAttributesOption struct {
Instrument string // Instrument specifies the instrument name.
InstrumentVersion string // Instrument specifies the instrument version.
InstrumentPattern string // InstrumentPattern specifies instrument by regular expression on Instrument name.
// Instrument specifies the instrument name.
Instrument string
// Instrument specifies the instrument version.
InstrumentVersion string
// InstrumentPattern specifies instrument by regular expression on Instrument name.
// Example:
// 1. given `.+` will match all instruments.
// 2. given `github.com/gogf/gf.+` will match all goframe instruments.
InstrumentPattern string
}
// GetGlobalAttributesOption binds the global attributes to certain instrument.
@ -39,17 +47,13 @@ var (
// SetGlobalAttributes appends global attributes according `SetGlobalAttributesOption`.
// It appends global attributes to all metrics if given `SetGlobalAttributesOption` is empty.
// It appends global attributes to certain instrument by given `SetGlobalAttributesOption`.
func SetGlobalAttributes(attrs Attributes, option ...SetGlobalAttributesOption) {
func SetGlobalAttributes(attrs Attributes, option SetGlobalAttributesOption) {
globalAttributesMu.Lock()
defer globalAttributesMu.Unlock()
var usedOption SetGlobalAttributesOption
if len(option) > 0 {
usedOption = option[0]
}
globalAttributes = append(
globalAttributes, globalAttributeItem{
Attributes: attrs,
SetGlobalAttributesOption: usedOption,
SetGlobalAttributesOption: option,
},
)
}
@ -62,17 +66,21 @@ func GetGlobalAttributes(option GetGlobalAttributesOption) Attributes {
defer globalAttributesMu.Unlock()
var attributes = make(Attributes, 0)
for _, attrItem := range globalAttributes {
if option.InstrumentVersion != "" && attrItem.InstrumentVersion != option.InstrumentVersion {
continue
}
if attrItem.InstrumentPattern == "" {
if attrItem.Instrument != option.Instrument {
continue
}
} else {
// instrument name.
if attrItem.InstrumentPattern != "" {
if !gregex.IsMatchString(attrItem.InstrumentPattern, option.Instrument) {
continue
}
} else {
if (attrItem.Instrument != "" || option.Instrument != "") &&
attrItem.Instrument != option.Instrument {
continue
}
}
// instrument version.
if (attrItem.InstrumentVersion != "" || option.InstrumentVersion != "") &&
attrItem.InstrumentVersion != option.InstrumentVersion {
continue
}
attributes = append(attributes, attrItem.Attributes...)
}

View File

@ -81,10 +81,7 @@ func Test_GlobalAttributes(t *testing.T) {
t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{
Instrument: "ins_a",
InstrumentVersion: "",
}), Attributes{
NewAttribute("a", 1),
NewAttribute("d", 4),
})
}), Attributes{})
t.Assert(GetGlobalAttributes(GetGlobalAttributesOption{
Instrument: "ins_a",
InstrumentVersion: "1.1",

View File

@ -2,5 +2,5 @@ package gf
const (
// VERSION is the current GoFrame version.
VERSION = "v2.6.4"
VERSION = "v2.7.0"
)