Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

Reproduce #3996: PostHog template distinct id is not read by PostHog (#3997)

roboeve[bot]2fa1f5cbc426
diff --git a/apps/docs/lib/integrations/posthog-instrumentation.test.ts b/apps/docs/lib/integrations/posthog-instrumentation.test.ts
new file mode 100644
index 000000000..be620388b
--- /dev/null
+++ b/apps/docs/lib/integrations/posthog-instrumentation.test.ts
@@ -0,0 +1,47 @@
+import { describe, expect, it, vi } from "vitest";
+
+type RuntimeContextResolver = (input: unknown) => Record<string, unknown> | undefined;
+
+const captured = vi.hoisted(() => ({
+ runtimeContext: undefined as RuntimeContextResolver | undefined,
+}));
+
+vi.mock("eve/instrumentation/otel", () => ({
+ otelIntegration: (options: { runtimeContext: RuntimeContextResolver }) => {
+ captured.runtimeContext = options.runtimeContext;
+ return options;
+ },
+}));
+vi.mock("@posthog/ai/otel", () => ({ PostHogTraceExporter: class {} }));
+
+// PostHog resolves each span's distinct id from these span attributes, in order:
+// https://github.com/PostHog/posthog/blob/master/rust/capture/src/otel/identity.rs
+const POSTHOG_SPAN_DISTINCT_ID_KEYS = [
+ "ai.telemetry.metadata.posthog_distinct_id",
+ "ai.settings.context.posthog_distinct_id",
+ "posthog.distinct_id",
+ "user.id",
+] as const;
+
+function posthogDistinctId(attributes: Record<string, unknown>): unknown {
+ const key = POSTHOG_SPAN_DISTINCT_ID_KEYS.find((candidate) => attributes[candidate]);
+ return key === undefined ? undefined : attributes[key];
+}
+
+describe("PostHog instrumentation template", () => {
+ it.fails("records the session initiator under a span attribute PostHog reads as the distinct id", async () => {
+ await import("../../registry/instrumentation/posthog");
+ const { runtimeContext } = captured;
+ if (runtimeContext === undefined) throw new Error("template did not register runtimeContext");
+
+ const context = runtimeContext({
+ session: { auth: { current: null, initiator: { principalId: "user-1" } } },
+ });
+ // eve records each runtime-context key on model spans as `ai.settings.context.<key>`.
+ const spanAttributes = Object.fromEntries(
+ Object.entries(context ?? {}).map(([key, value]) => [`ai.settings.context.${key}`, value]),
+ );
+
+ expect(posthogDistinctId(spanAttributes), JSON.stringify(spanAttributes)).toBe("user-1");
+ });
+});