Skip to content
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

feat: only add response header for sampled traces #56

Conversation

costela
Copy link
Contributor

@costela costela commented Jul 10, 2024

This changes the behavior of WithTraceIDResponseHeader to only add the header if the trace was actually sampled. A trace-id for a trace that wasn't sent anywhere doesn't seem particularly useful?

This could be considered a breaking change. Alternatively, we could add an extra option to control that, but that seems a bit overkill IMHO?

@riandyrn
Copy link
Owner

riandyrn commented Jul 16, 2024

Hi, @costela

First of all, thanks for your PR!

However, I'm still not sure if removing the trace ID from the response header is a good idea for the non-sampled trace.

The reason is that the trace ID might be used by underlying middleware to do something else, such as check whether the trace context is propagated to the service (because when the trace is not sampled, the context is still propagated to the child service) and maybe for any other use cases.

However, I agree with you that we should let the user know when the trace is not sampled because otherwise, the user might think that the trace is sampled when, in reality, it is not.

So, what do you think if we just add a new response header called X-Trace-Sampled with the value set to either true or false? We set it to true if the trace is sampled and vice versa.

cc: @ilhamsyahids, @ProtozoaJr

@ilhamsyahids
Copy link
Collaborator

ilhamsyahids commented Jul 16, 2024

Hello @costela,

Do you have any concern or specific needs to not add trace id as headers for non-sample trace?

As @riandyrn mentioned, trace id might be use by underlying middleware despite sampled or not, like #22

@costela costela force-pushed the only-add-response-header-if-trace-was-sampled branch from b65553e to 17fe0df Compare July 22, 2024 10:35
@costela
Copy link
Contributor Author

costela commented Jul 22, 2024

hi @riandyrn, @ilhamsyahids

We have a similar use case, where a middleware does some reporting on traces, and the "unused" traceID was throwing us off.

Your suggestion seems totally sufficient for our case as well. However, it doesn't quite fit with the current header customization in WithTraceIDResponseHeader. I just reworked the PR for a first pass, but if you'd like to make the samped header also customizeable, we'd probably need another separate option function (which feels a bit overkill 🙈).

PTAL 🙏

@costela
Copy link
Contributor Author

costela commented Jul 31, 2024

did anyone get a chance to look at the proposed changes? 😇

@ilhamsyahids
Copy link
Collaborator

Hi, @costela

Thanks for the update

Can you please also add test for sampled trace? You might need to check the example

@@ -32,7 +36,7 @@ func (o optionFunc) apply(c *config) {
o(c)
}

// Filter is a predicate used to determine whether a given http.request should
// Filter is a predicate used to determine whether a given [http.Request] should
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this change is necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are using the relatively new doc linking format and make navigating from comments a bit more comfortable.

But of course: if you'd prefer to keep this MR focused, I can just remove the changes.

@@ -138,7 +142,7 @@ func WithPublicEndpoint() Option {
// incoming span context. Otherwise, the generated span will be set as the
// child span of the incoming span context.
//
// Essentially it has the same functionality as WithPublicEndpoint but with
// Essentially it has the same functionality as [WithPublicEndpoint] but with
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think this change is necessary.

middleware.go Outdated
@@ -23,7 +24,9 @@ const (
// requests. The serverName parameter should describe the name of the
// (virtual) server handling the request.
func Middleware(serverName string, opts ...Option) func(next http.Handler) http.Handler {
cfg := config{}
cfg := config{
TraceSampledResponseKey: defaultTraceSampledResponseHeaderKey,
Copy link
Owner

Choose a reason for hiding this comment

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

I think it's better to set this in 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.

done, PTAL

@riandyrn
Copy link
Owner

riandyrn commented Aug 5, 2024

Hello, @costela

Thanks for your latest commit. However, we figured out it's better to just let the end user modify their response. So we decided to create #57.

Would you please give your feedback there? It is basically the general form of this PR.

@costela
Copy link
Contributor Author

costela commented Aug 5, 2024

deprecated by #57

@costela costela closed this Aug 5, 2024
@costela
Copy link
Contributor Author

costela commented Aug 12, 2024

superceded by #59

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