Comments (10)
Awesome, thank you!
from atreugo.
Hi @rantav,
The *fasthttp.RequestCtx
and *atreugo.RequestCtx
implements the interface of context.Context
, so you could use the own RequestCtx to create a new copy of the context and modify it or any other thing.
So I don't understand why it's not enought the ctx.SetUserValue()
and ctx.UserValue()
, it's the same to do r = r.WithContext(context.WithValue(r.Context(), addedTagsKey{}, &tags))
What do you need exactly? Because, you need to store custom values in context (RequestCtx
) to use in another handlers/views/middlewares in the same request, right?
from atreugo.
The difference of ctx.SetUserValue()
with context.WithValue()
, is just the keys must be a strings not interfaces.
But i think these wouldn't be a problem.
from atreugo.
Hi @savsgio, thanks for the quick response, but I'm afraid that would not be enough.
Here's the scenario. I use opencensus, which adds stats and tracing so I create a before and after middleware using server.UseBefore
and server.UseAfter
In the before middleware I do:
newCtx, span = trace.StartSpan(ctx, name)
This operation creates a new span and returns a new context newCtx
. This newCtx contains a interface{}
key (out of my control) to the new span.
From now on downstream middlewares or views are supposed to use the newCtx and in this newCtx they expect to find the span.
Here's how you read the span from the context:
span = trace.FromContext(ctx) // ctx is supposed to be the newCtx here, otherwise it would not work
Reference: https://godoc.org/go.opencensus.io/plugin/ochttp#hdr-Tracing
So two pieces are missing:
- Be able to use an
interface{}
key - Pass the new context to
ctx.Next()
when calling the next middleware or view.
For example see how the request r
is being updated here https://github.com/census-instrumentation/opencensus-go/blob/643eada29081047b355cfaa1ceb9bc307a10423c/plugin/ochttp/server.go#L91
from atreugo.
Could you give me the trace.StartSpan(...)
documentation please?
from atreugo.
Okay, i could see that trace.StartSpan(...)
return a context.Context
, so the new context you must store in request ctx with ctx.SetUserValue("traceCtx", newCtx)
in before middleware and recover it in after middlewares with newCtx := ctx.UserValue("traceCtx").(context.Context)
and use it in trace.FromContext(ctx)
.
Maybe i could do it something similar in atreugo to ease this functionality, trying to make it as close as possible to net / http
from atreugo.
Your suggestion to store the newCtx using ctx.SetUserValue("traceCtx", newCtx)
is indeed the workaround I employed. But this looks unnatural (storing a context onto the context feels like Yack shaving...) plus, still the call to trace.FromContext(ctx)
would not work on the provided atreugo.RequestCtx
, as most users would expect (it would silently fail, e.g. return nil b/c it can't find it there), rather I"d have to:
// This returns nothing, which could be surprising to users
trace.FromContext(ctx)
// this workaround works, but is not obvious
newCtx := ctx.UserValue("traceCtx").(context.Context)
// and only now:
t := trace.FromContext(newCtx)
from atreugo.
I've been upload the fix, now you should do:
// Before middleware
newCtx, span = trace.StartSpan(ctx, name)
ctx.AttachContext(newCtx)
// After middleware or view
trace.FromContext(ctx)
I will release a new version throughout today.
from atreugo.
Hi, what happened to v10.1.0
? Did you delete it?
from atreugo.
Ohh sorry, yes i deleted it after a few minutes of release, because i want to include the fix about the attached ctx.
I wil release it now.
And sorry again.
from atreugo.
Related Issues (20)
- Possible breaking change in patch version? "path must begin with '/' in path ''" HOT 2
- AttachContext overrides previous AttachContext HOT 2
- Hi, Savsgio, how to add CORS middleware? HOT 30
- Is it possible to run "after" middleware even when "before" middleware exits error? HOT 5
- Unable to match URL with "/" ending HOT 1
- Unsupported path parameters with root / prefix HOT 1
- Problem with trailing "/" when optional url param is empty (11 version) - 308 redirect HOT 3
- I can not install HOT 1
- Error! installing with go get github.com/savsgio/atreugo HOT 5
- Cannot update/download deps HOT 1
- any sort of html template kind of real life example code to go with? router with html hello world at least HOT 1
- Instrumenting servers / paths to add metrics doesn't work when view returns an error HOT 1
- Use atreugo and autocert HOT 2
- Support custom logger HOT 4
- Possible race condition in context propagation HOT 3
- rate limit middleware request HOT 3
- Race condition when calling RequestCtx.Value() HOT 4
- Sentry APM Middleware HOT 1
- Occasional panic HOT 1
- Configurable JSON Marshaller HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from atreugo.