Skip to content

Add middleware option to supply StartSpanOptions#80

Open
vpranckaitis wants to merge 3 commits into
opentracing-contrib:masterfrom
vpranckaitis:middleware_start_span_options
Open

Add middleware option to supply StartSpanOptions#80
vpranckaitis wants to merge 3 commits into
opentracing-contrib:masterfrom
vpranckaitis:middleware_start_span_options

Conversation

@vpranckaitis

@vpranckaitis vpranckaitis commented Mar 21, 2025

Copy link
Copy Markdown

This commit adds a middleware option that supplies StartSpanOptions for StartSpan() call.

Concrete use case where this is useful: adding additional tags to the span through opentracing.Tag{} option, so that those tags could be used for deciding whether to sample this span.

Summary by CodeRabbit

  • New Features
    • HTTP server middleware now supports custom OpenTracing span options, allowing developers to customize span creation and add custom tags for incoming requests.

Review Change Stack

@vpranckaitis

Copy link
Copy Markdown
Author

@lucacome drawing your attention to this since you made most of the recent contributions. Does this change make sense?

@lucacome lucacome requested a review from Copilot April 1, 2025 18:27

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a middleware option to supply custom StartSpanOptions for tracing, enabling users to append additional tags (e.g. via opentracing.Tag) to spans for dynamic sampling decisions.

  • Introduces the MWStartSpanOptions middleware option and its corresponding helper function.
  • Adds a new test (TestStartSpanOptionsOption) to verify that the custom StartSpanOptions are correctly applied.
  • Updates the MiddlewareFunc and supporting collectStartSpanOptions function in server.go to integrate the new option.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
nethttp/server_test.go New test case added to validate the behavior of the StartSpanOptions.
nethttp/server.go New MWStartSpanOptions option added and integrated into the middleware.
Comments suppressed due to low confidence (2)

nethttp/server_test.go:250

  • [nitpick] Using the string literal "" to represent a missing tag in the test may lead to confusion. Consider using an empty string or a different sentinel value to clearly indicate the absence of the tag.
if !ok {

nethttp/server.go:183

  • [nitpick] The default RPCServerOption is prepended before user-supplied options, which may limit users' ability to override its behavior. Consider documenting the ordering or allowing user options to take precedence if intended.
startSpanOptions = append(startSpanOptions, mwStartSpanOptions...)

@vpranckaitis

Copy link
Copy Markdown
Author

@lucacome any comments? This one from copilot seem like a false-positive:

[nitpick] Using the string literal "" to represent a missing tag in the test may lead to confusion. Consider using an empty string or a different sentinel value to clearly indicate the absence of the tag.

The test already uses "<nil>" sentinel value.

@vpranckaitis

Copy link
Copy Markdown
Author

@lucacome in case you do not have time, maybe there's someone else who could do the review of this PR?

@lucacome

Copy link
Copy Markdown
Member

@coderabbitai review

@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai

coderabbitai Bot commented May 28, 2026

Copy link
Copy Markdown

Walkthrough

This pull request adds a new MWStartSpanOptions middleware configuration option that allows users to supply custom OpenTracing StartSpanOption values when the middleware creates server spans. The implementation extends the internal options struct, provides a public constructor, and updates span creation to merge user options with the existing ext.RPCServerOption.

Changes

Span options configuration

Layer / File(s) Summary
Span options contract and public API
nethttp/server.go
Imports updated, mwOptions struct extended with startSpanOptions callback field, and new exported MWStartSpanOptions function allows users to configure custom span options.
Span options middleware integration
nethttp/server.go
Middleware defaults now include startSpanOptions initialization, collectStartSpanOptions helper merges ext.RPCServerOption with user-provided options, and span creation updated to use collected options.
Test coverage for span options
nethttp/server_test.go
New TestStartSpanOptionsOption validates the feature across subtests, verifying custom tags are correctly applied to spans with and without user-provided options.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 62.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'Add middleware option to supply StartSpanOptions' directly and accurately describes the main change: adding a new middleware option (MWStartSpanOptions) that allows users to supply StartSpanOption values when creating server spans.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@nethttp/server.go`:
- Around line 55-58: The MWStartSpanOptions setter should guard against a nil
callback: when MWStartSpanOptions(f) is called with f == nil, normalize to a
no-op function instead of storing nil on mwOptions.startSpanOptions so
downstream code (the request handling path that calls options.startSpanOptions
or the code at lines ~179-180) cannot panic; update the MWStartSpanOptions
implementation to assign a default func(r *http.Request)
[]opentracing.StartSpanOption { return nil } whenever f is nil, or add a
nil-check and conditional invocation at the site that calls
options.startSpanOptions to ensure it is not dereferenced.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: fe6689fc-17c6-4a9d-b8f2-f7b435513efd

📥 Commits

Reviewing files that changed from the base of the PR and between 7754fed and d4c7e68.

📒 Files selected for processing (2)
  • nethttp/server.go
  • nethttp/server_test.go

Comment thread nethttp/server.go
Comment on lines +55 to +58
func MWStartSpanOptions(f func(r *http.Request) []opentracing.StartSpanOption) MWOption {
return func(options *mwOptions) {
options.startSpanOptions = f
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major | ⚡ Quick win

Guard against nil MWStartSpanOptions callback to prevent request-path panic.

If a caller passes MWStartSpanOptions(nil), Line 180 dereferences a nil function and panics for every request. Please normalize nil to a no-op callback (and/or guard before invocation).

Proposed fix
 func MWStartSpanOptions(f func(r *http.Request) []opentracing.StartSpanOption) MWOption {
 	return func(options *mwOptions) {
-		options.startSpanOptions = f
+		if f == nil {
+			options.startSpanOptions = func(r *http.Request) []opentracing.StartSpanOption { return nil }
+			return
+		}
+		options.startSpanOptions = f
 	}
 }
@@
 func collectStartSpanOptions(ctx opentracing.SpanContext, r *http.Request, opts mwOptions) []opentracing.StartSpanOption {
-	mwStartSpanOptions := opts.startSpanOptions(r)
+	if opts.startSpanOptions == nil {
+		return []opentracing.StartSpanOption{ext.RPCServerOption(ctx)}
+	}
+	mwStartSpanOptions := opts.startSpanOptions(r)
 
 	startSpanOptions := make([]opentracing.StartSpanOption, 0, len(mwStartSpanOptions)+1)
 	startSpanOptions = append(startSpanOptions, ext.RPCServerOption(ctx))
 	startSpanOptions = append(startSpanOptions, mwStartSpanOptions...)

Also applies to: 179-180

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@nethttp/server.go` around lines 55 - 58, The MWStartSpanOptions setter should
guard against a nil callback: when MWStartSpanOptions(f) is called with f ==
nil, normalize to a no-op function instead of storing nil on
mwOptions.startSpanOptions so downstream code (the request handling path that
calls options.startSpanOptions or the code at lines ~179-180) cannot panic;
update the MWStartSpanOptions implementation to assign a default func(r
*http.Request) []opentracing.StartSpanOption { return nil } whenever f is nil,
or add a nil-check and conditional invocation at the site that calls
options.startSpanOptions to ensure it is not dereferenced.

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.

3 participants