Skip to content
Closed
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Add `NewMiddleware` function in `go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp`. (#2964)
- Add the new `go.opentelemetry.io/contrib/instrgen` package to provide auto-generated source code instrumentation. (#3068, #3108)
- The `go.opentelemetry.io/contrib/exporters/autoexport` package to provide configuration of trace exporters with useful defaults and envar support. (#2753)
- New `WithSpanType` option in `go.opentelemetry.io/contrib/instrumentation/github.com/go-kit/kit/otelkit`. (#4044)

### Fixed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ type config struct {
// GetAttributes is an optional function that can extract trace attributes
// from the context and add them to the span.
GetAttributes func(ctx context.Context) []attribute.KeyValue

// SpanKind is the kind of span to be created by this middleware.
SpanKind trace.SpanKind
}

// Option configures an EndpointMiddleware.
Expand Down Expand Up @@ -106,3 +109,11 @@ func WithAttributeGetter(fn func(ctx context.Context) []attribute.KeyValue) Opti
o.GetAttributes = fn
})
}

// WithSpanKind sets the span kind for spans created by this middleware.
Comment thread
pellared marked this conversation as resolved.
// If this option is not provided, then trace.SpanKindServer is used.
func WithSpanKind(kind trace.SpanKind) Option {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that only two values of trace.SpanKind are valid:

  • trace.SpanKindServer
  • trace.SpanKindConsumer

Can we change and add WithSpanKindServer() and WithSpanKindConsumer() options instead?

Credits to @Aneurysm9

@jackwilsdon jackwilsdon Jul 14, 2023

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've been using SpanKindInternal on my endpoints and instead putting server/consumer on the transport's spans, as it's up to the transport whether the endpoint is called as part of a server or consumer. Changing this to just those two options somewhat limits the flexibility here.

The kit/tracing/opentracing package offers TraceEndpoint which allows using any span type.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not following. Why would you set to Internal?

as it's up to the transport whether the endpoint is called as part of a server or consumer

How would you modify the span kind? If it would be possible then you would not even need this functionality.

However, now I see that go-kit transport/endpoint can also be a "consumer" or "client" so probably it is better to keep as it is. Approving again 😉

@pellared pellared Jul 14, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked what the instrumentation does and it does not do any context extraction or injection, so is kind of makes internal spans... Also I do not think it applies any OTel Semantic Conventions.

I start to feel that this module should be deprecated and moved to https://github.com/go-kit/kit/tree/master/tracing 😬

return optionFunc(func(o *config) {
o.SpanKind = kind
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ const (
// tracing middleware, generic OpenTelemetry transport middleware or custom before
// and after transport functions.
func EndpointMiddleware(options ...Option) endpoint.Middleware {
cfg := &config{}
cfg := &config{
SpanKind: trace.SpanKindServer,
}

for _, o := range options {
o.apply(cfg)
Expand Down Expand Up @@ -71,7 +73,7 @@ func EndpointMiddleware(options ...Option) endpoint.Middleware {

opts := []trace.SpanStartOption{
trace.WithAttributes(cfg.Attributes...),
trace.WithSpanKind(trace.SpanKindServer),
trace.WithSpanKind(cfg.SpanKind),
}

if cfg.GetAttributes != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,4 +261,23 @@ func TestEndpointMiddleware(t *testing.T) {
assert.Contains(t, events[0].Attributes, attribute.String("exception.type", "go.opentelemetry.io/contrib/instrumentation/github.com/go-kit/kit/otelkit/test.customError"))
assert.Contains(t, events[0].Attributes, attribute.String("exception.message", "some business error"))
})

t.Run("CustomSpanKind", func(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

mw := otelkit.EndpointMiddleware(
otelkit.WithTracerProvider(provider),
otelkit.WithSpanKind(trace.SpanKindConsumer),
)

_, _ = mw(passEndpoint)(context.Background(), nil)

spans := sr.Ended()
require.Len(t, spans, 1)

span := spans[0]

assert.Equal(t, trace.SpanKindConsumer, span.SpanKind())
})
}