|
1 | 1 | 'use strict' |
2 | 2 |
|
| 3 | +const { |
| 4 | + SPAN_KIND, |
| 5 | + MODEL_PROVIDER, |
| 6 | + PARENT_ID_KEY, |
| 7 | + SESSION_ID, |
| 8 | + ROOT_PARENT_ID, |
| 9 | + INTEGRATION, |
| 10 | + DECORATOR |
| 11 | +} = require('./constants/tags') |
| 12 | + |
| 13 | +const ERROR_TYPE = require('../constants') |
| 14 | + |
3 | 15 | const telemetryMetrics = require('../telemetry/metrics') |
| 16 | + |
| 17 | +const LLMObsTagger = require('./tagger') |
| 18 | + |
4 | 19 | const llmobsMetrics = telemetryMetrics.manager.namespace('mlobs') |
5 | 20 |
|
6 | 21 | function incrementLLMObsSpanStartCount (tags, value = 1) { |
7 | 22 | llmobsMetrics.count('span.start', tags).inc(value) |
8 | 23 | } |
9 | 24 |
|
| 25 | +function incrementLLMObsSpanFinishedCount (span, value = 1) { |
| 26 | + const mlObsTags = LLMObsTagger.tagMap.get(span) |
| 27 | + const spanTags = span.context()._tags |
| 28 | + |
| 29 | + const isRootSpan = mlObsTags[PARENT_ID_KEY] === ROOT_PARENT_ID |
| 30 | + const hasSessionId = mlObsTags[SESSION_ID] != null |
| 31 | + const integration = mlObsTags[INTEGRATION] |
| 32 | + const autoInstrumented = integration != null |
| 33 | + const decorator = !!mlObsTags[DECORATOR] |
| 34 | + const spanKind = mlObsTags[SPAN_KIND] |
| 35 | + const modelProvider = mlObsTags[MODEL_PROVIDER] |
| 36 | + const error = spanTags.error || spanTags[ERROR_TYPE] |
| 37 | + |
| 38 | + const tags = { |
| 39 | + autoinstrumented: Number(autoInstrumented), |
| 40 | + has_session_id: Number(hasSessionId), |
| 41 | + is_root_span: Number(isRootSpan), |
| 42 | + span_kind: spanKind, |
| 43 | + integration: integration || 'N/A', |
| 44 | + error: error ? 1 : 0 |
| 45 | + } |
| 46 | + if (!autoInstrumented) { |
| 47 | + tags.decorator = Number(decorator) |
| 48 | + } |
| 49 | + if (modelProvider) { |
| 50 | + tags.model_provider = modelProvider |
| 51 | + } |
| 52 | + |
| 53 | + llmobsMetrics.count('span.finished', tags).inc(value) |
| 54 | +} |
| 55 | + |
10 | 56 | module.exports = { |
11 | | - incrementLLMObsSpanStartCount |
| 57 | + incrementLLMObsSpanStartCount, |
| 58 | + incrementLLMObsSpanFinishedCount |
12 | 59 | } |
0 commit comments