Skip to content

otelgrpc: add WithSpanKind option#8506

Merged
dmathieu merged 2 commits intoopen-telemetry:mainfrom
buhta:feature/span_kind
Feb 5, 2026
Merged

otelgrpc: add WithSpanKind option#8506
dmathieu merged 2 commits intoopen-telemetry:mainfrom
buhta:feature/span_kind

Conversation

@rauanmayemir
Copy link
Copy Markdown
Contributor

Summary

  • Add WithSpanKind option to override the default span kind for handlers

Details

This adds a WithSpanKind option to NewServerHandler and NewClientHandler that allows overriding the default span kind.

By default:

  • NewServerHandler creates spans with SpanKindServer
  • NewClientHandler creates spans with SpanKindClient

Use Case

When gRPC is used behind HTTP-to-gRPC transcoding layers (e.g., grpc-gateway, vanguard-go), the outer HTTP handler already creates a SERVER span. The inner gRPC handler creating another SERVER span results in duplicate service boundaries in trace visualizations.

Using WithSpanKind(trace.SpanKindInternal) allows marking these internal protocol translations correctly.

Example

grpc.StatsHandler(otelgrpc.NewServerHandler(
    otelgrpc.WithSpanKind(trace.SpanKindInternal),
))

Fixes #8490

Add WithSpanKind option to allow overriding the default span kind for
handlers. By default, NewServerHandler creates spans with SpanKindServer
and NewClientHandler creates spans with SpanKindClient.

This is useful when gRPC is used behind protocol translation layers
(e.g., grpc-gateway, vanguard-go) where the outer HTTP handler already
creates a SERVER span, and the inner gRPC handler should use
SpanKindInternal to avoid duplicate service boundaries in traces.

Fixes open-telemetry#8490
@rauanmayemir rauanmayemir requested review from a team and dashpole as code owners February 4, 2026 10:54
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Feb 4, 2026

CLA Signed
The committers listed above are authorized under a signed CLA.

Comment thread CHANGELOG.md Outdated
@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Feb 4, 2026

Please sign the CLA

@dmathieu dmathieu added the blocked: CLA Waiting on CLA to be signed before progress can be made label Feb 4, 2026
Co-authored-by: Damien Mathieu <42@dmathieu.com>
@rauanmayemir
Copy link
Copy Markdown
Contributor Author

rauanmayemir commented Feb 4, 2026

Should I squash and force push?

@rauanmayemir rauanmayemir requested a review from dmathieu February 4, 2026 11:23
@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Feb 4, 2026

No. We'll auto-squash when merging.

@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 4, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.2%. Comparing base (82cfc18) to head (0cf81f1).
⚠️ Report is 4 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          Coverage Diff          @@
##            main   #8506   +/-   ##
=====================================
  Coverage   82.2%   82.2%           
=====================================
  Files        179     179           
  Lines      13692   13700    +8     
=====================================
+ Hits       11257   11265    +8     
  Misses      2033    2033           
  Partials     402     402           
Files with missing lines Coverage Δ
...entation/google.golang.org/grpc/otelgrpc/config.go 87.8% <100.0%> (+0.7%) ⬆️
...n/google.golang.org/grpc/otelgrpc/stats_handler.go 98.4% <100.0%> (+<0.1%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@rauanmayemir
Copy link
Copy Markdown
Contributor Author

CLA is signed, changes are approved.
When will this be merged? (is it automatic)

@dmathieu blocked label is still there.

@dmathieu dmathieu removed the blocked: CLA Waiting on CLA to be signed before progress can be made label Feb 5, 2026
@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Feb 5, 2026

We require for PRs to be open for at least 24h before they are merged.

@dmathieu dmathieu merged commit 1a01fe1 into open-telemetry:main Feb 5, 2026
31 checks passed
@rauanmayemir rauanmayemir deleted the feature/span_kind branch February 5, 2026 10:05
@flc1125
Copy link
Copy Markdown
Member

flc1125 commented Feb 5, 2026

Sorry, although it has been merged, we might need to make some adjustments. I checked the semantic conventions for gRPC, and I think for Server or Client, their corresponding SpanKind must only be SpanKindServer and SpanKindClient, respectively.

Reference: https://opentelemetry.io/docs/specs/semconv/rpc/grpc/#client-span

This means we must enforce the definition of the corresponding Server and Client and remove the WithSpanKind() method.

@dmathieu
Copy link
Copy Markdown
Member

dmathieu commented Feb 5, 2026

Did you see the reasoning in #8490?

@flc1125
Copy link
Copy Markdown
Member

flc1125 commented Feb 5, 2026

Did you see the reasoning in #8490?

Sorry, I missed it. Currently, this PR seems reasonable.

@rauanmayemir
Copy link
Copy Markdown
Contributor Author

Had me worried for a second there. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: Configurable Span Kind for gRPC Stats Handler

5 participants