fix(web): client spans reach the trace proxy again (#12332)
Signed-off-by: Yordis Prieto <[email protected]>
This commit is contained in:
1 parent
ccad9f69aa
commit
bd5dc58c65
4 files changed
+64
-5
No files matched your search
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from "@effect/vitest";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
|
||||
import * as ClientTracer from "../observability/clientTracer";
|
||||
import { runtime } from "./runtime";
|
||||
|
||||
describe("web runtime", () => {
|
||||
it("routes client spans to the exporter client tracing configured", async () => {
|
||||
const exported: Array<string> = [];
|
||||
ClientTracer.setDelegate(
|
||||
Tracer.make({
|
||||
span(options) {
|
||||
exported.push(options.name);
|
||||
return new Tracer.NativeSpan(options);
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
try {
|
||||
await runtime.runPromise(Effect.void.pipe(Effect.withSpan("client.work")));
|
||||
} finally {
|
||||
ClientTracer.setDelegate(null);
|
||||
}
|
||||
|
||||
expect(exported).toEqual(["client.work"]);
|
||||
});
|
||||
});
|
||||
@@ -11,6 +11,7 @@ import { primaryEnvironmentHttpLayer } from "../environments/primary/httpLayer";
|
||||
import { browserCryptoLayer } from "../cloud/dpop";
|
||||
import { managedRelayClientLayer } from "../cloud/managedRelayLayer";
|
||||
import { resolveCloudPublicConfig, resolveRelayTracingConfig } from "../cloud/publicConfig";
|
||||
import * as ClientTracer from "../observability/clientTracer";
|
||||
|
||||
function configuredRelayUrl(): string {
|
||||
return resolveCloudPublicConfig().relayUrl ?? "http://relay.invalid";
|
||||
@@ -29,6 +30,7 @@ type RuntimeLayerSource =
|
||||
| typeof browserCryptoLayer
|
||||
| typeof Socket.layerWebSocketConstructorGlobal
|
||||
| typeof relayTracingLayer
|
||||
| typeof ClientTracer.layer
|
||||
| ReturnType<typeof managedRelayClientLayer>;
|
||||
|
||||
const primaryHttpRuntime = ManagedRuntime.make(
|
||||
@@ -56,6 +58,7 @@ const runtimeLayer = Layer.mergeAll(
|
||||
httpClientLayer,
|
||||
browserCryptoLayer,
|
||||
Socket.layerWebSocketConstructorGlobal,
|
||||
ClientTracer.layer,
|
||||
relayTracingLayer,
|
||||
managedRelayClientLayer(configuredRelayUrl()).pipe(
|
||||
Layer.provide(Layer.mergeAll(httpClientLayer, browserCryptoLayer)),
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
|
||||
let delegate: Tracer.Tracer | null = null;
|
||||
|
||||
/**
|
||||
* Points the installed tracer at the exporter `configureClientTracing` just built,
|
||||
* or at nothing while no exporter is configured.
|
||||
*/
|
||||
export function setDelegate(next: Tracer.Tracer | null): void {
|
||||
delegate = next;
|
||||
}
|
||||
|
||||
export function hasDelegate(): boolean {
|
||||
return delegate !== null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Installed once when the client runtime is built, before any exporter exists, so
|
||||
* client spans keep flowing to whatever exporter is configured later on.
|
||||
*/
|
||||
export const layer = Layer.succeed(
|
||||
Tracer.Tracer,
|
||||
Tracer.make({
|
||||
span(options) {
|
||||
return delegate?.span(options) ?? new Tracer.NativeSpan(options);
|
||||
},
|
||||
}),
|
||||
);
|
||||
@@ -2,13 +2,13 @@ import * as Exit from "effect/Exit";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as ManagedRuntime from "effect/ManagedRuntime";
|
||||
import * as Scope from "effect/Scope";
|
||||
import * as Tracer from "effect/Tracer";
|
||||
import { HttpClient } from "effect/unstable/http";
|
||||
import { OtlpExporter, OtlpSerialization, OtlpTracer } from "effect/unstable/observability";
|
||||
|
||||
import { settleAsyncResult, squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
|
||||
import { safeErrorLogAttributes } from "@t3tools/client-runtime/errors";
|
||||
import { resolvePrimaryEnvironmentHttpUrl } from "../environments/primary";
|
||||
import * as ClientTracer from "./clientTracer";
|
||||
import { primaryEnvironmentHttpLayer } from "../environments/primary/httpLayer";
|
||||
import { isElectron } from "../env";
|
||||
import { APP_VERSION } from "~/branding";
|
||||
@@ -30,7 +30,6 @@ const delegateRuntimeLayer = Layer.mergeAll(
|
||||
Layer.succeed(HttpClient.TracerDisabledWhen, () => true),
|
||||
);
|
||||
|
||||
let activeDelegate: Tracer.Tracer | null = null;
|
||||
let activeRuntime: ManagedRuntime.ManagedRuntime<never, never> | null = null;
|
||||
let activeScope: Scope.Closeable | null = null;
|
||||
let activeConfigKey: string | null = null;
|
||||
@@ -54,7 +53,7 @@ async function applyClientTracingConfig(config: ClientTracingConfig): Promise<vo
|
||||
const exportIntervalMs = Math.max(10, config.exportIntervalMs ?? DEFAULT_EXPORT_INTERVAL_MS);
|
||||
const nextConfigKey = `${otlpTracesUrl}|${exportIntervalMs}`;
|
||||
|
||||
if (activeConfigKey === nextConfigKey && activeDelegate !== null) {
|
||||
if (activeConfigKey === nextConfigKey && ClientTracer.hasDelegate()) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -64,7 +63,7 @@ async function applyClientTracingConfig(config: ClientTracingConfig): Promise<vo
|
||||
const previousRuntime = activeRuntime;
|
||||
const previousScope = activeScope;
|
||||
|
||||
activeDelegate = null;
|
||||
ClientTracer.setDelegate(null);
|
||||
activeRuntime = null;
|
||||
activeScope = null;
|
||||
|
||||
@@ -106,7 +105,7 @@ async function applyClientTracingConfig(config: ClientTracingConfig): Promise<vo
|
||||
return;
|
||||
}
|
||||
|
||||
activeDelegate = delegateResult.value;
|
||||
ClientTracer.setDelegate(delegateResult.value);
|
||||
activeRuntime = runtime;
|
||||
activeScope = scope;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user