Skip to content

Conversation

@andrewm4894
Copy link
Contributor

@andrewm4894 andrewm4894 commented Nov 21, 2025

Summary

Adds OpenTelemetry instrumentation providers for both Node.js and Python ecosystems to demonstrate automatic tracing with PostHog's OTEL ingestion endpoint.

Changes

Python - OpenAI OTEL v1 Provider (#12) ✅

  • Rename openai_otel.pyopenai_otel_v1.py for clarity
  • Uses community opentelemetry-instrumentation-openai package
  • Sends everything (metadata + message content) as span attributes
  • Only requires traces endpoint

Python - OpenAI OTEL v2 Provider (#13) ✅

  • Add openai_otel_v2.py using official opentelemetry-instrumentation-openai-v2
  • Sends span metadata to traces endpoint
  • Sends message content as log events to logs endpoint
  • Matches OpenTelemetry GenAI semantic conventions

Python - LlamaIndex OTEL Provider (#14) ⏸️ Deferred

  • Add llamaindex_otel.py for RAG queries with OTEL tracing
  • Note: Deferred due to LlamaIndex's unique span events architecture (stores data in OTEL span events rather than attributes/logs, requiring custom extraction logic)

Node.js - Mastra OTEL Provider (#18) ✅

  • Add mastra-otel.ts provider using Mastra's @mastra/otel-exporter
  • Successfully sends v1-style traces to PostHog OTLP endpoint
  • Includes automatic trace hierarchy and tool call tracking

Test Infrastructure

  • Add python/scripts/otel/ with test scripts for SDK, v1, and v2 comparison
  • Add node/scripts/otel/ with Mastra test script
  • Add run_otel_tests.sh batch runner for validation

Dependencies

  • Python: opentelemetry-instrumentation-openai-v2, llama-index-* packages
  • Node.js: @mastra/otel-exporter, tsx

Testing

Tested locally with PostHog OTEL ingestion endpoint:

  • OpenAI OTEL v1: Multi-turn conversations with tool calls ✅
  • OpenAI OTEL v2: Multi-turn conversations with tool calls ✅
  • Mastra: Multi-turn conversations with tool calls ✅

Run all Python OTEL tests:

./python/scripts/otel/run_otel_tests.sh

Run Mastra test:

./node/scripts/otel/run_mastra_test.sh

Related

andrewm4894 and others added 2 commits November 20, 2025 18:33
Split OpenAI OTEL provider into separate v1 and v2 implementations
to support both community and official instrumentation approaches:

v1 (opentelemetry-instrumentation-openai):
- Community maintained package
- Sends everything as span attributes (metadata + message content)
- Only requires traces endpoint
- Simpler setup, single endpoint

v2 (opentelemetry-instrumentation-openai-v2):
- Official OpenTelemetry package
- Sends metadata as span attributes
- Sends message content as separate log events
- Requires both traces and logs endpoints
- Follows official GenAI semantic conventions

This allows users to test and compare both approaches.
Add OTEL instrumentation providers for both Node.js and Python ecosystems:

Node.js - Mastra OTEL Provider (✅ Working):
- Add mastra-otel.ts provider using Mastra's automatic OTEL instrumentation
- Integrate as provider #18 in index.ts
- Add @mastra/core and @mastra/otel dependencies to package.json
- Add test scripts: test_mastra_otel.ts and run_mastra_test.sh
- Successfully sends traces to PostHog OTLP endpoint

Python - LlamaIndex OTEL Provider (⏸️ Deferred):
- Add llamaindex_otel.py provider for RAG queries with OTEL tracing
- Integrate as provider #14 in main.py
- Add llama-index dependencies to requirements.txt
- Note: Deferred due to LlamaIndex's unique span events architecture
  (stores data in OTEL span events rather than attributes/logs)

Test Infrastructure:
- Add OTEL test scripts for validation and debugging
- Add test_event_logger.py for event inspection
- Add run_otel_tests.sh for batch testing

This extends the app to support v1 OTEL instrumentation patterns (data in span
attributes) from modern frameworks like Mastra, complementing the existing v2
support (metadata in spans, content in logs).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@andrewm4894 andrewm4894 self-assigned this Nov 21, 2025
…ensitive information

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
@andrewm4894 andrewm4894 marked this pull request as ready for review November 27, 2025 10:04
@andrewm4894 andrewm4894 changed the title Add Mastra and LlamaIndex OTEL providers feat: Add OTEL providers (OpenAI v1/v2, Mastra, LlamaIndex) Nov 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants