fstien / ktor-opentracing-example Goto Github PK
View Code? Open in Web Editor NEWExample Ktor application instrumented with OpenTracing using ktor-opentracing
License: MIT License
Example Ktor application instrumented with OpenTracing using ktor-opentracing
License: MIT License
Hi,
I ran into an issue when trying to run parallel tasks on the server. In the project I modified the getAll
function of the client to call the U.S. Geological Survey API multiple times in parallel.
suspend fun getLatest(): Earthquake = withContext(Dispatchers.IO) {
span("EarthquakeClient.getLatest()") {
val earthquakes = (1..3)
.map { async { getAll() } }
.awaitAll()
.first()
val latest = earthquakes.first()
setTag("location", latest.location)
setTag("magnitude", latest.magnitude)
setTag("timeGMT", latest.timeGMT)
latest
}
}
The thing is that the spans are all messed up. At least they don't show up as I expected :
I expected something like that :
So I looked at what is done in the OpenTracingServer
feature and I thought the "problem" was all async tasks share the same Stack<Span>
, and as the proceed()
is wrapped in this theadLocalSpanStack
context, all modifications done to the Stack
is shared with other coroutines. My knowledge of coroutines is quite limited so I may have misunderstood...
What I ended up doing is :
suspend fun getLatest(): Earthquake = withContext(Dispatchers.IO + tracingContext()) {
span("EarthquakeClient.getLatest()") {
val earthquakes = (1..3)
.map { async(tracingContext()) { getAll() } }
.awaitAll()
.first()
val latest = earthquakes.first()
setTag("location", latest.location)
setTag("magnitude", latest.magnitude)
setTag("timeGMT", latest.timeGMT)
latest
}
}
With tracingContext()
defined as follows :
fun tracingContext() = threadLocalSpanStack.asContextElement(
threadLocalSpanStack.get()?.clone() as Stack<Span>?
?: Stack<Span>()
)
This way, I inherit the enclosing span context, but I don't share it between concurrent tasks. It renders my spans as I expected, but then again, I might be missing something.
How would you do it ? Would you expect the same span layout from my example ?
Thanks.
Julien
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.