Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withSpan/withSpanAsync helpers #2181

Closed
pauldraper opened this issue May 3, 2021 · 4 comments
Closed

withSpan/withSpanAsync helpers #2181

pauldraper opened this issue May 3, 2021 · 4 comments

Comments

@pauldraper
Copy link
Contributor

pauldraper commented May 3, 2021

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();
  }
}
@Flarna
Copy link
Member

Flarna commented May 3, 2021

maybe open-telemetry/opentelemetry-js-api#54 will fit your needs?

@obecny
Copy link
Member

obecny commented May 4, 2021

Well I would be happy to have this on api global level as stated here but seems it will not happen very soon. Unfortunately you are not the only one that see huge benefits in having such easy helpers, me too and I'm annoyed of using otel without such things. I was even thinking of simply creating a package called something like @opentelemetry-js-api-helpers etc. putting this anywhere and then simply combine all such helpers together. We don't need for such things any permission or wait then for any spec etc. It should be doable, unofficially but easier ;).

@dyladan
Copy link
Member

dyladan commented May 4, 2021

open-telemetry/opentelemetry-js-api#54 is under active development so i'm not sure what makes you think it isn't happening

@pauldraper
Copy link
Contributor Author

Sounds like API is the right place. Thanks! Closing the issue here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants