Skip to content

Add withTelemetry helper to reduce Sentry instrumentation boilerplate #165

@BYK

Description

@BYK

Summary

Add a withTelemetry helper function to reduce boilerplate code when wrapping functions with Sentry spans.

Motivation

Currently, Sentry span instrumentation requires verbose boilerplate:

function myFunction(arg: string): Promise<Result> {
  return Sentry.startSpan(
    {
      name: "myFunction",
      op: "some.operation",
      attributes: { "some.attr": arg },
      onlyIfParent: true,
    },
    async (span) => {
      try {
        const result = await doWork(arg);
        span.setStatus({ code: 1 });
        return result;
      } catch (error) {
        span.setStatus({ code: 2 });
        throw error;
      }
    }
  );
}

Proposed Solution

Create a helper similar to Spotlight's withTelemetry:

function withTelemetry<T>(
  name: string,
  op: string,
  fn: () => T | Promise<T>,
  attributes?: Record<string, string | number | boolean>
): Promise<T> {
  return Sentry.startSpan(
    { name, op, attributes, onlyIfParent: true },
    async (span) => {
      try {
        const result = await fn();
        span.setStatus({ code: 1 });
        return result;
      } catch (error) {
        span.setStatus({ code: 2 });
        throw error;
      }
    }
  );
}

Files to Update

  • src/lib/dsn/code-scanner.ts - scanDirectory
  • src/lib/dsn/project-root.ts - withFsSpan, findProjectRoot
  • Any other files with similar Sentry span patterns

References

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions