weflayr 0.20.5 → 0.21.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +2 -0
- package/package.json +2 -2
- package/src/auto-instrument.js +67 -23
- package/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +107 -0
- package/src/instrumentation/anthropic/set_telemetry_attributes.js +27 -0
- package/src/instrumentation/bedrock/get_cache_telemetry.js +104 -0
- package/src/instrumentation/bedrock/set_telemetry_attributes.js +27 -0
- package/src/instrumentation/cohere/get_chat_telemetry.js +88 -0
- package/src/instrumentation/cohere/get_embeddings_telemetry.js +97 -0
- package/src/instrumentation/cohere/set_telemetry_attributes.js +32 -0
- package/src/instrumentation/elevenlabs/full_instrumentation.js +236 -0
- package/src/instrumentation/google/get_audio_telemetry.js +31 -0
- package/src/instrumentation/google/get_cache_telemetry.js +27 -0
- package/src/instrumentation/google/get_embeddings_telemetry.js +78 -0
- package/src/instrumentation/google/get_provider_name.js +25 -0
- package/src/instrumentation/google/get_reasoning_telemetry.js +21 -0
- package/src/instrumentation/google/set_telemetry_attributes.js +98 -0
- package/src/instrumentation/openai/get_audio_telemetry.js +149 -0
- package/src/instrumentation/openai/get_cache_and_reasoning_telemetry.js +71 -0
- package/src/instrumentation/openai/get_embeddings_telemetry.js +95 -0
- package/src/instrumentation/openai/get_streaming_usage.js +81 -0
- package/src/instrumentation/openai/set_telemetry_attributes.js +39 -0
- package/src/otel/propagation.js +5 -0
- package/src/otel/span-filter.js +1 -5
- package/tests/index.test.js +42 -44
- package/tests/instrumentation/anthropic/get_cache_and_reasoning_telemetry.test.js +125 -0
- package/tests/instrumentation/bedrock/get_cache_telemetry.test.js +102 -0
- package/tests/instrumentation/cohere/get_chat_telemetry.test.js +148 -0
- package/tests/instrumentation/cohere/get_embeddings_telemetry.test.js +129 -0
- package/tests/instrumentation/elevenlabs/full_instrumentation.test.js +244 -0
- package/tests/instrumentation/google/get_embeddings_telemetry.test.js +125 -0
- package/tests/instrumentation/google/get_reasoning_telemetry.test.js +41 -0
- package/tests/instrumentation/google/set_telemetry_attributes.test.js +187 -0
- package/tests/instrumentation/openai/get_audio_telemetry.test.js +170 -0
- package/tests/instrumentation/openai/get_cache_and_reasoning_telemetry.test.js +83 -0
- package/tests/instrumentation/openai/get_embeddings_telemetry.test.js +126 -0
- package/tests/instrumentation/openai/get_streaming_usage.test.js +76 -0
- package/src/otel/openai-streaming-usage.js +0 -60
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hand-written OTel instrumentor for the ElevenLabs audio clients.
|
|
5
|
+
*
|
|
6
|
+
* No official OpenTelemetry instrumentor exists for the ElevenLabs SDK (it's
|
|
7
|
+
* audio, not an LLM), so this ships inside weflayr. It wraps the Text-to-Speech
|
|
8
|
+
* and Speech-to-Text entrypoints to emit one `gen_ai.*` span per call, which
|
|
9
|
+
* flows through the standard span processor -> exporter with no new transport code.
|
|
10
|
+
*
|
|
11
|
+
* The billable unit differs by API, so each span records the one ElevenLabs bills:
|
|
12
|
+
* - Text-to-Speech is billed per input character -> `gen_ai.usage.input_characters`.
|
|
13
|
+
* - Speech-to-Text is billed per second of audio -> `gen_ai.usage.input_seconds`.
|
|
14
|
+
*
|
|
15
|
+
* Entrypoints come in three result shapes, independent of which API they belong to:
|
|
16
|
+
* - *byte stream* methods resolve to a `ReadableStream` of audio chunks;
|
|
17
|
+
* - *object stream* methods resolve to an async-iterable of chunk objects;
|
|
18
|
+
* - *unary* methods resolve to the full response.
|
|
19
|
+
* The HTTP request -- plus any error -- only happens while a stream is awaited
|
|
20
|
+
* and consumed, so the span is started eagerly (to capture the
|
|
21
|
+
* `propagateMetadata` context active at the call site) and ended once the result
|
|
22
|
+
* is exhausted (streams) or returned (unary), giving an `elapsed_ms` that covers
|
|
23
|
+
* the whole call. The per-second billable unit is read off the STT response.
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
const { trace, SpanStatusCode } = require('@opentelemetry/api');
|
|
27
|
+
|
|
28
|
+
const TRACER_NAME = 'weflayr.instrumentation.elevenlabs';
|
|
29
|
+
const SYSTEM = 'elevenlabs';
|
|
30
|
+
const TTS_OPERATION = 'text_to_speech';
|
|
31
|
+
const STT_OPERATION = 'speech_to_text';
|
|
32
|
+
|
|
33
|
+
function _startSpan(operation, request) {
|
|
34
|
+
const span = trace.getTracer(TRACER_NAME).startSpan(operation);
|
|
35
|
+
span.setAttribute('gen_ai.system', SYSTEM);
|
|
36
|
+
span.setAttribute('gen_ai.operation.name', operation);
|
|
37
|
+
// `modelId` lives on the request body passed to every audio entrypoint.
|
|
38
|
+
const modelId = request && request.modelId;
|
|
39
|
+
if (modelId) {
|
|
40
|
+
span.setAttribute('gen_ai.request.model', modelId);
|
|
41
|
+
}
|
|
42
|
+
return span;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function _setInputCharacters(span, request) {
|
|
46
|
+
// `text` lives on the request body of every TTS entrypoint; the billable unit
|
|
47
|
+
// is its length.
|
|
48
|
+
span.setAttribute('gen_ai.usage.input_characters', ((request && request.text) || '').length);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function _setAudioDuration(span, response) {
|
|
52
|
+
// The transcription response exposes the transcribed audio length in seconds.
|
|
53
|
+
const durationSecs = response && response.audioDurationSecs;
|
|
54
|
+
if (durationSecs != null) {
|
|
55
|
+
span.setAttribute('gen_ai.usage.input_seconds', durationSecs);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function _recordError(span, error) {
|
|
60
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
61
|
+
span.recordException(error);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Proxy a `ReadableStream` of audio bytes so the span ends -- and any error is
|
|
65
|
+
// recorded -- once the whole stream has been consumed (or the consumer cancels).
|
|
66
|
+
// The span ends exactly once across every exit path.
|
|
67
|
+
function _consumeByteStream(span, responsePromise) {
|
|
68
|
+
let ended = false;
|
|
69
|
+
const end = () => {
|
|
70
|
+
if (ended) return;
|
|
71
|
+
ended = true;
|
|
72
|
+
span.end();
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
return responsePromise.then(
|
|
76
|
+
(audioStream) => {
|
|
77
|
+
const reader = audioStream.getReader();
|
|
78
|
+
return new ReadableStream({
|
|
79
|
+
async pull(controller) {
|
|
80
|
+
try {
|
|
81
|
+
const { done, value } = await reader.read();
|
|
82
|
+
if (done) {
|
|
83
|
+
end();
|
|
84
|
+
controller.close();
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
controller.enqueue(value);
|
|
88
|
+
} catch (error) {
|
|
89
|
+
_recordError(span, error);
|
|
90
|
+
end();
|
|
91
|
+
controller.error(error);
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
cancel(reason) {
|
|
95
|
+
end();
|
|
96
|
+
return reader.cancel(reason);
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
},
|
|
100
|
+
(error) => {
|
|
101
|
+
_recordError(span, error);
|
|
102
|
+
end();
|
|
103
|
+
throw error;
|
|
104
|
+
}
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Proxy an async-iterable of chunk objects (the streaming "with timestamps"
|
|
109
|
+
// variant) so the span ends once iteration completes.
|
|
110
|
+
function _consumeObjectStream(span, responsePromise) {
|
|
111
|
+
async function* iterate(stream) {
|
|
112
|
+
try {
|
|
113
|
+
for await (const chunk of stream) {
|
|
114
|
+
yield chunk;
|
|
115
|
+
}
|
|
116
|
+
} catch (error) {
|
|
117
|
+
_recordError(span, error);
|
|
118
|
+
throw error;
|
|
119
|
+
} finally {
|
|
120
|
+
span.end();
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return responsePromise.then(
|
|
125
|
+
(stream) => iterate(stream),
|
|
126
|
+
(error) => {
|
|
127
|
+
_recordError(span, error);
|
|
128
|
+
span.end();
|
|
129
|
+
throw error;
|
|
130
|
+
}
|
|
131
|
+
);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Wrap a request/response entrypoint: the span spans the single call, and any
|
|
135
|
+
// response-derived billable unit is read off the result.
|
|
136
|
+
function _consumeUnary(span, responsePromise, setResponseAttributes) {
|
|
137
|
+
return responsePromise.then(
|
|
138
|
+
(response) => {
|
|
139
|
+
if (setResponseAttributes) {
|
|
140
|
+
setResponseAttributes(span, response);
|
|
141
|
+
}
|
|
142
|
+
span.end();
|
|
143
|
+
return response;
|
|
144
|
+
},
|
|
145
|
+
(error) => {
|
|
146
|
+
_recordError(span, error);
|
|
147
|
+
span.end();
|
|
148
|
+
throw error;
|
|
149
|
+
}
|
|
150
|
+
);
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
class ElevenLabsInstrumentor {
|
|
154
|
+
constructor() {
|
|
155
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
156
|
+
this._patched = [];
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
instrument() {
|
|
160
|
+
let TextToSpeechClient;
|
|
161
|
+
let SpeechToTextClient;
|
|
162
|
+
try {
|
|
163
|
+
({
|
|
164
|
+
TextToSpeechClient,
|
|
165
|
+
} = require('@elevenlabs/elevenlabs-js/api/resources/textToSpeech/client/Client'));
|
|
166
|
+
({
|
|
167
|
+
SpeechToTextClient,
|
|
168
|
+
} = require('@elevenlabs/elevenlabs-js/api/resources/speechToText/client/Client'));
|
|
169
|
+
} catch (err) {
|
|
170
|
+
throw new Error(
|
|
171
|
+
'weflayr.autoInstrument(ELEVENLABS): the @elevenlabs/elevenlabs-js client is not ' +
|
|
172
|
+
'installed. Install it with: npm install @elevenlabs/elevenlabs-js\n' +
|
|
173
|
+
`Underlying error: ${err.message}`
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// TTS — request body is the 2nd arg (after voice_id); billed per character.
|
|
178
|
+
this._wrap(TextToSpeechClient.prototype, 'convert', {
|
|
179
|
+
operation: TTS_OPERATION,
|
|
180
|
+
requestArgIndex: 1,
|
|
181
|
+
setRequestAttributes: _setInputCharacters,
|
|
182
|
+
consume: _consumeByteStream,
|
|
183
|
+
});
|
|
184
|
+
this._wrap(TextToSpeechClient.prototype, 'stream', {
|
|
185
|
+
operation: TTS_OPERATION,
|
|
186
|
+
requestArgIndex: 1,
|
|
187
|
+
setRequestAttributes: _setInputCharacters,
|
|
188
|
+
consume: _consumeByteStream,
|
|
189
|
+
});
|
|
190
|
+
this._wrap(TextToSpeechClient.prototype, 'streamWithTimestamps', {
|
|
191
|
+
operation: TTS_OPERATION,
|
|
192
|
+
requestArgIndex: 1,
|
|
193
|
+
setRequestAttributes: _setInputCharacters,
|
|
194
|
+
consume: _consumeObjectStream,
|
|
195
|
+
});
|
|
196
|
+
this._wrap(TextToSpeechClient.prototype, 'convertWithTimestamps', {
|
|
197
|
+
operation: TTS_OPERATION,
|
|
198
|
+
requestArgIndex: 1,
|
|
199
|
+
setRequestAttributes: _setInputCharacters,
|
|
200
|
+
consume: (span, responsePromise) => _consumeUnary(span, responsePromise, null),
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
// STT — request body is the 1st arg; billed per second of audio.
|
|
204
|
+
this._wrap(SpeechToTextClient.prototype, 'convert', {
|
|
205
|
+
operation: STT_OPERATION,
|
|
206
|
+
requestArgIndex: 0,
|
|
207
|
+
setRequestAttributes: null,
|
|
208
|
+
consume: (span, responsePromise) => _consumeUnary(span, responsePromise, _setAudioDuration),
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
uninstrument() {
|
|
213
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
214
|
+
owner[methodName] = original;
|
|
215
|
+
}
|
|
216
|
+
this._patched = [];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
_wrap(owner, methodName, { operation, requestArgIndex, setRequestAttributes, consume }) {
|
|
220
|
+
const original = owner[methodName];
|
|
221
|
+
|
|
222
|
+
function wrapper(...args) {
|
|
223
|
+
const request = args[requestArgIndex];
|
|
224
|
+
const span = _startSpan(operation, request);
|
|
225
|
+
if (setRequestAttributes) {
|
|
226
|
+
setRequestAttributes(span, request);
|
|
227
|
+
}
|
|
228
|
+
return consume(span, original.apply(this, args));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
owner[methodName] = wrapper;
|
|
232
|
+
this._patched.push([owner, methodName, original]);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
module.exports = { ElevenLabsInstrumentor };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-modality audio token enrichment for the Google GenAI (Gemini) chat span.
|
|
5
|
+
*
|
|
6
|
+
* Neither instrumentor breaks usage down by modality, so for audio in/out (e.g.
|
|
7
|
+
* Gemini TTS) we read the response's per-modality token details and set
|
|
8
|
+
* `gen_ai.usage.{input,output}_tokens_details.audio_tokens` (normalized to
|
|
9
|
+
* token_audio_input / token_audio_output).
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// Sum the token counts whose modality is AUDIO across a *TokensDetails array.
|
|
13
|
+
function _audioTokens(details) {
|
|
14
|
+
if (!Array.isArray(details)) return 0;
|
|
15
|
+
return details.reduce(
|
|
16
|
+
(total, detail) => total + (detail && detail.modality === 'AUDIO' ? detail.tokenCount || 0 : 0),
|
|
17
|
+
0
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function setAudioTelemetry(span, response) {
|
|
22
|
+
const usage = response && response.usageMetadata;
|
|
23
|
+
if (!usage) return;
|
|
24
|
+
const inputAudio = _audioTokens(usage.promptTokensDetails);
|
|
25
|
+
const outputAudio = _audioTokens(usage.candidatesTokensDetails);
|
|
26
|
+
if (inputAudio) span.setAttribute('gen_ai.usage.input_tokens_details.audio_tokens', inputAudio);
|
|
27
|
+
if (outputAudio)
|
|
28
|
+
span.setAttribute('gen_ai.usage.output_tokens_details.audio_tokens', outputAudio);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
module.exports = { setAudioTelemetry, _audioTokens };
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Context-caching token enrichment for the Google GenAI (Gemini) chat span.
|
|
5
|
+
*
|
|
6
|
+
* Neither instrumentor records cached-content tokens, so we read
|
|
7
|
+
* `usageMetadata.cachedContentTokenCount` (explicit or implicit caching) and
|
|
8
|
+
* split it by modality: audio cached tokens go to
|
|
9
|
+
* `gen_ai.usage.input_tokens_details.cached_audio_tokens` (normalized to
|
|
10
|
+
* token_audio_cached) and the remainder to `gen_ai.usage.cache_read_input_tokens`
|
|
11
|
+
* (normalized to token_text_cached).
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const { _audioTokens } = require('./get_audio_telemetry');
|
|
15
|
+
|
|
16
|
+
function setCacheTelemetry(span, response) {
|
|
17
|
+
const usage = response && response.usageMetadata;
|
|
18
|
+
const cachedTokens = (usage && usage.cachedContentTokenCount) || 0;
|
|
19
|
+
if (!cachedTokens) return;
|
|
20
|
+
const cachedAudio = _audioTokens(usage.cacheTokensDetails);
|
|
21
|
+
if (cachedAudio)
|
|
22
|
+
span.setAttribute('gen_ai.usage.input_tokens_details.cached_audio_tokens', cachedAudio);
|
|
23
|
+
const cacheRead = Math.max(0, cachedTokens - cachedAudio);
|
|
24
|
+
if (cacheRead) span.setAttribute('gen_ai.usage.cache_read_input_tokens', cacheRead);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
module.exports = { setCacheTelemetry };
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Embeddings-endpoint telemetry for Google GenAI (Gemini).
|
|
5
|
+
*
|
|
6
|
+
* `@traceloop/instrumentation-google-generativeai` wraps generate_content but not
|
|
7
|
+
* embeddings, so this covers it by starting its own span on the private
|
|
8
|
+
* `Models.prototype.embedContentInternal` (which the public `embedContent`
|
|
9
|
+
* delegates to). The Gemini Developer API returns NO token usage on embedContent
|
|
10
|
+
* (only Vertex does), so the span records the billable unit weflayr can price:
|
|
11
|
+
* the input character count under `gen_ai.usage.input_characters` (normalized to
|
|
12
|
+
* count_char).
|
|
13
|
+
*/
|
|
14
|
+
|
|
15
|
+
const { trace, SpanStatusCode } = require('@opentelemetry/api');
|
|
16
|
+
|
|
17
|
+
const { getProviderName } = require('./get_provider_name');
|
|
18
|
+
|
|
19
|
+
const TRACER_NAME = 'weflayr.instrumentation.google.embeddings';
|
|
20
|
+
const EMBEDDINGS_OPERATION = 'embeddings';
|
|
21
|
+
|
|
22
|
+
// `contents` is a string, a Content/Part object, or an array of those; the
|
|
23
|
+
// billable unit is the total number of characters of text across them.
|
|
24
|
+
function _contentCharacters(contents) {
|
|
25
|
+
if (contents == null) return 0;
|
|
26
|
+
if (typeof contents === 'string') return contents.length;
|
|
27
|
+
if (Array.isArray(contents)) {
|
|
28
|
+
return contents.reduce((total, item) => total + _contentCharacters(item), 0);
|
|
29
|
+
}
|
|
30
|
+
if (typeof contents === 'object') {
|
|
31
|
+
if (typeof contents.text === 'string') return contents.text.length;
|
|
32
|
+
if (Array.isArray(contents.parts)) return _contentCharacters(contents.parts);
|
|
33
|
+
}
|
|
34
|
+
return 0;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _recordError(span, error) {
|
|
38
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
39
|
+
span.recordException(error);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Wrap `Models.prototype.embedContentInternal`, pushing the restore tuple onto
|
|
43
|
+
// the shared `patched` array the orchestrator owns for uninstrument().
|
|
44
|
+
function instrumentEmbeddings(Models, patched) {
|
|
45
|
+
const owner = Models.prototype;
|
|
46
|
+
const original = owner.embedContentInternal;
|
|
47
|
+
if (typeof original !== 'function') return;
|
|
48
|
+
|
|
49
|
+
function wrapper(params, ...rest) {
|
|
50
|
+
const span = trace.getTracer(TRACER_NAME).startSpan(EMBEDDINGS_OPERATION);
|
|
51
|
+
span.setAttribute('gen_ai.system', getProviderName(this));
|
|
52
|
+
span.setAttribute('gen_ai.operation.name', EMBEDDINGS_OPERATION);
|
|
53
|
+
if (params && params.model) {
|
|
54
|
+
// The chat path strips the leading "models/" prefix; mirror it.
|
|
55
|
+
span.setAttribute('gen_ai.request.model', params.model.replace(/^models\//, ''));
|
|
56
|
+
}
|
|
57
|
+
span.setAttribute(
|
|
58
|
+
'gen_ai.usage.input_characters',
|
|
59
|
+
_contentCharacters(params && params.contents)
|
|
60
|
+
);
|
|
61
|
+
return Promise.resolve(original.call(this, params, ...rest)).then(
|
|
62
|
+
(response) => {
|
|
63
|
+
span.end();
|
|
64
|
+
return response;
|
|
65
|
+
},
|
|
66
|
+
(error) => {
|
|
67
|
+
_recordError(span, error);
|
|
68
|
+
span.end();
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
owner.embedContentInternal = wrapper;
|
|
75
|
+
patched.push([owner, 'embedContentInternal', original]);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
module.exports = { instrumentEmbeddings };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Provider-name enrichment for the Google GenAI (Gemini) chat span.
|
|
5
|
+
*
|
|
6
|
+
* `@traceloop/instrumentation-google-generativeai` hardcodes
|
|
7
|
+
* `gen_ai.provider.name = "gcp.gen_ai"` and can't tell Vertex from the Gemini
|
|
8
|
+
* Developer API. We read the client's Vertex flag and return the value to set as
|
|
9
|
+
* `gen_ai.system` (which the normalizer prefers): `vertex_ai` / `gemini`,
|
|
10
|
+
* matching what the Python google-genai instrumentor reports natively. Node-only.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
// Read the Vertex-vs-Dev backend off the Models instance's api client.
|
|
14
|
+
function getProviderName(instance) {
|
|
15
|
+
try {
|
|
16
|
+
if (instance && instance.apiClient && typeof instance.apiClient.isVertexAI === 'function') {
|
|
17
|
+
return instance.apiClient.isVertexAI() ? 'vertex_ai' : 'gemini';
|
|
18
|
+
}
|
|
19
|
+
} catch {
|
|
20
|
+
// fall through to default
|
|
21
|
+
}
|
|
22
|
+
return 'gemini';
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
module.exports = { getProviderName };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Reasoning (thinking) token enrichment for the Google GenAI (Gemini) chat span.
|
|
5
|
+
*
|
|
6
|
+
* Neither instrumentor surfaces the thinking budget. Gemini reports thinking
|
|
7
|
+
* under `usageMetadata.thoughtsTokenCount`, which is SEPARATE from
|
|
8
|
+
* `candidatesTokenCount` (the visible output) and excluded from the totals, so
|
|
9
|
+
* without this the thinking tokens are lost. Stamp them as
|
|
10
|
+
* `gen_ai.usage.thoughts_token_count` so the normalizer can fold them into the
|
|
11
|
+
* completion count.
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
function setReasoningTelemetry(span, response) {
|
|
15
|
+
const usage = response && response.usageMetadata;
|
|
16
|
+
if (!usage) return;
|
|
17
|
+
const thoughts = usage.thoughtsTokenCount || 0;
|
|
18
|
+
if (thoughts) span.setAttribute('gen_ai.usage.thoughts_token_count', thoughts);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = { setReasoningTelemetry };
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Google GenAI (Gemini) telemetry complement — the registered orchestrator.
|
|
5
|
+
*
|
|
6
|
+
* Wires every weflayr-supplied patch the traceloop Google instrumentor misses:
|
|
7
|
+
* - chat span enrichment (one wrap of generateContent{,Stream}Internal that
|
|
8
|
+
* stamps provider name + audio tokens + cache tokens — see the get_*_telemetry
|
|
9
|
+
* files), and
|
|
10
|
+
* - the embeddings endpoint it doesn't cover at all.
|
|
11
|
+
*
|
|
12
|
+
* The public arrows delegate to these private prototype methods while the
|
|
13
|
+
* traceloop span is the active span, so enrichment lands on that span.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const { trace } = require('@opentelemetry/api');
|
|
17
|
+
|
|
18
|
+
const { getProviderName } = require('./get_provider_name');
|
|
19
|
+
const { setAudioTelemetry } = require('./get_audio_telemetry');
|
|
20
|
+
const { setCacheTelemetry } = require('./get_cache_telemetry');
|
|
21
|
+
const { setReasoningTelemetry } = require('./get_reasoning_telemetry');
|
|
22
|
+
const { instrumentEmbeddings } = require('./get_embeddings_telemetry');
|
|
23
|
+
|
|
24
|
+
class GoogleTelemetryInstrumentor {
|
|
25
|
+
constructor() {
|
|
26
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
27
|
+
this._patched = [];
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
instrument() {
|
|
31
|
+
let Models;
|
|
32
|
+
try {
|
|
33
|
+
({ Models } = require('@google/genai'));
|
|
34
|
+
} catch (err) {
|
|
35
|
+
throw new Error(
|
|
36
|
+
'weflayr.autoInstrument(GOOGLE_GENAI): the @google/genai client is not installed. ' +
|
|
37
|
+
'Install it with: npm install @google/genai\n' +
|
|
38
|
+
`Underlying error: ${err.message}`
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// Non-streaming: stamp provider up front, audio + cache tokens off the response.
|
|
43
|
+
this._wrapChat(Models.prototype, 'generateContentInternal', { stream: false });
|
|
44
|
+
// Streaming: stamp provider, then cache tokens off the final chunk's usageMetadata.
|
|
45
|
+
this._wrapChat(Models.prototype, 'generateContentStreamInternal', { stream: true });
|
|
46
|
+
// Embeddings: an endpoint the traceloop instrumentor doesn't cover at all.
|
|
47
|
+
instrumentEmbeddings(Models, this._patched);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
_wrapChat(owner, methodName, { stream }) {
|
|
51
|
+
const original = owner[methodName];
|
|
52
|
+
|
|
53
|
+
function wrapper(...args) {
|
|
54
|
+
const span = trace.getActiveSpan();
|
|
55
|
+
if (!span || !span.isRecording || !span.isRecording()) {
|
|
56
|
+
return original.apply(this, args);
|
|
57
|
+
}
|
|
58
|
+
span.setAttribute('gen_ai.system', getProviderName(this));
|
|
59
|
+
if (!stream) {
|
|
60
|
+
return Promise.resolve(original.apply(this, args)).then((response) => {
|
|
61
|
+
setAudioTelemetry(span, response);
|
|
62
|
+
setCacheTelemetry(span, response);
|
|
63
|
+
setReasoningTelemetry(span, response);
|
|
64
|
+
return response;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
// Streamed: usage (incl. cachedContentTokenCount) lands on the final chunk;
|
|
68
|
+
// observe the chunks as they pass through. The instrumentor keeps the span
|
|
69
|
+
// open until the stream is exhausted.
|
|
70
|
+
return Promise.resolve(original.apply(this, args)).then((stream_) => {
|
|
71
|
+
if (!stream_ || typeof stream_[Symbol.asyncIterator] !== 'function') return stream_;
|
|
72
|
+
return (async function* observeCache() {
|
|
73
|
+
for await (const chunk of stream_) {
|
|
74
|
+
try {
|
|
75
|
+
setCacheTelemetry(span, chunk);
|
|
76
|
+
setReasoningTelemetry(span, chunk);
|
|
77
|
+
} catch {
|
|
78
|
+
// Never break the caller's stream over an enrichment failure.
|
|
79
|
+
}
|
|
80
|
+
yield chunk;
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
owner[methodName] = wrapper;
|
|
87
|
+
this._patched.push([owner, methodName, original]);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
uninstrument() {
|
|
91
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
92
|
+
owner[methodName] = original;
|
|
93
|
+
}
|
|
94
|
+
this._patched = [];
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
module.exports = { GoogleTelemetryInstrumentor };
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hand-written OTel instrumentor for the OpenAI audio endpoints.
|
|
5
|
+
*
|
|
6
|
+
* The `@traceloop/instrumentation-openai` package wraps OpenAI's chat,
|
|
7
|
+
* completions, responses and image endpoints, but not the audio ones, so this
|
|
8
|
+
* ships inside weflayr to cover them. It wraps the Text-to-Speech and
|
|
9
|
+
* Speech-to-Text entrypoints to emit one `gen_ai.*` span per call, which flows
|
|
10
|
+
* through the standard span processor -> exporter with no new transport code. It
|
|
11
|
+
* is registered alongside the traceloop instrumentor whenever
|
|
12
|
+
* `autoInstrument({ aiSdks: 'openai' })` runs.
|
|
13
|
+
*
|
|
14
|
+
* The billable unit differs by API, so each span records the one OpenAI bills:
|
|
15
|
+
* - Text-to-Speech is billed per input character -> `gen_ai.usage.input_characters`.
|
|
16
|
+
* - Speech-to-Text is billed per second of audio -> `gen_ai.usage.input_seconds`.
|
|
17
|
+
*
|
|
18
|
+
* Both endpoints are request/response (unary): `audio.speech.create` resolves to
|
|
19
|
+
* the full audio and `audio.transcriptions.create` to the transcription. The STT
|
|
20
|
+
* audio length is only present when the call requests `response_format: 'verbose_json'`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const { trace, SpanStatusCode } = require('@opentelemetry/api');
|
|
24
|
+
|
|
25
|
+
const TRACER_NAME = 'weflayr.instrumentation.openai_audio';
|
|
26
|
+
const TTS_OPERATION = 'text_to_speech';
|
|
27
|
+
const STT_OPERATION = 'speech_to_text';
|
|
28
|
+
|
|
29
|
+
// Return the gen_ai.system for this call, matching how the OpenAI chat
|
|
30
|
+
// instrumentor attributes the vendor: Azure clients (reached via the same openai
|
|
31
|
+
// SDK) report `azure.ai.openai`, everything else `openai`.
|
|
32
|
+
function _detectSystem(instance) {
|
|
33
|
+
const baseURL = (instance && instance._client && instance._client.baseURL) || '';
|
|
34
|
+
return baseURL.toLowerCase().includes('openai.azure.com') ? 'azure.ai.openai' : 'openai';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function _startSpan(operation, body, system) {
|
|
38
|
+
const span = trace.getTracer(TRACER_NAME).startSpan(operation);
|
|
39
|
+
span.setAttribute('gen_ai.system', system);
|
|
40
|
+
span.setAttribute('gen_ai.operation.name', operation);
|
|
41
|
+
// `model` lives on the request body passed to both audio entrypoints.
|
|
42
|
+
const model = body && body.model;
|
|
43
|
+
if (model) {
|
|
44
|
+
span.setAttribute('gen_ai.request.model', model);
|
|
45
|
+
}
|
|
46
|
+
return span;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function _setInputCharacters(span, body) {
|
|
50
|
+
// `input` is the text passed to text_to_speech; the billable unit is its length.
|
|
51
|
+
span.setAttribute('gen_ai.usage.input_characters', ((body && body.input) || '').length);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function _setAudioDuration(span, response) {
|
|
55
|
+
// `duration` (seconds) is only present on a verbose transcription response;
|
|
56
|
+
// it is the billable unit for speech-to-text.
|
|
57
|
+
const durationSecs = response && response.duration;
|
|
58
|
+
if (durationSecs != null) {
|
|
59
|
+
span.setAttribute('gen_ai.usage.input_seconds', durationSecs);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function _recordError(span, error) {
|
|
64
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
65
|
+
span.recordException(error);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// Wrap a request/response entrypoint: the span spans the single call, and any
|
|
69
|
+
// response-derived billable unit is read off the result.
|
|
70
|
+
function _consumeUnary(span, responsePromise, setResponseAttributes) {
|
|
71
|
+
return responsePromise.then(
|
|
72
|
+
(response) => {
|
|
73
|
+
if (setResponseAttributes) {
|
|
74
|
+
setResponseAttributes(span, response);
|
|
75
|
+
}
|
|
76
|
+
span.end();
|
|
77
|
+
return response;
|
|
78
|
+
},
|
|
79
|
+
(error) => {
|
|
80
|
+
_recordError(span, error);
|
|
81
|
+
span.end();
|
|
82
|
+
throw error;
|
|
83
|
+
}
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
class OpenAIAudioInstrumentor {
|
|
88
|
+
constructor() {
|
|
89
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
90
|
+
this._patched = [];
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
instrument() {
|
|
94
|
+
let Speech;
|
|
95
|
+
let Transcriptions;
|
|
96
|
+
try {
|
|
97
|
+
({ Speech } = require('openai/resources/audio/speech'));
|
|
98
|
+
({ Transcriptions } = require('openai/resources/audio/transcriptions'));
|
|
99
|
+
} catch (err) {
|
|
100
|
+
throw new Error(
|
|
101
|
+
'weflayr.autoInstrument(OPENAI): the openai client is not installed. ' +
|
|
102
|
+
'Install it with: npm install openai\n' +
|
|
103
|
+
`Underlying error: ${err.message}`
|
|
104
|
+
);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
// TTS — request body is the 1st arg; billed per character.
|
|
108
|
+
this._wrap(Speech.prototype, {
|
|
109
|
+
operation: TTS_OPERATION,
|
|
110
|
+
setRequestAttributes: _setInputCharacters,
|
|
111
|
+
setResponseAttributes: null,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// STT — request body is the 1st arg; billed per second of audio.
|
|
115
|
+
this._wrap(Transcriptions.prototype, {
|
|
116
|
+
operation: STT_OPERATION,
|
|
117
|
+
setRequestAttributes: null,
|
|
118
|
+
setResponseAttributes: _setAudioDuration,
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
uninstrument() {
|
|
123
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
124
|
+
owner[methodName] = original;
|
|
125
|
+
}
|
|
126
|
+
this._patched = [];
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
_wrap(owner, { operation, setRequestAttributes, setResponseAttributes }) {
|
|
130
|
+
const original = owner.create;
|
|
131
|
+
|
|
132
|
+
function wrapper(body, ...rest) {
|
|
133
|
+
const span = _startSpan(operation, body, _detectSystem(this));
|
|
134
|
+
if (setRequestAttributes) {
|
|
135
|
+
setRequestAttributes(span, body);
|
|
136
|
+
}
|
|
137
|
+
return _consumeUnary(
|
|
138
|
+
span,
|
|
139
|
+
Promise.resolve(original.call(this, body, ...rest)),
|
|
140
|
+
setResponseAttributes
|
|
141
|
+
);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
owner.create = wrapper;
|
|
145
|
+
this._patched.push([owner, 'create', original]);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
module.exports = { OpenAIAudioInstrumentor };
|