fix(diagnostics-otel): ignore blank protocol env overrides (#110674)

This commit is contained in:
Wynne668
2026-07-19 04:38:44 +08:00
committed by GitHub
parent 0f5d030cb1
commit a9fb5b4e3f
2 changed files with 47 additions and 1 deletions

View File

@@ -210,6 +210,7 @@ const PROTO_KEY = "__proto__";
const MAX_TEST_OTEL_CONTENT_ATTRIBUTE_CHARS = 128 * 1024;
const OTEL_TRUNCATED_SUFFIX_MAX_CHARS = 20;
const ORIGINAL_OPENCLAW_OTEL_PRELOADED = process.env.OPENCLAW_OTEL_PRELOADED;
const ORIGINAL_OTEL_EXPORTER_OTLP_PROTOCOL = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
const ORIGINAL_OTEL_EXPORTER_OTLP_TRACES_ENDPOINT = process.env.OTEL_EXPORTER_OTLP_TRACES_ENDPOINT;
const ORIGINAL_OTEL_EXPORTER_OTLP_METRICS_ENDPOINT =
process.env.OTEL_EXPORTER_OTLP_METRICS_ENDPOINT;
@@ -563,6 +564,7 @@ describe("diagnostics-otel service", () => {
beforeEach(() => {
resetDiagnosticEventsForTest();
delete process.env.OPENCLAW_OTEL_PRELOADED;
delete process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
delete process.env.OTEL_SEMCONV_STABILITY_OPT_IN;
telemetryState.counters.clear();
telemetryState.histograms.clear();
@@ -598,6 +600,11 @@ describe("diagnostics-otel service", () => {
} else {
process.env.OPENCLAW_OTEL_PRELOADED = ORIGINAL_OPENCLAW_OTEL_PRELOADED;
}
if (ORIGINAL_OTEL_EXPORTER_OTLP_PROTOCOL === undefined) {
delete process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
} else {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = ORIGINAL_OTEL_EXPORTER_OTLP_PROTOCOL;
}
if (ORIGINAL_OTEL_SEMCONV_STABILITY_OPT_IN === undefined) {
delete process.env.OTEL_SEMCONV_STABILITY_OPT_IN;
} else {
@@ -1224,6 +1231,44 @@ describe("diagnostics-otel service", () => {
}
});
test("ignores blank OTLP protocol env overrides", async () => {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = " ";
const service = createDiagnosticsOtelService();
const ctx = createOtelContext(OTEL_TEST_ENDPOINT, { traces: true, metrics: true });
if (ctx.config.diagnostics?.otel) {
delete ctx.config.diagnostics.otel.protocol;
}
try {
await service.start(ctx);
expect(traceExporterCtor).toHaveBeenCalledOnce();
expect(metricExporterCtor).toHaveBeenCalledOnce();
expect(ctx.logger.warn).not.toHaveBeenCalledWith(
"diagnostics-otel: unsupported protocol ",
);
} finally {
await service.stop?.(ctx);
}
});
test("preserves nonblank OTLP protocol env overrides", async () => {
process.env.OTEL_EXPORTER_OTLP_PROTOCOL = " http/protobuf ";
const service = createDiagnosticsOtelService();
const ctx = createOtelContext(OTEL_TEST_ENDPOINT, { traces: true, metrics: true });
if (ctx.config.diagnostics?.otel) {
delete ctx.config.diagnostics.otel.protocol;
}
await service.start(ctx);
expect(traceExporterCtor).not.toHaveBeenCalled();
expect(metricExporterCtor).not.toHaveBeenCalled();
expect(ctx.logger.warn).toHaveBeenCalledWith(
"diagnostics-otel: unsupported protocol http/protobuf ",
);
});
test("exports trusted security events as stdout JSONL logs", async () => {
const service = createDiagnosticsOtelService();
const ctx = createOtelContext("", { logs: true, logsExporter: "stdout" });

View File

@@ -129,7 +129,8 @@ export function createDiagnosticsOtelService(): OpenClawPluginService {
return;
}
const protocol = otel.protocol ?? process.env.OTEL_EXPORTER_OTLP_PROTOCOL ?? "http/protobuf";
const envProtocol = process.env.OTEL_EXPORTER_OTLP_PROTOCOL;
const protocol = otel.protocol ?? (envProtocol?.trim() ? envProtocol : "http/protobuf");
if (otlpSignals.length > 0 && protocol !== "http/protobuf") {
emitForSignals(otlpSignals, {
exporter: "diagnostics-otel",