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

Proposal (Performance): Some convenience API to wrap a Promise with a Span/Transaction #3187

Closed
jennmueng opened this issue Jan 20, 2021 · 2 comments
Assignees

Comments

@jennmueng
Copy link
Member

jennmueng commented Jan 20, 2021

Suggestion for a simple convenience wrapper to wrap any promise. This comes from me instrumenting Tour and finding it tedious to have to wrap every promise manually when it could easily be done by a wrapper, so I made these convenience methods to use, and I think it could be very useful to our users to have these or something like them officially.

const withTransaction = <A>(
  promise: Promise<A>,
  context: TransactionContext
): Promise<A> => {
  const transaction = Sentry.startTransaction(context);

  return promise
    .catch((e) => {
      transaction.setStatus(SpanStatus.UnknownError);

      throw e;
    })
    .finally(() => {
      transaction.finish();
    });
};


const withSpan = <A>(
  promise: Promise<A>,
  context: SpanContext,
  transaction?: Transaction // Leave transaction arg empty to use scope transaction
): Promise<A> => {
  const _transaction =
    transaction ?? Sentry.getCurrentHub().getScope().getTransaction();

  const span = _transaction.startChild(context);

  return promise
    .catch((e) => {
      span.setStatus(SpanStatus.UnknownError);

      throw e;
    })
    .finally(() => {
      span.finish();
    });
};

Use case:

import { doSomething } from "some-library"

const wrappedDoSomething = Sentry.withTransaction(doSomething);

// Already wrapped with a transaction
Sentry.withTransaction(
  doSomething({
    param: "Hello",
  }),
  { op: "some-library-process", name: "Use lib" }
).then((response) => {
  // ...
});

// Similar with withSpan...

This is similar to getsentry/sentry-java#1154

@jennmueng jennmueng changed the title Proposal: Some convenience API to wrap a Promise Proposal: Some convenience API to wrap a Promise with a Span/Transaction Jan 20, 2021
@jennmueng jennmueng changed the title Proposal: Some convenience API to wrap a Promise with a Span/Transaction Proposal (Performance): Some convenience API to wrap a Promise with a Span/Transaction Jan 20, 2021
@jdrydn
Copy link

jdrydn commented Jun 3, 2021

I would take this issue further & add some generic tracing methods to support various use-cases:

const SentryTracing = require('@sentry/tracing');

function runTheThing() {
  // Should support async functions & promises too
}

SentryTracing.wrapHandler({ op: 'foo' }, runTheThing);

Or even a simple create-a-span-at-the-current-level function:

const Sentry = require('@sentry/node');
const SentryTracing = require('@sentry/tracing');

const span = SentryTracing.createChildSpan({
  op: 'foo'
});

try {
  // Do something optimistically that fails
} catch (err) {
  Sentry.captureException(err);
} finally {
  span && span.finish();
}

Anything to stop me writing these kinds of helper functions:

function startSentryTransactionChild({ op, description }) {
  try {
    const parentSpan = Sentry.getCurrentHub().getScope().getSpan();
    const span = parentSpan.startChild({ op, description });
    return span ? () => span.finish() : () => null;
  } catch (err) {
    return () => null;
  }
}

const stopSpan = startSentryTransactionChild({ op: 'foo' });
// Do something optimistically
stopSpan();

@github-actions
Copy link
Contributor

This issue has gone three weeks without activity. In another week, I will close it.

But! If you comment or otherwise update it, I will reset the clock, and if you label it Status: Backlog or Status: In Progress, I will leave it alone ... forever!


"A weed is but an unloved flower." ― Ella Wheeler Wilcox 🥀

@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jan 29, 2023
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

6 participants