Skip to content

Commit 54e2f65

Browse files
refactor(observability): use Effect's UrlParams.CurrentRedactedNames for span redaction
Effect 4.0.5 already redacts url.full and url.query on HTTP client and server spans by name. Add our credential params to that reference next to the header names, and drop the custom tracer wrapper and its wiring. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent a34cae1 commit 54e2f65

8 files changed

Lines changed: 54 additions & 71 deletions

File tree

‎apps/desktop/src/app/DesktopObservability.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import {
77
type SignalExport,
88
} from "@t3tools/shared/observability";
99
import * as HostProcess from "@t3tools/shared/HostProcess";
10-
import * as HttpObservability from "@t3tools/shared/httpObservability";
1110
import * as SharedObservability from "@t3tools/shared/observability";
1211
import * as OtelEnvironment from "@t3tools/shared/otelEnvironment";
1312
import { releaseChannelOf, releaseResourceAttributes } from "@t3tools/shared/releaseResource";
@@ -677,7 +676,7 @@ const layerTelemetry = Layer.unwrap(
677676
...(delegate ? { delegate } : {}),
678677
});
679678

680-
return Layer.succeed(Tracer.Tracer, HttpObservability.redactUrlAttributes(tracer));
679+
return Layer.succeed(Tracer.Tracer, tracer);
681680
}),
682681
).pipe(Layer.provide(OtlpExporter.layerFlusher));
683682

‎apps/server/src/observability/Observability.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ export const layer = Layer.unwrap(
7676
yield* heapAttribution.run;
7777

7878
return Layer.mergeAll(
79-
Layer.succeed(
80-
Tracer.Tracer,
81-
HttpObservability.redactUrlAttributes(heapAttribution.tracer),
82-
),
79+
Layer.succeed(Tracer.Tracer, heapAttribution.tracer),
8380
BrowserTraceCollector.layer(sink),
8481
);
8582
}),

‎apps/web/src/observability/clientTracer.ts‎

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import * as HttpObservability from "@t3tools/shared/httpObservability";
21
import * as Layer from "effect/Layer";
32
import * as Tracer from "effect/Tracer";
43

@@ -20,11 +19,9 @@ export function hasDelegate(): boolean {
2019
// Install before exporters exist so spans reach whichever exporter is configured later.
2120
export const layer = Layer.succeed(
2221
Tracer.Tracer,
23-
HttpObservability.redactUrlAttributes(
24-
Tracer.make({
25-
span(options) {
26-
return delegate?.span(options) ?? new Tracer.NativeSpan(options);
27-
},
28-
}),
29-
),
22+
Tracer.make({
23+
span(options) {
24+
return delegate?.span(options) ?? new Tracer.NativeSpan(options);
25+
},
26+
}),
3027
);

‎infra/relay/src/observability.ts‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import * as Alchemy from "alchemy";
55
import * as Axiom from "@alchemy.run/axiom";
66
import * as Cloudflare from "@alchemy.run/cloudflare";
77
import * as Output from "alchemy/Output";
8-
import * as HttpObservability from "@t3tools/shared/httpObservability";
98
import * as Cause from "effect/Cause";
109
import * as Effect from "effect/Effect";
1110
import * as Exit from "effect/Exit";
@@ -289,13 +288,15 @@ class RelayTraceSpan implements Tracer.Span {
289288
}
290289
}
291290

292-
const relayTracer = (delegate: Tracer.Tracer): Tracer.Tracer =>
293-
HttpObservability.redactUrlAttributes(
294-
Tracer.make({
295-
span: (options) => new RelayTraceSpan(delegate.span(options)),
296-
...(delegate.context ? { context: delegate.context } : {}),
297-
}),
298-
);
291+
const withSchemaErrorAttributes = (delegate: Tracer.Tracer): Tracer.Tracer =>
292+
Tracer.make({
293+
span: (options) => new RelayTraceSpan(delegate.span(options)),
294+
...(delegate.context ? { context: delegate.context } : {}),
295+
});
299296

300-
export const withRelayTracer = <A, E, R>(effect: Effect.Effect<A, E, R>): Effect.Effect<A, E, R> =>
301-
Effect.flatMap(Effect.tracer, (tracer) => effect.pipe(Effect.withTracer(relayTracer(tracer))));
297+
export const withSchemaErrorSpanAttributes = <A, E, R>(
298+
effect: Effect.Effect<A, E, R>,
299+
): Effect.Effect<A, E, R> =>
300+
Effect.flatMap(Effect.tracer, (tracer) =>
301+
effect.pipe(Effect.withTracer(withSchemaErrorAttributes(tracer))),
302+
);

‎infra/relay/src/worker.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,6 @@ export const layer = Api.make(
459459
Effect.withSpan("relay.apn_delivery_queue.process_message"),
460460
),
461461
),
462-
Observability.withRelayTracer,
463462
Effect.provide(layerRuntime),
464463
),
465464
);
@@ -477,7 +476,6 @@ export const layer = Api.make(
477476
stream.pipe(
478477
Stream.withSpan("relay.fcm_delivery_queue.process_batch"),
479478
Stream.runForEach(FcmDeliveryQueueConsumer.processMessage),
480-
Observability.withRelayTracer,
481479
Effect.provide(layerRuntime),
482480
),
483481
);
@@ -532,7 +530,7 @@ export const layer = Api.make(
532530
{ concurrency: 3, discard: true },
533531
).pipe(
534532
Effect.withSpan("relay.cron.prune_expired_state"),
535-
Observability.withRelayTracer,
533+
Observability.withSchemaErrorSpanAttributes,
536534
Effect.provide(layerRuntime),
537535
),
538536
);
@@ -553,7 +551,9 @@ export const layer = Api.make(
553551
Effect.provideService(HttpRouter.RouterConfig, RELAY_HTTP_ROUTER_CONFIG),
554552
withoutCapturedParentSpan,
555553
Effect.map((httpEffect) =>
556-
traceRelayHttpRequestWith(httpEffect, Layer.empty).pipe(Observability.withRelayTracer),
554+
traceRelayHttpRequestWith(httpEffect, Layer.empty).pipe(
555+
Observability.withSchemaErrorSpanAttributes,
556+
),
557557
),
558558
);
559559

