@neutrome/open-ai-router 0.5.2 → 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/otlp.ts +55 -20
- package/src/telemetry.test.ts +27 -10
package/package.json
CHANGED
package/src/otlp.ts
CHANGED
|
@@ -14,6 +14,12 @@ type OtlpAttribute = {
|
|
|
14
14
|
key: string;
|
|
15
15
|
value: { stringValue?: string; intValue?: string; boolValue?: boolean };
|
|
16
16
|
};
|
|
17
|
+
type OtlpLogRecord = {
|
|
18
|
+
timeUnixNano: string;
|
|
19
|
+
severityText: "ERROR";
|
|
20
|
+
body: { stringValue: string };
|
|
21
|
+
attributes: OtlpAttribute[];
|
|
22
|
+
};
|
|
17
23
|
|
|
18
24
|
export async function exportOtlp(
|
|
19
25
|
env: Environment,
|
|
@@ -30,34 +36,63 @@ export async function exportOtlp(
|
|
|
30
36
|
"content-type": "application/json",
|
|
31
37
|
...headersFor(env, signal),
|
|
32
38
|
},
|
|
33
|
-
body: JSON.stringify(
|
|
34
|
-
resourceSpans: [
|
|
35
|
-
{
|
|
36
|
-
resource: {
|
|
37
|
-
attributes: attributes({
|
|
38
|
-
"service.name":
|
|
39
|
-
stringEnv(env, "OTEL_SERVICE_NAME") ?? "neutrome-router",
|
|
40
|
-
}),
|
|
41
|
-
},
|
|
42
|
-
scopeSpans: [
|
|
43
|
-
{ scope: { name: "@neutrome/open-ai-router" }, spans },
|
|
44
|
-
],
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
}),
|
|
39
|
+
body: JSON.stringify(otlpPayload(env, signal, spans)),
|
|
48
40
|
});
|
|
49
41
|
if (!response.ok) {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
});
|
|
42
|
+
const responseBody = (await response.text()).slice(0, 2_000);
|
|
43
|
+
console.error(
|
|
44
|
+
`[telemetry] OTLP ${signal} export rejected: ${response.status} ${response.statusText}${responseBody ? `: ${responseBody}` : ""}`,
|
|
45
|
+
);
|
|
55
46
|
}
|
|
56
47
|
} catch (error) {
|
|
57
48
|
console.error("[telemetry] OTLP export failed", error);
|
|
58
49
|
}
|
|
59
50
|
}
|
|
60
51
|
|
|
52
|
+
function otlpPayload(
|
|
53
|
+
env: Environment,
|
|
54
|
+
signal: "traces" | "errors",
|
|
55
|
+
spans: OtlpSpan[],
|
|
56
|
+
): Record<string, unknown> {
|
|
57
|
+
const resource = {
|
|
58
|
+
attributes: attributes({
|
|
59
|
+
"service.name": stringEnv(env, "OTEL_SERVICE_NAME") ?? "neutrome-router",
|
|
60
|
+
}),
|
|
61
|
+
};
|
|
62
|
+
if (signal === "traces") {
|
|
63
|
+
return {
|
|
64
|
+
resourceSpans: [
|
|
65
|
+
{
|
|
66
|
+
resource,
|
|
67
|
+
scopeSpans: [{ scope: { name: "@neutrome/open-ai-router" }, spans }],
|
|
68
|
+
},
|
|
69
|
+
],
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
return {
|
|
73
|
+
resourceLogs: [
|
|
74
|
+
{
|
|
75
|
+
resource,
|
|
76
|
+
scopeLogs: [
|
|
77
|
+
{
|
|
78
|
+
scope: { name: "@neutrome/open-ai-router" },
|
|
79
|
+
logRecords: spans.map(errorLogRecord),
|
|
80
|
+
},
|
|
81
|
+
],
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function errorLogRecord(span: OtlpSpan): OtlpLogRecord {
|
|
88
|
+
return {
|
|
89
|
+
timeUnixNano: span.endTimeUnixNano,
|
|
90
|
+
severityText: "ERROR",
|
|
91
|
+
body: { stringValue: span.status?.message ?? span.name },
|
|
92
|
+
attributes: span.attributes,
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
61
96
|
export function rootOtlpSpan(input: {
|
|
62
97
|
traceId: string;
|
|
63
98
|
spanId: string;
|
package/src/telemetry.test.ts
CHANGED
|
@@ -45,15 +45,34 @@ describe("createRouterTelemetry", () => {
|
|
|
45
45
|
expect(fetchMock.mock.calls[1]?.[1]?.headers).toMatchObject({
|
|
46
46
|
authorization: "Bearer error-token",
|
|
47
47
|
});
|
|
48
|
+
expect(
|
|
49
|
+
JSON.parse(String(fetchMock.mock.calls[1]?.[1]?.body)),
|
|
50
|
+
).toMatchObject({
|
|
51
|
+
resourceLogs: [
|
|
52
|
+
{
|
|
53
|
+
scopeLogs: [
|
|
54
|
+
{
|
|
55
|
+
logRecords: [
|
|
56
|
+
{
|
|
57
|
+
severityText: "ERROR",
|
|
58
|
+
body: { stringValue: "upstream unavailable" },
|
|
59
|
+
},
|
|
60
|
+
],
|
|
61
|
+
},
|
|
62
|
+
],
|
|
63
|
+
},
|
|
64
|
+
],
|
|
65
|
+
});
|
|
48
66
|
fetchMock.mockRestore();
|
|
49
67
|
});
|
|
50
68
|
|
|
51
69
|
it("logs a rejected exporter response without exposing credentials", async () => {
|
|
52
|
-
const fetchMock = vi
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
)
|
|
70
|
+
const fetchMock = vi.spyOn(globalThis, "fetch").mockResolvedValue(
|
|
71
|
+
new Response("invalid authentication", {
|
|
72
|
+
status: 401,
|
|
73
|
+
statusText: "Unauthorized",
|
|
74
|
+
}),
|
|
75
|
+
);
|
|
57
76
|
const errorSpy = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
58
77
|
const telemetry = createRouterTelemetry({
|
|
59
78
|
OTEL_EXPORTER_OTLP_TRACES_ENDPOINT: "https://traces.example/v1/traces",
|
|
@@ -67,11 +86,9 @@ describe("createRouterTelemetry", () => {
|
|
|
67
86
|
|
|
68
87
|
await telemetry.flush();
|
|
69
88
|
|
|
70
|
-
expect(errorSpy).toHaveBeenCalledWith(
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
statusText: "Unauthorized",
|
|
74
|
-
});
|
|
89
|
+
expect(errorSpy).toHaveBeenCalledWith(
|
|
90
|
+
"[telemetry] OTLP traces export rejected: 401 Unauthorized: invalid authentication",
|
|
91
|
+
);
|
|
75
92
|
fetchMock.mockRestore();
|
|
76
93
|
errorSpy.mockRestore();
|
|
77
94
|
});
|