Routing OpenTelemetry logs to Sentry using OTLP
If you’ve already instrumented your application with OpenTelemetry, integrating with Sentry doesn’t require replacing your existing instrumentation. By configuring just two environment variables, you can route your logs directly to Sentry’s OTLP endpoint without modifying any SDK code or re-instrumenting your application.
Why You’d Use OTLP Instead of the Native SDK
The primary advantage of OTLP is maintaining vendor-neutral logging code that remains decoupled from any specific observability backend. This approach proves beneficial when you:
- Already have OpenTelemetry logging implemented
- Send logs to multiple observability platforms
- Require vendor-neutral instrumentation
- Work with AI/LLM frameworks using OpenTelemetry natively
- Want to leverage the broader OpenTelemetry ecosystem
However, if starting fresh and using only Sentry, the native Sentry SDK offers superior integration including automatic issue creation, session replay integration, breadcrumb tracking, and error correlation features. OTLP support through Sentry remains in beta and currently lacks these integrated capabilities.
Prerequisites
Required setup includes:
- Active Sentry account (free tier acceptable)
- Node.js 18 or later
- Basic Express.js familiarity
Create a new Sentry project selecting Express as the platform, skipping DSN setup instructions since you’ll use the OTLP endpoint instead.
Getting Your Sentry OTLP Credentials
Navigate to your Sentry project settings:
- Click Settings in the left sidebar
- Under Organization, select Projects
- Choose your project from the list
- Click Client Keys (DSN) under SDK Setup
- Select the OpenTelemetry tab
- Click Expand to reveal OTLP values
Collect these credentials:
- OTLP Logs Endpoint:
https://o{ORG_ID}.ingest.us.sentry.io/api/{PROJECT_ID}/integration/otlp/v1/logs - OTLP Logs Endpoint Headers:
x-sentry-auth=sentry sentry_key={YOUR_PUBLIC_KEY}
Most OTLP exporters require headers as key/value pairs rather than full header strings, necessitating header parsing in your application.
Connecting Your OpenTelemetry App to Sentry
Cloning the Starter Application
Execute these commands to clone the payment processing sample app:
git clone https://github.com/getsentry/otlp-logging-sentry.git
cd otlp-logging-sentry
npm install
The starter app includes pre-configured OpenTelemetry SDK, structured logging implementation, multiple log severity levels (INFO, DEBUG, WARN, ERROR), and rich log attributes throughout.
Configuring Sentry as OTLP Destination
Create your environment configuration file:
cp .env.example .env
Edit .env with your Sentry credentials:
OTEL_EXPORTER_OTLP_LOGS_ENDPOINT=https://o{YOUR_ORG_ID}.ingest.us.sentry.io/api/{YOUR_PROJECT_ID}/integration/otlp/v1/logs
OTEL_EXPORTER_OTLP_LOGS_HEADERS=x-sentry-auth=sentry sentry_key={YOUR_PUBLIC_KEY}
OTEL_SERVICE_NAME=payment-processing-service
PORT=3000
Replace placeholders with actual Sentry credentials. The OTEL_SERVICE_NAME enables service-based filtering in Sentry.
Testing the Integration
Start the application:
npm start
Expected console output:
OpenTelemetry logging initialized
Service: payment-processing-service
Payment Processing Service running on http://localhost:3000
Generating Test Logs
In a separate terminal, trigger a payment processing request:
curl -X POST http://localhost:3000/process-payment \
-H "Content-Type: application/json" \
-d '{"amount": 100, "currency": "USD"}'
Logs should appear in Sentry within 30-60 seconds.
Troubleshooting
If logs remain absent after verifying credentials, enable OpenTelemetry debug logging:
import { diag, DiagConsoleLogger, DiagLogLevel } from '@opentelemetry/api';
diag.setLogger(new DiagConsoleLogger(), DiagLogLevel.DEBUG);
Consult the OpenTelemetry troubleshooting guide for additional debugging options.
Handling Array Attributes
Array attributes are viewable in log detail but aren’t yet searchable or filterable—a current OTLP beta limitation. For searchable data, use separate attributes or join arrays:
// Can view, but can't search
attributes: {
'user.preferences': ['fiction', 'mystery', 'sci-fi']
}
// Searchable
attributes: {
'user.preferences': 'fiction,mystery,sci-fi'
}
Managing Memory Usage
The BatchLogRecordProcessor may be batching excessive logs. Adjust batch sizing:
loggerProvider.addLogRecordProcessor(
new BatchLogRecordProcessor(logExporter, {
maxQueueSize: 1000, // Default: 2048
maxExportBatchSize: 256, // Default: 512
scheduledDelayMillis: 5000 // Default: 1000
})
);
Consult the BatchLogRecordProcessor documentation for configuration details.
FAQs
When should I use the native Sentry SDK?
The native SDK suits scenarios where you're starting fresh and Sentry is your primary observability tool. Benefits include automatic issue creation from logs, session replay integration, automatic correlation with errors, transactions, and user sessions, and automatic integration with Sentry error tracking and breadcrumbs.
When should I use OTLP?
OTLP is optimal when you've already invested in OpenTelemetry and prefer flexibility in backend selection. Choose OTLP if you already have OpenTelemetry logging in your codebase, send logs to multiple observability backends, need vendor-neutral instrumentation, or work with AI and LLM frameworks using OpenTelemetry by default.
Why aren't my logs appearing in Sentry?
First verify credentials match settings in Settings → Client Keys (DSN) → OpenTelemetry (OTLP). Logs typically appear within 30-60 seconds after transmission. If logs remain absent, check console output for the initialization message and enable OpenTelemetry debug logging.
Why are some log attributes missing?
OpenTelemetry attributes must be primitive types (strings, numbers, booleans). Complex objects won't serialize through OTLP. Flatten nested structures by using dot notation like 'user.id' and 'user.name' instead of nested objects.
Can I search or filter by array attributes?
Array attributes are viewable in log detail but aren't yet searchable or filterable—a current OTLP beta limitation. For searchable data, use separate attributes or join arrays into comma-separated strings.