CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-sentry--minimal

Minimal Sentry SDK for library authors that delegates to configured client when embedded into applications

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

index.mddocs/

@sentry/minimal

@sentry/minimal is a lightweight Sentry SDK designed specifically for library authors. It provides a minimal set of Sentry functions that delegate to a configured client when embedded into an application, allowing libraries to add error tracking without bundling the entire SDK or being dependent on a specific platform.

Package Information

  • Package Name: @sentry/minimal
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @sentry/minimal

Core Imports

import * as Sentry from '@sentry/minimal';

Individual imports:

import {
  captureException,
  captureMessage,
  captureEvent,
  addBreadcrumb,
  configureScope,
  withScope,
  setUser,
  setTag,
  setTags,
  setExtra,
  setExtras,
  setContext,
  startTransaction
} from '@sentry/minimal';

For CommonJS:

const Sentry = require('@sentry/minimal');
const { captureException, captureMessage } = require('@sentry/minimal');

Basic Usage

import * as Sentry from '@sentry/minimal';

// Add a breadcrumb for future events
Sentry.addBreadcrumb({
  message: 'User clicked button',
  category: 'ui',
  level: 'info'
});

// Capture exceptions, messages or manual events
Sentry.captureMessage('Hello, world!');
Sentry.captureException(new Error('Something went wrong'));
Sentry.captureEvent({
  message: 'Manual event',
  level: 'warning'
});

// Set context information
Sentry.configureScope(scope => {
  scope.setUser({ id: '4711', email: 'user@example.com' });
  scope.setTag('component', 'MyLibrary');
  scope.setExtra('debug_info', { version: '1.0.0' });
});

Architecture

@sentry/minimal operates as a thin wrapper that:

  • Delegates Operations: All functions delegate to the current Sentry hub via getCurrentHub()
  • No SDK Initialization: Library authors don't need to initialize the SDK - it uses whatever client is configured by the consuming application
  • Error Handling: Throws descriptive errors when no hub is available or methods are missing
  • Lightweight: Minimal bundle size with only essential functionality for library instrumentation

Capabilities

Exception Capture

Captures exception events and sends them to Sentry.

/**
 * Captures an exception event and sends it to Sentry.
 * @param exception - An exception-like object
 * @param captureContext - Optional context for the capture operation
 * @returns The generated eventId
 */
function captureException(exception: any, captureContext?: CaptureContext): string;

type CaptureContext = Scope | Partial<ScopeContext> | ((scope: Scope) => Scope);

interface ScopeContext {
  user: User;
  level: Severity;
  extra: Extras;
  contexts: Contexts;
  tags: Record<string, Primitive>;
  fingerprint: string[];
  requestSession: RequestSession;
}

Message Capture

Captures message events and sends them to Sentry.

/**
 * Captures a message event and sends it to Sentry.
 * @param message - The message to send to Sentry
 * @param captureContext - Optional context or severity level
 * @returns The generated eventId
 */
function captureMessage(message: string, captureContext?: CaptureContext | Severity): string;

enum Severity {
  Fatal = 'fatal',
  Error = 'error',
  Warning = 'warning',
  Log = 'log',
  Info = 'info',
  Debug = 'debug',
  Critical = 'critical'
}

Event Capture

Captures manually created events and sends them to Sentry.

/**
 * Captures a manually created event and sends it to Sentry.
 * @param event - The event to send to Sentry
 * @returns The generated eventId
 */
function captureEvent(event: Event): string;

interface Event {
  event_id?: string;
  message?: string;
  timestamp?: number;
  start_timestamp?: number;
  level?: Severity;
  platform?: string;
  logger?: string;
  server_name?: string;
  release?: string;
  dist?: string;
  environment?: string;
  sdk?: SdkInfo;
  request?: Request;
  transaction?: string;
  modules?: Record<string, string>;
  fingerprint?: string[];
  exception?: {
    values?: Exception[];
  };
  stacktrace?: Stacktrace;
  breadcrumbs?: Breadcrumb[];
  contexts?: Contexts;
  tags?: Record<string, Primitive>;
  extra?: Extras;
  user?: User;
  type?: EventType;
  spans?: Span[];
  measurements?: Measurements;
  debug_meta?: DebugMeta;
  sdkProcessingMetadata?: Record<string, any>;
}

Breadcrumb Management

Records breadcrumbs which will be attached to future events.

/**
 * Records a new breadcrumb which will be attached to future events.
 * Breadcrumbs provide context on user actions prior to an error or crash.
 * @param breadcrumb - The breadcrumb to record
 */
function addBreadcrumb(breadcrumb: Breadcrumb): void;

interface Breadcrumb {
  message?: string;
  category?: string;
  level?: Severity;
  timestamp?: number;
  type?: string;
  data?: Record<string, any>;
}

