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

Add baggage to request context #131

Merged
merged 1 commit into from
Oct 3, 2024
Merged

Add baggage to request context #131

merged 1 commit into from
Oct 3, 2024

Conversation

bryanhuhta
Copy link
Contributor

This PR modifies the existing k6 middleware to also add the OTEL baggage to the request context. This makes it easier for users to pass the baggage along when they make subrequests.

It also adds a few more tests and does some code refactoring.

Adding the baggage to the request context makes it easier for users to
pass the baggage along in subsequent requests.
@bryanhuhta bryanhuhta self-assigned this Sep 30, 2024
@bryanhuhta bryanhuhta requested review from a team as code owners September 30, 2024 20:55
Comment on lines +50 to +64
func setBaggageContextFromHeader(r *http.Request) (*http.Request, bool) {
baggageHeader := r.Header.Get("Baggage")
if baggageHeader == "" {
return r, false
}

b, err := baggage.Parse(baggageHeader)
if err != nil {
return r, false
}

ctx := baggage.ContextWithBaggage(r.Context(), b)
return r.WithContext(ctx), true
}

Copy link
Contributor

@kolesnikovae kolesnikovae Oct 1, 2024

Choose a reason for hiding this comment

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

Just a side note:

I’m wondering if we need to parse the baggage here, especially to propagate it further. It seems like too much for a thin middleware that is only responsible for k6 integration.

I believe the OTel baggage should already be in the context. In fact, we expect the app to be fully instrumented, with baggage handled across all services in the call chain (i.e., the HTTP/gRPC client must propagate it)

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that parsing the baggage in this middleware might be doing too much.

However, I assume parsing does provide a safeguard by validating and normalizing the baggage(?). What if we:

  1. Keep the current implementation
  2. Add a comment explaining this reasoning and the assumption of parsed baggage
  3. Offer a separate, optional middleware for baggage parsing

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 method is largely a convenience feature for users so they can get set up with the k6 integration more quickly. I'm not opposed to removing this and requiring users to do this plumbing on their own, but I did think it would make it easier to adopt initially.

Keep in mind this parsing doesn't happen every request. It will only occur when there

  • the baggage has not already been parsed and placed in the context and
  • there is a Bagggage header

If those two conditions are true, then we parse the baggage. Again, happy to pull this out if the consensus is this shouldn't exist in our library code.

Copy link
Contributor

Choose a reason for hiding this comment

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

the baggage has not already been parsed and placed in the context

From the implementation, it appears that whenever there's a Baggage header present, the Parse function is always called, regardless of whether the baggage has been parsed before (maybe I'm missing something).

I also reviewed the OTel baggage Parse function implementation, it appears there's no internal mechanism to avoid re-parsing.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that this is not an issue, and it should not block you. Just shared my thoughts – something to take into account before we make this GA

Copy link
Contributor

@marcsanmi marcsanmi left a comment

Choose a reason for hiding this comment

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

LGTM, just left my 2 cents regarding baggage parsing.

Comment on lines +50 to +64
func setBaggageContextFromHeader(r *http.Request) (*http.Request, bool) {
baggageHeader := r.Header.Get("Baggage")
if baggageHeader == "" {
return r, false
}

b, err := baggage.Parse(baggageHeader)
if err != nil {
return r, false
}

ctx := baggage.ContextWithBaggage(r.Context(), b)
return r.WithContext(ctx), true
}

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree that parsing the baggage in this middleware might be doing too much.

However, I assume parsing does provide a safeguard by validating and normalizing the baggage(?). What if we:

  1. Keep the current implementation
  2. Add a comment explaining this reasoning and the assumption of parsed baggage
  3. Offer a separate, optional middleware for baggage parsing

@bryanhuhta bryanhuhta merged commit a917cea into main Oct 3, 2024
7 of 8 checks passed
@bryanhuhta bryanhuhta deleted the k6-baggage-context branch October 3, 2024 20:31
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