Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

arshukla98
Copy link
Contributor

Closes: #7247

@github-actions github-actions bot requested review from akats7 and flc1125 April 24, 2025 07:35
Copy link

codecov bot commented Apr 24, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 81.1%. Comparing base (c1aeca6) to head (9de11d8).
Report is 14 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@          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           
Files with missing lines Coverage Δ
...ntation/github.com/gin-gonic/gin/otelgin/config.go 100.0% <100.0%> (ø)
...umentation/github.com/gin-gonic/gin/otelgin/gin.go 93.1% <100.0%> (+0.1%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@arshukla98 arshukla98 marked this pull request as ready for review April 28, 2025 07:14
@arshukla98 arshukla98 requested a review from a team as a code owner April 28, 2025 07:14
Copy link
Member

@dmathieu dmathieu left a 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.

@arshukla98 arshukla98 marked this pull request as draft April 28, 2025 08:04
@arshukla98 arshukla98 closed this Apr 30, 2025
@arshukla98 arshukla98 reopened this Apr 30, 2025
@@ -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 {
Copy link
Member

Choose a reason for hiding this comment

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

WithSpanStartOptions

Copy link
Contributor Author

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)
Copy link
Member

Choose a reason for hiding this comment

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

WithSpanStartOptions

Copy link
Contributor Author

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)),
Copy link
Member

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?

Copy link
Contributor Author

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](

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.

Copy link
Member

@flc1125 flc1125 May 5, 2025

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)),
Copy link
Member

Choose a reason for hiding this comment

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

WithSpanStartOptions

Copy link
Contributor Author

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) {
Copy link
Member

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.

Copy link
Contributor Author

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.

func TestWithSpanOptions_CustomAttributesAndSpanKind(t *testing.T) {
sr := tracetest.NewSpanRecorder()
provider := sdktrace.NewTracerProvider(sdktrace.WithSpanProcessor(sr))

@arshukla98 arshukla98 marked this pull request as ready for review May 5, 2025 08:26
@flc1125
Copy link
Member

flc1125 commented May 5, 2025

Additionally, please correct the title of this PR.

@arshukla98 arshukla98 marked this pull request as draft May 5, 2025 13:12
@arshukla98 arshukla98 changed the title otelgin: Add a WithSpanOptions option otelgin: Add a WithSpanStartOptions option May 6, 2025
Copy link
Member

@flc1125 flc1125 left a 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.

@arshukla98 arshukla98 marked this pull request as ready for review May 6, 2025 10:23
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.

otelgin: Add a WithSpanOptions option
3 participants