‎packages/shared/src/httpObservability.test.ts‎

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,18 @@ import * as Tracer from "effect/Tracer";
44
import * as HttpClient from "effect/http/HttpClient";
55
import * as HttpClientResponse from "effect/http/HttpClientResponse";
66

7-
import { redactUrlAttributes } from "./httpObservability.ts";
7+
import * as HttpObservability from "./httpObservability.ts";
88

9-
describe("redactUrlAttributes", () => {
9+
describe("HttpObservability.layer", () => {
1010
it.effect("redacts credential query parameters on HTTP client spans", () => {
1111
const spans: Array<Tracer.NativeSpan> = [];
12-
const tracer = redactUrlAttributes(
13-
Tracer.make({
14-
span: (options) => {
15-
const span = new Tracer.NativeSpan(options);
16-
spans.push(span);
17-
return span;
18-
},
19-
}),
20-
);
12+
const tracer = Tracer.make({
13+
span: (options) => {
14+
const span = new Tracer.NativeSpan(options);
15+
spans.push(span);
16+
return span;
17+
},
18+
});
2119
const client = HttpClient.make((request) =>
2220
Effect.succeed(HttpClientResponse.fromWeb(request, new Response("ok"))),
2321
);
@@ -30,6 +28,6 @@ describe("redactUrlAttributes", () => {
3028
expect(span.attributes.get("url.query")).toBe(
3129
"wsTicket=REDACTED&clientOs=linux&dpop=REDACTED",
3230
);
33-
}).pipe(Effect.withTracer(tracer));
31+
}).pipe(Effect.provide(HttpObservability.layer), Effect.withTracer(tracer));
3432
});
3533
});
Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,26 @@
11
import * as Effect from "effect/Effect";
22
import * as Layer from "effect/Layer";
3-
import * as Tracer from "effect/Tracer";
43
import * as Headers from "effect/http/Headers";
4+
import * as UrlParams from "effect/http/UrlParams";
55

6-
export const layer = Layer.effect(
7-
Headers.CurrentRedactedNames,
8-
// `expo-session` carries the `eas login` session secret on Expo API requests.
9-
Effect.map(Headers.CurrentRedactedNames, (names) => [...names, "dpop", "expo-session"]),
6+
export const layer = Layer.mergeAll(
7+
Layer.effect(
8+
Headers.CurrentRedactedNames,
9+
// `expo-session` carries the `eas login` session secret on Expo API requests.
10+
Effect.map(Headers.CurrentRedactedNames, (names) => [...names, "dpop", "expo-session"]),
11+
),
12+
Layer.effect(
13+
UrlParams.CurrentRedactedNames,
14+
// Socket tickets and proofs, pairing tokens, and OAuth codes travel in query strings.
15+
Effect.map(UrlParams.CurrentRedactedNames, (names) => [
16+
...names,
17+
"wsTicket",
18+
"dpop",
19+
"token",
20+
"pairingUrl",
21+
"code",
22+
"state",
23+
"challenge",
24+
]),
25+
),
1026
);
11-
12-
/** Query parameters that carry credentials: socket tickets and proofs, pairing tokens, OAuth codes. */
13-
const REDACTED_URL_PARAMS = /(^|[?&])(wsTicket|dpop|token|pairingUrl|code|state|challenge)=[^&#]*/g;
14-
15-
const redactUrlAttribute = (key: string, value: unknown): unknown =>
16-
(key === "url.full" || key === "url.query") && typeof value === "string"
17-
? value.replace(REDACTED_URL_PARAMS, "$1$2=REDACTED")
18-
: value;
19-
20-
/** Effect's HTTP client and server spans record `url.full` and `url.query` verbatim. */
21-
export const redactUrlAttributes = (delegate: Tracer.Tracer): Tracer.Tracer =>
22-
Tracer.make({
23-
span(options) {
24-
const span = delegate.span(options);
25-
const attribute = span.attribute.bind(span);
26-
span.attribute = (key, value) => attribute(key, redactUrlAttribute(key, value));
27-
return span;
28-
},
29-
...(delegate.context ? { context: delegate.context } : {}),
30-
});

‎packages/shared/src/relayTracing.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import * as Tracer from "effect/Tracer";
88
import type { HttpClient } from "effect/http";
99
import { OtlpSerialization, OtlpTracer } from "effect/observability";
1010

11-
import { redactUrlAttributes } from "./httpObservability.ts";
12-
1311
export interface RelayClientTracingConfig {
1412
readonly tracesUrl: string;
1513
readonly tracesDataset: string;
@@ -180,9 +178,6 @@ export function layer(
180178

181179
return Layer.effect(
182180
RelayClientTracer,
183-
Tracer.Tracer.pipe(
184-
Effect.map((tracer) => nonInterferingTracer(redactUrlAttributes(tracer))),
185-
Effect.asSome,
186-
),
181+
Tracer.Tracer.pipe(Effect.map(nonInterferingTracer), Effect.asSome),
187182
).pipe(Layer.provide(layerTracer));
188183
}

0 commit comments

Comments
 (0)