weflayr 0.20.6 → 0.22.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 +1 -1
- package/package.json +2 -2
- package/src/auto-instrument.js +64 -31
- package/src/instrumentation/anthropic/get_cache_and_reasoning_telemetry.js +107 -0
- package/src/instrumentation/anthropic/get_streaming_stop_reason_telemetry.js +121 -0
- package/src/instrumentation/anthropic/get_tool_definitions_telemetry.js +50 -0
- package/src/instrumentation/anthropic/set_telemetry_attributes.js +37 -0
- package/src/instrumentation/bedrock/get_cache_telemetry.js +104 -0
- package/src/instrumentation/bedrock/get_input_message_content_telemetry.js +94 -0
- package/src/instrumentation/bedrock/get_streaming_output_message_telemetry.js +87 -0
- package/src/instrumentation/bedrock/set_telemetry_attributes.js +36 -0
- package/src/instrumentation/cohere/get_chat_telemetry.js +88 -0
- package/src/instrumentation/cohere/get_embeddings_telemetry.js +107 -0
- package/src/instrumentation/cohere/set_telemetry_attributes.js +32 -0
- package/src/instrumentation/google/get_audio_telemetry.js +30 -0
- package/src/instrumentation/google/get_cache_telemetry.js +25 -0
- package/src/instrumentation/google/get_embeddings_telemetry.js +99 -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 +107 -0
- package/src/instrumentation/openai/get_streaming_usage.js +81 -0
- package/src/instrumentation/openai/set_telemetry_attributes.js +39 -0
- package/src/otel/exporter.js +1 -6
- package/src/otel/propagation.js +5 -0
- package/src/otel/span-filter.js +0 -5
- package/src/otel/span-processor.js +30 -1
- package/src/setup.js +1 -2
- package/tests/index.test.js +144 -44
- package/tests/instrumentation/anthropic/get_cache_and_reasoning_telemetry.test.js +158 -0
- package/tests/instrumentation/anthropic/get_streaming_stop_reason_telemetry.test.js +117 -0
- package/tests/instrumentation/anthropic/get_tool_definitions_telemetry.test.js +82 -0
- package/tests/instrumentation/bedrock/get_cache_telemetry.test.js +102 -0
- package/tests/instrumentation/bedrock/get_input_message_content_telemetry.test.js +89 -0
- package/tests/instrumentation/bedrock/get_streaming_output_message_telemetry.test.js +87 -0
- package/tests/instrumentation/cohere/get_chat_telemetry.test.js +148 -0
- package/tests/instrumentation/cohere/get_embeddings_telemetry.test.js +132 -0
- package/tests/instrumentation/{elevenlabs.test.js → elevenlabs/full_instrumentation.test.js} +21 -5
- package/tests/instrumentation/google/get_embeddings_telemetry.test.js +128 -0
- package/tests/instrumentation/google/get_reasoning_telemetry.test.js +41 -0
- package/tests/instrumentation/google/set_telemetry_attributes.test.js +219 -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 +145 -0
- package/tests/instrumentation/openai/get_streaming_usage.test.js +105 -0
- package/src/otel/openai-streaming-usage.js +0 -60
- /package/src/instrumentation/{elevenlabs.js → elevenlabs/full_instrumentation.js} +0 -0
|
@@ -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 };
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Cached-prompt token enrichment complement for the non-streamed OpenAI chat path.
|
|
5
|
+
*
|
|
6
|
+
* `@traceloop/instrumentation-openai` records `gen_ai.usage.{input,output}_tokens`
|
|
7
|
+
* for non-streamed calls but never reads `usage.prompt_tokens_details.cached_tokens`,
|
|
8
|
+
* so spans for prompt-cache hits carry no cached-token count. The Python
|
|
9
|
+
* OpenLLMetry instrumentor sets `gen_ai.usage.cache_read_input_tokens` from that
|
|
10
|
+
* field, so this brings the Node SDK to parity.
|
|
11
|
+
*
|
|
12
|
+
* It wraps the instrumentor instance's `_endSpan({ span, type, result })`, which
|
|
13
|
+
* finalizes the span after the response resolves, letting us stamp the
|
|
14
|
+
* cached-token count off `result.usage` while the span is still open. OpenAI has
|
|
15
|
+
* no separate cache-write count, so only `cache_read_input_tokens` is set.
|
|
16
|
+
*
|
|
17
|
+
* The same hook surfaces the reasoning-token count: reasoning models report it as
|
|
18
|
+
* `usage.completion_tokens_details.reasoning_tokens` (a subset of output_tokens)
|
|
19
|
+
* which the Node instrumentor drops, so this stamps the flat `reasoning_tokens`
|
|
20
|
+
* attribute (the form the Python instrumentor emits and the normalizer reads).
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
const CACHE_READ_INPUT_TOKENS_ATTR = 'gen_ai.usage.cache_read_input_tokens';
|
|
24
|
+
const REASONING_TOKENS_ATTR = 'gen_ai.usage.reasoning_tokens';
|
|
25
|
+
|
|
26
|
+
class OpenAIPromptCachingInstrumentor {
|
|
27
|
+
constructor() {
|
|
28
|
+
// [instrumentation, methodName, originalFunction] for uninstrument().
|
|
29
|
+
this._patched = [];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// No-op (and never throws) if the instrumentor's internals have changed shape —
|
|
33
|
+
// the worst case is a missing cached-token count, never a crash.
|
|
34
|
+
instrument(instrumentation) {
|
|
35
|
+
const original = instrumentation && instrumentation._endSpan;
|
|
36
|
+
if (typeof original !== 'function') return;
|
|
37
|
+
|
|
38
|
+
instrumentation._endSpan = function endSpanWithCache(args) {
|
|
39
|
+
try {
|
|
40
|
+
const span = args && args.span;
|
|
41
|
+
const usage = args && args.result && args.result.usage;
|
|
42
|
+
const recording = span && span.isRecording && span.isRecording();
|
|
43
|
+
const cachedTokens =
|
|
44
|
+
usage && usage.prompt_tokens_details && usage.prompt_tokens_details.cached_tokens;
|
|
45
|
+
if (cachedTokens && recording) {
|
|
46
|
+
span.setAttribute(CACHE_READ_INPUT_TOKENS_ATTR, cachedTokens);
|
|
47
|
+
}
|
|
48
|
+
const reasoningTokens =
|
|
49
|
+
usage &&
|
|
50
|
+
usage.completion_tokens_details &&
|
|
51
|
+
usage.completion_tokens_details.reasoning_tokens;
|
|
52
|
+
if (reasoningTokens && recording) {
|
|
53
|
+
span.setAttribute(REASONING_TOKENS_ATTR, reasoningTokens);
|
|
54
|
+
}
|
|
55
|
+
} catch {
|
|
56
|
+
// Never break span finalization over an enrichment failure.
|
|
57
|
+
}
|
|
58
|
+
return original.call(this, args);
|
|
59
|
+
};
|
|
60
|
+
this._patched.push([instrumentation, '_endSpan', original]);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
uninstrument() {
|
|
64
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
65
|
+
owner[methodName] = original;
|
|
66
|
+
}
|
|
67
|
+
this._patched = [];
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
module.exports = { OpenAIPromptCachingInstrumentor };
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Hand-written OTel instrumentor for the OpenAI embeddings endpoint.
|
|
5
|
+
*
|
|
6
|
+
* `@traceloop/instrumentation-openai` (the package weflayr uses for OpenAI on
|
|
7
|
+
* Node) does not wrap the embeddings endpoint, so this ships inside weflayr to
|
|
8
|
+
* cover it. The Python SDK gets embeddings for free from OpenLLMetry's
|
|
9
|
+
* instrumentor, so there is no Python counterpart to this file — but the span it
|
|
10
|
+
* emits is shaped to normalize identically. It is registered alongside the
|
|
11
|
+
* traceloop instrumentor whenever `autoInstrument({ aiSdks: 'openai' })` runs.
|
|
12
|
+
*
|
|
13
|
+
* Embeddings are billed per input token, so the span records the prompt tokens
|
|
14
|
+
* read off the response under `gen_ai.usage.input_tokens`; there is no completion.
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
const { trace, SpanStatusCode } = require('@opentelemetry/api');
|
|
18
|
+
|
|
19
|
+
const TRACER_NAME = 'weflayr.instrumentation.openai_embeddings';
|
|
20
|
+
const EMBEDDINGS_OPERATION = 'embeddings';
|
|
21
|
+
|
|
22
|
+
// Return the gen_ai.system for this call, matching how the OpenAI chat
|
|
23
|
+
// instrumentor attributes the vendor: Azure clients (reached via the same openai
|
|
24
|
+
// SDK) report `azure.ai.openai`, everything else `openai`.
|
|
25
|
+
function _detectSystem(instance) {
|
|
26
|
+
const baseURL = (instance && instance._client && instance._client.baseURL) || '';
|
|
27
|
+
return baseURL.toLowerCase().includes('openai.azure.com') ? 'azure.ai.openai' : 'openai';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function _recordError(span, error) {
|
|
31
|
+
span.setStatus({ code: SpanStatusCode.ERROR, message: error?.message ?? String(error) });
|
|
32
|
+
span.recordException(error);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
class OpenAIEmbeddingsInstrumentor {
|
|
36
|
+
constructor() {
|
|
37
|
+
// [ownerPrototype, methodName, originalFunction] for uninstrument().
|
|
38
|
+
this._patched = [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
instrument() {
|
|
42
|
+
let Embeddings;
|
|
43
|
+
try {
|
|
44
|
+
({ Embeddings } = require('openai/resources/embeddings'));
|
|
45
|
+
} catch (err) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
'weflayr.autoInstrument(OPENAI): the openai client is not installed. ' +
|
|
48
|
+
'Install it with: npm install openai\n' +
|
|
49
|
+
`Underlying error: ${err.message}`
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const owner = Embeddings.prototype;
|
|
54
|
+
const original = owner.create;
|
|
55
|
+
|
|
56
|
+
function wrapper(body, ...rest) {
|
|
57
|
+
const span = trace.getTracer(TRACER_NAME).startSpan(EMBEDDINGS_OPERATION);
|
|
58
|
+
span.setAttribute('gen_ai.system', _detectSystem(this));
|
|
59
|
+
span.setAttribute('gen_ai.operation.name', EMBEDDINGS_OPERATION);
|
|
60
|
+
if (body && body.model) {
|
|
61
|
+
span.setAttribute('gen_ai.request.model', body.model);
|
|
62
|
+
}
|
|
63
|
+
// One user message per input element, matching the shape the chat
|
|
64
|
+
// instrumentors emit. The span processor strips it when message-content
|
|
65
|
+
// capture is disabled.
|
|
66
|
+
if (body && body.input != null) {
|
|
67
|
+
const inputs = Array.isArray(body.input) ? body.input : [body.input];
|
|
68
|
+
span.setAttribute(
|
|
69
|
+
'gen_ai.input.messages',
|
|
70
|
+
JSON.stringify(
|
|
71
|
+
inputs.map((content) => ({ role: 'user', parts: [{ type: 'text', content }] }))
|
|
72
|
+
)
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
return Promise.resolve(original.call(this, body, ...rest)).then(
|
|
76
|
+
(response) => {
|
|
77
|
+
if (response && response.model) {
|
|
78
|
+
span.setAttribute('gen_ai.response.model', response.model);
|
|
79
|
+
}
|
|
80
|
+
const promptTokens = response && response.usage && response.usage.prompt_tokens;
|
|
81
|
+
if (promptTokens != null) {
|
|
82
|
+
span.setAttribute('gen_ai.usage.input_tokens', promptTokens);
|
|
83
|
+
}
|
|
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.create = wrapper;
|
|
96
|
+
this._patched.push([owner, 'create', original]);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
uninstrument() {
|
|
100
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
101
|
+
owner[methodName] = original;
|
|
102
|
+
}
|
|
103
|
+
this._patched = [];
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
module.exports = { OpenAIEmbeddingsInstrumentor };
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Streamed token-usage enrichment complement for the OpenAI chat path.
|
|
5
|
+
*
|
|
6
|
+
* `@traceloop/instrumentation-openai` does not record token usage for streamed
|
|
7
|
+
* calls: its stream handler accumulates `choices[].delta` but never reads the
|
|
8
|
+
* final usage chunk OpenAI sends when the request sets
|
|
9
|
+
* `stream_options: { include_usage: true }`. Non-streaming calls work because
|
|
10
|
+
* OpenAI returns `usage` in the response body, which the instrumentor reads
|
|
11
|
+
* directly. The Python OpenLLMetry instrumentor reads the streamed usage, so
|
|
12
|
+
* this gap is Node-only.
|
|
13
|
+
*
|
|
14
|
+
* It wraps the instrumentor instance's `_streamingWrapPromise`: that handler
|
|
15
|
+
* hands us the span and ends it only after its chunk loop, so when the usage
|
|
16
|
+
* chunk arrives the span is still open and we set the same `gen_ai.usage.*`
|
|
17
|
+
* attributes the instrumentor sets for non-streaming calls.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
const USAGE_INPUT_TOKENS_ATTR = 'gen_ai.usage.input_tokens';
|
|
21
|
+
const USAGE_OUTPUT_TOKENS_ATTR = 'gen_ai.usage.output_tokens';
|
|
22
|
+
const USAGE_TOTAL_TOKENS_ATTR = 'gen_ai.usage.total_tokens';
|
|
23
|
+
const CACHE_READ_INPUT_TOKENS_ATTR = 'gen_ai.usage.cache_read_input_tokens';
|
|
24
|
+
|
|
25
|
+
function _setUsageAttributes(span, usage) {
|
|
26
|
+
if (typeof usage.prompt_tokens === 'number') {
|
|
27
|
+
span.setAttribute(USAGE_INPUT_TOKENS_ATTR, usage.prompt_tokens);
|
|
28
|
+
}
|
|
29
|
+
if (typeof usage.completion_tokens === 'number') {
|
|
30
|
+
span.setAttribute(USAGE_OUTPUT_TOKENS_ATTR, usage.completion_tokens);
|
|
31
|
+
}
|
|
32
|
+
if (typeof usage.total_tokens === 'number') {
|
|
33
|
+
span.setAttribute(USAGE_TOTAL_TOKENS_ATTR, usage.total_tokens);
|
|
34
|
+
}
|
|
35
|
+
// Cached-prompt tokens ride in the same streamed usage chunk; mirror the
|
|
36
|
+
// non-streamed cache backfill in get_cache_and_reasoning_telemetry.js.
|
|
37
|
+
const cachedTokens = usage.prompt_tokens_details && usage.prompt_tokens_details.cached_tokens;
|
|
38
|
+
if (cachedTokens) {
|
|
39
|
+
span.setAttribute(CACHE_READ_INPUT_TOKENS_ATTR, cachedTokens);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
class OpenAIStreamingUsageInstrumentor {
|
|
44
|
+
constructor() {
|
|
45
|
+
// [instrumentation, methodName, originalFunction] for uninstrument().
|
|
46
|
+
this._patched = [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// No-op if the instrumentor's internals have changed shape — the worst case is
|
|
50
|
+
// streamed spans without token counts, never a crash.
|
|
51
|
+
instrument(instrumentation) {
|
|
52
|
+
const original = instrumentation && instrumentation._streamingWrapPromise;
|
|
53
|
+
if (typeof original !== 'function') return;
|
|
54
|
+
|
|
55
|
+
instrumentation._streamingWrapPromise = function streamingWrapWithUsage(streamArgs) {
|
|
56
|
+
const span = streamArgs && streamArgs.span;
|
|
57
|
+
const wrappedStream = original.call(this, streamArgs);
|
|
58
|
+
if (!span) return wrappedStream;
|
|
59
|
+
|
|
60
|
+
return (async function* backfillUsage() {
|
|
61
|
+
for await (const chunk of wrappedStream) {
|
|
62
|
+
const usage = chunk && chunk.usage;
|
|
63
|
+
if (usage && span.isRecording()) {
|
|
64
|
+
_setUsageAttributes(span, usage);
|
|
65
|
+
}
|
|
66
|
+
yield chunk;
|
|
67
|
+
}
|
|
68
|
+
})();
|
|
69
|
+
};
|
|
70
|
+
this._patched.push([instrumentation, '_streamingWrapPromise', original]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
uninstrument() {
|
|
74
|
+
for (const [owner, methodName, original] of this._patched) {
|
|
75
|
+
owner[methodName] = original;
|
|
76
|
+
}
|
|
77
|
+
this._patched = [];
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
module.exports = { OpenAIStreamingUsageInstrumentor };
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* OpenAI telemetry complement — the registered orchestrator.
|
|
5
|
+
*
|
|
6
|
+
* Wires every weflayr-supplied patch the traceloop OpenAI instrumentor misses:
|
|
7
|
+
* - the audio (TTS/STT) and embeddings endpoints it doesn't cover, and
|
|
8
|
+
* - usage it drops on the chat endpoint it does cover: streamed token usage and
|
|
9
|
+
* non-streamed cached-prompt tokens (these enrich the traceloop instrumentor
|
|
10
|
+
* instance, hence the `instrumentation` argument).
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
const { OpenAIAudioInstrumentor } = require('./get_audio_telemetry');
|
|
14
|
+
const { OpenAIEmbeddingsInstrumentor } = require('./get_embeddings_telemetry');
|
|
15
|
+
const { OpenAIStreamingUsageInstrumentor } = require('./get_streaming_usage');
|
|
16
|
+
const { OpenAIPromptCachingInstrumentor } = require('./get_cache_and_reasoning_telemetry');
|
|
17
|
+
|
|
18
|
+
class OpenAITelemetryInstrumentor {
|
|
19
|
+
constructor() {
|
|
20
|
+
this._subInstrumentors = [
|
|
21
|
+
new OpenAIAudioInstrumentor(),
|
|
22
|
+
new OpenAIEmbeddingsInstrumentor(),
|
|
23
|
+
new OpenAIStreamingUsageInstrumentor(),
|
|
24
|
+
new OpenAIPromptCachingInstrumentor(),
|
|
25
|
+
];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// The endpoint complements ignore `instrumentation`; the streaming/cache ones
|
|
29
|
+
// patch its internal handlers.
|
|
30
|
+
instrument(instrumentation) {
|
|
31
|
+
for (const sub of this._subInstrumentors) sub.instrument(instrumentation);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
uninstrument() {
|
|
35
|
+
for (const sub of this._subInstrumentors) sub.uninstrument();
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = { OpenAITelemetryInstrumentor };
|
package/src/otel/exporter.js
CHANGED
|
@@ -77,7 +77,6 @@ function buildExporter({
|
|
|
77
77
|
});
|
|
78
78
|
const originalExport = exporter.export.bind(exporter);
|
|
79
79
|
const wrappedExport = _exportWithRetry(originalExport, { maxRetries, maxRetryMs });
|
|
80
|
-
let warned = false;
|
|
81
80
|
exporter.export = (spans, resultCallback) => {
|
|
82
81
|
const start = Date.now();
|
|
83
82
|
debug('exporter.export: posting count=%d', spans.length);
|
|
@@ -93,11 +92,7 @@ function buildExporter({
|
|
|
93
92
|
result?.code,
|
|
94
93
|
result?.error?.message || result?.error || '(none)'
|
|
95
94
|
);
|
|
96
|
-
|
|
97
|
-
// even without NODE_DEBUG=weflayr. Stay quiet after — repeated warns
|
|
98
|
-
// on a misconfigured key would just spam.
|
|
99
|
-
if (warnOnExportError && !warned) {
|
|
100
|
-
warned = true;
|
|
95
|
+
if (warnOnExportError) {
|
|
101
96
|
// console.warn writes to stderr synchronously. process.emitWarning
|
|
102
97
|
// is queued for the next tick, so a caller doing process.exit() from
|
|
103
98
|
// their error handler races past it and the warning is lost.
|
package/src/otel/propagation.js
CHANGED
|
@@ -62,6 +62,11 @@ function _buildAttributes({ featureName, customerId, providerName, extraTags, sp
|
|
|
62
62
|
* propagateMetadata call" contract.
|
|
63
63
|
*/
|
|
64
64
|
function propagateMetadata(options, fn) {
|
|
65
|
+
const { featureName, customerId } = options ?? {};
|
|
66
|
+
if (featureName == null || customerId == null) {
|
|
67
|
+
throw new Error('propagateMetadata: `featureName` and `customerId` are required');
|
|
68
|
+
}
|
|
69
|
+
|
|
65
70
|
if (typeof fn === 'function') {
|
|
66
71
|
return _runWithMetadata(options, fn);
|
|
67
72
|
}
|
package/src/otel/span-filter.js
CHANGED
|
@@ -27,18 +27,13 @@ const KNOWN_LLM_INSTRUMENTATION_SCOPE_PREFIXES = [
|
|
|
27
27
|
'opentelemetry.instrumentation.google_generativeai',
|
|
28
28
|
'opentelemetry.instrumentation.openai',
|
|
29
29
|
'opentelemetry.instrumentation.openai_v2',
|
|
30
|
-
'opentelemetry.instrumentation.vertex_ai',
|
|
31
|
-
'opentelemetry.instrumentation.vertexai',
|
|
32
30
|
'weflayr.instrumentation.elevenlabs',
|
|
33
31
|
'strands-agents',
|
|
34
32
|
'vllm',
|
|
35
33
|
'@traceloop/instrumentation-openai',
|
|
36
34
|
'@traceloop/instrumentation-anthropic',
|
|
37
35
|
'@traceloop/instrumentation-bedrock',
|
|
38
|
-
'@traceloop/instrumentation-azure',
|
|
39
36
|
'@traceloop/instrumentation-cohere',
|
|
40
|
-
'@traceloop/instrumentation-together',
|
|
41
|
-
'@traceloop/instrumentation-vertexai',
|
|
42
37
|
'@traceloop/instrumentation-google-generativeai',
|
|
43
38
|
'@traceloop/instrumentation-langchain',
|
|
44
39
|
'@traceloop/instrumentation-llamaindex',
|
|
@@ -22,6 +22,29 @@ const { EXTRA_TAG_PREFIX, getMergedMetadataFromContext } = require('./propagatio
|
|
|
22
22
|
const { isDefaultExportSpan, isKnownLLMInstrumentor } = require('./span-filter');
|
|
23
23
|
const { debug } = require('../logger');
|
|
24
24
|
|
|
25
|
+
// setup({captureMessageContent: ...}) records the choice in this env var (the
|
|
26
|
+
// official OTel GenAI one). Instrumentors gate content on their own knobs
|
|
27
|
+
// (openllmetry reads its own config and captures by default), so the processor
|
|
28
|
+
// enforces the flag itself: when the env var is "false", message content is
|
|
29
|
+
// stripped from every span before export.
|
|
30
|
+
const CONTENT_CAPTURE_ENV = 'OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT';
|
|
31
|
+
|
|
32
|
+
// The new-semconv JSON message attributes and the legacy indexed
|
|
33
|
+
// `gen_ai.prompt.{i}.*` / `gen_ai.completion.{i}.*` attributes.
|
|
34
|
+
const CONTENT_ATTRIBUTES = [
|
|
35
|
+
'gen_ai.input.messages',
|
|
36
|
+
'gen_ai.output.messages',
|
|
37
|
+
'gen_ai.system_instructions',
|
|
38
|
+
];
|
|
39
|
+
const CONTENT_ATTRIBUTE_PREFIXES = ['gen_ai.prompt.', 'gen_ai.completion.'];
|
|
40
|
+
|
|
41
|
+
function _isContentAttribute(key) {
|
|
42
|
+
return (
|
|
43
|
+
CONTENT_ATTRIBUTES.includes(key) ||
|
|
44
|
+
CONTENT_ATTRIBUTE_PREFIXES.some((prefix) => key.startsWith(prefix))
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
|
|
25
48
|
class WeflayrSpanProcessor {
|
|
26
49
|
constructor({ exporter, exportMode = 'batch', defaultTags = {}, mask, shouldExportSpan } = {}) {
|
|
27
50
|
if (!exporter) {
|
|
@@ -77,6 +100,12 @@ class WeflayrSpanProcessor {
|
|
|
77
100
|
}
|
|
78
101
|
debug('span end: exporting name=%s scope=%s', span.name, span.instrumentationScope?.name);
|
|
79
102
|
|
|
103
|
+
if ((process.env[CONTENT_CAPTURE_ENV] || '').toLowerCase() === 'false') {
|
|
104
|
+
for (const key of Object.keys(span.attributes)) {
|
|
105
|
+
if (_isContentAttribute(key)) delete span.attributes[key];
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
80
109
|
if (this._mask) {
|
|
81
110
|
try {
|
|
82
111
|
const masked = this._mask(span.attributes);
|
|
@@ -120,4 +149,4 @@ class WeflayrSpanProcessor {
|
|
|
120
149
|
}
|
|
121
150
|
}
|
|
122
151
|
|
|
123
|
-
module.exports = { WeflayrSpanProcessor };
|
|
152
|
+
module.exports = { CONTENT_CAPTURE_ENV, WeflayrSpanProcessor };
|
package/src/setup.js
CHANGED
|
@@ -5,13 +5,12 @@ const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
|
|
|
5
5
|
const { resourceFromAttributes } = require('@opentelemetry/resources');
|
|
6
6
|
|
|
7
7
|
const { buildExporter } = require('./otel/exporter');
|
|
8
|
-
const { WeflayrSpanProcessor } = require('./otel/span-processor');
|
|
8
|
+
const { CONTENT_CAPTURE_ENV, WeflayrSpanProcessor } = require('./otel/span-processor');
|
|
9
9
|
const { debug } = require('./logger');
|
|
10
10
|
const { version: SDK_VERSION } = require('../package.json');
|
|
11
11
|
|
|
12
12
|
const SDK_LANGUAGE = 'javascript';
|
|
13
13
|
const DEFAULT_BASE_URL = 'https://api.weflayr.com';
|
|
14
|
-
const CONTENT_CAPTURE_ENV = 'OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT';
|
|
15
14
|
|
|
16
15
|
const _state = {
|
|
17
16
|
tracerProvider: null,
|