Skip to content

Provide syntactic sugar to attach a context then automatically detach after an operation #56

@fractalwrench

Description

@fractalwrench

Similar to #4 it would be nice to create syntactic sugar that attaches a Context and then detaches it after performing an operation. Rather than using AutoCloseable it would be preferable to create a custom function on the Context interface.

Currently, this would be required to perform the operation:

fun example(ctx: Context) {
    // set 'ctx' as the implicit context
    val scope: Scope = ctx.attach()

    // span has 'ctx' set as its parent via implicit context
    tracer.createSpan("my_span")

    // unset 'ctx' and restore the previous implicit context.
    scope.detach()
}

Whereas I think we should aim for an API with fewer moving parts from a library consumer's perspective, similar to this:

fun example(ctx: Context) {
    ctx.asImplicitContext {
        tracer.createSpan("my_span")
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions