Skip to content
This repository has been archived by the owner on Jul 31, 2023. It is now read-only.

Add accessor functions for ochttp's addedTagsKey #944

Open
basvanbeek opened this issue Oct 11, 2018 · 10 comments
Open

Add accessor functions for ochttp's addedTagsKey #944

basvanbeek opened this issue Oct 11, 2018 · 10 comments
Labels

Comments

@basvanbeek
Copy link
Member

Is your feature request related to a problem? Please describe.

The current route tagging logic only works for routers supporting http.Handler. Some routers, like the one from gin do not support this. Because of this a large part of ochttp would need to be copied to a specific gin middleware (as demonstrated by @sagikazarmark in https://github.com/sagikazarmark/go-gin-gorm-opencensus).

Describe the solution you'd like
To allow usage of standard ochttp on the outer shell with tag.Mutator access from within gin handlers we could provide standard context accessors for the addedTags slice and make gin integration much easier with our standard plugin.

sagikazarmark added a commit to sagikazarmark/go-gin-gorm-opencensus that referenced this issue Oct 11, 2018
The only reason this integration was added to support Gin route information.

An issue has been raised to support route information in ochttp with
non-standard handlers:

census-instrumentation/opencensus-go#944
sagikazarmark added a commit to sagikazarmark/go-gin-gorm-opencensus that referenced this issue Oct 11, 2018
The only reason this integration was added to support Gin route information.

An issue has been raised to support route information in ochttp with
non-standard handlers:

census-instrumentation/opencensus-go#944
@sagikazarmark
Copy link
Contributor

I would be happy to provide a PR if you are okay with providing access to the tag mutator.

@semistrict
Copy link
Contributor

I would prefer to generalize this slightly differently. Instead of exposing the general ability to add tags (which is more powerful than required), let's rename WithRouteTag to WithRoute so that we can re-use it for adding the route to the Span as the http.route attribute. We can then provide a GetRoute(context) function.

@sagikazarmark
Copy link
Contributor

That wouldn't solve the problem as the route would only become available later:

ochttp -> custom routing strategy -> add route info to span

@semistrict
Copy link
Contributor

What if we extracted the logic for HTTP request processing, something like:

func (h *ochttp.Handler)  Start(req *http.Request) *http.Request {
...
  return req.WithContext(newCtx)
}

func (h *ochttp.Handler) End(req *http.Request, route string) {
  span := trace.FromContext(req)
  span.AddAttribute(trace.StringAttribute("http.route", route))
  // add route as tag and call stats.RecordWithTag
}

Then frameworks could construct an appropriate http.Request and call Start and End directly (rather than relying on Handle).

@sagikazarmark
Copy link
Contributor

That could work, but it would also mean that ochttp cannot the be most outer layer of the http request, so that the amount of instrumented code would possibly be less.

What if we don't expose the whole tag adding layer, just add a method that adds the route to the tag list in the context?

@semistrict
Copy link
Contributor

Yeah, that's fine I think. We should just note that it isn't safe to call it from a separate goroutine. Can we call it ochttp.SetRoute(req, route) and have it set the http.route attribute on the associated span as well?

@sagikazarmark
Copy link
Contributor

sagikazarmark commented Nov 6, 2018

I think it should accept (and return) a context. Context propagation is not necessarily tied to an http request within the framework boundaries.

What should it do if a tag list is not available in the context? Create it?

@semistrict
Copy link
Contributor

Yeah, I can imagine that there would be cases where you might not have convenient access to the http.Request so using context seems fine.

If the tag list is not available there is no point creating it because the context that you return with the newly added tag list will not really be used for anything. Come to think of it, the function should probably not return a context, since it isn't adding anything to the context but rather internally mutating it.

so maybe the signature should just be:

func SetRoute(ctx context.Context, route string)

If the tag list is not in the context that means the ochttp.Handler was not installed correctly. I would be inclined to log an error sync.Once and ignore. The alternative would be to panic which seems too drastic.

@sagikazarmark
Copy link
Contributor

Come to think of it, the function should probably not return a context, since it isn't adding anything to the context but rather internally mutating it.

I agree, it was only an option if we decided to add tag list to the context if it doesn't exist.

The signature looks good.

@sagikazarmark
Copy link
Contributor

Is there anything I can do to move things forward here?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants