-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(evals): add OTEL tracer-provider module + deps (unwired) #2352
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
miguelg719
wants to merge
1
commit into
miguelgonzalez/evals-langsmith-gating
from
miguelgonzalez/evals-otel-provider
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| import { ProxyTracerProvider, type Tracer } from "@opentelemetry/api"; | ||
| import { | ||
| BatchSpanProcessor, | ||
| type SpanProcessor, | ||
| } from "@opentelemetry/sdk-trace-base"; | ||
| import { NodeTracerProvider } from "@opentelemetry/sdk-trace-node"; | ||
|
|
||
| import { hasLangSmithApiKey, resolveTraceTransport } from "./langsmith.js"; | ||
|
|
||
| const TRACER_NAME = "stagehand-evals"; | ||
| const SHUTDOWN_TIMEOUT_MS = 10_000; | ||
| const NOOP_TRACER = new ProxyTracerProvider().getTracer(TRACER_NAME); | ||
|
|
||
| // Provider state lives on globalThis, NOT in module scope: the CLI bundle | ||
| // (esbuild --bundle) inlines this module once per import site, so module-level | ||
| // variables would give each importer its own copy — the runner would register | ||
| // the provider in one copy while tracedSpan reads NOOP from another, silently | ||
| // dropping every span. A Symbol.for-keyed global is shared across all copies. | ||
| type TracingState = { | ||
| provider: NodeTracerProvider | null; | ||
| providerPromise: Promise<NodeTracerProvider | null> | null; | ||
| }; | ||
| const STATE_KEY = Symbol.for("stagehand.evals.otel.state"); | ||
| function state(): TracingState { | ||
| const g = globalThis as { [STATE_KEY]?: TracingState }; | ||
| return (g[STATE_KEY] ??= { provider: null, providerPromise: null }); | ||
| } | ||
|
|
||
| /** Test-only: clear shared provider state between vitest module resets. */ | ||
| export function resetTracingStateForTests(): void { | ||
| const g = globalThis as { [STATE_KEY]?: TracingState }; | ||
| delete g[STATE_KEY]; | ||
| } | ||
|
|
||
| export async function buildTracerProvider(options?: { | ||
| braintrustParent?: string; | ||
| }): Promise<NodeTracerProvider | null> { | ||
| if (resolveTraceTransport() !== "otel") { | ||
| return null; | ||
| } | ||
|
|
||
| const s = state(); | ||
| if (s.provider) { | ||
| return s.provider; | ||
| } | ||
|
|
||
| // The provider is initialized once per process, so the first call's options win. | ||
| const pendingProvider = | ||
| s.providerPromise ?? | ||
| (s.providerPromise = initializeTracerProvider(options)); | ||
|
|
||
| try { | ||
| return await pendingProvider; | ||
| } finally { | ||
| if (s.providerPromise === pendingProvider) { | ||
| s.providerPromise = null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| async function initializeTracerProvider(options?: { | ||
| braintrustParent?: string; | ||
| }): Promise<NodeTracerProvider | null> { | ||
| const spanProcessors: SpanProcessor[] = []; | ||
|
|
||
| const braintrustApiKey = process.env.BRAINTRUST_API_KEY; | ||
| if (braintrustApiKey) { | ||
| const { OTLPTraceExporter } = await import( | ||
| "@opentelemetry/exporter-trace-otlp-proto" | ||
| ); | ||
| const braintrustProjectName = | ||
| process.env.CI === "true" ? "stagehand" : "stagehand-dev"; | ||
| const parent = | ||
| options?.braintrustParent ?? | ||
| process.env.BRAINTRUST_OTEL_PARENT ?? | ||
| `project_name:${braintrustProjectName}`; | ||
| spanProcessors.push( | ||
| new BatchSpanProcessor( | ||
| new OTLPTraceExporter({ | ||
| url: | ||
| process.env.BRAINTRUST_OTEL_URL ?? | ||
| "https://api.braintrust.dev/otel/v1/traces", | ||
| headers: { | ||
| Authorization: `Bearer ${braintrustApiKey}`, | ||
| "x-bt-parent": parent, | ||
| }, | ||
| }), | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| if (hasLangSmithApiKey() && process.env.LANGSMITH_TRACING === "true") { | ||
| const { LangSmithOTLPTraceExporter } = await import( | ||
| "langsmith/experimental/otel/exporter" | ||
| ); | ||
| spanProcessors.push( | ||
| new BatchSpanProcessor(new LangSmithOTLPTraceExporter()), | ||
| ); | ||
| } | ||
|
|
||
| if (spanProcessors.length === 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const nextProvider = new NodeTracerProvider({ spanProcessors }); | ||
| nextProvider.register(); | ||
| state().provider = nextProvider; | ||
| return nextProvider; | ||
| } | ||
|
|
||
| export function getTracer(): Tracer { | ||
| return state().provider?.getTracer(TRACER_NAME) ?? NOOP_TRACER; | ||
| } | ||
|
|
||
| export async function shutdownTracing(): Promise<void> { | ||
| const s = state(); | ||
| if (resolveTraceTransport() !== "otel" || !s.provider) { | ||
| return; | ||
| } | ||
|
|
||
| const activeProvider = s.provider; | ||
| s.provider = null; | ||
|
|
||
| let timeout: ReturnType<typeof setTimeout> | undefined; | ||
| const timeoutPromise = new Promise<never>((_, reject) => { | ||
| timeout = setTimeout(() => { | ||
| reject( | ||
| new Error( | ||
| `Timed out shutting down tracing after ${SHUTDOWN_TIMEOUT_MS}ms.`, | ||
| ), | ||
| ); | ||
| }, SHUTDOWN_TIMEOUT_MS); | ||
| timeout.unref?.(); | ||
| }); | ||
|
|
||
| try { | ||
| await Promise.race([ | ||
| activeProvider | ||
| .forceFlush() | ||
| .catch(() => {}) | ||
| .then(() => activeProvider.shutdown()), | ||
| timeoutPromise, | ||
| ]); | ||
| } finally { | ||
| if (timeout) { | ||
| clearTimeout(timeout); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| import { afterAll, beforeEach, describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const mocks = vi.hoisted(() => { | ||
| const braintrustExporter = vi.fn(function () { | ||
| return { | ||
| export: vi.fn(), | ||
| shutdown: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
| }); | ||
| const langSmithExporter = vi.fn(function () { | ||
| return { | ||
| export: vi.fn(), | ||
| shutdown: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
| }); | ||
| const nodeTracerProvider = vi.fn(function (options: unknown) { | ||
| return { | ||
| forceFlush: vi.fn().mockResolvedValue(undefined), | ||
| getTracer: vi.fn(), | ||
| options, | ||
| register: vi.fn(), | ||
| shutdown: vi.fn().mockResolvedValue(undefined), | ||
| }; | ||
| }); | ||
|
|
||
| return { | ||
| braintrustExporter, | ||
| langSmithExporter, | ||
| nodeTracerProvider, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock("@opentelemetry/exporter-trace-otlp-proto", () => ({ | ||
| OTLPTraceExporter: mocks.braintrustExporter, | ||
| })); | ||
|
|
||
| vi.mock("langsmith/experimental/otel/exporter", () => ({ | ||
| LangSmithOTLPTraceExporter: mocks.langSmithExporter, | ||
| })); | ||
|
|
||
| vi.mock("@opentelemetry/sdk-trace-node", () => ({ | ||
| NodeTracerProvider: mocks.nodeTracerProvider, | ||
| })); | ||
|
|
||
| const originalEnv = { ...process.env }; | ||
|
|
||
| function constructedSpanProcessors(): unknown[] { | ||
| const options = mocks.nodeTracerProvider.mock.calls[0]?.[0] as | ||
| | { spanProcessors: unknown[] } | ||
| | undefined; | ||
| return options?.spanProcessors ?? []; | ||
| } | ||
|
|
||
| describe("buildTracerProvider", () => { | ||
| beforeEach(async () => { | ||
| process.env = { ...originalEnv }; | ||
| delete process.env.BRAINTRUST_API_KEY; | ||
| delete process.env.BRAINTRUST_OTEL_PARENT; | ||
| delete process.env.BRAINTRUST_OTEL_URL; | ||
| delete process.env.CI; | ||
| delete process.env.EVAL_TRACE_TRANSPORT; | ||
| delete process.env.LANGSMITH_API_KEY; | ||
| delete process.env.LANGSMITH_TRACING; | ||
| vi.clearAllMocks(); | ||
| vi.resetModules(); | ||
| // Provider state is globalThis-backed (bundle-duplication proof), so a | ||
| // module reset alone no longer isolates tests — use the module's own | ||
| // test-reset helper to clear the shared slot. | ||
| const { resetTracingStateForTests } = await import( | ||
| "../../framework/otel.js" | ||
| ); | ||
| resetTracingStateForTests(); | ||
| }); | ||
|
|
||
| afterAll(() => { | ||
| process.env = originalEnv; | ||
| }); | ||
|
|
||
| it("does not construct a provider in native mode", async () => { | ||
| const { buildTracerProvider } = await import("../../framework/otel.js"); | ||
|
|
||
| await expect(buildTracerProvider()).resolves.toBeNull(); | ||
| expect(mocks.nodeTracerProvider).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("constructs a provider with both backend processors", async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| process.env.BRAINTRUST_API_KEY = "braintrust-test-key"; | ||
| process.env.LANGSMITH_API_KEY = "langsmith-test-key"; | ||
| process.env.LANGSMITH_TRACING = "true"; | ||
| const { buildTracerProvider } = await import("../../framework/otel.js"); | ||
|
|
||
| await expect(buildTracerProvider()).resolves.not.toBeNull(); | ||
| expect(mocks.braintrustExporter).toHaveBeenCalledWith({ | ||
| url: "https://api.braintrust.dev/otel/v1/traces", | ||
| headers: { | ||
| Authorization: "Bearer braintrust-test-key", | ||
| "x-bt-parent": "project_name:stagehand-dev", | ||
| }, | ||
| }); | ||
| expect(mocks.langSmithExporter).toHaveBeenCalledOnce(); | ||
| expect(constructedSpanProcessors()).toHaveLength(2); | ||
| }); | ||
|
|
||
| it("constructs a provider with only the Braintrust processor", async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| process.env.BRAINTRUST_API_KEY = "braintrust-test-key"; | ||
| const { buildTracerProvider } = await import("../../framework/otel.js"); | ||
|
|
||
| await expect(buildTracerProvider()).resolves.not.toBeNull(); | ||
| expect(mocks.braintrustExporter).toHaveBeenCalledWith({ | ||
| url: "https://api.braintrust.dev/otel/v1/traces", | ||
| headers: { | ||
| Authorization: "Bearer braintrust-test-key", | ||
| "x-bt-parent": "project_name:stagehand-dev", | ||
| }, | ||
| }); | ||
| expect(mocks.langSmithExporter).not.toHaveBeenCalled(); | ||
| expect(constructedSpanProcessors()).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("uses the provided Braintrust parent over the environment default", async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| process.env.BRAINTRUST_API_KEY = "braintrust-test-key"; | ||
| process.env.BRAINTRUST_OTEL_PARENT = "project_name:from-env"; | ||
| const { buildTracerProvider } = await import("../../framework/otel.js"); | ||
|
|
||
| await expect( | ||
| buildTracerProvider({ braintrustParent: "project_name:custom" }), | ||
| ).resolves.not.toBeNull(); | ||
| expect(mocks.braintrustExporter).toHaveBeenCalledWith({ | ||
| url: "https://api.braintrust.dev/otel/v1/traces", | ||
| headers: { | ||
| Authorization: "Bearer braintrust-test-key", | ||
| "x-bt-parent": "project_name:custom", | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it("constructs a provider with only the LangSmith processor", async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| process.env.LANGSMITH_API_KEY = "langsmith-test-key"; | ||
| process.env.LANGSMITH_TRACING = "true"; | ||
| const { buildTracerProvider } = await import("../../framework/otel.js"); | ||
|
|
||
| await expect(buildTracerProvider()).resolves.not.toBeNull(); | ||
| expect(mocks.braintrustExporter).not.toHaveBeenCalled(); | ||
| expect(mocks.langSmithExporter).toHaveBeenCalledOnce(); | ||
| expect(constructedSpanProcessors()).toHaveLength(1); | ||
| }); | ||
|
|
||
| it("shuts down the provider when forceFlush rejects", async () => { | ||
| process.env.EVAL_TRACE_TRANSPORT = "otel"; | ||
| process.env.BRAINTRUST_API_KEY = "braintrust-test-key"; | ||
| const { buildTracerProvider, shutdownTracing } = await import( | ||
| "../../framework/otel.js" | ||
| ); | ||
|
|
||
| await buildTracerProvider(); | ||
| const activeProvider = mocks.nodeTracerProvider.mock.results[0]?.value; | ||
| activeProvider.forceFlush.mockRejectedValueOnce(new Error("export failed")); | ||
|
|
||
| await expect(shutdownTracing()).resolves.toBeUndefined(); | ||
| expect(activeProvider.shutdown).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.