Scope Configuration

Configures context information that will be attached to events.

/**
 * Callback to set context information onto the scope.
 * @param callback - Callback function that receives the current scope
 */
function configureScope(callback: (scope: Scope) => void): void;

/**
 * Creates a new scope and executes the given operation within it.
 * The scope is automatically removed once the operation finishes or throws.
 * @param callback - Callback that will be enclosed in push/popScope
 */
function withScope(callback: (scope: Scope) => void): void;

interface Scope {
  setUser(user: User | null): this;
  setTag(key: string, value: Primitive): this;
  setTags(tags: Record<string, Primitive>): this;
  setExtra(key: string, extra: any): this;
  setExtras(extras: Record<string, any>): this;
  setContext(name: string, context: Record<string, any> | null): this;
  setLevel(level: Severity): this;
  setFingerprint(fingerprint: string[]): this;
  clear(): this;
}

Context Management

Functions for setting various types of context information.

/**
 * Updates user context information for future events.
 * @param user - User context object or null to unset
 */
function setUser(user: User | null): void;

/**
 * Set key:value that will be sent as tags data with events.
 * Can also be used to unset a tag by passing undefined.
 * @param key - String key of tag
 * @param value - Value of tag
 */
function setTag(key: string, value: Primitive): void;

/**
 * Set an object that will be merged as tags data with events.
 * @param tags - Tags context object to merge into current context
 */
function setTags(tags: Record<string, Primitive>): void;

/**
 * Set key:value that will be sent as extra data with events.
 * @param key - String key of extra data
 * @param extra - Any kind of data (will be normalized)
 */
function setExtra(key: string, extra: any): void;

/**
 * Set an object that will be merged as extra data with events.
 * @param extras - Extras object to merge into current context
 */
function setExtras(extras: Record<string, any>): void;

/**
 * Sets context data with the given name.
 * @param name - Name of the context
 * @param context - Any kind of data (will be normalized)
 */
function setContext(name: string, context: Record<string, any> | null): void;

interface User {
  id?: string;
  username?: string;
  email?: string;
  ip_address?: string;
  [key: string]: any;
}

type Primitive = number | string | boolean | bigint | symbol | null | undefined;

Transaction Management

Creates transactions for manual tracing instrumentation.

/**
 * Starts a new Transaction and returns it. This is the entry point to manual tracing instrumentation.
 * The transaction must be finished with a call to its .finish() method.
 * @param context - Properties of the new Transaction
 * @param customSamplingContext - Information given to the transaction sampling function
 * @returns The transaction which was just started
 */
function startTransaction(
  context: TransactionContext,
  customSamplingContext?: CustomSamplingContext
): Transaction;

interface TransactionContext extends SpanContext {
  /** Human-readable identifier for the transaction */
  name: string;
  /** If true, sets end timestamp to highest child span timestamp */
  trimEnd?: boolean;
  /** If this transaction has a parent, the parent's sampling decision */
  parentSampled?: boolean;
  /** Metadata associated with the transaction, for internal SDK use */
  metadata?: TransactionMetadata;
}

interface SpanContext {
  description?: string;
  op?: string;
  status?: string;
  parentSpanId?: string;
  sampled?: boolean;
  spanId?: string;
  traceId?: string;
  tags?: Record<string, Primitive>;
  data?: Record<string, any>;
  startTimestamp?: number;
  endTimestamp?: number;
}

interface CustomSamplingContext {
  [key: string]: any;
}

interface Transaction extends TransactionContext, Span {
  spanId: string;
  traceId: string;
  startTimestamp: number;
  tags: Record<string, Primitive>;
  data: Record<string, any>;
  metadata: TransactionMetadata;
  
  /** Set the name of the transaction */
  setName(name: string): void;
  /** Returns current transaction properties as TransactionContext */
  toContext(): TransactionContext;
  /** Updates the current transaction with a new TransactionContext */
  updateWithContext(transactionContext: TransactionContext): this;
}

interface TransactionMetadata {
  transactionSampling?: { rate?: number; method: TransactionSamplingMethod };
  tracestate?: { sentry?: string; thirdparty?: string };
  requestPath?: string;
}

type TransactionSamplingMethod = 'explicitly_set' | 'client_sampler' | 'client_rate' | 'inheritance';

Internal Client Access

Internal helper function for calling methods on the latest client.

/**
 * Calls a function on the latest client. Use with caution - meant as internal helper.
 * Not guaranteed that the client actually implements the function.
 * @param method - The method to call on the client
 * @param args - Arguments to pass to the client method
 * @hidden
 */
function _callOnClient(method: string, ...args: any[]): void;

Types

interface Exception {
  type?: string;
  value?: string;
  module?: string;
  thread_id?: string;
  stacktrace?: {
    frames?: StackFrame[];
  };
}

