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

🤗 [Question]: Opentelemetry uses c.UserContext(), how to use with c.Context()? #1218

Closed
benjajaja opened this issue Jan 5, 2025 · 3 comments
Labels
🤔 Question Further information is requested

Comments

@benjajaja
Copy link

benjajaja commented Jan 5, 2025

Question Description

My project with fiber extensively uses the context provided by fiber at c.Context() - not c.UserContext(). That is, middlewares operate on c.Context() (I believe also most official/contrib ones do), and then handlers deal with transport stuff and ultimately call "business logic" code, passing c.Context(). The docs are not clear about the distinction.

The opentel middleware uses c.UserContext(), and nicely starts a span when handling a request, but that span is not set on c.Context(). I suspect this simply is because there is a method c.SetUserContext() but no c.SetContext(). The opentel package itself does not expose the context key in any way and so it is impossible to copy the value from one to the other. The opentel package follow the conical (to my knowledge) way of creating a new ctx var á la ctx, err = SetThing(ctx), which is inherently incompatible with fiber's c.Context().

In other words, it is not possible to use trace.Tracer.Start with fiber's c.Context(), only c.UserContext().

What can I do in this scenario?

@benjajaja benjajaja added the 🤔 Question Further information is requested label Jan 5, 2025
@gaby
Copy link
Member

gaby commented Jan 5, 2025

@benjajaja

In fiber v2:

  • UserContext() is for the typical golang context.Context()
  • Context() is for the fasthttp RequestCtx()

For fiber v3 to avoid confusion:

  • Context() will be context.Context()
  • RequestCtx() will be fasthttp RequestCtx()
  • UserContext() was removed.

Let me check the OTEL middleware and report back

@gaby
Copy link
Member

gaby commented Jan 5, 2025

@benjajaja Yes the OTEL package uses UserContext() which corresponds to a typical golang context.Context().

If you need to pass something to it, use SetUserContext(). The otel package doesnt know how to handle a fasthttp Request Ctx.

@benjajaja
Copy link
Author

That's unfortunate. Very good call to remove it for v3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤔 Question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants