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 all 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
- 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)
- 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

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
22 changes: 11 additions & 11 deletions exporters/otlp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ const (
}`
)

type ExporterOption func(*Config)
type ExporterOption func(*config)

type Config struct {
type config struct {
canDialInsecure bool
collectorAddr string
compressor string
Expand All @@ -80,7 +80,7 @@ func WorkerCount(n uint) ExporterOption {
if n == 0 {
n = DefaultNumWorkers
}
return func(cfg *Config) {
return func(cfg *config) {
cfg.numWorkers = n
}
}
Expand All @@ -89,7 +89,7 @@ func WorkerCount(n uint) ExporterOption {
// 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() ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.canDialInsecure = true
}
}
Expand All @@ -98,15 +98,15 @@ func WithInsecure() ExporterOption {
// connect to the collector on. If unset, it will instead try to use
// connect to DefaultCollectorHost:DefaultCollectorPort.
func WithAddress(addr string) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.collectorAddr = addr
}
}

// WithReconnectionPeriod allows one to set the delay between next connection attempt
// after failing to connect with the collector.
func WithReconnectionPeriod(rp time.Duration) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.reconnectionPeriod = rp
}
}
Expand All @@ -117,14 +117,14 @@ func WithReconnectionPeriod(rp time.Duration) ExporterOption {
// 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) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.compressor = compressor
}
}

// WithHeaders will send the provided headers with gRPC requests
func WithHeaders(headers map[string]string) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.headers = headers
}
}
Expand All @@ -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
// or by certificate rotation, so it is up to the caller to decide what to use.
func WithTLSCredentials(creds credentials.TransportCredentials) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.clientCredentials = creds
}
}

// WithGRPCServiceConfig defines the default gRPC service config used.
func WithGRPCServiceConfig(serviceConfig string) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.grpcServiceConfig = serviceConfig
}
}
Expand All @@ -151,7 +151,7 @@ func WithGRPCServiceConfig(serviceConfig string) ExporterOption {
// with some other configuration the GRPC specified via the collector the ones here will
// take preference since they are set last.
func WithGRPCDialOption(opts ...grpc.DialOption) ExporterOption {
return func(cfg *Config) {
return func(cfg *config) {
cfg.grpcDialOptions = opts
}
}
21 changes: 13 additions & 8 deletions exporters/otlp/otlp.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,27 @@ type Exporter struct {

backgroundConnectionDoneCh chan bool

c Config
c config
metadata metadata.MD
}

var _ tracesdk.SpanExporter = (*Exporter)(nil)
var _ metricsdk.Exporter = (*Exporter)(nil)

func configureOptions(cfg *Config, opts ...ExporterOption) {
// newConfig initializes a config struct with default values and applies
// any ExporterOptions provided.
func newConfig(opts ...ExporterOption) config {
cfg := config{
numWorkers: DefaultNumWorkers,
grpcServiceConfig: DefaultGRPCServiceConfig,
}
for _, opt := range opts {
opt(cfg)
opt(&cfg)
}
return cfg
}

// NewExporter constructs a new Exporter and starts it.
func NewExporter(opts ...ExporterOption) (*Exporter, error) {
exp := NewUnstartedExporter(opts...)
if err := exp.Start(); err != nil {
Expand All @@ -73,13 +81,10 @@ func NewExporter(opts ...ExporterOption) (*Exporter, error) {
return exp, nil
}

// NewUnstartedExporter constructs a new Exporter and does not start it.
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
4 changes: 2 additions & 2 deletions exporters/stdout/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ type Config struct {
DisableMetricExport bool
}

// Configure creates a validated Config configured with options.
func Configure(options ...Option) (Config, error) {
// NewConfig creates a validated Config configured with options.
func NewConfig(options ...Option) (Config, error) {
config := Config{
Writer: defaultWriter,
PrettyPrint: defaultPrettyPrint,
Expand Down
2 changes: 1 addition & 1 deletion exporters/stdout/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (

// NewExporter creates an Exporter with the passed options.
func NewExporter(options ...Option) (*Exporter, error) {
config, err := Configure(options...)
config, err := NewConfig(options...)
if err != nil {
return nil, err
}
Expand Down