Skip to content

withSpan/withSpanAsync helpers #2181

@pauldraper

Description

@pauldraper

I have to rewrite this in every project. Is there somewhere this can fit?

function withSpan<T>(span: Span, f: () => T): T {
  try {
    return context.with(setSpan(context.active(), span), f);
  } catch (e) {
    span.setStatus({
      code: SpanStatusCode.ERROR,
      message: (e && (e.stack || e.message)) || String(e),
    });
    throw e;
  } finally {
    span.end();
  }
}

async function withSpanAsync<T>(span: Span, f: () => Promise<T>): Promise<T> {
  try {
    return await context.with(setSpan(context.active(), span), f);
  } catch (e) {
    span.setStatus({
      code: SpanStatusCode.ERROR,
      message: (e && (e.stack || e.message)) || String(e),
    });
    throw e;
  } finally {
    span.end();
  }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions