weflayr 0.24.0 → 0.26.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/README.md +2 -2
- package/dist/{api → src/api}/generated/facade.gen.d.ts +86 -12
- package/dist/{api → src/api}/generated/facade.gen.js +4 -2
- package/dist/src/api/generated/index.d.ts +2 -0
- package/dist/{api → src/api}/generated/index.js +3 -1
- package/dist/{api → src/api}/generated/sdk.gen.d.ts +11 -3
- package/dist/{api → src/api}/generated/sdk.gen.js +34 -4
- package/{src/api/generated/types.gen.ts → dist/src/api/generated/types.gen.d.ts} +436 -153
- package/dist/{api → src/api}/makeEndpoint.js +1 -3
- package/dist/src/auto-instrument.d.ts +42 -0
- package/dist/src/auto-instrument.js +156 -0
- package/dist/src/index.d.ts +9 -0
- package/dist/src/index.js +49 -0
- package/dist/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.d.ts +20 -0
- package/dist/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +114 -0
- package/dist/src/instrumentation/anthropic/get_cache_config_telemetry.d.ts +28 -0
- package/dist/src/instrumentation/anthropic/get_cache_config_telemetry.js +92 -0
- package/dist/src/instrumentation/anthropic/get_streaming_accumulation_telemetry.d.ts +25 -0
- package/dist/src/instrumentation/anthropic/get_streaming_accumulation_telemetry.js +141 -0
- package/dist/src/instrumentation/anthropic/get_structured_output_schema_telemetry.d.ts +9 -0
- package/dist/src/instrumentation/anthropic/get_structured_output_schema_telemetry.js +17 -0
- package/dist/src/instrumentation/anthropic/get_tool_definitions_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/anthropic/get_tool_definitions_telemetry.js +50 -0
- package/dist/src/instrumentation/anthropic/set_telemetry_attributes.d.ts +15 -0
- package/dist/src/instrumentation/anthropic/set_telemetry_attributes.js +39 -0
- package/dist/src/instrumentation/bedrock/get_cache_config_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/bedrock/get_cache_config_telemetry.js +97 -0
- package/dist/src/instrumentation/bedrock/get_cache_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/bedrock/get_cache_telemetry.js +102 -0
- package/dist/src/instrumentation/bedrock/get_input_message_content_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/bedrock/get_input_message_content_telemetry.js +91 -0
- package/dist/src/instrumentation/bedrock/get_model_id_telemetry.d.ts +21 -0
- package/dist/src/instrumentation/bedrock/get_model_id_telemetry.js +54 -0
- package/dist/src/instrumentation/bedrock/get_reasoning_output_telemetry.d.ts +19 -0
- package/dist/src/instrumentation/bedrock/get_reasoning_output_telemetry.js +81 -0
- package/dist/src/instrumentation/bedrock/get_streaming_output_message_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/bedrock/get_streaming_output_message_telemetry.js +126 -0
- package/dist/src/instrumentation/bedrock/get_tool_call_output_telemetry.d.ts +17 -0
- package/dist/src/instrumentation/bedrock/get_tool_call_output_telemetry.js +85 -0
- package/dist/src/instrumentation/bedrock/get_tool_definitions_telemetry.d.ts +15 -0
- package/dist/src/instrumentation/bedrock/get_tool_definitions_telemetry.js +68 -0
- package/dist/src/instrumentation/bedrock/set_telemetry_attributes.d.ts +18 -0
- package/dist/src/instrumentation/bedrock/set_telemetry_attributes.js +47 -0
- package/dist/src/instrumentation/cohere/get_chat_telemetry.d.ts +23 -0
- package/dist/src/instrumentation/cohere/get_chat_telemetry.js +77 -0
- package/dist/src/instrumentation/cohere/get_embeddings_telemetry.d.ts +18 -0
- package/dist/src/instrumentation/cohere/get_embeddings_telemetry.js +88 -0
- package/dist/src/instrumentation/cohere/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/cohere/get_structured_output_schema_telemetry.js +18 -0
- package/dist/src/instrumentation/cohere/get_tool_call_output_telemetry.d.ts +31 -0
- package/dist/src/instrumentation/cohere/get_tool_call_output_telemetry.js +106 -0
- package/dist/src/instrumentation/cohere/get_tool_definitions_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/cohere/get_tool_definitions_telemetry.js +75 -0
- package/dist/src/instrumentation/cohere/set_telemetry_attributes.d.ts +17 -0
- package/dist/src/instrumentation/cohere/set_telemetry_attributes.js +44 -0
- package/dist/src/instrumentation/elevenlabs/full_instrumentation.d.ts +28 -0
- package/dist/src/instrumentation/elevenlabs/full_instrumentation.js +209 -0
- package/dist/src/instrumentation/google/get_audio_telemetry.d.ts +9 -0
- package/dist/src/instrumentation/google/get_audio_telemetry.js +28 -0
- package/dist/src/instrumentation/google/get_cache_operations_telemetry.d.ts +23 -0
- package/dist/src/instrumentation/google/get_cache_operations_telemetry.js +154 -0
- package/dist/src/instrumentation/google/get_cache_telemetry.d.ts +16 -0
- package/{src → dist/src}/instrumentation/google/get_cache_telemetry.js +18 -19
- package/dist/src/instrumentation/google/get_embeddings_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/google/get_embeddings_telemetry.js +85 -0
- package/dist/src/instrumentation/google/get_provider_name.d.ts +11 -0
- package/{src → dist/src}/instrumentation/google/get_provider_name.js +11 -12
- package/{src/instrumentation/google/get_reasoning_telemetry.js → dist/src/instrumentation/google/get_reasoning_telemetry.d.ts} +1 -11
- package/dist/src/instrumentation/google/get_reasoning_telemetry.js +21 -0
- package/dist/src/instrumentation/google/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/google/get_structured_output_schema_telemetry.js +19 -0
- package/dist/src/instrumentation/google/set_telemetry_attributes.d.ts +18 -0
- package/dist/src/instrumentation/google/set_telemetry_attributes.js +98 -0
- package/dist/src/instrumentation/openai/get_audio_telemetry.d.ts +25 -0
- package/dist/src/instrumentation/openai/get_audio_telemetry.js +123 -0
- package/dist/src/instrumentation/openai/get_bedrock_mantle_telemetry.d.ts +35 -0
- package/dist/src/instrumentation/openai/get_bedrock_mantle_telemetry.js +148 -0
- package/dist/src/instrumentation/openai/get_cache_and_reasoning_telemetry.d.ts +27 -0
- package/dist/src/instrumentation/openai/get_cache_and_reasoning_telemetry.js +72 -0
- package/dist/src/instrumentation/openai/get_cache_config_telemetry.d.ts +28 -0
- package/dist/src/instrumentation/openai/get_cache_config_telemetry.js +91 -0
- package/dist/src/instrumentation/openai/get_embeddings_telemetry.d.ts +18 -0
- package/dist/src/instrumentation/openai/get_embeddings_telemetry.js +86 -0
- package/dist/src/instrumentation/openai/get_streaming_usage.d.ts +21 -0
- package/dist/src/instrumentation/openai/get_streaming_usage.js +74 -0
- package/dist/src/instrumentation/openai/get_structured_output_schema_telemetry.d.ts +12 -0
- package/dist/src/instrumentation/openai/get_structured_output_schema_telemetry.js +20 -0
- package/dist/src/instrumentation/openai/set_telemetry_attributes.d.ts +17 -0
- package/dist/src/instrumentation/openai/set_telemetry_attributes.js +47 -0
- package/dist/src/instrumentation/shared/get_structured_output_schema_telemetry.d.ts +35 -0
- package/dist/src/instrumentation/shared/get_structured_output_schema_telemetry.js +76 -0
- package/dist/src/instrumentation/vercel/full_instrumentation.d.ts +3 -0
- package/dist/src/instrumentation/vercel/full_instrumentation.js +54 -0
- package/dist/src/instrumentation/vercel/get_cache_creation_telemetry.d.ts +22 -0
- package/dist/src/instrumentation/vercel/get_cache_creation_telemetry.js +66 -0
- package/dist/src/instrumentation/vercel/get_structured_output_schema_telemetry.d.ts +24 -0
- package/dist/src/instrumentation/vercel/get_structured_output_schema_telemetry.js +84 -0
- package/dist/src/logger.d.ts +1 -0
- package/dist/src/logger.js +6 -0
- package/dist/src/otel/exporter.d.ts +19 -0
- package/dist/src/otel/exporter.js +94 -0
- package/dist/src/otel/propagation.d.ts +46 -0
- package/dist/src/otel/propagation.js +110 -0
- package/dist/src/otel/span-filter.d.ts +3 -0
- package/dist/src/otel/span-filter.js +61 -0
- package/dist/src/otel/span-processor.d.ts +55 -0
- package/dist/src/otel/span-processor.js +169 -0
- package/dist/src/otel/vercel-ai-sdk-span-filter.d.ts +12 -0
- package/dist/src/otel/vercel-ai-sdk-span-filter.js +88 -0
- package/dist/src/setup.d.ts +50 -0
- package/dist/src/setup.js +155 -0
- package/package.json +11 -5
- package/.prettierignore +0 -2
- package/.prettierrc.json +0 -7
- package/LICENSE-MIT +0 -21
- package/NOTICE +0 -12
- package/dist/api/generated/index.d.ts +0 -2
- package/dist/api/generated/types.gen.d.ts +0 -1005
- package/eslint.config.mjs +0 -36
- package/index.d.ts +0 -170
- package/openapi-ts.config.mjs +0 -9
- package/scripts/generate_facade.mjs +0 -86
- package/src/api/caseConversion.ts +0 -58
- package/src/api/generated/client/client.gen.ts +0 -277
- package/src/api/generated/client/index.ts +0 -27
- package/src/api/generated/client/types.gen.ts +0 -218
- package/src/api/generated/client/utils.gen.ts +0 -316
- package/src/api/generated/client.gen.ts +0 -16
- package/src/api/generated/core/auth.gen.ts +0 -48
- package/src/api/generated/core/bodySerializer.gen.ts +0 -82
- package/src/api/generated/core/params.gen.ts +0 -178
- package/src/api/generated/core/pathSerializer.gen.ts +0 -171
- package/src/api/generated/core/queryKeySerializer.gen.ts +0 -117
- package/src/api/generated/core/serverSentEvents.gen.ts +0 -242
- package/src/api/generated/core/types.gen.ts +0 -110
- package/src/api/generated/core/utils.gen.ts +0 -140
- package/src/api/generated/facade.gen.ts +0 -104
- package/src/api/generated/index.ts +0 -4
- package/src/api/generated/sdk.gen.ts +0 -175
- package/src/api/index.ts +0 -45
- package/src/api/makeEndpoint.ts +0 -54
- package/src/auto-instrument.js +0 -172
- package/src/index.js +0 -19
- package/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +0 -118
- package/src/instrumentation/anthropic/get_cache_config_telemetry.js +0 -90
- package/src/instrumentation/anthropic/get_streaming_stop_reason_telemetry.js +0 -121
- package/src/instrumentation/anthropic/get_tool_definitions_telemetry.js +0 -55
- package/src/instrumentation/anthropic/set_telemetry_attributes.js +0 -39
- package/src/instrumentation/bedrock/get_cache_config_telemetry.js +0 -106
- package/src/instrumentation/bedrock/get_cache_telemetry.js +0 -109
- package/src/instrumentation/bedrock/get_input_message_content_telemetry.js +0 -94
- package/src/instrumentation/bedrock/get_streaming_output_message_telemetry.js +0 -87
- package/src/instrumentation/bedrock/set_telemetry_attributes.js +0 -40
- package/src/instrumentation/cohere/get_chat_telemetry.js +0 -88
- package/src/instrumentation/cohere/get_embeddings_telemetry.js +0 -107
- package/src/instrumentation/cohere/set_telemetry_attributes.js +0 -32
- package/src/instrumentation/elevenlabs/full_instrumentation.js +0 -236
- package/src/instrumentation/google/get_audio_telemetry.js +0 -30
- package/src/instrumentation/google/get_cache_operations_telemetry.js +0 -171
- package/src/instrumentation/google/get_embeddings_telemetry.js +0 -99
- package/src/instrumentation/google/set_telemetry_attributes.js +0 -104
- package/src/instrumentation/openai/get_audio_telemetry.js +0 -149
- package/src/instrumentation/openai/get_cache_and_reasoning_telemetry.js +0 -71
- package/src/instrumentation/openai/get_cache_config_telemetry.js +0 -63
- package/src/instrumentation/openai/get_embeddings_telemetry.js +0 -107
- package/src/instrumentation/openai/get_streaming_usage.js +0 -81
- package/src/instrumentation/openai/set_telemetry_attributes.js +0 -41
- package/src/instrumentation/vercel/full_instrumentation.js +0 -52
- package/src/instrumentation/vercel/get_cache_creation_telemetry.js +0 -70
- package/src/logger.js +0 -8
- package/src/otel/exporter.js +0 -108
- package/src/otel/propagation.js +0 -146
- package/src/otel/span-filter.js +0 -69
- package/src/otel/span-processor.js +0 -170
- package/src/otel/vercel-ai-sdk-span-filter.js +0 -79
- package/src/setup.js +0 -167
- package/tests/api/index.test.js +0 -437
- package/tests/index.test.js +0 -884
- package/tests/instrumentation/anthropic/get_cache_and_reasoning_telemetry.test.js +0 -181
- package/tests/instrumentation/anthropic/get_cache_config_telemetry.test.js +0 -92
- package/tests/instrumentation/anthropic/get_streaming_stop_reason_telemetry.test.js +0 -117
- package/tests/instrumentation/anthropic/get_tool_definitions_telemetry.test.js +0 -97
- package/tests/instrumentation/bedrock/get_cache_config_telemetry.test.js +0 -107
- package/tests/instrumentation/bedrock/get_cache_telemetry.test.js +0 -118
- package/tests/instrumentation/bedrock/get_input_message_content_telemetry.test.js +0 -89
- package/tests/instrumentation/bedrock/get_streaming_output_message_telemetry.test.js +0 -87
- package/tests/instrumentation/cohere/get_chat_telemetry.test.js +0 -148
- package/tests/instrumentation/cohere/get_embeddings_telemetry.test.js +0 -132
- package/tests/instrumentation/elevenlabs/full_instrumentation.test.js +0 -258
- package/tests/instrumentation/google/get_cache_operations_telemetry.test.js +0 -130
- package/tests/instrumentation/google/get_embeddings_telemetry.test.js +0 -128
- package/tests/instrumentation/google/get_reasoning_telemetry.test.js +0 -41
- package/tests/instrumentation/google/set_telemetry_attributes.test.js +0 -219
- package/tests/instrumentation/openai/get_audio_telemetry.test.js +0 -170
- package/tests/instrumentation/openai/get_cache_and_reasoning_telemetry.test.js +0 -83
- package/tests/instrumentation/openai/get_cache_config_telemetry.test.js +0 -83
- package/tests/instrumentation/openai/get_embeddings_telemetry.test.js +0 -145
- package/tests/instrumentation/openai/get_streaming_usage.test.js +0 -105
- package/tests/instrumentation/vercel/full_instrumentation.test.js +0 -16
- package/tests/instrumentation/vercel/get_cache_creation_telemetry.test.js +0 -96
- package/tests/otel/vercel-ai-sdk-span-filter.test.js +0 -159
- package/tsconfig.json +0 -18
- /package/dist/{api → src/api}/caseConversion.d.ts +0 -0
- /package/dist/{api → src/api}/caseConversion.js +0 -0
- /package/dist/{api → src/api}/generated/client/client.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/client.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client/index.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/index.js +0 -0
- /package/dist/{api → src/api}/generated/client/types.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/types.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client/utils.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client/utils.gen.js +0 -0
- /package/dist/{api → src/api}/generated/client.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/client.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/auth.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/auth.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/bodySerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/bodySerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/params.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/params.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/pathSerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/pathSerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/queryKeySerializer.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/queryKeySerializer.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/serverSentEvents.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/serverSentEvents.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/types.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/types.gen.js +0 -0
- /package/dist/{api → src/api}/generated/core/utils.gen.d.ts +0 -0
- /package/dist/{api → src/api}/generated/core/utils.gen.js +0 -0
- /package/dist/{api → src/api}/generated/types.gen.js +0 -0
- /package/dist/{api → src/api}/index.d.ts +0 -0
- /package/dist/{api → src/api}/index.js +0 -0
- /package/dist/{api → src/api}/makeEndpoint.d.ts +0 -0
|
@@ -1,236 +0,0 @@
|
|
|
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 };
|
|
@@ -1,30 +0,0 @@
|
|
|
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`.
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
// Sum the token counts whose modality is AUDIO across a *TokensDetails array.
|
|
12
|
-
function _audioTokens(details) {
|
|
13
|
-
if (!Array.isArray(details)) return 0;
|
|
14
|
-
return details.reduce(
|
|
15
|
-
(total, detail) => total + (detail && detail.modality === 'AUDIO' ? detail.tokenCount || 0 : 0),
|
|
16
|
-
0
|
|
17
|
-
);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function setAudioTelemetry(span, response) {
|
|
21
|
-
const usage = response && response.usageMetadata;
|
|
22
|
-
if (!usage) return;
|
|
23
|
-
const inputAudio = _audioTokens(usage.promptTokensDetails);
|
|
24
|
-
const outputAudio = _audioTokens(usage.candidatesTokensDetails);
|
|
25
|
-
if (inputAudio) span.setAttribute('gen_ai.usage.input_tokens_details.audio_tokens', inputAudio);
|
|
26
|
-
if (outputAudio)
|
|
27
|
-
span.setAttribute('gen_ai.usage.output_tokens_details.audio_tokens', outputAudio);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
module.exports = { setAudioTelemetry, _audioTokens };
|
|
@@ -1,171 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Explicit-cache lifecycle telemetry for Google GenAI (Gemini).
|
|
5
|
-
*
|
|
6
|
-
* Explicit caching happens through API calls that are not LLM calls — the
|
|
7
|
-
* caller uploads content once via `client.caches.create` (returning a
|
|
8
|
-
* `cachedContents/{id}` name later referenced by `generateContent`), and may
|
|
9
|
-
* extend its TTL (`caches.update`) or remove it (`caches.delete`). No
|
|
10
|
-
* instrumentor covers these endpoints, so the cached content and its lifetime
|
|
11
|
-
* were invisible to the telemetry.
|
|
12
|
-
*
|
|
13
|
-
* One span per lifecycle call, carrying:
|
|
14
|
-
* - `weflayr.cache.operation` — create / update / delete
|
|
15
|
-
* - `weflayr.cache.cached_content_id` — the cache's server-generated name
|
|
16
|
-
* - `weflayr.cache.expire_time` — absolute expiry (create / update)
|
|
17
|
-
* - `gen_ai.usage.input_tokens` — the cached content's token count
|
|
18
|
-
* (create only; billed at the standard input rate)
|
|
19
|
-
* - the cached content itself (`gen_ai.input.messages` /
|
|
20
|
-
* `gen_ai.system_instructions` / `gen_ai.tool.definitions`) — the content
|
|
21
|
-
* is sent only on the create call and never repeated in the calls that use
|
|
22
|
-
* the cache, so this is the one place it can be captured. The span
|
|
23
|
-
* processor strips these when message-content capture is disabled.
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
const { trace, SpanStatusCode } = require('@opentelemetry/api');
|
|
27
|
-
|
|
28
|
-
const { getProviderName } = require('./get_provider_name');
|
|
29
|
-
|
|
30
|
-
const TRACER_NAME = 'weflayr.instrumentation.google.cache_operations';
|
|
31
|
-
|
|
32
|
-
const OPERATION_ATTR = 'weflayr.cache.operation';
|
|
33
|
-
const CACHED_CONTENT_ID_ATTR = 'weflayr.cache.cached_content_id';
|
|
34
|
-
const EXPIRE_TIME_ATTR = 'weflayr.cache.expire_time';
|
|
35
|
-
|
|
36
|
-
const OPERATIONS = ['create', 'update', 'delete'];
|
|
37
|
-
|
|
38
|
-
// The cached content mapped to the role+parts message shape the chat path uses.
|
|
39
|
-
function _contentToMessage(content) {
|
|
40
|
-
if (typeof content === 'string') {
|
|
41
|
-
return { role: 'user', parts: [{ type: 'text', content }] };
|
|
42
|
-
}
|
|
43
|
-
const role = (content && content.role) || 'user';
|
|
44
|
-
const rawParts = (content && content.parts) || [];
|
|
45
|
-
const parts = [];
|
|
46
|
-
for (const rawPart of rawParts) {
|
|
47
|
-
if (typeof rawPart === 'string') {
|
|
48
|
-
parts.push({ type: 'text', content: rawPart });
|
|
49
|
-
} else if (rawPart && typeof rawPart.text === 'string') {
|
|
50
|
-
parts.push({ type: 'text', content: rawPart.text });
|
|
51
|
-
} else if (rawPart && rawPart.inlineData) {
|
|
52
|
-
// Non-text parts (e.g. audio) become a byte-less blob stub so span
|
|
53
|
-
// attributes never carry raw media payloads.
|
|
54
|
-
parts.push({ type: 'blob', mime_type: rawPart.inlineData.mimeType || '' });
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
return { role, parts };
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function _setSystemInstruction(span, systemInstruction) {
|
|
61
|
-
if (systemInstruction == null) return;
|
|
62
|
-
const texts =
|
|
63
|
-
typeof systemInstruction === 'string'
|
|
64
|
-
? [systemInstruction]
|
|
65
|
-
: (systemInstruction.parts || [])
|
|
66
|
-
.map((part) => part && part.text)
|
|
67
|
-
.filter((text) => typeof text === 'string');
|
|
68
|
-
if (texts.length) {
|
|
69
|
-
span.setAttribute(
|
|
70
|
-
'gen_ai.system_instructions',
|
|
71
|
-
JSON.stringify(texts.map((text) => ({ type: 'text', content: text })))
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function _setToolDefinitions(span, config) {
|
|
77
|
-
const definitions = [];
|
|
78
|
-
for (const tool of (config && config.tools) || []) {
|
|
79
|
-
for (const declaration of (tool && tool.functionDeclarations) || []) {
|
|
80
|
-
definitions.push(declaration);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
if (definitions.length) {
|
|
84
|
-
span.setAttribute('gen_ai.tool.definitions', JSON.stringify(definitions));
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Stamp the server-side facts of the cache from a create/update response.
|
|
89
|
-
function _stampCachedContent(span, cachedContent, operation) {
|
|
90
|
-
if (!cachedContent) return;
|
|
91
|
-
if (cachedContent.name) {
|
|
92
|
-
span.setAttribute(CACHED_CONTENT_ID_ATTR, String(cachedContent.name));
|
|
93
|
-
}
|
|
94
|
-
if (cachedContent.expireTime) {
|
|
95
|
-
span.setAttribute(EXPIRE_TIME_ATTR, String(cachedContent.expireTime));
|
|
96
|
-
}
|
|
97
|
-
// Gemini bills the cached tokens once, at creation; an update (TTL extension)
|
|
98
|
-
// returns the same usageMetadata but bills nothing.
|
|
99
|
-
if (operation !== 'create') return;
|
|
100
|
-
const totalTokens = cachedContent.usageMetadata && cachedContent.usageMetadata.totalTokenCount;
|
|
101
|
-
if (totalTokens) {
|
|
102
|
-
span.setAttribute('gen_ai.usage.input_tokens', totalTokens);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
function _startSpan(operation, params, system) {
|
|
107
|
-
const span = trace.getTracer(TRACER_NAME).startSpan(`cache_${operation}`);
|
|
108
|
-
span.setAttribute('gen_ai.system', system);
|
|
109
|
-
span.setAttribute('gen_ai.operation.name', `cache_${operation}`);
|
|
110
|
-
span.setAttribute(OPERATION_ATTR, operation);
|
|
111
|
-
if (params && params.model) {
|
|
112
|
-
// The chat path strips the leading "models/" prefix; mirror it.
|
|
113
|
-
span.setAttribute('gen_ai.request.model', params.model.replace(/^models\//, ''));
|
|
114
|
-
}
|
|
115
|
-
// update/delete address the cache by name; create gets it from the response.
|
|
116
|
-
if (params && params.name) {
|
|
117
|
-
span.setAttribute(CACHED_CONTENT_ID_ATTR, String(params.name));
|
|
118
|
-
}
|
|
119
|
-
if (operation === 'create') {
|
|
120
|
-
const config = (params && params.config) || {};
|
|
121
|
-
if (config.contents != null) {
|
|
122
|
-
const contentsList = Array.isArray(config.contents) ? config.contents : [config.contents];
|
|
123
|
-
span.setAttribute(
|
|
124
|
-
'gen_ai.input.messages',
|
|
125
|
-
JSON.stringify(contentsList.map(_contentToMessage))
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
_setSystemInstruction(span, config.systemInstruction);
|
|
129
|
-
_setToolDefinitions(span, config);
|
|
130
|
-
}
|
|
131
|
-
return span;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
function _recordError(span, error) {
|
|
135
|
-
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
136
|
-
span.recordException(error);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
// Wrap `Caches.prototype.create/update/delete`, pushing the restore tuples onto
|
|
140
|
-
// the shared `patched` array the orchestrator owns for uninstrument().
|
|
141
|
-
function instrumentCacheOperations(Caches, patched) {
|
|
142
|
-
const owner = Caches.prototype;
|
|
143
|
-
for (const operation of OPERATIONS) {
|
|
144
|
-
const original = owner[operation];
|
|
145
|
-
if (typeof original !== 'function') continue;
|
|
146
|
-
|
|
147
|
-
const wrapper = function cacheOperationWrapper(params, ...rest) {
|
|
148
|
-
const span = _startSpan(operation, params, getProviderName(this));
|
|
149
|
-
return Promise.resolve(original.call(this, params, ...rest)).then(
|
|
150
|
-
(response) => {
|
|
151
|
-
try {
|
|
152
|
-
_stampCachedContent(span, response, operation);
|
|
153
|
-
} catch {
|
|
154
|
-
// Never break the caller over an enrichment failure.
|
|
155
|
-
}
|
|
156
|
-
span.end();
|
|
157
|
-
return response;
|
|
158
|
-
},
|
|
159
|
-
(error) => {
|
|
160
|
-
_recordError(span, error);
|
|
161
|
-
span.end();
|
|
162
|
-
throw error;
|
|
163
|
-
}
|
|
164
|
-
);
|
|
165
|
-
};
|
|
166
|
-
owner[operation] = wrapper;
|
|
167
|
-
patched.push([owner, operation, original]);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
module.exports = { instrumentCacheOperations };
|
|
@@ -1,99 +0,0 @@
|
|
|
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
|
-
* num_input_chars).
|
|
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
|
-
// The embedded text mapped to the role+parts message shape the chat path uses:
|
|
38
|
-
// a string (or {text} / {parts} object) becomes one user message per content.
|
|
39
|
-
function _contentToMessage(content) {
|
|
40
|
-
if (typeof content === 'string') {
|
|
41
|
-
return { role: 'user', parts: [{ type: 'text', content }] };
|
|
42
|
-
}
|
|
43
|
-
if (content && typeof content.text === 'string') {
|
|
44
|
-
return { role: 'user', parts: [{ type: 'text', content: content.text }] };
|
|
45
|
-
}
|
|
46
|
-
return { role: 'user', parts: [] };
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function _recordError(span, error) {
|
|
50
|
-
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
51
|
-
span.recordException(error);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Wrap `Models.prototype.embedContentInternal`, pushing the restore tuple onto
|
|
55
|
-
// the shared `patched` array the orchestrator owns for uninstrument().
|
|
56
|
-
function instrumentEmbeddings(Models, patched) {
|
|
57
|
-
const owner = Models.prototype;
|
|
58
|
-
const original = owner.embedContentInternal;
|
|
59
|
-
if (typeof original !== 'function') return;
|
|
60
|
-
|
|
61
|
-
function wrapper(params, ...rest) {
|
|
62
|
-
const span = trace.getTracer(TRACER_NAME).startSpan(EMBEDDINGS_OPERATION);
|
|
63
|
-
span.setAttribute('gen_ai.system', getProviderName(this));
|
|
64
|
-
span.setAttribute('gen_ai.operation.name', EMBEDDINGS_OPERATION);
|
|
65
|
-
if (params && params.model) {
|
|
66
|
-
// The chat path strips the leading "models/" prefix; mirror it.
|
|
67
|
-
span.setAttribute('gen_ai.request.model', params.model.replace(/^models\//, ''));
|
|
68
|
-
}
|
|
69
|
-
span.setAttribute(
|
|
70
|
-
'gen_ai.usage.input_characters',
|
|
71
|
-
_contentCharacters(params && params.contents)
|
|
72
|
-
);
|
|
73
|
-
// The embedded text is the prompt; stamp it like the chat path does. The
|
|
74
|
-
// span processor strips it when message-content capture is disabled.
|
|
75
|
-
if (params && params.contents != null) {
|
|
76
|
-
const contentsList = Array.isArray(params.contents) ? params.contents : [params.contents];
|
|
77
|
-
span.setAttribute(
|
|
78
|
-
'gen_ai.input.messages',
|
|
79
|
-
JSON.stringify(contentsList.map(_contentToMessage))
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
return Promise.resolve(original.call(this, params, ...rest)).then(
|
|
83
|
-
(response) => {
|
|
84
|
-
span.end();
|
|
85
|
-
return response;
|
|
86
|
-
},
|
|
87
|
-
(error) => {
|
|
88
|
-
_recordError(span, error);
|
|
89
|
-
span.end();
|
|
90
|
-
throw error;
|
|
91
|
-
}
|
|
92
|
-
);
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
owner.embedContentInternal = wrapper;
|
|
96
|
-
patched.push([owner, 'embedContentInternal', original]);
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
module.exports = { instrumentEmbeddings };
|
|
@@ -1,104 +0,0 @@
|
|
|
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, setCacheConfigTelemetry } = require('./get_cache_telemetry');
|
|
21
|
-
const { setReasoningTelemetry } = require('./get_reasoning_telemetry');
|
|
22
|
-
const { instrumentEmbeddings } = require('./get_embeddings_telemetry');
|
|
23
|
-
const { instrumentCacheOperations } = require('./get_cache_operations_telemetry');
|
|
24
|
-
|
|
25
|
-
class GoogleTelemetryInstrumentor {
|
|
26
|
-
constructor() {
|
|
27
|
-
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
28
|
-
this._patched = [];
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
instrument() {
|
|
32
|
-
let Models;
|
|
33
|
-
let Caches;
|
|
34
|
-
try {
|
|
35
|
-
({ Models, Caches } = require('@google/genai'));
|
|
36
|
-
} catch (err) {
|
|
37
|
-
throw new Error(
|
|
38
|
-
'weflayr.autoInstrument(GOOGLE_GENAI): the @google/genai client is not installed. ' +
|
|
39
|
-
'Install it with: npm install @google/genai\n' +
|
|
40
|
-
`Underlying error: ${err.message}`
|
|
41
|
-
);
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
// Non-streaming: stamp provider up front, audio + cache tokens off the response.
|
|
45
|
-
this._wrapChat(Models.prototype, 'generateContentInternal', { stream: false });
|
|
46
|
-
// Streaming: stamp provider, then cache tokens off the final chunk's usageMetadata.
|
|
47
|
-
this._wrapChat(Models.prototype, 'generateContentStreamInternal', { stream: true });
|
|
48
|
-
// Embeddings: an endpoint the traceloop instrumentor doesn't cover at all.
|
|
49
|
-
instrumentEmbeddings(Models, this._patched);
|
|
50
|
-
// Explicit-cache lifecycle (caches.create/update/delete): endpoints the
|
|
51
|
-
// traceloop instrumentor doesn't cover at all.
|
|
52
|
-
if (Caches) instrumentCacheOperations(Caches, this._patched);
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
_wrapChat(owner, methodName, { stream }) {
|
|
56
|
-
const original = owner[methodName];
|
|
57
|
-
|
|
58
|
-
function wrapper(...args) {
|
|
59
|
-
const span = trace.getActiveSpan();
|
|
60
|
-
if (!span || !span.isRecording || !span.isRecording()) {
|
|
61
|
-
return original.apply(this, args);
|
|
62
|
-
}
|
|
63
|
-
span.setAttribute('gen_ai.system', getProviderName(this));
|
|
64
|
-
setCacheConfigTelemetry(span, args[0] && args[0].config);
|
|
65
|
-
if (!stream) {
|
|
66
|
-
return Promise.resolve(original.apply(this, args)).then((response) => {
|
|
67
|
-
setAudioTelemetry(span, response);
|
|
68
|
-
setCacheTelemetry(span, response);
|
|
69
|
-
setReasoningTelemetry(span, response);
|
|
70
|
-
return response;
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
// Streamed: usage (incl. cachedContentTokenCount) lands on the final chunk;
|
|
74
|
-
// observe the chunks as they pass through. The instrumentor keeps the span
|
|
75
|
-
// open until the stream is exhausted.
|
|
76
|
-
return Promise.resolve(original.apply(this, args)).then((stream_) => {
|
|
77
|
-
if (!stream_ || typeof stream_[Symbol.asyncIterator] !== 'function') return stream_;
|
|
78
|
-
return (async function* observeCache() {
|
|
79
|
-
for await (const chunk of stream_) {
|
|
80
|
-
try {
|
|
81
|
-
setCacheTelemetry(span, chunk);
|
|
82
|
-
setReasoningTelemetry(span, chunk);
|
|
83
|
-
} catch {
|
|
84
|
-
// Never break the caller's stream over an enrichment failure.
|
|
85
|
-
}
|
|
86
|
-
yield chunk;
|
|
87
|
-
}
|
|
88
|
-
})();
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
owner[methodName] = wrapper;
|
|
93
|
-
this._patched.push([owner, methodName, original]);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
uninstrument() {
|
|
97
|
-
for (const [owner, methodName, original] of this._patched) {
|
|
98
|
-
owner[methodName] = original;
|
|
99
|
-
}
|
|
100
|
-
this._patched = [];
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
module.exports = { GoogleTelemetryInstrumentor };
|