Coder Social home page Coder Social logo

fstien / ktor-opentracing-example Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 467 KB

Example Ktor application instrumented with OpenTracing using ktor-opentracing

License: MIT License

Kotlin 100.00%
ktor opentracing instrumentation ktor-feature observability distributed-tracing example-project

ktor-opentracing-example's People

Contributors

fstien avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

ktor-opentracing-example's Issues

Explanation / support for concurrent coroutines

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 :

Capture d’écran 2020-11-30 à 11 26 53

I expected something like that :

Capture d’écran 2020-11-30 à 11 28 24

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

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.