Skip to content

Commit

Permalink
Refactor configs (#1882)
Browse files Browse the repository at this point in the history
* trace: Refactor sampling config

* tracer: Refactor TracerProviderConfig

* Update the changelog

* Refactor sdk/metric/controller/basic config

* Refactor sdk/metric/processor/basic config

* Refactor sdk/resource config

* Refactor oteltest config

* Refactor exporters/otlp configs

* Refactor exporters/stdout config

* Refactor exporters/trace/jaeger configs

* Refactor exporters/trace/zipkin config

* Unexport stdout.NewConfig

* Refactor zipkin.go

* Refactor provider.go
  • Loading branch information
pellared authored May 14, 2021
1 parent 6324ada commit d23cc61
Show file tree
Hide file tree
Showing 27 changed files with 299 additions and 285 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Spans created by the global `Tracer` obtained from `go.opentelemetry.io/otel`, prior to a functioning `TracerProvider` being set, now propagate the span context from their parent if one exists. (#1901)
- The `"go.opentelemetry.io/otel".Tracer` function now accepts tracer options. (#1902)
- Move the `go.opentelemetry.io/otel/unit` package to `go.opentelemetry.io/otel/metric/unit`. (#1903)
- Refactor option types according to the contribution style guide. (#1882)

### Deprecated

Expand Down
20 changes: 14 additions & 6 deletions exporters/otlp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ const (
)

// ExporterOption are setting options passed to an Exporter on creation.
type ExporterOption func(*config)
type ExporterOption interface {
apply(*config)
}

type exporterOptionFunc func(*config)

func (fn exporterOptionFunc) apply(cfg *config) {
fn(cfg)
}

type config struct {
exportKindSelector metricsdk.ExportKindSelector
Expand All @@ -39,14 +47,14 @@ type config struct {
// aggregation). If not specified otherwise, exporter will use a
// cumulative export kind selector.
func WithMetricExportKindSelector(selector metricsdk.ExportKindSelector) ExporterOption {
return func(cfg *config) {
return exporterOptionFunc(func(cfg *config) {
cfg.exportKindSelector = selector
}
})
}

// SplitDriverOption provides options for setting up a split driver.
type SplitDriverOption interface {
Apply(*splitDriver)
apply(*splitDriver)
}

// WithMetricDriver allows one to set the driver used for metrics
Expand All @@ -59,7 +67,7 @@ type metricDriverOption struct {
driver ProtocolDriver
}

func (o metricDriverOption) Apply(s *splitDriver) {
func (o metricDriverOption) apply(s *splitDriver) {
s.metric = o.driver
}

Expand All @@ -73,6 +81,6 @@ type traceDriverOption struct {
driver ProtocolDriver
}

func (o traceDriverOption) Apply(s *splitDriver) {
func (o traceDriverOption) apply(s *splitDriver) {
s.trace = o.driver
}
2 changes: 1 addition & 1 deletion exporters/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewUnstartedExporter(driver ProtocolDriver, opts ...ExporterOption) *Export
exportKindSelector: metricsdk.CumulativeExportKindSelector(),
}
for _, opt := range opts {
opt(&cfg)
opt.apply(&cfg)
}
return &Exporter{
cfg: cfg,
Expand Down
2 changes: 1 addition & 1 deletion exporters/otlp/otlpgrpc/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func NewDriver(opts ...Option) otlp.ProtocolDriver {
cfg := otlpconfig.NewDefaultConfig()
otlpconfig.ApplyGRPCEnvConfigs(&cfg)
for _, opt := range opts {
opt.ApplyGRPCOption(&cfg)
opt.applyGRPCOption(&cfg)
}

d := &driver{}
Expand Down
66 changes: 37 additions & 29 deletions exporters/otlp/otlpgrpc/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,57 +28,65 @@ import (

// Option applies an option to the gRPC driver.
type Option interface {
applyGRPCOption(*otlpconfig.Config)
}

type wrappedOption struct {
otlpconfig.GRPCOption
}

func (w wrappedOption) applyGRPCOption(cfg *otlpconfig.Config) {
w.ApplyGRPCOption(cfg)
}

// WithInsecure disables client transport security for the exporter's gRPC connection
// just like grpc.WithInsecure() https://pkg.go.dev/google.golang.org/grpc#WithInsecure
// does. Note, by default, client security is required unless WithInsecure is used.
func WithInsecure() Option {
return otlpconfig.WithInsecure()
return wrappedOption{otlpconfig.WithInsecure()}
}

// WithTracesInsecure disables client transport security for the traces exporter's gRPC connection
// just like grpc.WithInsecure() https://pkg.go.dev/google.golang.org/grpc#WithInsecure
// does. Note, by default, client security is required unless WithInsecure is used.
func WithTracesInsecure() Option {
return otlpconfig.WithInsecureTraces()
return wrappedOption{otlpconfig.WithInsecureTraces()}
}

// WithInsecureMetrics disables client transport security for the metrics exporter's gRPC connection
// just like grpc.WithInsecure() https://pkg.go.dev/google.golang.org/grpc#WithInsecure
// does. Note, by default, client security is required unless WithInsecure is used.
func WithInsecureMetrics() Option {
return otlpconfig.WithInsecureMetrics()
return wrappedOption{otlpconfig.WithInsecureMetrics()}
}

// WithEndpoint allows one to set the endpoint that the exporter will
// connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort.
func WithEndpoint(endpoint string) Option {
return otlpconfig.WithEndpoint(endpoint)
return wrappedOption{otlpconfig.WithEndpoint(endpoint)}
}

// WithTracesEndpoint allows one to set the traces endpoint that the exporter will
// connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort.
func WithTracesEndpoint(endpoint string) Option {
return otlpconfig.WithTracesEndpoint(endpoint)
return wrappedOption{otlpconfig.WithTracesEndpoint(endpoint)}
}

// WithMetricsEndpoint allows one to set the metrics endpoint that the exporter will
// connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort.
func WithMetricsEndpoint(endpoint string) Option {
return otlpconfig.WithMetricsEndpoint(endpoint)
return wrappedOption{otlpconfig.WithMetricsEndpoint(endpoint)}
}

// WithReconnectionPeriod allows one to set the delay between next connection attempt
// after failing to connect with the collector.
func WithReconnectionPeriod(rp time.Duration) otlpconfig.GRPCOption {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
func WithReconnectionPeriod(rp time.Duration) Option {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.ReconnectionPeriod = rp
})
})}
}

func compressorToCompression(compressor string) otlp.Compression {
Expand All @@ -97,7 +105,7 @@ func compressorToCompression(compressor string) otlp.Compression {
// compressors auto-register on import, such as gzip, which can be registered by calling
// `import _ "google.golang.org/grpc/encoding/gzip"`.
func WithCompressor(compressor string) Option {
return otlpconfig.WithCompression(compressorToCompression(compressor))
return wrappedOption{otlpconfig.WithCompression(compressorToCompression(compressor))}
}

// WithTracesCompression will set the compressor for the gRPC client to use when sending traces requests.
Expand All @@ -106,7 +114,7 @@ func WithCompressor(compressor string) Option {
// compressors auto-register on import, such as gzip, which can be registered by calling
// `import _ "google.golang.org/grpc/encoding/gzip"`.
func WithTracesCompression(compressor string) Option {
return otlpconfig.WithTracesCompression(compressorToCompression(compressor))
return wrappedOption{otlpconfig.WithTracesCompression(compressorToCompression(compressor))}
}

// WithMetricsCompression will set the compressor for the gRPC client to use when sending metrics requests.
Expand All @@ -115,22 +123,22 @@ func WithTracesCompression(compressor string) Option {
// compressors auto-register on import, such as gzip, which can be registered by calling
// `import _ "google.golang.org/grpc/encoding/gzip"`.
func WithMetricsCompression(compressor string) Option {
return otlpconfig.WithMetricsCompression(compressorToCompression(compressor))
return wrappedOption{otlpconfig.WithMetricsCompression(compressorToCompression(compressor))}
}

// WithHeaders will send the provided headers with gRPC requests.
func WithHeaders(headers map[string]string) Option {
return otlpconfig.WithHeaders(headers)
return wrappedOption{otlpconfig.WithHeaders(headers)}
}

// WithTracesHeaders will send the provided headers with gRPC traces requests.
func WithTracesHeaders(headers map[string]string) Option {
return otlpconfig.WithTracesHeaders(headers)
return wrappedOption{otlpconfig.WithTracesHeaders(headers)}
}

// WithMetricsHeaders will send the provided headers with gRPC metrics requests.
func WithMetricsHeaders(headers map[string]string) Option {
return otlpconfig.WithMetricsHeaders(headers)
return wrappedOption{otlpconfig.WithMetricsHeaders(headers)}
}

// WithTLSCredentials allows the connection to use TLS credentials
Expand All @@ -139,10 +147,10 @@ func WithMetricsHeaders(headers map[string]string) Option {
// these credentials can be done in many ways e.g. plain file, in code tls.Config
// or by certificate rotation, so it is up to the caller to decide what to use.
func WithTLSCredentials(creds credentials.TransportCredentials) Option {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.Traces.GRPCCredentials = creds
cfg.Metrics.GRPCCredentials = creds
})
})}
}

// WithTracesTLSCredentials allows the connection to use TLS credentials
Expand All @@ -151,9 +159,9 @@ func WithTLSCredentials(creds credentials.TransportCredentials) Option {
// these credentials can be done in many ways e.g. plain file, in code tls.Config
// or by certificate rotation, so it is up to the caller to decide what to use.
func WithTracesTLSCredentials(creds credentials.TransportCredentials) Option {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.Traces.GRPCCredentials = creds
})
})}
}

// WithMetricsTLSCredentials allows the connection to use TLS credentials
Expand All @@ -162,43 +170,43 @@ func WithTracesTLSCredentials(creds credentials.TransportCredentials) Option {
// these credentials can be done in many ways e.g. plain file, in code tls.Config
// or by certificate rotation, so it is up to the caller to decide what to use.
func WithMetricsTLSCredentials(creds credentials.TransportCredentials) Option {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.Metrics.GRPCCredentials = creds
})
})}
}

