diff --git a/.github/workflows/ci-main.yml b/.github/workflows/ci-main.yml index 7d8b7e08afa..949cdd7f3d9 100644 --- a/.github/workflows/ci-main.yml +++ b/.github/workflows/ci-main.yml @@ -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 diff --git a/cmd/gf/go.mod b/cmd/gf/go.mod index 40aa38e8fa1..04758b7dfee 100644 --- a/cmd/gf/go.mod +++ b/cmd/gf/go.mod @@ -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 diff --git a/cmd/gf/go.sum b/cmd/gf/go.sum index 99713497257..349b148ec49 100644 --- a/cmd/gf/go.sum +++ b/cmd/gf/go.sum @@ -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= diff --git a/contrib/config/apollo/go.mod b/contrib/config/apollo/go.mod index cbeb7bc0172..b5491d8039d 100644 --- a/contrib/config/apollo/go.mod +++ b/contrib/config/apollo/go.mod @@ -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 ( diff --git a/contrib/config/consul/go.mod b/contrib/config/consul/go.mod index 39bc67d45dd..661ef42cda5 100644 --- a/contrib/config/consul/go.mod +++ b/contrib/config/consul/go.mod @@ -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 ) diff --git a/contrib/config/kubecm/go.mod b/contrib/config/kubecm/go.mod index 2e9dfbfc3ac..ec02a81e5b1 100644 --- a/contrib/config/kubecm/go.mod +++ b/contrib/config/kubecm/go.mod @@ -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 diff --git a/contrib/config/nacos/go.mod b/contrib/config/nacos/go.mod index 4b94745a5a3..2bff274dc64 100644 --- a/contrib/config/nacos/go.mod +++ b/contrib/config/nacos/go.mod @@ -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 ) diff --git a/contrib/config/polaris/go.mod b/contrib/config/polaris/go.mod index 92aee5bbcf2..c6de1225844 100644 --- a/contrib/config/polaris/go.mod +++ b/contrib/config/polaris/go.mod @@ -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 ) diff --git a/contrib/drivers/clickhouse/go.mod b/contrib/drivers/clickhouse/go.mod index c8f363cae44..5a5b4751a85 100644 --- a/contrib/drivers/clickhouse/go.mod +++ b/contrib/drivers/clickhouse/go.mod @@ -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 ) diff --git a/contrib/drivers/dm/go.mod b/contrib/drivers/dm/go.mod index 8b4e2906ba3..b235bb0abe5 100644 --- a/contrib/drivers/dm/go.mod +++ b/contrib/drivers/dm/go.mod @@ -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 ( diff --git a/contrib/drivers/mssql/go.mod b/contrib/drivers/mssql/go.mod index e030295c628..d1fce18a7f7 100644 --- a/contrib/drivers/mssql/go.mod +++ b/contrib/drivers/mssql/go.mod @@ -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 ( diff --git a/contrib/drivers/mysql/go.mod b/contrib/drivers/mysql/go.mod index 0def3b10cd7..36489962da8 100644 --- a/contrib/drivers/mysql/go.mod +++ b/contrib/drivers/mysql/go.mod @@ -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 ( diff --git a/contrib/drivers/oracle/go.mod b/contrib/drivers/oracle/go.mod index c2cc4ba25a9..c9586e101e0 100644 --- a/contrib/drivers/oracle/go.mod +++ b/contrib/drivers/oracle/go.mod @@ -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 ) diff --git a/contrib/drivers/pgsql/go.mod b/contrib/drivers/pgsql/go.mod index 25cc91b466c..3c957b9d109 100644 --- a/contrib/drivers/pgsql/go.mod +++ b/contrib/drivers/pgsql/go.mod @@ -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 ) diff --git a/contrib/drivers/sqlite/go.mod b/contrib/drivers/sqlite/go.mod index 70d05516f72..bc7afb7f381 100644 --- a/contrib/drivers/sqlite/go.mod +++ b/contrib/drivers/sqlite/go.mod @@ -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 ( diff --git a/contrib/drivers/sqlitecgo/go.mod b/contrib/drivers/sqlitecgo/go.mod index 58780219b4a..15f45a45dce 100644 --- a/contrib/drivers/sqlitecgo/go.mod +++ b/contrib/drivers/sqlitecgo/go.mod @@ -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 ) diff --git a/contrib/metric/otelmetric/go.mod b/contrib/metric/otelmetric/go.mod index 78637e24162..f4595318d4d 100644 --- a/contrib/metric/otelmetric/go.mod +++ b/contrib/metric/otelmetric/go.mod @@ -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 diff --git a/contrib/metric/otelmetric/otelmetric.go b/contrib/metric/otelmetric/otelmetric.go index e42e2fa07eb..1de7d30c722 100644 --- a/contrib/metric/otelmetric/otelmetric.go +++ b/contrib/metric/otelmetric/otelmetric.go @@ -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) diff --git a/contrib/metric/otelmetric/otelmetric_option.go b/contrib/metric/otelmetric/otelmetric_option.go new file mode 100644 index 00000000000..a1aa0604b0b --- /dev/null +++ b/contrib/metric/otelmetric/otelmetric_option.go @@ -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 + }) +} diff --git a/contrib/metric/otelmetric/otelmetric_prometheus.go b/contrib/metric/otelmetric/otelmetric_prometheus.go new file mode 100644 index 00000000000..4f341558a7b --- /dev/null +++ b/contrib/metric/otelmetric/otelmetric_prometheus.go @@ -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() +} diff --git a/contrib/metric/otelmetric/otelmetric_provider.go b/contrib/metric/otelmetric/otelmetric_provider.go index 653a1bed394..57ac895b822 100644 --- a/contrib/metric/otelmetric/otelmetric_provider.go +++ b/contrib/metric/otelmetric/otelmetric_provider.go @@ -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`, diff --git a/contrib/metric/otelmetric/otelmetric_z_unit_http_test.go b/contrib/metric/otelmetric/otelmetric_z_unit_http_test.go index 129f925ae4e..36a644f0d8e 100644 --- a/contrib/metric/otelmetric/otelmetric_z_unit_http_test.go +++ b/contrib/metric/otelmetric/otelmetric_z_unit_http_test.go @@ -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) diff --git a/contrib/metric/otelmetric/otelmetric_z_unit_test.go b/contrib/metric/otelmetric/otelmetric_z_unit_test.go index db1d398cbff..9b6b78fe6f6 100644 --- a/contrib/metric/otelmetric/otelmetric_z_unit_test.go +++ b/contrib/metric/otelmetric/otelmetric_z_unit_test.go @@ -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) diff --git a/contrib/nosql/redis/go.mod b/contrib/nosql/redis/go.mod index a987c24c7c6..f4ffb5a41a1 100644 --- a/contrib/nosql/redis/go.mod +++ b/contrib/nosql/redis/go.mod @@ -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 diff --git a/contrib/registry/etcd/go.mod b/contrib/registry/etcd/go.mod index 8473aafc496..36bcfdfe975 100644 --- a/contrib/registry/etcd/go.mod +++ b/contrib/registry/etcd/go.mod @@ -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 ) diff --git a/contrib/registry/file/go.mod b/contrib/registry/file/go.mod index 5a49114b254..0a9e35939e8 100644 --- a/contrib/registry/file/go.mod +++ b/contrib/registry/file/go.mod @@ -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 diff --git a/contrib/registry/nacos/go.mod b/contrib/registry/nacos/go.mod index c0f1c272d6b..01e321c1d9c 100644 --- a/contrib/registry/nacos/go.mod +++ b/contrib/registry/nacos/go.mod @@ -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 ) diff --git a/contrib/registry/polaris/go.mod b/contrib/registry/polaris/go.mod index 7802c55dbcb..371b397d6b5 100644 --- a/contrib/registry/polaris/go.mod +++ b/contrib/registry/polaris/go.mod @@ -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 ) diff --git a/contrib/registry/zookeeper/go.mod b/contrib/registry/zookeeper/go.mod index bf405bb4805..9a5194934b9 100644 --- a/contrib/registry/zookeeper/go.mod +++ b/contrib/registry/zookeeper/go.mod @@ -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 ) diff --git a/contrib/rpc/grpcx/go.mod b/contrib/rpc/grpcx/go.mod index 09f926cf701..d46f66108fd 100644 --- a/contrib/rpc/grpcx/go.mod +++ b/contrib/rpc/grpcx/go.mod @@ -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 diff --git a/contrib/sdk/httpclient/go.mod b/contrib/sdk/httpclient/go.mod index 6b3d72bbb82..7085acdd6b2 100644 --- a/contrib/sdk/httpclient/go.mod +++ b/contrib/sdk/httpclient/go.mod @@ -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 diff --git a/contrib/trace/jaeger/go.mod b/contrib/trace/jaeger/go.mod index 7542437a5b1..be2bd4a8675 100644 --- a/contrib/trace/jaeger/go.mod +++ b/contrib/trace/jaeger/go.mod @@ -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 diff --git a/contrib/trace/otlpgrpc/go.mod b/contrib/trace/otlpgrpc/go.mod index 93fcdfe415f..a29ada4e2d1 100644 --- a/contrib/trace/otlpgrpc/go.mod +++ b/contrib/trace/otlpgrpc/go.mod @@ -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 diff --git a/contrib/trace/otlphttp/go.mod b/contrib/trace/otlphttp/go.mod index 9bd149ba7fa..b5144c7e426 100644 --- a/contrib/trace/otlphttp/go.mod +++ b/contrib/trace/otlphttp/go.mod @@ -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 diff --git a/example/go.mod b/example/go.mod index ee9c232e971..e78dcd0a8b1 100644 --- a/example/go.mod +++ b/example/go.mod @@ -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 diff --git a/example/go.sum b/example/go.sum index 213adfc229e..b4f129fa5cd 100644 --- a/example/go.sum +++ b/example/go.sum @@ -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= diff --git a/example/metric/basic/main.go b/example/metric/basic/main.go index 354108d88de..d31c696dd25 100644 --- a/example/metric/basic/main.go +++ b/example/metric/basic/main.go @@ -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") } diff --git a/example/metric/callback/main.go b/example/metric/callback/main.go index 263f5c12bba..3cc4021000c 100644 --- a/example/metric/callback/main.go +++ b/example/metric/callback/main.go @@ -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") } diff --git a/example/metric/dynamic_attributes/main.go b/example/metric/dynamic_attributes/main.go index 00475666102..291cf396942 100644 --- a/example/metric/dynamic_attributes/main.go +++ b/example/metric/dynamic_attributes/main.go @@ -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") } diff --git a/example/metric/global_attributes/main.go b/example/metric/global_attributes/main.go index 1684afede50..53149d7b1ea 100644 --- a/example/metric/global_attributes/main.go +++ b/example/metric/global_attributes/main.go @@ -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") } diff --git a/example/metric/http_client/main.go b/example/metric/http_client/main.go index 4729639e6a6..08830920443 100644 --- a/example/metric/http_client/main.go +++ b/example/metric/http_client/main.go @@ -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") } diff --git a/example/metric/http_server/main.go b/example/metric/http_server/main.go index db86b8106b3..be86932923b 100644 --- a/example/metric/http_server/main.go +++ b/example/metric/http_server/main.go @@ -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() } diff --git a/example/metric/meter_attributes/main.go b/example/metric/meter_attributes/main.go index 5a42da83897..1ef26cd8ada 100644 --- a/example/metric/meter_attributes/main.go +++ b/example/metric/meter_attributes/main.go @@ -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") } diff --git a/example/metric/prometheus/prometheus.go b/example/metric/prometheus/prometheus.go index 5b2f44c7a28..6eff369c5cb 100644 --- a/example/metric/prometheus/prometheus.go +++ b/example/metric/prometheus/prometheus.go @@ -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() } diff --git a/os/gmetric/gmetric_global_attributes.go b/os/gmetric/gmetric_global_attributes.go index cc4c7e64604..fa00d7c134a 100644 --- a/os/gmetric/gmetric_global_attributes.go +++ b/os/gmetric/gmetric_global_attributes.go @@ -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,18 +66,22 @@ 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 { + // instrument name. + if attrItem.InstrumentPattern != "" { + if !gregex.IsMatchString(attrItem.InstrumentPattern, option.Instrument) { continue } } else { - if !gregex.IsMatchString(attrItem.InstrumentPattern, option.Instrument) { + 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...) } return attributes diff --git a/os/gmetric/gmetric_z_unit_internal_test.go b/os/gmetric/gmetric_z_unit_internal_test.go index 34aafff9d57..f155b04a2a5 100644 --- a/os/gmetric/gmetric_z_unit_internal_test.go +++ b/os/gmetric/gmetric_z_unit_internal_test.go @@ -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", diff --git a/version.go b/version.go index d994d629da4..c2afe8a5351 100644 --- a/version.go +++ b/version.go @@ -2,5 +2,5 @@ package gf const ( // VERSION is the current GoFrame version. - VERSION = "v2.6.4" + VERSION = "v2.7.0" )