Same agent Smaller bill. The tools and infrastructure to get more work from your AI. Fewer tokens. Less compute. Lower cost.
TypeScript agent-harnessagent-harness-engineeringai-engineeringai-observabilityamd-gpuclaude-codeclickhousecodexcoding-agentcoding-agentscursorgenaigpu-monitoringllm-observabilityllmopsnvidia-smiobservabilityopen-sourceopenaiopentelemetry
feat: add ElevenLabs auto-instrumentation for text-to-speech (#1255)
Jaysheel Dodia16635d15aecd
diff --git a/README.md b/README.mdindex cfe2304d..5b604098 100644--- a/README.md+++ b/README.md@@ -326,7 +326,7 @@ OpenLIT auto-instruments **50+ LLM providers, AI frameworks, and vector database<table><tr>-<td align="center" width="160"><a href="https://docs.openlit.io/latest/sdk/integrations/elevenlabs"><b>ElevenLabs</b></a><br/><img src="https://img.shields.io/badge/Python-3776AB?logo=python&logoColor=white&style=flat-square" height="18"></td>+<td align="center" width="160"><a href="https://docs.openlit.io/latest/sdk/integrations/elevenlabs"><b>ElevenLabs</b></a><br/><img src="https://img.shields.io/badge/Python-3776AB?logo=python&logoColor=white&style=flat-square" height="18"> <img src="https://img.shields.io/badge/TypeScript-3178C6?logo=typescript&logoColor=white&style=flat-square" height="18"></td><td align="center" width="160"><a href="https://docs.openlit.io/latest/sdk/integrations/assemblyai"><b>AssemblyAI</b></a><br/><img src="https://img.shields.io/badge/Python-3776AB?logo=python&logoColor=white&style=flat-square" height="18"></td><td align="center" width="160"><a href="https://docs.openlit.io/latest/sdk/integrations/mcp"><b>MCP</b></a><br/><img src="https://img.shields.io/badge/Python-3776AB?logo=python&logoColor=white&style=flat-square" height="18"></td></tr>diff --git a/docs/latest/sdk/integrations/elevenlabs.mdx b/docs/latest/sdk/integrations/elevenlabs.mdxindex 18ae1e7d..74ec6479 100644--- a/docs/latest/sdk/integrations/elevenlabs.mdx+++ b/docs/latest/sdk/integrations/elevenlabs.mdx@@ -5,21 +5,76 @@ sidebarTitle: 'ElevenLabs'import IntegrationMethods from '/snippets/integration-methods.mdx';-OpenLIT uses OpenTelemetry Auto-Instrumentation to help you monitor LLM applications built using Audio and Speech models from ElevenLabs. This includes tracking performance, costs, Voice inputs and settings and how users interact with the application.+OpenLIT uses OpenTelemetry Auto-Instrumentation to help you monitor LLM applications built using Audio and Speech models from ElevenLabs. This includes tracking performance, costs, voice inputs and settings, and how users interact with the application.Auto-instrumentation means you don't have to set up monitoring manually for different LLMs, frameworks, or databases. By simply adding OpenLIT in your application, all the necessary monitoring configurations are automatically set up.-The integration is compatible with-- ElevenLabs Python SDK client `>= 1.4.0`+The integration is compatible with+- ElevenLabs Python SDK client `>= 1.4.0`+- ElevenLabs TypeScript SDK client (`@elevenlabs/elevenlabs-js` or `elevenlabs`) `>= 1.0.0`-## Get started+## Supported APIs++| SDK | Instrumented methods |+|-----|---------------------|+| **TypeScript** | `ElevenLabsClient.textToSpeech.convert`, `.stream`, `.convertWithTimestamps` |+| **Python** | `TextToSpeechClient.convert` and `AsyncTextToSpeechClient.convert` |++TypeScript covers more TTS methods than Python. Python instruments `.convert` only (sync and async).++## Prerequisites++- Install the ElevenLabs SDK yourself. OpenLIT does not bundle it as a dependency.+- For TypeScript, call `openlit.init()` **before** the ElevenLabs module is first loaded so OpenTelemetry can hook into the SDK at runtime.++## TypeScript example++```typescript+import openlit from "openlit";++openlit.init({ otlpEndpoint: "YOUR_OTEL_ENDPOINT" });++import { ElevenLabsClient } from "@elevenlabs/elevenlabs-js";++const client = new ElevenLabsClient({ apiKey: process.env.ELEVENLABS_API_KEY! });+const audio = await client.textToSpeech.convert("voice-id", {+ text: "Hello",+ modelId: "eleven_multilingual_v2", // also accepts model_id / model+});+```++## Configuration++| Option | Behavior |+|--------|----------|+| `captureMessageContent` | Gates `gen_ai.input.messages` / `gen_ai.output.messages` on spans and in events |+| `disableEvents` | Suppresses all inference events |+| `disableMetrics` | Global — metrics instruments are not set up |+| `disabledInstrumentors: ['elevenlabs']` | Disables this instrumentation |++<Note>+On the Python SDK, inference events are only emitted when `capture_message_content` is enabled. On the TypeScript SDK, inference events are emitted when `disableEvents` is `false`, regardless of `captureMessageContent` (message fields within the event are still gated by content capture).+</Note>++## Get started<Steps><Step title="Install OpenLIT">Open your command line or terminal and run:- ```shell- pip install openlit- ```+ <Tabs>+ <Tab title="Python">+ ```shell+ pip install openlit+ ```+ </Tab>+ <Tab title="Typescript">+ ```shell+ npm install openlit+ npm install @elevenlabs/elevenlabs-js+ ```+ The `elevenlabs` package is also supported.+ </Tab>+ </Tabs></Step><IntegrationMethods /></Steps>@@ -43,4 +98,4 @@ The integration is compatible withhref="/latest/controller/overview">Discover and instrument LLM traffic across Kubernetes, Docker, and Linux using eBPF — no code changes required.- </Card>\ No newline at end of file+ </Card>diff --git a/docs/latest/sdk/integrations/overview.mdx b/docs/latest/sdk/integrations/overview.mdxindex bfd596f1..aec88ae5 100644--- a/docs/latest/sdk/integrations/overview.mdx+++ b/docs/latest/sdk/integrations/overview.mdx@@ -583,6 +583,7 @@ Start integrating and monitoring your applications with OpenLIT in just one line><div style={{position: "absolute", top: "12px", right: "12px", display: "flex", gap: "4px"}}><img src="https://cdn.simpleicons.org/python/ffa500" width="16" height="16" alt="Python SDK" title="Python SDK" />+ <img src="https://cdn.simpleicons.org/typescript/ffa500" width="16" height="16" alt="TypeScript SDK" title="TypeScript SDK" /></div></Card>diff --git a/sdk/typescript/README.md b/sdk/typescript/README.mdindex 6dce368f..823e5125 100644--- a/sdk/typescript/README.md+++ b/sdk/typescript/README.md@@ -48,6 +48,10 @@ This project proudly follows and maintains the [Semantic Conventions](https://gi| [✅ Replicate](https://docs.openlit.io/latest/integrations/replicate) || [✅ Azure OpenAI](https://docs.openlit.io/latest/integrations/azure-openai) *(via OpenAI SDK)* |+| Audio / Speech |+| ------------------------------------------------------------------------------------------- |+| [✅ ElevenLabs](https://docs.openlit.io/latest/sdk/integrations/elevenlabs) |+| Vector Databases || ------------------------------------------------------------------------------------------- || [✅ Chroma](https://docs.openlit.io/latest/integrations/chromadb) |diff --git a/sdk/typescript/src/instrumentation/__tests__/elevenlabs-wrapper.test.ts b/sdk/typescript/src/instrumentation/__tests__/elevenlabs-wrapper.test.tsnew file mode 100644index 00000000..9467fe55--- /dev/null+++ b/sdk/typescript/src/instrumentation/__tests__/elevenlabs-wrapper.test.ts@@ -0,0 +1,379 @@+import { Span, trace } from '@opentelemetry/api';+import ElevenLabsWrapper from '../elevenlabs/wrapper';+import OpenlitConfig from '../../config';+import OpenLitHelper from '../../helpers';+import BaseWrapper from '../base-wrapper';+import SemanticConvention from '../../semantic-convention';++jest.mock('../../../src/config');+jest.mock('../../../src/helpers');+jest.mock('../../../src/instrumentation/base-wrapper');++const mockTracer = trace.getTracer('test-tracer');++describe('ElevenLabsWrapper', () => {+ let span: Span;++ beforeEach(() => {+ span = mockTracer.startSpan('test-span');+ span.setAttribute = jest.fn();+ span.addEvent = jest.fn();+ jest.clearAllMocks();++ (OpenLitHelper as any).buildInputMessages = jest+ .fn()+ .mockImplementation((messages: Array<{ role: string; content: string }>) =>+ JSON.stringify(+ messages.map((m) => ({+ role: m.role,+ parts: [{ type: 'text', content: m.content }],+ }))+ )+ );+ (OpenLitHelper as any).buildOutputMessages = jest+ .fn()+ .mockImplementation((text: string, finishReason: string) =>+ JSON.stringify([+ {+ role: 'assistant',+ parts: [{ type: 'text', content: text }],+ finish_reason: finishReason,+ },+ ])+ );+ (BaseWrapper as any).setBaseSpanAttributes = jest.fn();+ });++ afterEach(() => {+ span.end();+ });++ describe('_parseAudioArgs', () => {+ it('should parse voice id and options from positional args', () => {+ const parsed = ElevenLabsWrapper._parseAudioArgs([+ 'voice-id-123',+ {+ text: 'Hello world',+ model_id: 'eleven_turbo_v2',+ voice_settings: { stability: 0.5 },+ output_format: 'pcm_16000',+ },+ ]);++ expect(parsed).toEqual({+ voiceId: 'voice-id-123',+ options: {+ text: 'Hello world',+ model_id: 'eleven_turbo_v2',+ voice_settings: { stability: 0.5 },+ output_format: 'pcm_16000',+ },+ requestModel: 'eleven_turbo_v2',+ text: 'Hello world',+ voiceSettings: { stability: 0.5 },+ outputFormat: 'pcm_16000',+ });+ });++ it('should parse camelCase modelId from the official JS SDK', () => {+ const parsed = ElevenLabsWrapper._parseAudioArgs([+ 'voice-id-123',+ {+ text: 'Hello',+ modelId: 'eleven_turbo_v2_5',+ voiceSettings: { stability: 0.5 },+ outputFormat: 'mp3_44100_128',+ },+ ]);++ expect(parsed.requestModel).toBe('eleven_turbo_v2_5');+ expect(parsed.voiceSettings).toEqual({ stability: 0.5 });+ expect(parsed.outputFormat).toBe('mp3_44100_128');+ });++ it('should parse voice id and options from a single object arg', () => {+ const parsed = ElevenLabsWrapper._parseAudioArgs([+ {+ voice_id: 'voice-id-456',+ text: 'Nested args',+ model: 'eleven_monolingual_v1',+ },+ ]);++ expect(parsed.voiceId).toBe('voice-id-456');+ expect(parsed.requestModel).toBe('eleven_monolingual_v1');+ expect(parsed.text).toBe('Nested args');+ });+ });++ describe('_commonAudioSetter', () => {+ it('should set span attributes and return metric parameters', () => {+ const mockArgs = [+ 'voice-id-123',+ {+ text: 'Hello world',+ model_id: 'eleven_multilingual_v2',+ voice_settings: { stability: 0.75, similarity_boost: 0.85 },+ output_format: 'mp3_44100_128',+ },+ ];++ (OpenlitConfig as any).pricingInfo = {};+ (OpenlitConfig as any).disableEvents = true;+ (OpenlitConfig as any).captureMessageContent = true;+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);++ const metricParams = ElevenLabsWrapper._commonAudioSetter({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.convert',+ span,+ isStream: false,+ });++ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE,+ 'voice-id-123'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS,+ JSON.stringify({ stability: 0.75, similarity_boost: 0.85 })+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_OUTPUT_TYPE,+ 'mp3_44100_128'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_REQUEST_IS_STREAM,+ false+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_SERVER_TTFT,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_SERVER_TBT,+ 0+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_PROVIDER_NAME,+ 'elevenlabs'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_RESPONSE_MODEL,+ 'eleven_multilingual_v2'+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_INPUT_MESSAGES,+ JSON.stringify([{ role: 'user', parts: [{ type: 'text', content: 'Hello world' }] }])+ );+ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,+ JSON.stringify([{ role: 'assistant', parts: [{ type: 'text', content: '[audio generated]' }], finish_reason: 'stop' }])+ );++ expect(metricParams).toEqual({+ genAIEndpoint: 'elevenlabs.textToSpeech.convert',+ model: 'eleven_multilingual_v2',+ cost: 0.005,+ aiSystem: 'elevenlabs',+ serverAddress: 'api.elevenlabs.io',+ serverPort: 443,+ });+ });++ it('should stamp the ElevenLabs package version on the span', () => {+ const mockArgs = ['voice-id-123', { text: 'Hello world' }];++ (OpenlitConfig as any).pricingInfo = {};+ (OpenlitConfig as any).disableEvents = true;+ (OpenlitConfig as any).captureMessageContent = false;+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);++ ElevenLabsWrapper._commonAudioSetter({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.convert',+ span,+ isStream: false,+ sdkVersion: '2.54.0',+ });++ expect(span.setAttribute).toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_SDK_VERSION,+ '2.54.0'+ );+ });++ it('should not set message content if captureMessageContent is false', () => {+ const mockArgs = [+ 'voice-id-123',+ {+ text: 'Hello world',+ },+ ];++ (OpenlitConfig as any).pricingInfo = {};+ (OpenlitConfig as any).disableEvents = true;+ (OpenlitConfig as any).captureMessageContent = false;+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);++ ElevenLabsWrapper._commonAudioSetter({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.convert',+ span,+ isStream: false,+ });++ expect(span.setAttribute).not.toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_INPUT_MESSAGES,+ expect.any(String)+ );+ expect(span.setAttribute).not.toHaveBeenCalledWith(+ SemanticConvention.GEN_AI_OUTPUT_MESSAGES,+ expect.any(String)+ );+ });++ it('should emit inference event if disableEvents is false', () => {+ const mockArgs = [+ 'voice-id-123',+ {+ text: 'Hello world',+ },+ ];++ (OpenlitConfig as any).pricingInfo = {};+ (OpenlitConfig as any).disableEvents = false;+ (OpenlitConfig as any).captureMessageContent = true;+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);++ const spyEmit = jest.spyOn(OpenLitHelper, 'emitInferenceEvent');++ ElevenLabsWrapper._commonAudioSetter({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.convert',+ span,+ isStream: false,+ });++ expect(spyEmit).toHaveBeenCalledWith(span, expect.objectContaining({+ [SemanticConvention.GEN_AI_OPERATION]: 'audio',+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: 'eleven_multilingual_v2',+ }));+ });+ });++ describe('_streamGenerator', () => {+ it('should yield chunks and call metric/setter methods', async () => {+ const mockArgs = [+ 'voice-id-123',+ {+ text: 'Hello world streaming',+ model_id: 'eleven_multilingual_v2',+ },+ ];++ const mockResponse = (async function* () {+ yield Buffer.from('chunk1');+ yield Buffer.from('chunk2');+ })();++ (OpenlitConfig as any).pricingInfo = {};+ (OpenlitConfig as any).disableEvents = true;+ jest.spyOn(OpenLitHelper, 'getAudioModelCost').mockReturnValue(0.005);++ jest.spyOn(ElevenLabsWrapper, '_commonAudioSetter').mockReturnValue({+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ model: 'eleven_multilingual_v2',+ cost: 0.005,+ aiSystem: 'elevenlabs',+ });++ const generator = ElevenLabsWrapper._streamGenerator({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ response: mockResponse,+ span,+ });++ const chunks = [];+ for await (const chunk of generator) {+ chunks.push(chunk);+ }++ expect(chunks).toHaveLength(2);+ expect(ElevenLabsWrapper._commonAudioSetter).toHaveBeenCalledWith(expect.objectContaining({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ span,+ isStream: true,+ ttft: expect.any(Number),+ tbt: expect.any(Number),+ }));++ expect(BaseWrapper.recordMetrics).toHaveBeenCalledWith(span, {+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ model: 'eleven_multilingual_v2',+ cost: 0.005,+ aiSystem: 'elevenlabs',+ });+ });++ it('should record error metrics when streaming fails', async () => {+ const mockArgs = [+ 'voice-id-123',+ {+ text: 'Hello world streaming',+ model_id: 'eleven_multilingual_v2',+ },+ ];++ const mockResponse = (async function* () {+ yield Buffer.from('chunk1');+ throw new Error('stream failed');+ })();++ const generator = ElevenLabsWrapper._streamGenerator({+ args: mockArgs,+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ response: mockResponse,+ span,+ });++ await expect(async () => {+ for await (const _chunk of generator) {+ // consume until error+ }+ }).rejects.toThrow('stream failed');++ expect(OpenLitHelper.handleException).toHaveBeenCalledWith(span, expect.any(Error));+ expect(BaseWrapper.recordMetrics).toHaveBeenCalledWith(span, {+ genAIEndpoint: 'elevenlabs.textToSpeech.stream',+ model: 'eleven_multilingual_v2',+ aiSystem: 'elevenlabs',+ serverAddress: 'api.elevenlabs.io',+ serverPort: 443,+ errorType: 'Error',+ });+ });+ });+});diff --git a/sdk/typescript/src/instrumentation/elevenlabs/index.ts b/sdk/typescript/src/instrumentation/elevenlabs/index.tsnew file mode 100644index 00000000..bebaaeea--- /dev/null+++ b/sdk/typescript/src/instrumentation/elevenlabs/index.ts@@ -0,0 +1,130 @@+import {+ InstrumentationBase,+ InstrumentationModuleDefinition,+ InstrumentationNodeModuleDefinition,+ isWrapped,+} from '@opentelemetry/instrumentation';+import { InstrumentationConfig } from '@opentelemetry/instrumentation';+import { diag } from '@opentelemetry/api';+import { INSTRUMENTATION_PREFIX } from '../../constant';+import ElevenLabsWrapper from './wrapper';++export interface ElevenLabsInstrumentationConfig extends InstrumentationConfig {}++export default class OpenlitElevenLabsInstrumentation extends InstrumentationBase {+ private _textToSpeechProto: any = null;++ constructor(config: ElevenLabsInstrumentationConfig = {}) {+ super(`${INSTRUMENTATION_PREFIX}/instrumentation-elevenlabs`, '1.0.0', config);+ }++ protected init(): void | InstrumentationModuleDefinition | InstrumentationModuleDefinition[] {+ const elevenlabsModule = new InstrumentationNodeModuleDefinition(+ 'elevenlabs',+ ['>=1.4.0'],+ (moduleExports, moduleVersion) => {+ this._patch(moduleExports, moduleVersion);+ return moduleExports;+ },+ (moduleExports) => {+ if (moduleExports !== undefined) {+ this._unpatch(moduleExports);+ }+ }+ );++ const elevenlabsJsModule = new InstrumentationNodeModuleDefinition(+ '@elevenlabs/elevenlabs-js',+ ['>=1.0.0'],+ (moduleExports, moduleVersion) => {+ this._patch(moduleExports, moduleVersion);+ return moduleExports;+ },+ (moduleExports) => {+ if (moduleExports !== undefined) {+ this._unpatch(moduleExports);+ }+ }+ );++ return [elevenlabsModule, elevenlabsJsModule];+ }++ public manualPatch(elevenlabs: any): void {+ this._patch(elevenlabs);+ }++ protected _patch(moduleExports: any, moduleVersion?: string) {+ try {+ const OriginalElevenLabsClient = moduleExports.ElevenLabsClient;+ if (!OriginalElevenLabsClient || typeof OriginalElevenLabsClient !== 'function') {+ return;+ }++ if (isWrapped(OriginalElevenLabsClient)) {+ this._unwrap(moduleExports, 'ElevenLabsClient');+ }++ const tracer = this.tracer;+ const sdkVersion = moduleVersion ? String(moduleVersion) : undefined;++ this._wrap(moduleExports, 'ElevenLabsClient', (original: (...args: any[]) => any) => {+ return (...clientArgs: any[]) => {+ const client = new (original as any)(...clientArgs);++ if (client && client.textToSpeech) {+ const textToSpeechProto = Object.getPrototypeOf(client.textToSpeech);+ if (textToSpeechProto) {+ this._textToSpeechProto = textToSpeechProto;++ if (textToSpeechProto.convert && !isWrapped(textToSpeechProto.convert)) {+ this._wrap(+ textToSpeechProto,+ 'convert',+ ElevenLabsWrapper._patchConvert(tracer, 'convert', sdkVersion)+ );+ }++ if (textToSpeechProto.stream && !isWrapped(textToSpeechProto.stream)) {+ this._wrap(+ textToSpeechProto,+ 'stream',+ ElevenLabsWrapper._patchStream(tracer, 'stream', sdkVersion)+ );+ }++ if (textToSpeechProto.convertWithTimestamps && !isWrapped(textToSpeechProto.convertWithTimestamps)) {+ this._wrap(+ textToSpeechProto,+ 'convertWithTimestamps',+ ElevenLabsWrapper._patchConvert(tracer, 'convertWithTimestamps', sdkVersion)+ );+ }+ }+ }++ return client;+ };+ });+ } catch (e) {+ diag.error('elevenlabs instrumentation: error in _patch method', e);+ }+ }++ protected _unpatch(moduleExports: any) {+ if (moduleExports.ElevenLabsClient) {+ this._unwrap(moduleExports, 'ElevenLabsClient');+ }+ if (this._textToSpeechProto) {+ if (isWrapped(this._textToSpeechProto.convert)) {+ this._unwrap(this._textToSpeechProto, 'convert');+ }+ if (isWrapped(this._textToSpeechProto.stream)) {+ this._unwrap(this._textToSpeechProto, 'stream');+ }+ if (isWrapped(this._textToSpeechProto.convertWithTimestamps)) {+ this._unwrap(this._textToSpeechProto, 'convertWithTimestamps');+ }+ }+ }+}diff --git a/sdk/typescript/src/instrumentation/elevenlabs/wrapper.ts b/sdk/typescript/src/instrumentation/elevenlabs/wrapper.tsnew file mode 100644index 00000000..6268bcfa--- /dev/null+++ b/sdk/typescript/src/instrumentation/elevenlabs/wrapper.ts@@ -0,0 +1,333 @@+import { Span, SpanKind, Tracer, context, trace, Attributes } from '@opentelemetry/api';+import OpenlitConfig from '../../config';+import OpenLitHelper, {+ isFrameworkLlmActive,+ getFrameworkParentContext,+} from '../../helpers';+import SemanticConvention from '../../semantic-convention';+import BaseWrapper, { BaseSpanAttributes } from '../base-wrapper';++function spanCreationAttrs(+ operationName: string,+ requestModel: string,+ serverAddress: string,+ serverPort: number+): Attributes {+ return {+ [SemanticConvention.GEN_AI_OPERATION]: operationName,+ [SemanticConvention.GEN_AI_PROVIDER_NAME_OTEL]: ElevenLabsWrapper.aiSystem,+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,+ [SemanticConvention.SERVER_ADDRESS]: serverAddress,+ [SemanticConvention.SERVER_PORT]: serverPort,+ };+}++class ElevenLabsWrapper extends BaseWrapper {+ static aiSystem = SemanticConvention.GEN_AI_SYSTEM_ELEVENLABS;+ static serverAddress = 'api.elevenlabs.io';+ static serverPort = 443;++ static _parseAudioArgs(args: any[]): {+ voiceId: string;+ options: Record<string, any>;+ requestModel: string;+ text: string;+ voiceSettings: unknown;+ outputFormat: string;+ } {+ const voiceId =+ typeof args[0] === 'string'+ ? args[0]+ : (args[0]?.voice_id || args[0]?.voiceId || '');+ const options =+ typeof args[0] === 'object' && args[0] !== null && typeof args[0] !== 'string'+ ? args[0]+ : (args[1] || {});+ const requestModel =+ options.modelId || options.model_id || options.model || 'eleven_multilingual_v2';+ const text = options.text || options.input || '';+ const voiceSettings = options.voiceSettings ?? options.voice_settings ?? '';+ const outputFormat =+ options.outputFormat || options.output_format || 'mp3_44100_128';++ return { voiceId, options, requestModel, text, voiceSettings, outputFormat };+ }++ static _patchConvert(tracer: Tracer, methodName: string, sdkVersion?: string): any {+ const genAIEndpoint = `elevenlabs.textToSpeech.${methodName}`;+ return (originalMethod: (...args: any[]) => any) => {+ return async function (this: any, ...args: any[]) {+ if (isFrameworkLlmActive()) return originalMethod.apply(this, args);++ const { requestModel } = ElevenLabsWrapper._parseAudioArgs(args);++ const spanName = `${SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} ${requestModel}`;+ const effectiveCtx = getFrameworkParentContext() ?? context.active();+ const span = tracer.startSpan(spanName, {+ kind: SpanKind.CLIENT,+ attributes: spanCreationAttrs(+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,+ requestModel,+ ElevenLabsWrapper.serverAddress,+ ElevenLabsWrapper.serverPort+ ),+ }, effectiveCtx);++ return context.with(trace.setSpan(effectiveCtx, span), async () => {+ let metricParams;+ const startTime = Date.now();+ try {+ const response = await originalMethod.apply(this, args);+ const ttft = (Date.now() - startTime) / 1000;++ metricParams = ElevenLabsWrapper._commonAudioSetter({+ args,+ genAIEndpoint,+ span,+ ttft,+ tbt: 0,+ isStream: false,+ sdkVersion,+ });++ return response;+ } catch (e: any) {+ OpenLitHelper.handleException(span, e);+ BaseWrapper.recordMetrics(span, {+ genAIEndpoint,+ model: requestModel,+ aiSystem: ElevenLabsWrapper.aiSystem,+ serverAddress: ElevenLabsWrapper.serverAddress,+ serverPort: ElevenLabsWrapper.serverPort,+ errorType: e?.constructor?.name || '_OTHER',+ });+ throw e;+ } finally {+ span.end();+ if (metricParams) {+ BaseWrapper.recordMetrics(span, metricParams);+ }+ }+ });+ };+ };+ }++ static _patchStream(tracer: Tracer, methodName: string, sdkVersion?: string): any {+ const genAIEndpoint = `elevenlabs.textToSpeech.${methodName}`;+ return (originalMethod: (...args: any[]) => any) => {+ return async function (this: any, ...args: any[]) {+ if (isFrameworkLlmActive()) return originalMethod.apply(this, args);++ const { requestModel } = ElevenLabsWrapper._parseAudioArgs(args);++ const spanName = `${SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO} ${requestModel}`;+ const effectiveCtx = getFrameworkParentContext() ?? context.active();+ const span = tracer.startSpan(spanName, {+ kind: SpanKind.CLIENT,+ attributes: spanCreationAttrs(+ SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,+ requestModel,+ ElevenLabsWrapper.serverAddress,+ ElevenLabsWrapper.serverPort+ ),+ }, effectiveCtx);++ return context.with(trace.setSpan(effectiveCtx, span), async () => {+ try {+ const response = await originalMethod.apply(this, args);++ return OpenLitHelper.createStreamProxy(+ response,+ ElevenLabsWrapper._streamGenerator({+ args,+ genAIEndpoint,+ response,+ span,+ sdkVersion,+ })+ );+ } catch (e: any) {+ OpenLitHelper.handleException(span, e);+ BaseWrapper.recordMetrics(span, {+ genAIEndpoint,+ model: requestModel,+ aiSystem: ElevenLabsWrapper.aiSystem,+ serverAddress: ElevenLabsWrapper.serverAddress,+ serverPort: ElevenLabsWrapper.serverPort,+ errorType: e?.constructor?.name || '_OTHER',+ });+ span.end();+ throw e;+ }+ });+ };+ };+ }++ static async *_streamGenerator({+ args,+ genAIEndpoint,+ response,+ span,+ sdkVersion,+ }: {+ args: any[];+ genAIEndpoint: string;+ response: any;+ span: Span;+ sdkVersion?: string;+ }): AsyncGenerator<unknown, any, unknown> {+ let metricParams;+ const timestamps: number[] = [];+ const startTime = Date.now();++ try {+ for await (const chunk of response) {+ timestamps.push(Date.now());+ yield chunk;+ }++ const ttft = timestamps.length > 0 ? (timestamps[0] - startTime) / 1000 : 0;+ let tbt = 0;+ if (timestamps.length > 1) {+ const timeDiffs = timestamps.slice(1).map((t, i) => t - timestamps[i]);+ tbt = timeDiffs.reduce((a, b) => a + b, 0) / timeDiffs.length / 1000;+ }++ metricParams = ElevenLabsWrapper._commonAudioSetter({+ args,+ genAIEndpoint,+ span,+ ttft,+ tbt,+ isStream: true,+ sdkVersion,+ });+ } catch (e: any) {+ OpenLitHelper.handleException(span, e);+ const { requestModel } = ElevenLabsWrapper._parseAudioArgs(args);+ BaseWrapper.recordMetrics(span, {+ genAIEndpoint,+ model: requestModel,+ aiSystem: ElevenLabsWrapper.aiSystem,+ serverAddress: ElevenLabsWrapper.serverAddress,+ serverPort: ElevenLabsWrapper.serverPort,+ errorType: e?.constructor?.name || '_OTHER',+ });+ throw e;+ } finally {+ span.end();+ if (metricParams) {+ BaseWrapper.recordMetrics(span, metricParams);+ }+ }+ }++ static _commonAudioSetter({+ args,+ genAIEndpoint,+ span,+ ttft = 0,+ tbt = 0,+ isStream = false,+ sdkVersion,+ }: {+ args: any[];+ genAIEndpoint: string;+ span: Span;+ ttft?: number;+ tbt?: number;+ isStream?: boolean;+ sdkVersion?: string;+ }): BaseSpanAttributes {+ const captureContent = OpenlitConfig.captureMessageContent;++ const {+ voiceId,+ requestModel,+ text,+ voiceSettings,+ outputFormat,+ } = ElevenLabsWrapper._parseAudioArgs(args);++ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_AUDIO_VOICE, voiceId);+ span.setAttribute(+ SemanticConvention.GEN_AI_REQUEST_AUDIO_SETTINGS,+ voiceSettings === '' || voiceSettings == null+ ? ''+ : typeof voiceSettings === 'object'+ ? JSON.stringify(voiceSettings)+ : String(voiceSettings)+ );+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_TYPE, outputFormat);+ span.setAttribute(SemanticConvention.GEN_AI_REQUEST_IS_STREAM, isStream);++ span.setAttribute(SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS, 0);+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS, 0);+ span.setAttribute(SemanticConvention.GEN_AI_CLIENT_TOKEN_USAGE, 0);+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, 0);+ span.setAttribute(SemanticConvention.GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, 0);+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TTFT, ttft);+ span.setAttribute(SemanticConvention.GEN_AI_SERVER_TBT, tbt);++ const pricingInfo = OpenlitConfig.pricingInfo || {};+ const cost = OpenLitHelper.getAudioModelCost(requestModel, pricingInfo, text);++ ElevenLabsWrapper.setBaseSpanAttributes(span, {+ genAIEndpoint,+ model: requestModel,+ cost,+ aiSystem: ElevenLabsWrapper.aiSystem,+ serverAddress: ElevenLabsWrapper.serverAddress,+ serverPort: ElevenLabsWrapper.serverPort,+ });++ // Python stamps gen_ai.system and the ElevenLabs package version (not OpenLIT's).+ span.setAttribute(SemanticConvention.GEN_AI_PROVIDER_NAME, ElevenLabsWrapper.aiSystem);+ span.setAttribute(SemanticConvention.GEN_AI_RESPONSE_MODEL, requestModel);+ if (sdkVersion) {+ span.setAttribute(SemanticConvention.GEN_AI_SDK_VERSION, sdkVersion);+ }++ let inputMessagesJson: string | undefined;+ let outputMessagesJson: string | undefined;++ if (captureContent) {+ inputMessagesJson = OpenLitHelper.buildInputMessages([{ role: 'user', content: text }]);+ outputMessagesJson = OpenLitHelper.buildOutputMessages('[audio generated]', 'stop');++ span.setAttribute(SemanticConvention.GEN_AI_INPUT_MESSAGES, inputMessagesJson);+ span.setAttribute(SemanticConvention.GEN_AI_OUTPUT_MESSAGES, outputMessagesJson);+ }++ if (!OpenlitConfig.disableEvents) {+ const eventAttrs: Attributes = {+ [SemanticConvention.GEN_AI_OPERATION]: SemanticConvention.GEN_AI_OPERATION_TYPE_AUDIO,+ [SemanticConvention.GEN_AI_REQUEST_MODEL]: requestModel,+ [SemanticConvention.GEN_AI_RESPONSE_MODEL]: requestModel,+ [SemanticConvention.SERVER_ADDRESS]: ElevenLabsWrapper.serverAddress,+ [SemanticConvention.SERVER_PORT]: ElevenLabsWrapper.serverPort,+ [SemanticConvention.GEN_AI_OUTPUT_TYPE]: outputFormat,+ [SemanticConvention.GEN_AI_USAGE_INPUT_TOKENS]: 0,+ [SemanticConvention.GEN_AI_USAGE_OUTPUT_TOKENS]: 0,+ };+ if (captureContent) {+ if (inputMessagesJson) eventAttrs[SemanticConvention.GEN_AI_INPUT_MESSAGES] = inputMessagesJson;+ if (outputMessagesJson) eventAttrs[SemanticConvention.GEN_AI_OUTPUT_MESSAGES] = outputMessagesJson;+ }+ OpenLitHelper.emitInferenceEvent(span, eventAttrs);+ }++ return {+ genAIEndpoint,+ model: requestModel,+ cost,+ aiSystem: ElevenLabsWrapper.aiSystem,+ serverAddress: ElevenLabsWrapper.serverAddress,+ serverPort: ElevenLabsWrapper.serverPort,+ };+ }+}++export default ElevenLabsWrapper;diff --git a/sdk/typescript/src/instrumentation/index.ts b/sdk/typescript/src/instrumentation/index.tsindex ab296c10..8b3cf7f1 100644--- a/sdk/typescript/src/instrumentation/index.ts+++ b/sdk/typescript/src/instrumentation/index.ts@@ -33,6 +33,7 @@ import CursorSDKInstrumentation from './cursor-sdk';import AstraInstrumentation from './astra';import MCPInstrumentation from './mcp';import Mem0Instrumentation from './mem0';+import ElevenLabsInstrumentation from './elevenlabs';/*** OTel community instrumentations loaded dynamically (like Python SDK).@@ -108,6 +109,7 @@ export default class Instrumentations {'astra': new AstraInstrumentation(),mcp: new MCPInstrumentation(),mem0: new Mem0Instrumentation(),+ elevenlabs: new ElevenLabsInstrumentation(),};static setup(diff --git a/sdk/typescript/src/semantic-convention.ts b/sdk/typescript/src/semantic-convention.tsindex c9cf11ff..ae13c4f6 100644--- a/sdk/typescript/src/semantic-convention.ts+++ b/sdk/typescript/src/semantic-convention.ts@@ -85,6 +85,7 @@ export default class SemanticConvention {static GEN_AI_REQUEST_EMBEDDING_DIMENSION = 'gen_ai.request.embedding_dimension';static GEN_AI_REQUEST_TOOL_CHOICE = 'gen_ai.request.tool_choice';static GEN_AI_REQUEST_AUDIO_VOICE = 'gen_ai.request.audio_voice';+ static GEN_AI_REQUEST_AUDIO_SETTINGS = 'gen_ai.request.audio_settings';static GEN_AI_REQUEST_AUDIO_RESPONSE_FORMAT = 'gen_ai.request.audio_response_format';static GEN_AI_REQUEST_AUDIO_SPEED = 'gen_ai.request.audio_speed';static GEN_AI_REQUEST_FINETUNE_STATUS = 'gen_ai.request.fine_tune_status';@@ -245,6 +246,7 @@ export default class SemanticConvention {static GEN_AI_SYSTEM_GOOGLE_ADK = 'google_adk';static GEN_AI_SYSTEM_STRANDS = 'strands_agents';static GEN_AI_SYSTEM_CURSOR = 'cursor';+ static GEN_AI_SYSTEM_ELEVENLABS = 'elevenlabs';static GEN_AI_SYSTEM_MCP = 'mcp';static GEN_AI_SYSTEM_MEM0 = 'mem0';diff --git a/sdk/typescript/src/types.ts b/sdk/typescript/src/types.tsindex 0c5e9591..a5807a31 100644--- a/sdk/typescript/src/types.ts+++ b/sdk/typescript/src/types.ts@@ -3,7 +3,7 @@ import { NodeTracerProvider } from '@opentelemetry/sdk-trace-node';import { metrics } from '@opentelemetry/api';import type { Guard } from './guard/base';-export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp' | 'mem0';+export type InstrumentationType = 'openai' | 'anthropic' | 'cohere' | 'groq' | 'mistral' | 'google-ai' | 'vertexai' | 'together' | 'ollama' | 'vercel-ai' | 'langchain' | 'langgraph' | 'pinecone' | 'bedrock' | 'llamaindex' | 'huggingface' | 'replicate' | 'chroma' | 'qdrant' | 'milvus' | 'astra' | 'azure-ai-inference' | 'openai-agents' | 'strands' | 'google-adk' | 'claude-agent-sdk' | 'cursor-sdk' | 'ai21' | 'gradient' | 'mcp' | 'mem0' | 'elevenlabs';export type OpenlitInstrumentations = Partial<Record<InstrumentationType, any>>;