// WithServiceConfig defines the default gRPC service config used.
func WithServiceConfig(serviceConfig string) Option {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.ServiceConfig = serviceConfig
})
})}
}

// WithDialOption opens support to any grpc.DialOption to be used. If it conflicts
// with some other configuration the GRPC specified via the collector the ones here will
// take preference since they are set last.
func WithDialOption(opts ...grpc.DialOption) Option {
return otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
return wrappedOption{otlpconfig.NewGRPCOption(func(cfg *otlpconfig.Config) {
cfg.DialOptions = opts
})
})}
}

// WithTimeout tells the driver the max waiting time for the backend to process
// each spans or metrics batch. If unset, the default will be 10 seconds.
func WithTimeout(duration time.Duration) Option {
return otlpconfig.WithTimeout(duration)
return wrappedOption{otlpconfig.WithTimeout(duration)}
}

// WithTracesTimeout tells the driver the max waiting time for the backend to process
// each spans batch. If unset, the default will be 10 seconds.
func WithTracesTimeout(duration time.Duration) Option {
return otlpconfig.WithTracesTimeout(duration)
return wrappedOption{otlpconfig.WithTracesTimeout(duration)}
}

// WithMetricsTimeout tells the driver the max waiting time for the backend to process
// each metrics batch. If unset, the default will be 10 seconds.
func WithMetricsTimeout(duration time.Duration) Option {
return otlpconfig.WithMetricsTimeout(duration)
return wrappedOption{otlpconfig.WithMetricsTimeout(duration)}
}

// WithRetry configures the retry policy for transient errors that may occurs when
Expand All @@ -207,5 +215,5 @@ func WithMetricsTimeout(duration time.Duration) Option {
// retry policy will retry after 5 seconds and increase exponentially after each
// error for a total of 1 minute.
func WithRetry(settings otlp.RetrySettings) Option {
return otlpconfig.WithRetry(settings)
return wrappedOption{otlpconfig.WithRetry(settings)}
}
2 changes: 1 addition & 1 deletion exporters/otlp/otlphttp/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewDriver(opts ...Option) otlp.ProtocolDriver {
cfg := otlpconfig.NewDefaultConfig()
otlpconfig.ApplyHTTPEnvConfigs(&cfg)
for _, opt := range opts {
opt.ApplyHTTPOption(&cfg)
opt.applyHTTPOption(&cfg)
}

for pathPtr, defaultPath := range map[*string]string{
Expand Down
Loading

0 comments on commit d23cc61

Please sign in to comment.