Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend use of initializers for config structs #1163

Merged
merged 7 commits into from
Sep 11, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Move `tools` package under `internal`. (#1141)
- Move `go.opentelemetry.io/otel/api/correlation` package to `go.opentelemetry.io/otel/api/baggage`. (#1142)
The `correlation.CorrelationContext` propagator has been renamed `baggage.Baggage`. Other exported functions and types are unchanged.
- 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)

## [0.11.0] - 2020-08-24

Expand Down
16 changes: 8 additions & 8 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ how the user can extend the configuration.
It is important that `config` are not shared across package boundaries.
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
all options to create a configured `config`.

```go
// configure returns an appropriately configured config.
func configure([]Option) config {
// newConfig returns an appropriately configured config.
func newConfig([]Option) config {
// Set default values for config.
config := config{/* […] */}
for _, option := range options {
Expand All @@ -209,7 +209,7 @@ error as well that is expected to be handled by the instantiation function
or propagated to the user.

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`

Expand All @@ -218,7 +218,7 @@ To set the value of the options a `config` contains, a corresponding

```go
type Option interface {
Apply(*Config)
Apply(*config)
}
```

Expand All @@ -244,7 +244,7 @@ func With*(…) Option { … }
```go
type defaultFalseOption bool

func (o defaultFalseOption) Apply(c *Config) {
func (o defaultFalseOption) Apply(c *config) {
c.Bool = bool(o)
}

Expand All @@ -257,7 +257,7 @@ func WithOption() Option {
```go
type defaultTrueOption bool

func (o defaultTrueOption) Apply(c *Config) {
func (o defaultTrueOption) Apply(c *config) {
c.Bool = bool(o)
}

Expand All @@ -274,7 +274,7 @@ type myTypeOption struct {
MyType MyType
}

func (o myTypeOption) Apply(c *Config) {
func (o myTypeOption) Apply(c *config) {
c.MyType = o.MyType
}

Expand Down
2 changes: 1 addition & 1 deletion api/trace/tracetest/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type config struct {
SpanRecorder SpanRecorder
}

func configure(opts ...Option) config {
func newConfig(opts ...Option) config {
conf := config{}
for _, opt := range opts {
opt.Apply(&conf)
Expand Down
2 changes: 1 addition & 1 deletion api/trace/tracetest/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var _ trace.Provider = (*Provider)(nil)

func NewProvider(opts ...Option) *Provider {
return &Provider{
config: configure(opts...),
config: newConfig(opts...),
tracers: make(map[instrumentation]*Tracer),
}
}
Expand Down
15 changes: 8 additions & 7 deletions exporters/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,15 @@ type Exporter struct {
var _ tracesdk.SpanExporter = (*Exporter)(nil)
var _ metricsdk.Exporter = (*Exporter)(nil)

func configureOptions(cfg *Config, opts ...ExporterOption) {
func NewConfig(opts ...ExporterOption) Config {
XSAM marked this conversation as resolved.
Show resolved Hide resolved
cfg := Config{
numWorkers: DefaultNumWorkers,
grpcServiceConfig: DefaultGRPCServiceConfig,
}
for _, opt := range opts {
opt(cfg)
opt(&cfg)
}
return cfg
}

func NewExporter(opts ...ExporterOption) (*Exporter, error) {
Expand All @@ -75,11 +80,7 @@ func NewExporter(opts ...ExporterOption) (*Exporter, error) {

func NewUnstartedExporter(opts ...ExporterOption) *Exporter {
e := new(Exporter)
e.c = Config{
numWorkers: DefaultNumWorkers,
grpcServiceConfig: DefaultGRPCServiceConfig,
}
configureOptions(&e.c, opts...)
e.c = NewConfig(opts...)
if len(e.c.headers) > 0 {
e.metadata = metadata.New(e.c.headers)
}
Expand Down