Skip to content

Commit

Permalink
fix(exporters): use parseHeaders() to ensure header-values are not 'u…
Browse files Browse the repository at this point in the history
…ndefined' (#4540)

* fix(exporters): use parseHeaders() to ensure header-values are not 'undefined'

* chore: changelog

* fixup! fix(exporters): use parseHeaders() to ensure header-values are not 'undefined'
  • Loading branch information
pichlermarc authored Mar 14, 2024
1 parent 1b4999f commit 3a426e8
Show file tree
Hide file tree
Showing 10 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions experimental/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ All notable changes to experimental packages in this project will be documented

### :bug: (Bug Fix)

* fix(exporter-*-otlp-*): use parseHeaders() to ensure header-values are not 'undefined' #4540
* Fixes a bug where passing `undefined` as a header value would crash the end-user app after the export timeout elapsed.

### :books: (Refine Doc)

### :house: (Internal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import type {
import type { OTLPExporterNodeConfigBase } from '@opentelemetry/otlp-exporter-base';
import type { IExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';
import { getEnv, baggageUtils } from '@opentelemetry/core';
import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
import {
OTLPExporterNodeBase,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import { createExportLogsServiceRequest } from '@opentelemetry/otlp-transformer';

import { getDefaultUrl } from '../config';
Expand Down Expand Up @@ -50,7 +53,7 @@ export class OTLPLogExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
),
...config.headers,
...parseHeaders(config?.headers),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
OTLPExporterConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import {
OTLPProtoExporterNodeBase,
Expand Down Expand Up @@ -57,7 +58,7 @@ export class OTLPLogExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_LOGS_HEADERS
),
...config.headers,
...parseHeaders(config?.headers),
};
}
convert(logs: ReadableLogRecord[]): IExportLogsServiceRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

import { ReadableSpan, SpanExporter } from '@opentelemetry/sdk-trace-base';
import { getEnv, baggageUtils } from '@opentelemetry/core';
import { OTLPExporterNodeBase } from '@opentelemetry/otlp-exporter-base';
import {
OTLPExporterNodeBase,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrl,
Expand Down Expand Up @@ -49,7 +52,7 @@ export class OTLPTraceExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...config.headers,
...parseHeaders(config?.headers),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import {
OTLPProtoExporterNodeBase,
Expand Down Expand Up @@ -52,7 +53,7 @@ export class OTLPTraceExporter
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_TRACES_HEADERS
),
...config.headers,
...parseHeaders(config?.headers),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
IExportMetricsServiceResponse,
} from '@opentelemetry/otlp-transformer';
import { VERSION } from './version';
import { parseHeaders } from '@opentelemetry/otlp-exporter-base';

const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
Expand All @@ -49,7 +50,7 @@ class OTLPMetricExporterProxy extends OTLPGRPCExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
...parseHeaders(config?.headers),
};
super(
config,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import {
createExportMetricsServiceRequest,
Expand All @@ -48,7 +49,7 @@ class OTLPExporterNodeProxy extends OTLPExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
...parseHeaders(config?.headers),
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
OTLPExporterNodeConfigBase,
appendResourcePathToUrl,
appendRootPathToUrlIfNeeded,
parseHeaders,
} from '@opentelemetry/otlp-exporter-base';
import {
createExportMetricsServiceRequest,
Expand All @@ -51,7 +52,7 @@ class OTLPMetricExporterNodeProxy extends OTLPProtoExporterNodeBase<
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_METRICS_HEADERS
),
...config?.headers,
...parseHeaders(config?.headers),
};
}

Expand Down
4 changes: 3 additions & 1 deletion experimental/packages/otlp-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ export function parseHeaders(
if (typeof value !== 'undefined') {
headers[key] = String(value);
} else {
diag.warn(`Header "${key}" has wrong value and will be ignored`);
diag.warn(
`Header "${key}" has invalid value (${value}) and will be ignored`
);
}
});
return headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ describe('utils', () => {
const args = spyWarn.args[0];
assert.strictEqual(
args[0],
'Header "foo1" has wrong value and will be ignored'
'Header "foo1" has invalid value (undefined) and will be ignored'
);
});

Expand Down

0 comments on commit 3a426e8

Please sign in to comment.