|
1 | | -import { context, createContextKey } from "@opentelemetry/api"; |
2 | 1 | import { ReadableSpan, Tracer } from "@opentelemetry/sdk-trace-node"; |
3 | 2 |
|
4 | 3 | import { PromptKernelRequest } from "../api/types/PromptKernelRequest"; |
|
7 | 6 | HUMANLOOP_FILE_TYPE_KEY, |
8 | 7 | HUMANLOOP_LOG_KEY, |
9 | 8 | HUMANLOOP_META_FUNCTION_NAME, |
10 | | - HUMANLOOP_PARENT_SPAN_CTX_KEY, |
11 | 9 | HUMANLOOP_PATH_KEY, |
12 | | - HUMANLOOP_TRACE_FLOW_CTX_KEY, |
| 10 | + HUMANLOOP_PROMPT_SPAN_NAME, |
13 | 11 | NestedDict, |
14 | 12 | jsonifyIfNotString, |
15 | 13 | writeToOpenTelemetrySpan, |
@@ -56,57 +54,53 @@ export function promptUtilityFactory<I, M, O>( |
56 | 54 | .reduce((obj, [key, value]) => ({ ...obj, [key]: value }), {}); |
57 | 55 | } |
58 | 56 |
|
59 | | - const parentSpanContextKey = createContextKey(HUMANLOOP_PARENT_SPAN_CTX_KEY); |
60 | | - const flowMetadataKey = createContextKey(HUMANLOOP_TRACE_FLOW_CTX_KEY); |
61 | | - |
62 | 57 | // @ts-ignore |
63 | | - return opentelemetryTracer.startActiveSpan("humanloop.prompt", async (span) => { |
64 | | - const ctx = context.active(); |
65 | | - const spanId = span.spanContext().spanId; |
| 58 | + return opentelemetryTracer.startActiveSpan( |
| 59 | + HUMANLOOP_PROMPT_SPAN_NAME, |
| 60 | + async (span) => { |
| 61 | + // Add span attributes |
| 62 | + span = span.setAttribute(HUMANLOOP_PATH_KEY, path || func.name); |
| 63 | + span = span.setAttribute(HUMANLOOP_FILE_TYPE_KEY, "prompt"); |
| 64 | + span = span.setAttribute(HUMANLOOP_META_FUNCTION_NAME, func.name); |
| 65 | + |
| 66 | + if (version) { |
| 67 | + writeToOpenTelemetrySpan( |
| 68 | + span as unknown as ReadableSpan, |
| 69 | + { |
| 70 | + ...version, |
| 71 | + } as unknown as NestedDict, |
| 72 | + "humanloop.file.prompt", |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + // Execute the wrapped function in a child context |
| 77 | + let output: O | null; |
| 78 | + let error: string | null = null; |
| 79 | + try { |
| 80 | + output = await func(inputs, messages); |
| 81 | + } catch (err: any) { |
| 82 | + console.error(`Error calling ${func.name}:`, err); |
| 83 | + output = null; |
| 84 | + error = err.message || String(err); |
| 85 | + } |
66 | 86 |
|
67 | | - // Add span attributes |
68 | | - span = span.setAttribute(HUMANLOOP_PATH_KEY, path || func.name); |
69 | | - span = span.setAttribute(HUMANLOOP_FILE_TYPE_KEY, "prompt"); |
70 | | - span = span.setAttribute(HUMANLOOP_META_FUNCTION_NAME, func.name); |
| 87 | + const promptLog = { |
| 88 | + output: jsonifyIfNotString(func, output), |
| 89 | + error, |
| 90 | + inputs: inputs, |
| 91 | + messages: messages, |
| 92 | + }; |
71 | 93 |
|
72 | | - if (version) { |
73 | 94 | writeToOpenTelemetrySpan( |
74 | 95 | span as unknown as ReadableSpan, |
75 | | - { |
76 | | - ...version, |
77 | | - } as unknown as NestedDict, |
78 | | - "humanloop.file.prompt", |
| 96 | + // @ts-ignore |
| 97 | + promptLog, |
| 98 | + HUMANLOOP_LOG_KEY, |
79 | 99 | ); |
80 | | - } |
81 | | - |
82 | | - // Execute the wrapped function in a child context |
83 | | - let output: O | null; |
84 | | - let error: string | null = null; |
85 | | - try { |
86 | | - output = await func(inputs, messages); |
87 | | - } catch (err: any) { |
88 | | - console.error(`Error calling ${func.name}:`, err); |
89 | | - // @ts-ignore |
90 | | - output = null; |
91 | | - error = err.message || String(err); |
92 | | - } |
93 | | - |
94 | | - const promptLog = { |
95 | | - output: jsonifyIfNotString(func, output), |
96 | | - error, |
97 | | - inputs: inputs, |
98 | | - messages: messages, |
99 | | - }; |
100 | | - |
101 | | - writeToOpenTelemetrySpan( |
102 | | - span as unknown as ReadableSpan, |
103 | | - // @ts-ignore |
104 | | - promptLog, |
105 | | - HUMANLOOP_LOG_KEY, |
106 | | - ); |
107 | 100 |
|
108 | | - span.end(); |
109 | | - return output; |
110 | | - }); |
| 101 | + span.end(); |
| 102 | + return output; |
| 103 | + }, |
| 104 | + ); |
111 | 105 | }; |
112 | 106 | } |
0 commit comments