Skip to content

Commit

Permalink
Update metrics and tracing configs (#2463)
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenlj authored Oct 31, 2023
1 parent 15c43b8 commit 689115e
Show file tree
Hide file tree
Showing 11 changed files with 148 additions and 104 deletions.
6 changes: 0 additions & 6 deletions client/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,12 +354,6 @@ func WithForce(force bool) ClientOption {
}
}

func WithTracingKey(tracingKey string) ClientOption {
return func(opts *ClientOptions) {
opts.Reference.TracingKey = tracingKey
}
}

func WithMeshProviderPort(port int) ClientOption {
return func(opts *ClientOptions) {
opts.Reference.MeshProviderPort = port
Expand Down
39 changes: 21 additions & 18 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ func compatRootConfig(c *InstanceOptions) *config.RootConfig {
regCompat[k] = compatRegistryConfig(v)
}

traCompat := make(map[string]*config.TracingConfig)
for k, v := range c.Tracing {
traCompat[k] = compatTracingConfig(v)
}

return &config.RootConfig{
Application: compatApplicationConfig(c.Application),
Protocols: proCompat,
Expand All @@ -54,7 +49,7 @@ func compatRootConfig(c *InstanceOptions) *config.RootConfig {
Provider: compatProviderConfig(c.Provider),
Consumer: compatConsumerConfig(c.Consumer),
Metric: compatMetricConfig(c.Metric),
Tracing: traCompat,
Otel: compatOtelConfig(c.Otel),
Logger: compatLoggerConfig(c.Logger),
Shutdown: compatShutdownConfig(c.Shutdown),
EventDispatcherType: c.EventDispatcherType,
Expand Down Expand Up @@ -273,24 +268,32 @@ func compatMetricConfig(c *global.MetricConfig) *config.MetricConfig {
return nil
}
return &config.MetricConfig{
Enable: c.Enable,
Port: c.Port,
Path: c.Path,
Prometheus: compatMetricPrometheusConfig(c.Prometheus),
Aggregation: compatMetricAggregationConfig(c.Aggregation),
Protocol: c.Protocol,
Enable: c.Enable,
Port: c.Port,
Path: c.Path,
Prometheus: compatMetricPrometheusConfig(c.Prometheus),
Aggregation: compatMetricAggregationConfig(c.Aggregation),
Protocol: c.Protocol,
EnableMetadata: c.EnableMetadata,
EnableRegistry: c.EnableRegistry,
EnableConfigCenter: c.EnableConfigCenter,
EnableRpc: c.EnableRpc,
}
}

func compatTracingConfig(c *global.TracingConfig) *config.TracingConfig {
func compatOtelConfig(c *global.OtelConfig) *config.OtelConfig {
if c == nil {
return nil
}
return &config.TracingConfig{
Name: c.Name,
ServiceName: c.ServiceName,
Address: c.Address,
UseAgent: c.UseAgent,
return &config.OtelConfig{
TraceConfig: &config.OtelTraceConfig{
Enable: c.TraceConfig.Enable,
Exporter: c.TraceConfig.Exporter,
Endpoint: c.TraceConfig.Endpoint,
Propagator: c.TraceConfig.Propagator,
SampleMode: c.TraceConfig.SampleMode,
SampleRatio: c.TraceConfig.SampleRatio,
},
}
}

Expand Down
1 change: 1 addition & 0 deletions config/tracing_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
)

// TracingConfig is the configuration of the tracing.
// It's designed to be replaced with config.OtelConfig
type TracingConfig struct {
Name string `default:"jaeger" yaml:"name" json:"name,omitempty" property:"name"` // jaeger or zipkin(todo)
ServiceName string `yaml:"serviceName" json:"serviceName,omitempty" property:"serviceName"`
Expand Down
4 changes: 0 additions & 4 deletions dubbo.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func (ins *Instance) NewServer(opts ...server.ServerOption) (*server.Server, err
appCfg := ins.insOpts.Application
regsCfg := ins.insOpts.Registries
prosCfg := ins.insOpts.Protocols
trasCfg := ins.insOpts.Tracing
sdCfg := ins.insOpts.Shutdown
if appCfg != nil {
srvOpts = append(srvOpts,
Expand All @@ -126,9 +125,6 @@ func (ins *Instance) NewServer(opts ...server.ServerOption) (*server.Server, err
if prosCfg != nil {
srvOpts = append(srvOpts, server.SetServer_Protocols(prosCfg))
}
if trasCfg != nil {
srvOpts = append(srvOpts, server.SetServer_Tracings(trasCfg))
}
if sdCfg != nil {
srvOpts = append(srvOpts, server.SetServer_Shutdown(sdCfg))
}
Expand Down
16 changes: 10 additions & 6 deletions global/metric_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ package global

// MetricConfig This is the config struct for all metrics implementation
type MetricConfig struct {
Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"`
Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"`
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"`
Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"`
Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"`
Port string `default:"9090" yaml:"port" json:"port,omitempty" property:"port"`
Path string `default:"/metrics" yaml:"path" json:"path,omitempty" property:"path"`
Protocol string `default:"prometheus" yaml:"protocol" json:"protocol,omitempty" property:"protocol"`
Prometheus *PrometheusConfig `yaml:"prometheus" json:"prometheus" property:"prometheus"`
Aggregation *AggregateConfig `yaml:"aggregation" json:"aggregation" property:"aggregation"`
EnableMetadata *bool `default:"true" yaml:"enable-metadata" json:"enable-metadata,omitempty" property:"enable-metadata"`
EnableRegistry *bool `default:"true" yaml:"enable-registry" json:"enable-registry,omitempty" property:"enable-registry"`
EnableConfigCenter *bool `default:"true" yaml:"enable-config-center" json:"enable-config-center,omitempty" property:"enable-config-center"`
EnableRpc *bool `default:"true" yaml:"enable-rpc" json:"enable-rpc,omitempty" property:"enable-rpc"`
}

type AggregateConfig struct {
Expand Down
38 changes: 38 additions & 0 deletions global/otel_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package global

// OtelConfig is the configuration of the tracing.
type OtelConfig struct {
TraceConfig *OtelTraceConfig `yaml:"trace" json:"trace,omitempty" property:"trace"`
}

type OtelTraceConfig struct {
Enable *bool `default:"false" yaml:"enable" json:"enable,omitempty" property:"enable"`
Exporter string `default:"stdout" yaml:"exporter" json:"exporter,omitempty" property:"exporter"` // stdout, jaeger, zipkin, otlp-http, otlp-grpc
Endpoint string `default:"" yaml:"endpoint" json:"endpoint,omitempty" property:"endpoint"`
Propagator string `default:"w3c" yaml:"propagator" json:"propagator,omitempty" property:"propagator"` // one of w3c(standard), b3(for zipkin),
SampleMode string `default:"ratio" yaml:"sample-mode" json:"sample-mode,omitempty" property:"sample-mode"` // one of always, never, ratio
SampleRatio float64 `default:"0.5" yaml:"sample-ratio" json:"sample-ratio,omitempty" property:"sample-ratio"` // [0.0, 1.0]
}

func DefaultOtelConfig() *OtelConfig {
return &OtelConfig{
TraceConfig: &OtelTraceConfig{},
}
}
22 changes: 22 additions & 0 deletions metrics/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ func WithPrometheusGatewayInterval(interval time.Duration) Option {
}
}

func WithConfigCenterEnabled() Option {
return func(opts *Options) {
b := true
opts.Metric.EnableConfigCenter = &b
}
}

func WithMetadataEnabled() Option {
return func(opts *Options) {
b := true
opts.Metric.EnableMetadata = &b
}
}

func WithRegistryEnabled() Option {
return func(opts *Options) {
b := true
opts.Metric.EnableRegistry = &b
}
}

// WithEnabled this will enable rpc and tracing by default, config-center, metadata and registry metrics will still be in disable state.
func WithEnabled() Option {
return func(opts *Options) {
b := true
Expand Down
16 changes: 6 additions & 10 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type InstanceOptions struct {
Provider *global.ProviderConfig `yaml:"provider" json:"provider" property:"provider"`
Consumer *global.ConsumerConfig `yaml:"consumer" json:"consumer" property:"consumer"`
Metric *global.MetricConfig `yaml:"metrics" json:"metrics,omitempty" property:"metrics"`
Tracing map[string]*global.TracingConfig `yaml:"tracing" json:"tracing,omitempty" property:"tracing"`
Otel *global.OtelConfig `yaml:"otel" json:"otel,omitempty" property:"otel"`
Logger *global.LoggerConfig `yaml:"logger" json:"logger,omitempty" property:"logger"`
Shutdown *global.ShutdownConfig `yaml:"shutdown" json:"shutdown,omitempty" property:"shutdown"`
// todo(DMwangnima): router feature would be supported in the future
Expand All @@ -66,7 +66,7 @@ func defaultInstanceOptions() *InstanceOptions {
Provider: global.DefaultProviderConfig(),
Consumer: global.DefaultConsumerConfig(),
Metric: global.DefaultMetricConfig(),
Tracing: make(map[string]*global.TracingConfig),
Otel: global.DefaultOtelConfig(),
Logger: global.DefaultLoggerConfig(),
Shutdown: global.DefaultShutdownConfig(),
Custom: global.DefaultCustomConfig(),
Expand Down Expand Up @@ -134,10 +134,8 @@ func (rc *InstanceOptions) init(opts ...InstanceOption) error {
if err := rcCompat.Metric.Init(rcCompat); err != nil {
return err
}
for _, t := range rcCompat.Tracing {
if err := t.Init(); err != nil {
return err
}
if err := rcCompat.Otel.Init(rcCompat.Application); err != nil {
return err
}

routers := rcCompat.Router
Expand Down Expand Up @@ -247,14 +245,12 @@ func WithRegistry(opts ...registry.Option) InstanceOption {
}
}

// WithTracing otel configuration, currently only supports tracing
func WithTracing(opts ...trace.Option) InstanceOption {
traceOpts := trace.NewOptions(opts...)

return func(insOpts *InstanceOptions) {
if insOpts.Tracing == nil {
insOpts.Tracing = make(map[string]*global.TracingConfig)
}
insOpts.Tracing[traceOpts.ID] = traceOpts.Tracing
insOpts.Otel.TraceConfig = traceOpts.Otel.TraceConfig
}
}

Expand Down
81 changes: 50 additions & 31 deletions otel/trace/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,87 +17,106 @@

package trace

import (
"fmt"
)

import (
"dubbo.apache.org/dubbo-go/v3/global"
)

type Options struct {
Tracing *global.TracingConfig

ID string
Otel *global.OtelConfig
}

func defaultOptions() *Options {
return &Options{Tracing: &global.TracingConfig{}}
return &Options{Otel: global.DefaultOtelConfig()}
}

func NewOptions(opts ...Option) *Options {
defOpts := defaultOptions()
for _, opt := range opts {
opt(defOpts)
}
return defOpts
}

type Option func(*Options)

func WithEnabled() Option {
return func(opts *Options) {
b := true
opts.Otel.TraceConfig.Enable = &b
}
}

if defOpts.Tracing.Name == "" {
panic(fmt.Sprintf("Please specify the tracing system to use, eg. WithZipkin()"))
func WithStdoutExporter() Option {
return func(opts *Options) {
opts.Otel.TraceConfig.Exporter = "stdout"
}
if defOpts.ID == "" {
defOpts.ID = defOpts.Tracing.Name
}

func WithJaegerExporter() Option {
return func(opts *Options) {
opts.Otel.TraceConfig.Exporter = "jaeger"
}
}

return defOpts
func WithZipkinExporter() Option {
return func(opts *Options) {
opts.Otel.TraceConfig.Exporter = "zipkin"
}
}

type Option func(*Options)
func WithOtlpHttpExporter() Option {
return func(opts *Options) {
opts.Otel.TraceConfig.Exporter = "otlp-http"
}
}

func WithID(id string) Option {
func WithOtlpGrpcExporter() Option {
return func(opts *Options) {
opts.ID = id
opts.Otel.TraceConfig.Exporter = "otlp-grpc"
}
}

func WithZipkin() Option {
// WithW3cPropagator w3c(standard)
func WithW3cPropagator() Option {
return func(opts *Options) {
opts.Tracing.Name = "zipkin"
opts.Otel.TraceConfig.Propagator = "w3c"
}
}

func WithJaeger() Option {
// WithB3Propagator b3(for zipkin)
func WithB3Propagator() Option {
return func(opts *Options) {
opts.Tracing.Name = "jaeger"
opts.Otel.TraceConfig.Propagator = "b3"
}
}

func WithOltp() Option {
// WithRatio only takes effect when WithRatioMode is set
func WithRatio(ratio float64) Option {
return func(opts *Options) {
opts.Tracing.Name = "oltp"
opts.Otel.TraceConfig.SampleRatio = ratio
}
}

func WithStdout() Option {
func WithRatioMode() Option {
return func(opts *Options) {
opts.Tracing.Name = "stdout"
opts.Otel.TraceConfig.Propagator = "ratio"
}
}

func WithServiceName(name string) Option {
func WithAlwaysMode() Option {
return func(opts *Options) {
opts.Tracing.ServiceName = name
opts.Otel.TraceConfig.Propagator = "always"
}
}

func WithAddress(address string) Option {
func WithNeverMode() Option {
return func(opts *Options) {
opts.Tracing.Address = address
opts.Otel.TraceConfig.Propagator = "never"
}
}

func WithUseAgent() Option {
func WithEndpoint(endpoint string) Option {
return func(opts *Options) {
b := true
opts.Tracing.UseAgent = &b
opts.Otel.TraceConfig.Endpoint = endpoint
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 689115e

Please sign in to comment.