Skip to content

Commit 6204723

Browse files
author
Andrei Bratu
committed
Release 0.8.19:
* Fixed wrong interactions between private tracer and global tracer
1 parent 6ef9e64 commit 6204723

File tree

6 files changed

+28
-42
lines changed

6 files changed

+28
-42
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "humanloop",
3-
"version": "0.8.18",
3+
"version": "0.8.19",
44
"private": false,
55
"repository": "https://github.com/humanloop/humanloop-node",
66
"main": "./index.js",

src/context.ts

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import * as contextApi from "@opentelemetry/api";
1+
import * as otel_api from "@opentelemetry/api";
2+
import { AsyncHooksContextManager } from "@opentelemetry/context-async-hooks";
23

34
import { HumanloopRuntimeError } from "./error";
45
import {
@@ -7,15 +8,18 @@ import {
78
HUMANLOOP_CONTEXT_TRACE_ID,
89
} from "./otel/constants";
910

11+
export const HL_CONTEXT = new AsyncHooksContextManager();
12+
HL_CONTEXT.enable();
13+
1014
export function getTraceId(): string | undefined {
11-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
12-
const value = contextApi.context.active().getValue(key);
15+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
16+
const value = HL_CONTEXT.active().getValue(key);
1317
return (value || undefined) as string | undefined;
1418
}
1519

16-
export function setTraceId(flowLogId: string): contextApi.Context {
17-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
18-
return contextApi.context.active().setValue(key, flowLogId);
20+
export function setTraceId(flowLogId: string): otel_api.Context {
21+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_TRACE_ID);
22+
return HL_CONTEXT.active().setValue(key, flowLogId);
1923
}
2024

2125
export type DecoratorContext = {
@@ -26,14 +30,14 @@ export type DecoratorContext = {
2630

2731
export function setDecoratorContext(
2832
decoratorContext: DecoratorContext,
29-
): contextApi.Context {
30-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
31-
return contextApi.context.active().setValue(key, decoratorContext);
33+
): otel_api.Context {
34+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
35+
return HL_CONTEXT.active().setValue(key, decoratorContext);
3236
}
3337

3438
export function getDecoratorContext(): DecoratorContext | undefined {
35-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
36-
return (contextApi.context.active().getValue(key) || undefined) as
39+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_DECORATOR);
40+
return (HL_CONTEXT.active().getValue(key) || undefined) as
3741
| DecoratorContext
3842
| undefined;
3943
}
@@ -134,14 +138,14 @@ export class EvaluationContext {
134138

135139
export function setEvaluationContext(
136140
evaluationContext: EvaluationContext,
137-
): contextApi.Context {
138-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
139-
return contextApi.context.active().setValue(key, evaluationContext);
141+
): otel_api.Context {
142+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
143+
return HL_CONTEXT.active().setValue(key, evaluationContext);
140144
}
141145

142146
export function getEvaluationContext(): EvaluationContext | undefined {
143-
const key = contextApi.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
144-
return (contextApi.context.active().getValue(key) || undefined) as
147+
const key = otel_api.createContextKey(HUMANLOOP_CONTEXT_EVALUATION);
148+
return (HL_CONTEXT.active().getValue(key) || undefined) as
145149
| EvaluationContext
146150
| undefined;
147151
}

src/decorators/flow.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
import * as contextApi from "@opentelemetry/api";
21
import { Tracer } from "@opentelemetry/api";
32
import { ReadableSpan } from "@opentelemetry/sdk-trace-node";
43

54
import { HumanloopClient } from "../Client";
65
import { ChatMessage, FlowLogRequest, FlowLogResponse } from "../api";
7-
import { getTraceId, setDecoratorContext, setTraceId } from "../context";
6+
import { HL_CONTEXT, getTraceId, setDecoratorContext, setTraceId } from "../context";
87
import { jsonifyIfNotString, writeToOpenTelemetrySpan } from "../otel";
98
import {
109
HUMANLOOP_FILE_TYPE_KEY,
@@ -42,7 +41,7 @@ export function flowUtilityFactory<I, O>(
4241
? I
4342
: never,
4443
) => {
45-
return contextApi.context.with(
44+
return HL_CONTEXT.with(
4645
setDecoratorContext({
4746
path: path,
4847
type: fileType,
@@ -77,7 +76,7 @@ export function flowUtilityFactory<I, O>(
7776
...initLogInputs,
7877
});
7978

80-
return await contextApi.context.with(
79+
return await HL_CONTEXT.with(
8180
setTraceId(flowLogResponse.id),
8281
async () => {
8382
let logOutput: string | undefined;

src/decorators/prompt.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
import * as contextApi from "@opentelemetry/api";
2-
3-
import { setDecoratorContext } from "../evals";
1+
import { HL_CONTEXT, setDecoratorContext } from "../context";
42

53
export function promptDecoratorFactory<I, O>(path: string, callable: (inputs: I) => O) {
64
const fileType = "prompt";
75

86
const wrappedFunction = (inputs: I) => {
9-
return contextApi.context.with(
7+
return HL_CONTEXT.with(
108
setDecoratorContext({
119
path: path,
1210
type: fileType,

src/evals/run.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
* Functions in this module should be accessed via the Humanloop client. They should
88
* not be called directly.
99
*/
10-
import * as contextApi from "@opentelemetry/api";
1110
import cliProgress from "cli-progress";
1211
import _, { capitalize } from "lodash";
1312

1413
import {
1514
BooleanEvaluatorStatsResponse,
16-
ChatMessage,
1715
CreateEvaluationRequestEvaluatorsItem,
1816
DatapointResponse,
1917
DatasetResponse,
@@ -35,25 +33,23 @@ import {
3533
PromptRequest,
3634
PromptResponse,
3735
RunStatsResponse,
38-
ToolKernelRequest,
3936
ToolLogRequest,
4037
ToolRequest,
4138
ToolResponse,
4239
} from "../api";
4340
import {
4441
EvaluationContext,
42+
HL_CONTEXT,
4543
getEvaluationContext,
4644
setEvaluationContext,
4745
} from "../context";
4846
import { HumanloopRuntimeError } from "../error";
4947
import { Humanloop, HumanloopClient } from "../index";
50-
import { jsonifyIfNotString } from "../otel/helpers";
5148
import {
5249
Dataset,
5350
Evaluator,
5451
EvaluatorCheck,
5552
File,
56-
FileResponse,
5753
LocalEvaluator,
5854
LocalEvaluatorReturnTypeEnum,
5955
OnlineEvaluator,
@@ -371,7 +367,7 @@ export async function runEval<I, O>(
371367
throw new Error(`Datapoint 'inputs' attribute is undefined.`);
372368
}
373369

374-
contextApi.context.with(
370+
HL_CONTEXT.with(
375371
setEvaluationContext(
376372
new EvaluationContext({
377373
sourceDatapointId: datapoint.id,

src/humanloop.client.ts

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,13 @@ class HumanloopTracerSingleton {
166166
),
167167
],
168168
});
169-
const instrumentations: Instrumentation[] = [];
170169
if (config.instrumentProviders?.OpenAI) {
171170
const openaiInstrumentation = new OpenAIInstrumentation({
172171
enrichTokens: true,
173172
});
174173
openaiInstrumentation.manuallyInstrument(config.instrumentProviders.OpenAI);
175174
openaiInstrumentation.setTracerProvider(this.tracerProvider);
176175
openaiInstrumentation.enable();
177-
instrumentations.push(openaiInstrumentation);
178176
}
179177
if (config.instrumentProviders?.Anthropic) {
180178
const anthropicInstrumentation = new AnthropicInstrumentation();
@@ -183,7 +181,6 @@ class HumanloopTracerSingleton {
183181
);
184182
anthropicInstrumentation.setTracerProvider(this.tracerProvider);
185183
anthropicInstrumentation.enable();
186-
instrumentations.push(anthropicInstrumentation);
187184
}
188185
if (config.instrumentProviders?.CohereAI) {
189186
const cohereInstrumentation = new CohereInstrumentation();
@@ -192,16 +189,8 @@ class HumanloopTracerSingleton {
192189
);
193190
cohereInstrumentation.setTracerProvider(this.tracerProvider);
194191
cohereInstrumentation.enable();
195-
instrumentations.push(cohereInstrumentation);
196192
}
197193

198-
this.tracerProvider.register();
199-
200-
registerInstrumentations({
201-
tracerProvider: this.tracerProvider,
202-
instrumentations,
203-
});
204-
205194
this.tracer = this.tracerProvider.getTracer("humanloop.sdk");
206195
}
207196

0 commit comments

Comments
 (0)