Recommend use of initializers for config structs (#1163)

* Recommend use of initializers for config structs

Updates style guide and changes existing configure() implementations to
newConfig() as appropriate.

* Add CHANGELOG.md entry

* Update stdout exporter config initializer

* Add documentation to exported functions

* Convert otlp.Config to unexported otlp.config
This commit is contained in:
Anthony Mirabella 2020-09-11 17:14:25 -04:00 committed by GitHub
parent c9726efd60
commit d91de437b2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 33 deletions

View File

@ -35,6 +35,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Rename `ParentOrElse` sampler to `ParentBased` and allow setting samplers depending on parent span. (#1153) - Rename `ParentOrElse` sampler to `ParentBased` and allow setting samplers depending on parent span. (#1153)
- In the `go.opentelemetry.io/otel/api/trace` package, `SpanConfigure` was renamed to `NewSpanConfig`. (#1155) - In the `go.opentelemetry.io/otel/api/trace` package, `SpanConfigure` was renamed to `NewSpanConfig`. (#1155)
- Change `dependabot.yml` to add a `Skip Changelog` label to dependabot-sourced PRs. (#1161) - Change `dependabot.yml` to add a `Skip Changelog` label to dependabot-sourced PRs. (#1161)
- The [configuration style guide](https://github.com/open-telemetry/opentelemetry-go/blob/master/CONTRIBUTING.md#config) has been updated to
recommend the use of `newConfig()` instead of `configure()`. (#1163)
- The `otlp.Config` type has been unexported and changed to `otlp.config`, along with its initializer. (#1163)
## [0.11.0] - 2020-08-24 ## [0.11.0] - 2020-08-24

View File

@ -187,13 +187,13 @@ how the user can extend the configuration.
It is important that `config` are not shared across package boundaries. It is important that `config` are not shared across package boundaries.
Meaning a `config` from one package should not be directly used by another. Meaning a `config` from one package should not be directly used by another.
Optionally, it is common to include a `configure` function (with the same Optionally, it is common to include a `newConfig` function (with the same
naming scheme). This function wraps any defaults setting and looping over naming scheme). This function wraps any defaults setting and looping over
all options to create a configured `config`. all options to create a configured `config`.
```go ```go
// configure returns an appropriately configured config. // newConfig returns an appropriately configured config.
func configure([]Option) config { func newConfig([]Option) config {
// Set default values for config. // Set default values for config.
config := config{/* […] */} config := config{/* […] */}
for _, option := range options { for _, option := range options {
@ -209,7 +209,7 @@ error as well that is expected to be handled by the instantiation function
or propagated to the user. or propagated to the user.
Given the design goal of not having the user need to work with the `config`, Given the design goal of not having the user need to work with the `config`,
the `configure` function should also be unexported. the `newConfig` function should also be unexported.
#### `Option` #### `Option`
@ -218,7 +218,7 @@ To set the value of the options a `config` contains, a corresponding
```go ```go
type Option interface { type Option interface {
Apply(*Config) Apply(*config)
} }
``` ```
@ -244,7 +244,7 @@ func With*(…) Option { … }
```go ```go
type defaultFalseOption bool type defaultFalseOption bool
func (o defaultFalseOption) Apply(c *Config) { func (o defaultFalseOption) Apply(c *config) {
c.Bool = bool(o) c.Bool = bool(o)
} }
@ -257,7 +257,7 @@ func WithOption() Option {
```go ```go
type defaultTrueOption bool type defaultTrueOption bool
func (o defaultTrueOption) Apply(c *Config) { func (o defaultTrueOption) Apply(c *config) {
c.Bool = bool(o) c.Bool = bool(o)
} }
@ -274,7 +274,7 @@ type myTypeOption struct {
MyType MyType MyType MyType
} }
func (o myTypeOption) Apply(c *Config) { func (o myTypeOption) Apply(c *config) {
c.MyType = o.MyType c.MyType = o.MyType
} }

View File

@ -49,7 +49,7 @@ type config struct {
SpanRecorder SpanRecorder SpanRecorder SpanRecorder
} }
func configure(opts ...Option) config { func newConfig(opts ...Option) config {
conf := config{} conf := config{}
for _, opt := range opts { for _, opt := range opts {
opt.Apply(&conf) opt.Apply(&conf)

View File

@ -31,7 +31,7 @@ var _ trace.Provider = (*Provider)(nil)
func NewProvider(opts ...Option) *Provider { func NewProvider(opts ...Option) *Provider {
return &Provider{ return &Provider{
config: configure(opts...), config: newConfig(opts...),
tracers: make(map[instrumentation]*Tracer), tracers: make(map[instrumentation]*Tracer),
} }
} }

View File

@ -61,9 +61,9 @@ const (
}` }`
) )
type ExporterOption func(*Config) type ExporterOption func(*config)
type Config struct { type config struct {
canDialInsecure bool canDialInsecure bool
collectorAddr string collectorAddr string
compressor string compressor string
@ -80,7 +80,7 @@ func WorkerCount(n uint) ExporterOption {
if n == 0 { if n == 0 {
n = DefaultNumWorkers n = DefaultNumWorkers
} }
return func(cfg *Config) { return func(cfg *config) {
cfg.numWorkers = n cfg.numWorkers = n
} }
} }
@ -89,7 +89,7 @@ func WorkerCount(n uint) ExporterOption {
// just like grpc.WithInsecure() https://pkg.go.dev/google.golang.org/grpc#WithInsecure // 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. // does. Note, by default, client security is required unless WithInsecure is used.
func WithInsecure() ExporterOption { func WithInsecure() ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.canDialInsecure = true cfg.canDialInsecure = true
} }
} }
@ -98,7 +98,7 @@ func WithInsecure() ExporterOption {
// connect to the collector on. If unset, it will instead try to use // connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort. // connect to DefaultCollectorHost:DefaultCollectorPort.
func WithAddress(addr string) ExporterOption { func WithAddress(addr string) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.collectorAddr = addr cfg.collectorAddr = addr
} }
} }
@ -106,7 +106,7 @@ func WithAddress(addr string) ExporterOption {
// WithReconnectionPeriod allows one to set the delay between next connection attempt // WithReconnectionPeriod allows one to set the delay between next connection attempt
// after failing to connect with the collector. // after failing to connect with the collector.
func WithReconnectionPeriod(rp time.Duration) ExporterOption { func WithReconnectionPeriod(rp time.Duration) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.reconnectionPeriod = rp cfg.reconnectionPeriod = rp
} }
} }
@ -117,14 +117,14 @@ func WithReconnectionPeriod(rp time.Duration) ExporterOption {
// compressors auto-register on import, such as gzip, which can be registered by calling // compressors auto-register on import, such as gzip, which can be registered by calling
// `import _ "google.golang.org/grpc/encoding/gzip"` // `import _ "google.golang.org/grpc/encoding/gzip"`
func WithCompressor(compressor string) ExporterOption { func WithCompressor(compressor string) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.compressor = compressor cfg.compressor = compressor
} }
} }
// WithHeaders will send the provided headers with gRPC requests // WithHeaders will send the provided headers with gRPC requests
func WithHeaders(headers map[string]string) ExporterOption { func WithHeaders(headers map[string]string) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.headers = headers cfg.headers = headers
} }
} }
@ -135,14 +135,14 @@ func WithHeaders(headers map[string]string) ExporterOption {
// these credentials can be done in many ways e.g. plain file, in code tls.Config // 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. // or by certificate rotation, so it is up to the caller to decide what to use.
func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption { func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.clientCredentials = creds cfg.clientCredentials = creds
} }
} }
// WithGRPCServiceConfig defines the default gRPC service config used. // WithGRPCServiceConfig defines the default gRPC service config used.
func WithGRPCServiceConfig(serviceConfig string) ExporterOption { func WithGRPCServiceConfig(serviceConfig string) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.grpcServiceConfig = serviceConfig cfg.grpcServiceConfig = serviceConfig
} }
} }
@ -151,7 +151,7 @@ func WithGRPCServiceConfig(serviceConfig string) ExporterOption {
// with some other configuration the GRPC specified via the collector the ones here will // with some other configuration the GRPC specified via the collector the ones here will
// take preference since they are set last. // take preference since they are set last.
func WithGRPCDialOption(opts ...grpc.DialOption) ExporterOption { func WithGRPCDialOption(opts ...grpc.DialOption) ExporterOption {
return func(cfg *Config) { return func(cfg *config) {
cfg.grpcDialOptions = opts cfg.grpcDialOptions = opts
} }
} }

View File

@ -52,19 +52,27 @@ type Exporter struct {
backgroundConnectionDoneCh chan bool backgroundConnectionDoneCh chan bool
c Config c config
metadata metadata.MD metadata metadata.MD
} }
var _ tracesdk.SpanExporter = (*Exporter)(nil) var _ tracesdk.SpanExporter = (*Exporter)(nil)
var _ metricsdk.Exporter = (*Exporter)(nil) var _ metricsdk.Exporter = (*Exporter)(nil)
func configureOptions(cfg *Config, opts ...ExporterOption) { // newConfig initializes a config struct with default values and applies
for _, opt := range opts { // any ExporterOptions provided.
opt(cfg) func newConfig(opts ...ExporterOption) config {
cfg := config{
numWorkers: DefaultNumWorkers,
grpcServiceConfig: DefaultGRPCServiceConfig,
} }
for _, opt := range opts {
opt(&cfg)
}
return cfg
} }
// NewExporter constructs a new Exporter and starts it.
func NewExporter(opts ...ExporterOption) (*Exporter, error) { func NewExporter(opts ...ExporterOption) (*Exporter, error) {
exp := NewUnstartedExporter(opts...) exp := NewUnstartedExporter(opts...)
if err := exp.Start(); err != nil { if err := exp.Start(); err != nil {
@ -73,13 +81,10 @@ func NewExporter(opts ...ExporterOption) (*Exporter, error) {
return exp, nil return exp, nil
} }
// NewUnstartedExporter constructs a new Exporter and does not start it.
func NewUnstartedExporter(opts ...ExporterOption) *Exporter { func NewUnstartedExporter(opts ...ExporterOption) *Exporter {
e := new(Exporter) e := new(Exporter)
e.c = Config{ e.c = newConfig(opts...)
numWorkers: DefaultNumWorkers,
grpcServiceConfig: DefaultGRPCServiceConfig,
}
configureOptions(&e.c, opts...)
if len(e.c.headers) > 0 { if len(e.c.headers) > 0 {
e.metadata = metadata.New(e.c.headers) e.metadata = metadata.New(e.c.headers)
} }

View File

@ -64,8 +64,8 @@ type Config struct {
DisableMetricExport bool DisableMetricExport bool
} }
// Configure creates a validated Config configured with options. // NewConfig creates a validated Config configured with options.
func Configure(options ...Option) (Config, error) { func NewConfig(options ...Option) (Config, error) {
config := Config{ config := Config{
Writer: defaultWriter, Writer: defaultWriter,
PrettyPrint: defaultPrettyPrint, PrettyPrint: defaultPrettyPrint,

View File

@ -37,7 +37,7 @@ var (
// NewExporter creates an Exporter with the passed options. // NewExporter creates an Exporter with the passed options.
func NewExporter(options ...Option) (*Exporter, error) { func NewExporter(options ...Option) (*Exporter, error) {
config, err := Configure(options...) config, err := NewConfig(options...)
if err != nil { if err != nil {
return nil, err return nil, err
} }