Skip to content

Commit 56f02c4

Browse files
authored
fix(otel): fail closed when configured proxies are invalid (#118612)
1 parent 06957a6 commit 56f02c4

3 files changed

Lines changed: 78 additions & 24 deletions

File tree

‎extensions/diagnostics-otel/src/service-exporter.ts‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,7 @@ export function resolveOtelHttpAgentOptions(params: {
119119
const agent = createNodeProxyAgent({ mode: "env", targetUrl: url, agentOptions });
120120
return agent ? () => agent : undefined;
121121
} catch {
122-
logger.warn(
123-
`diagnostics-otel: env proxy agent unavailable for OTLP ${signalIdentifier.toLowerCase()} exporter; falling back to default Node agent`,
124-
);
125-
return undefined;
122+
throw new Error("Configured telemetry proxy is invalid or unsupported; refusing direct export");
126123
}
127124
}
128125

‎extensions/diagnostics-otel/src/service.test.ts‎

Lines changed: 75 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,32 +1869,89 @@ describe("diagnostics-otel service", () => {
18691869
}
18701870
});
18711871

1872-
test("falls back to default OTLP agents when env proxy agent creation fails", async () => {
1872+
test.each([
1873+
["traces", { traces: true }, "unsupported proxy protocol"],
1874+
["metrics", { metrics: true }, "invalid proxy URL"],
1875+
["logs", { logs: true }, "unsupported proxy protocol"],
1876+
] as const)(
1877+
"refuses direct %s export when the configured proxy cannot initialize",
1878+
async (_signal, signals, errorMessage) => {
1879+
createNodeProxyAgentMock.mockImplementation(() => {
1880+
throw new Error(errorMessage);
1881+
});
1882+
1883+
await expect(
1884+
startOtelService({ endpoint: "https://collector.example.com/otlp", ...signals }),
1885+
).rejects.toThrow(
1886+
"Configured telemetry proxy is invalid or unsupported; refusing direct export",
1887+
);
1888+
1889+
expect(traceExporterCtor).not.toHaveBeenCalled();
1890+
expect(metricExporterCtor).not.toHaveBeenCalled();
1891+
expect(logExporterCtor).not.toHaveBeenCalled();
1892+
},
1893+
);
1894+
1895+
test("redacts proxy credentials from telemetry startup failures", async () => {
1896+
const proxyPassword = "qa-otel-proxy-password-sentinel";
18731897
createNodeProxyAgentMock.mockImplementation(() => {
1874-
throw new Error("unsupported proxy protocol");
1898+
throw new Error(`Invalid proxy URL: "https://operator:${proxyPassword}@proxy.example.com"`);
18751899
});
18761900

1877-
const { ctx } = await startOtelService({
1901+
const failure = await startOtelService({
18781902
endpoint: "https://collector.example.com/otlp",
18791903
traces: true,
1880-
metrics: true,
1881-
logs: true,
1882-
});
1904+
}).catch((error: unknown) => error);
18831905

1884-
expect(firstExporterOptions(traceExporterCtor).httpAgentOptions).toBeUndefined();
1885-
expect(firstExporterOptions(metricExporterCtor).httpAgentOptions).toBeUndefined();
1886-
expect(firstExporterOptions(logExporterCtor).httpAgentOptions).toBeUndefined();
1887-
expect(ctx.logger.warn).toHaveBeenCalledWith(
1888-
"diagnostics-otel: env proxy agent unavailable for OTLP traces exporter; falling back to default Node agent",
1889-
);
1890-
expect(ctx.logger.warn).toHaveBeenCalledWith(
1891-
"diagnostics-otel: env proxy agent unavailable for OTLP metrics exporter; falling back to default Node agent",
1892-
);
1893-
expect(ctx.logger.warn).toHaveBeenCalledWith(
1894-
"diagnostics-otel: env proxy agent unavailable for OTLP logs exporter; falling back to default Node agent",
1895-
);
1906+
expect(failure).toBeInstanceOf(Error);
1907+
expect(failure).toMatchObject({
1908+
message: "Configured telemetry proxy is invalid or unsupported; refusing direct export",
1909+
});
1910+
expect(failure).not.toHaveProperty("cause");
1911+
expect(String(failure)).not.toContain(proxyPassword);
1912+
expect(traceExporterCtor).not.toHaveBeenCalled();
18961913
});
18971914

1915+
test.each([
1916+
{
1917+
disabledSignal: "traces",
1918+
enabledSignal: "metrics",
1919+
disabledEndpoint: "tracesEndpoint",
1920+
signals: { traces: false, metrics: true },
1921+
},
1922+
{
1923+
disabledSignal: "metrics",
1924+
enabledSignal: "traces",
1925+
disabledEndpoint: "metricsEndpoint",
1926+
signals: { traces: true, metrics: false },
1927+
},
1928+
] as const)(
1929+
"does not resolve proxy settings for disabled $disabledSignal export",
1930+
async ({ disabledSignal, enabledSignal, disabledEndpoint, signals }) => {
1931+
createNodeProxyAgentMock.mockImplementation(({ targetUrl }: { targetUrl: string }) => {
1932+
if (targetUrl.includes(`disabled-${disabledSignal}.example.com`)) {
1933+
throw new Error("invalid disabled-signal proxy");
1934+
}
1935+
return nodeProxyAgent;
1936+
});
1937+
1938+
await startOtelService({
1939+
endpoint: "https://collector.example.com/otlp",
1940+
...signals,
1941+
configure: (ctx) => {
1942+
ctx.config.diagnostics!.otel![disabledEndpoint] =
1943+
`https://disabled-${disabledSignal}.example.com/otlp`;
1944+
},
1945+
});
1946+
1947+
expect(createNodeProxyAgentCalls()).toEqual([
1948+
expect.objectContaining({
1949+
targetUrl: `https://collector.example.com/otlp/v1/${enabledSignal}`,
1950+
}),
1951+
]);
1952+
},
1953+
);
1954+
18981955
test("leaves OTLP HTTP exporters on their default agents when env proxy is bypassed", async () => {
18991956
await startOtelService({
19001957
endpoint: "https://collector.example.com/otlp",

‎extensions/diagnostics-otel/src/service.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,12 +175,12 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
175175
path: "v1/metrics",
176176
});
177177
const traceHttpAgentOptions = resolveOtelHttpAgentOptions({
178-
url: traceUrl,
178+
url: tracesEnabled ? traceUrl : undefined,
179179
signalIdentifier: "TRACES",
180180
logger: ctx.logger,
181181
});
182182
const metricHttpAgentOptions = resolveOtelHttpAgentOptions({
183-
url: metricUrl,
183+
url: metricsEnabled ? metricUrl : undefined,
184184
signalIdentifier: "METRICS",
185185
logger: ctx.logger,
186186
});

0 commit comments

Comments
 (0)