Langfuse JS/TS SDKs
    Preparing search index...

    Class LangfuseSpanProcessor

    OpenTelemetry span processor for sending spans to Langfuse.

    This processor extends the standard BatchSpanProcessor to provide:

    • Automatic batching and flushing of spans to Langfuse
    • Media content extraction and upload from base64 data URIs
    • Data masking capabilities for sensitive information
    • Conditional span export based on custom logic
    • Environment and release tagging
    import { NodeSDK } from '@opentelemetry/sdk-node';
    import { LangfuseSpanProcessor } from '@langfuse/otel';

    const sdk = new NodeSDK({
    spanProcessors: [
    new LangfuseSpanProcessor({
    publicKey: 'pk_...',
    secretKey: 'sk_...',
    baseUrl: 'https://cloud.langfuse.com',
    environment: 'production',
    mask: ({ data }) => {
    // Mask sensitive data
    return data.replace(/api_key=\w+/g, 'api_key=***');
    }
    })
    ]
    });

    sdk.start();

    Implements

    • SpanProcessor
    Index

    Constructors

    • Creates a new LangfuseSpanProcessor instance.

      Parameters

      Returns LangfuseSpanProcessor

      const processor = new LangfuseSpanProcessor({
      publicKey: 'pk_...',
      secretKey: 'sk_...',
      environment: 'staging',
      flushAt: 10,
      flushInterval: 2,
      mask: ({ data }) => {
      // Custom masking logic
      return typeof data === 'string'
      ? data.replace(/secret_\w+/g, 'secret_***')
      : data;
      },
      shouldExportSpan: ({ otelSpan }) => {
      // Only export spans from specific services
      return otelSpan.name.startsWith('my-service');
      }
      });

    Methods

    • Forces an immediate flush of all pending spans and media uploads.

      Returns Promise<void>

      Promise that resolves when all pending operations are complete

    • Called when a span ends. Processes the span for export to Langfuse.

      This method:

      1. Checks if the span should be exported using the shouldExportSpan function
      2. Applies data masking to sensitive attributes
      3. Handles media content extraction and upload
      4. Logs span details in debug mode
      5. Passes the span to the parent processor for export

      Parameters

      • span: ReadableSpan

        The span that ended

      Returns void

    • Called when a span is started. Adds environment and release attributes to the span.

      Parameters

      • span: Span

        The span that was started

      • parentContext: any

        The parent context

      Returns void

    • Gracefully shuts down the processor, ensuring all pending operations are completed.

      Returns Promise<void>

      Promise that resolves when shutdown is complete