-
Notifications
You must be signed in to change notification settings - Fork 651
otelgin: Add a WithSpanStartOptions option #7261
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #7261 +/- ##
=====================================
Coverage 81.1% 81.1%
=====================================
Files 204 204
Lines 18140 18147 +7
=====================================
+ Hits 14720 14727 +7
Misses 3001 3001
Partials 419 419
🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is missing tests and a changelog entry.
@@ -89,6 +90,14 @@ func WithPropagators(propagators propagation.TextMapPropagator) Option { | |||
}) | |||
} | |||
|
|||
// WithSpanOptions configures an additional set of | |||
// trace.SpanOptions, which are applied to each new span. | |||
func WithSpanOptions(opts ...oteltrace.SpanStartOption) Option { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithSpanStartOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.
CHANGELOG.md
Outdated
@@ -21,6 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm | |||
- Allow configuring samplers in `go.opentelemetry.io/contrib/otelconf`. (#7148) | |||
- Slog log bridge now sets `SeverityText` attribute using source value in `go.opentelemetry.io/contrib/bridges/otelslog`. (#7198) | |||
- Add `http.route` metric attribute in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7275) | |||
- Add the `WithSpanOptions` option to add extra attributes, links, etc. to the spans it generates in `go.opentelemetry.io/contrib/instrumentation/github.com/gin-gonic/gin/otelgin`. (#7261) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithSpanStartOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.
@@ -31,6 +31,13 @@ const ( | |||
// server handling the request. | |||
func Middleware(service string, opts ...Option) gin.HandlerFunc { | |||
cfg := config{} | |||
|
|||
defaultOpts := []Option{ | |||
WithSpanOptions(oteltrace.WithSpanKind(oteltrace.SpanKindServer)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reason for moving this part here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This structure is also used in otelhttp
, as seen [here](
opentelemetry-go-contrib/instrumentation/net/http/otelhttp/handler.go
Lines 52 to 60 in f368d04
func NewMiddleware(operation string, opts ...Option) func(http.Handler) http.Handler { | |
h := middleware{ | |
operation: operation, | |
} | |
defaultOpts := []Option{ | |
WithSpanOptions(trace.WithSpanKind(trace.SpanKindServer)), | |
WithSpanNameFormatter(defaultHandlerFormatter), | |
} |
Let me know if you’d prefer to remove it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
According to the principle of Occam's Razor: Do not multiply entities beyond necessity.
From what I understand, the operating result would be the same even without modifying this part.
router := gin.New() | ||
router.Use(otelgin.Middleware("foobar", | ||
otelgin.WithTracerProvider(provider), | ||
otelgin.WithSpanOptions(trace.WithAttributes(customAttr)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
WithSpanStartOptions
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.
assert.Contains(t, span.Attributes(), customAttr) | ||
} | ||
|
||
func TestWithSpanOptions_PreservesDefaultSpanKind(t *testing.T) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this part can be integrated with TestWithSpanOptions_AddsCustomAttributes
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. The implementation has been incorporated into TestWithSpanOptions_CustomAttributesAndSpanKind
.
opentelemetry-go-contrib/instrumentation/github.com/gin-gonic/gin/otelgin/test/gin_test.go
Lines 270 to 273 in 9b26672
func TestWithSpanOptions_CustomAttributesAndSpanKind(t *testing.T) { | |
sr := tracetest.NewSpanRecorder() | |
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr)) |
Additionally, please correct the title of this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are ready for review, you can remove the draft mode.
Closes: #7247