Skip to content

Feature request: Tracer hook/callback API for span creation interception #5926

@schickling

Description

@schickling

Summary

I'd like to request an API to intercept span creation events without having to monkey-patch the tracer. Currently, to track span metadata (like building a span path/tree), the only option is to patch tracer.span() directly, which feels hacky and fragile.

Use Case

In our UI framework, we wrap user effects with Effect.withSpan('ui.effect', { root: true }) and want to log the "deepest" span path when the effect completes. For example:

[handleClick → fetchData → processResult] Trace URL: https://...

Since Effect.ensuring() runs after inner spans have closed, we can't access the span hierarchy at that point. Our current workaround patches the tracer to track the deepest span path in a root span attribute as spans are created.

Current Workaround

const ensureTracerPatched = (tracer: Tracer.Tracer) => {
  const originalSpan = tracer.span
  tracer.span = function (name, parent, context, links, startTime, kind, options) {
    const span = originalSpan.apply(this, arguments)
    // Track span path in root span attribute...
    return span
  }
}

Proposed Solutions

Any of these would help:

  1. Tracer hook/callback: An official API to register callbacks for span lifecycle events (create, end)
  2. In-memory span tree: A way to query the current span hierarchy from within an effect
  3. Span creation interceptor: A composable way to wrap span creation similar to how layers work

Environment

  • Effect version: 3.x
  • Platform: Browser (Electron renderer)

Happy to provide more context or help with implementation if needed!

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