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

otelkit: Refactor option type #858

Merged
merged 2 commits into from
Jul 20, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
34 changes: 21 additions & 13 deletions instrumentation/github.com/go-kit/kit/otelkit/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,49 +50,57 @@ type config struct {
}

// Option configures an EndpointMiddleware.
type Option func(*config)
type Option interface {
apply(*config)
}

type optionFunc func(*config)

func (o optionFunc) apply(c *config) {
o(c)
}

// WithTracerProvider specifies a tracer provider to use for creating a tracer.
// If none is specified, the global provider is used.
func WithTracerProvider(provider trace.TracerProvider) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.TracerProvider = provider
}
})
}

// WithIgnoreBusinessError if set to true will not treat a business error
// identified through the endpoint.Failer interface as a span error.
func WithIgnoreBusinessError(val bool) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.IgnoreBusinessError = val
}
})
}

// WithOperation sets an operation name for an endpoint.
// Use this when you register a middleware for each endpoint.
func WithOperation(operation string) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.Operation = operation
}
})
}

// WithOperationGetter sets an operation name getter function in config.
func WithOperationGetter(fn func(ctx context.Context, name string) string) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.GetOperation = fn
}
})
}

// WithAttributes sets the default attributes for the spans created by the Endpoint tracer.
func WithAttributes(attrs ...attribute.KeyValue) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.Attributes = attrs
}
})
}

// WithAttributeGetter extracts additional attributes from the context.
func WithAttributeGetter(fn func(ctx context.Context) []attribute.KeyValue) Option {
return func(o *config) {
return optionFunc(func(o *config) {
o.GetAttributes = fn
}
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func EndpointMiddleware(options ...Option) endpoint.Middleware {
cfg := &config{}

for _, o := range options {
o(cfg)
o.apply(cfg)
}

if cfg.TracerProvider == nil {
Expand Down