[codex] Structure OTLP export diagnostics (#3407)
Co-authored-by: codex <[email protected]>
This commit is contained in:
1 parent
a14abc6665
commit
f9cf047b6f
3 files changed
+232
-21
No files matched your search
@@ -1,6 +1,12 @@
|
||||
import { describe, expect, it } from "vite-plus/test";
|
||||
|
||||
import { isLoopbackHostname, resolveDevRedirectUrl } from "./http.ts";
|
||||
import {
|
||||
BrowserOtlpTraceCollectionError,
|
||||
BrowserOtlpTraceDecodeError,
|
||||
BrowserOtlpTraceExportError,
|
||||
isLoopbackHostname,
|
||||
resolveDevRedirectUrl,
|
||||
} from "./http.ts";
|
||||
|
||||
describe("http dev routing", () => {
|
||||
it("treats localhost and loopback addresses as local", () => {
|
||||
@@ -25,3 +31,73 @@ describe("http dev routing", () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("browser OTLP diagnostics", () => {
|
||||
it("retains decode causes without retaining the trace payload", () => {
|
||||
const cause = new Error("private trace payload detail");
|
||||
const error = BrowserOtlpTraceDecodeError.fromPayload({ resourceSpans: [] }, cause);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
resourceSpanCount: 0,
|
||||
cause,
|
||||
message: "Failed to decode browser OTLP payload with 0 resource spans.",
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
expect(error).not.toHaveProperty("bodyJson");
|
||||
});
|
||||
|
||||
it("describes malformed trace payloads without inspecting unsafe fields", () => {
|
||||
const cause = new Error("private malformed trace payload detail");
|
||||
const error = BrowserOtlpTraceDecodeError.fromPayload(
|
||||
{ resourceSpans: "private malformed resource spans" },
|
||||
cause,
|
||||
);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
resourceSpanCount: 0,
|
||||
cause,
|
||||
message: "Failed to decode browser OTLP payload with 0 resource spans.",
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
expect(error).not.toHaveProperty("resourceSpans");
|
||||
});
|
||||
|
||||
it("retains local collection causes with a structural record count", () => {
|
||||
const records = [{ type: "private trace record" }];
|
||||
const cause = new Error("private local collector detail");
|
||||
const error = BrowserOtlpTraceCollectionError.fromRecords(records, cause);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
recordCount: 1,
|
||||
cause,
|
||||
message: "Failed to collect 1 browser OTLP trace records locally.",
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
expect(error).not.toHaveProperty("records");
|
||||
});
|
||||
|
||||
it("redacts collector URL credentials while retaining the exact cause", () => {
|
||||
const collectorUrl =
|
||||
"https://collector-user:[email protected]/private/v1/traces?access_token=collector-secret#collector-fragment";
|
||||
const cause = new Error("collector transport failed");
|
||||
const error = BrowserOtlpTraceExportError.fromCollectorUrl(collectorUrl, cause);
|
||||
|
||||
expect(error).toMatchObject({
|
||||
collectorUrlInputLength: collectorUrl.length,
|
||||
collectorUrlProtocol: "https:",
|
||||
collectorUrlHostname: "traces.example.test",
|
||||
cause,
|
||||
});
|
||||
expect(error.cause).toBe(cause);
|
||||
expect(error).not.toHaveProperty("collectorUrl");
|
||||
for (const secret of [
|
||||
"collector-user",
|
||||
"collector-password",
|
||||
"/private/v1/traces",
|
||||
"collector-secret",
|
||||
"collector-fragment",
|
||||
]) {
|
||||
expect(error.message).not.toContain(secret);
|
||||
}
|
||||
});
|
||||
});
|
||||
+130
-20
@@ -5,12 +5,13 @@ import {
|
||||
EnvironmentHttpApi,
|
||||
} from "@t3tools/contracts";
|
||||
import { decodeOtlpTraceRecords } from "@t3tools/shared/observability";
|
||||
import * as Data from "effect/Data";
|
||||
import { getUrlDiagnostics } from "@t3tools/shared/urlDiagnostics";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Path from "effect/Path";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { cast } from "effect/Function";
|
||||
import {
|
||||
HttpBody,
|
||||
@@ -108,10 +109,95 @@ export const serverEnvironmentHttpApiLayer = HttpApiBuilder.group(
|
||||
}),
|
||||
);
|
||||
|
||||
class DecodeOtlpTraceRecordsError extends Data.TaggedError("DecodeOtlpTraceRecordsError")<{
|
||||
readonly cause: unknown;
|
||||
readonly bodyJson: OtlpTracer.TraceData;
|
||||
}> {}
|
||||
function errorDiagnosticTag(cause: unknown): string {
|
||||
if (
|
||||
typeof cause === "object" &&
|
||||
cause !== null &&
|
||||
"_tag" in cause &&
|
||||
typeof cause._tag === "string"
|
||||
) {
|
||||
return cause._tag;
|
||||
}
|
||||
if (cause instanceof Error) return cause.name;
|
||||
return typeof cause;
|
||||
}
|
||||
|
||||
export class BrowserOtlpTraceDecodeError extends Schema.TaggedErrorClass<BrowserOtlpTraceDecodeError>()(
|
||||
"BrowserOtlpTraceDecodeError",
|
||||
{
|
||||
resourceSpanCount: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
static fromPayload(payload: unknown, cause: unknown): BrowserOtlpTraceDecodeError {
|
||||
const resourceSpanCount =
|
||||
typeof payload === "object" &&
|
||||
payload !== null &&
|
||||
"resourceSpans" in payload &&
|
||||
Array.isArray(payload.resourceSpans)
|
||||
? payload.resourceSpans.length
|
||||
: 0;
|
||||
|
||||
return new BrowserOtlpTraceDecodeError({
|
||||
resourceSpanCount,
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Failed to decode browser OTLP payload with ${this.resourceSpanCount} resource spans.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class BrowserOtlpTraceCollectionError extends Schema.TaggedErrorClass<BrowserOtlpTraceCollectionError>()(
|
||||
"BrowserOtlpTraceCollectionError",
|
||||
{
|
||||
recordCount: Schema.Number,
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
static fromRecords(
|
||||
records: ReadonlyArray<unknown>,
|
||||
cause: unknown,
|
||||
): BrowserOtlpTraceCollectionError {
|
||||
return new BrowserOtlpTraceCollectionError({
|
||||
recordCount: records.length,
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
return `Failed to collect ${this.recordCount} browser OTLP trace records locally.`;
|
||||
}
|
||||
}
|
||||
|
||||
export class BrowserOtlpTraceExportError extends Schema.TaggedErrorClass<BrowserOtlpTraceExportError>()(
|
||||
"BrowserOtlpTraceExportError",
|
||||
{
|
||||
collectorUrlInputLength: Schema.Number,
|
||||
collectorUrlProtocol: Schema.optionalKey(Schema.String),
|
||||
collectorUrlHostname: Schema.optionalKey(Schema.String),
|
||||
cause: Schema.Defect(),
|
||||
},
|
||||
) {
|
||||
static fromCollectorUrl(collectorUrl: string, cause: unknown): BrowserOtlpTraceExportError {
|
||||
const diagnostics = getUrlDiagnostics(collectorUrl);
|
||||
return new BrowserOtlpTraceExportError({
|
||||
collectorUrlInputLength: diagnostics.inputLength,
|
||||
...(diagnostics.protocol === undefined ? {} : { collectorUrlProtocol: diagnostics.protocol }),
|
||||
...(diagnostics.hostname === undefined ? {} : { collectorUrlHostname: diagnostics.hostname }),
|
||||
cause,
|
||||
});
|
||||
}
|
||||
|
||||
override get message(): string {
|
||||
const collector =
|
||||
this.collectorUrlHostname === undefined
|
||||
? "the configured collector"
|
||||
: this.collectorUrlHostname;
|
||||
return `Failed to export browser OTLP traces to ${collector} (collector URL input length ${this.collectorUrlInputLength}).`;
|
||||
}
|
||||
}
|
||||
|
||||
export const otlpTracesProxyRouteLayer = HttpRouter.add(
|
||||
"POST",
|
||||
@@ -127,15 +213,31 @@ export const otlpTracesProxyRouteLayer = HttpRouter.add(
|
||||
|
||||
yield* Effect.try({
|
||||
try: () => decodeOtlpTraceRecords(bodyJson),
|
||||
catch: (cause) => new DecodeOtlpTraceRecordsError({ cause, bodyJson }),
|
||||
catch: (cause) => BrowserOtlpTraceDecodeError.fromPayload(bodyJson, cause),
|
||||
}).pipe(
|
||||
Effect.flatMap((records) => browserTraceCollector.record(records)),
|
||||
Effect.catch((cause) =>
|
||||
Effect.logWarning("Failed to decode browser OTLP traces", {
|
||||
cause,
|
||||
bodyJson,
|
||||
}),
|
||||
Effect.flatMap((records) =>
|
||||
browserTraceCollector
|
||||
.record(records)
|
||||
.pipe(
|
||||
Effect.catchDefect((cause) =>
|
||||
Effect.fail(BrowserOtlpTraceCollectionError.fromRecords(records, cause)),
|
||||
),
|
||||
),
|
||||
),
|
||||
Effect.catchTags({
|
||||
BrowserOtlpTraceDecodeError: (error) =>
|
||||
Effect.logWarning(error.message, {
|
||||
errorTag: error._tag,
|
||||
resourceSpanCount: error.resourceSpanCount,
|
||||
causeTag: errorDiagnosticTag(error.cause),
|
||||
}),
|
||||
BrowserOtlpTraceCollectionError: (error) =>
|
||||
Effect.logWarning(error.message, {
|
||||
errorTag: error._tag,
|
||||
recordCount: error.recordCount,
|
||||
causeTag: errorDiagnosticTag(error.cause),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
if (otlpTracesUrl === undefined) {
|
||||
@@ -149,15 +251,23 @@ export const otlpTracesProxyRouteLayer = HttpRouter.add(
|
||||
.pipe(
|
||||
Effect.flatMap(HttpClientResponse.filterStatusOk),
|
||||
Effect.as(HttpServerResponse.empty({ status: 204 })),
|
||||
Effect.tapError((cause) =>
|
||||
Effect.logWarning("Failed to export browser OTLP traces", {
|
||||
cause,
|
||||
otlpTracesUrl,
|
||||
}),
|
||||
),
|
||||
Effect.orElseSucceed(() =>
|
||||
HttpServerResponse.text("Trace export failed.", { status: 502 }),
|
||||
Effect.mapError((cause) =>
|
||||
BrowserOtlpTraceExportError.fromCollectorUrl(otlpTracesUrl, cause),
|
||||
),
|
||||
Effect.catchTags({
|
||||
BrowserOtlpTraceExportError: (error) =>
|
||||
Effect.logWarning(error.message, {
|
||||
errorTag: error._tag,
|
||||
collectorUrlInputLength: error.collectorUrlInputLength,
|
||||
...(error.collectorUrlProtocol === undefined
|
||||
? {}
|
||||
: { collectorUrlProtocol: error.collectorUrlProtocol }),
|
||||
...(error.collectorUrlHostname === undefined
|
||||
? {}
|
||||
: { collectorUrlHostname: error.collectorUrlHostname }),
|
||||
causeTag: errorDiagnosticTag(error.cause),
|
||||
}).pipe(Effect.as(HttpServerResponse.text("Trace export failed.", { status: 502 }))),
|
||||
}),
|
||||
);
|
||||
}).pipe(
|
||||
Effect.catchTags({
|
||||
|
||||
@@ -4027,6 +4027,31 @@ it.layer(NodeServices.layer)("server router seam", (it) => {
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("continues browser OTLP requests when local trace collection defects", () =>
|
||||
Effect.gen(function* () {
|
||||
const payload = yield* makeBrowserOtlpPayload("client.test");
|
||||
|
||||
yield* buildAppUnderTest({
|
||||
layers: {
|
||||
browserTraceCollector: {
|
||||
record: () => Effect.die(new Error("private local collector failure")),
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const response = yield* HttpClient.post("/api/observability/v1/traces", {
|
||||
headers: {
|
||||
cookie: yield* getAuthenticatedSessionCookieHeader(),
|
||||
"content-type": "application/json",
|
||||
},
|
||||
// @effect-diagnostics-next-line preferSchemaOverJson:off
|
||||
body: HttpBody.text(JSON.stringify(payload), "application/json"),
|
||||
});
|
||||
|
||||
assert.equal(response.status, 204);
|
||||
}).pipe(Effect.provide(NodeHttpServer.layerTest)),
|
||||
);
|
||||
|
||||
it.effect("routes websocket rpc server.upsertKeybinding", () =>
|
||||
Effect.gen(function* () {
|
||||
const rule: KeybindingRule = {
|
||||
|
||||
Reference in new issue
Block a user