Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

observability.test.ts

85 lines3.3 KB

1import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer";
2import { expect, it } from "@effect/vitest";
3import * as Deferred from "effect/Deferred";
4import * as Effect from "effect/Effect";
5import * as Redacted from "effect/Redacted";
6import * as Schema from "effect/Schema";
7import * as HttpServer from "effect/unstable/http/HttpServer";
8import * as HttpServerRequest from "effect/unstable/http/HttpServerRequest";
9import * as HttpServerResponse from "effect/unstable/http/HttpServerResponse";
10import type { OtlpTracer } from "effect/unstable/observability";
11​
12import * as EnvironmentConnector from "./environments/EnvironmentConnector.ts";
13import { makeRelayTraceLayer } from "./observability.ts";
14​
15interface ExportedRequest {
16 readonly authorization: string | undefined;
17 readonly body: string;
18 readonly dataset: string | undefined;
20​
21const otlpAttributeValue = (value: {
22 readonly stringValue?: string | null;
23 readonly boolValue?: boolean | null;
24 readonly intValue?: number | null;
25 readonly doubleValue?: number | null;
26}) => value.stringValue ?? value.boolValue ?? value.intValue ?? value.doubleValue;
27​
28const decodeJson = Schema.decodeUnknownEffect(Schema.UnknownFromJsonString);
29​
30it.effect("exports schema error fields as span attributes", () =>
31 Effect.gen(function* () {
32 const exportedRequest = yield* Deferred.make<ExportedRequest>();
33 yield* HttpServer.serveEffect(
34 Effect.gen(function* () {
35 const request = yield* HttpServerRequest.HttpServerRequest;
36 yield* Deferred.succeed(exportedRequest, {
37 authorization: request.headers.authorization,
38 body: yield* request.text,
39 dataset: request.headers["x-axiom-dataset"],
40 });
41 return HttpServerResponse.empty({ status: 204 });
42 }),
43 );
44​
45 yield* Effect.fail(
46 new EnvironmentConnector.EnvironmentConnectNotAuthorized({
47 environmentId: "environment-1",
48 operation: "connect",
49 reason: "managed_endpoint_allocation_not_ready",
50 }),
51 ).pipe(
52 Effect.withSpan("relay.test.schema_error"),
53 Effect.exit,
54 Effect.provide(
55 makeRelayTraceLayer({
56 tracesEndpoint: "/v1/traces",
57 tracesDatasetName: "relay-test-traces",
58 ingestToken: Redacted.make("test-token"),
59 }),
60 ),
61 );
62​
63 const request = yield* Deferred.await(exportedRequest).pipe(Effect.timeout("1 second"));
64 const payload = (yield* decodeJson(request.body)) as OtlpTracer.TraceData;
65 const span = payload.resourceSpans
66 .flatMap((resourceSpan) => resourceSpan.scopeSpans)
67 .flatMap((scopeSpan) => scopeSpan.spans)
68 .find((candidate) => candidate.name === "relay.test.schema_error");
69 const attributes = Object.fromEntries(
70 (span?.attributes ?? []).map((attribute) => [
71 attribute.key,
72 otlpAttributeValue(attribute.value),
73 ]),
74 );
75​
76 expect(request.authorization).toBe("Bearer test-token");
77 expect(request.dataset).toBe("relay-test-traces");
78 expect(attributes).toMatchObject({
79 "error.type": "EnvironmentConnectNotAuthorized",
80 "error.environmentId": "environment-1",
81 "error.operation": "connect",
82 "error.reason": "managed_endpoint_allocation_not_ready",
83 });
84 }).pipe(Effect.provide(NodeHttpServer.layerTest), Effect.scoped),
85);