interface StackFrame {
  filename?: string;
  function?: string;
  module?: string;
  platform?: string;
  lineno?: number;
  colno?: number;
  abs_path?: string;
  context_line?: string;
  pre_context?: string[];
  post_context?: string[];
  in_app?: boolean;
  vars?: Record<string, any>;
}

interface Request {
  url?: string;
  method?: string;
  data?: any;
  query_string?: string;
  cookies?: Record<string, string>;
  headers?: Record<string, string>;
  env?: Record<string, string>;
}

interface Span extends SpanContext {
  spanId: string;
  traceId: string;
  startTimestamp: number;
  tags: Record<string, Primitive>;
  data: Record<string, any>;
  transaction?: Transaction;
  
  /** Sets the finish timestamp on the current span */
  finish(endTimestamp?: number): void;
  /** Sets the tag attribute on the current span */
  setTag(key: string, value: Primitive): this;
  /** Sets the data attribute on the current span */
  setData(key: string, value: any): this;
  /** Sets the status attribute on the current span */
  setStatus(status: string): this;
  /** Sets the status attribute based on http code */
  setHttpStatus(httpStatus: number): this;
  /** Creates a new Span while setting current Span.id as parentSpanId */
  startChild(spanContext?: Pick<SpanContext, Exclude<keyof SpanContext, 'spanId' | 'sampled' | 'traceId' | 'parentSpanId'>>): Span;
  /** Determines whether span was successful */
  isSuccess(): boolean;
  /** Return a traceparent compatible header string */
  toTraceparent(): string;
  /** Returns current span properties as SpanContext */
  toContext(): SpanContext;
  /** Updates the current span with a new SpanContext */
  updateWithContext(spanContext: SpanContext): this;
  /** Convert the object to JSON for spans array info only */
  getTraceContext(): {
    data?: Record<string, any>;
    description?: string;
    op?: string;
    parent_span_id?: string;
    span_id: string;
    status?: string;
    tags?: Record<string, Primitive>;
    trace_id: string;
  };
  /** Convert the object to JSON */
  toJSON(): {
    data?: Record<string, any>;
    description?: string;
    op?: string;
    parent_span_id?: string;
    span_id: string;
    start_timestamp: number;
    status?: string;
    tags?: Record<string, Primitive>;
    timestamp?: number;
    trace_id: string;
  };
}

// Additional supporting types
interface Exception {
  type?: string;
  value?: string;
  mechanism?: Mechanism;
  module?: string;
  thread_id?: number;
  stacktrace?: Stacktrace;
}

interface StackFrame {
  filename?: string;
  function?: string;
  module?: string;
  platform?: string;
  lineno?: number;
  colno?: number;
  abs_path?: string;
  context_line?: string;
  pre_context?: string[];
  post_context?: string[];
  in_app?: boolean;
  instruction_addr?: string;
  addr_mode?: string;
  vars?: Record<string, any>;
}

interface Request {
  url?: string;
  method?: string;
  data?: any;
  query_string?: QueryParams;
  cookies?: Record<string, string>;
  env?: Record<string, string>;
  headers?: Record<string, string>;
}

interface Stacktrace {
  frames?: StackFrame[];
}

interface SdkInfo {
  name?: string;
  version?: string;
  integrations?: string[];
  packages?: Package[];
}

interface Package {
  name: string;
  version: string;
}

interface Mechanism {
  type: string;
  description?: string;
  help_link?: string;
  handled?: boolean;
  data?: Record<string, any>;
  synthetic?: boolean;
}

interface DebugMeta {
  images?: DebugImage[];
}

interface DebugImage {
  type: string;
  debug_id?: string;
  code_id?: string;
  name?: string;
}

interface RequestSession {
  status?: RequestSessionStatus;
}

type Contexts = Record<string, Context>;
type Context = Record<string, any>;
type EventType = 'transaction' | 'default';
type Measurements = Record<string, { value: number }>;
type QueryParams = string | Record<string, string> | Array<[string, string]>;
type RequestSessionStatus = 'ok' | 'errored' | 'crashed';

Error Handling

All functions will throw an error if:

  • No hub is defined (getCurrentHub() returns undefined)
  • The requested method is not found on the hub

The error message format is: "No hub defined or ${method} was not found on the hub, please open a bug report."

Usage Notes

  • No Initialization Required: Library authors should not initialize the SDK - this is handled by the consuming application's main Sentry SDK
  • Context Isolation: Use withScope() to isolate context changes to prevent interfering with the user's global context
  • Minimal Interference: Be cautious when setting user context, tags, or extras as these might override the user's values
  • Type Safety: All functions are fully typed for TypeScript usage with proper generic support where applicable
  • Lazy Evaluation: All operations are synchronous and delegate immediately to the configured hub

docs

index.md

tile.json