Skip to content

Commit

Permalink
fix(otlp-exporter): normalize http headers to lower case
Browse files Browse the repository at this point in the history
  • Loading branch information
llc1123 committed May 18, 2023
1 parent fc50a08 commit 06d5086
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@ export abstract class OTLPExporterBrowserBase<
*/
constructor(config: OTLPExporterConfigBase = {}) {
super(config);

const headersBeforeUserAgent = {
Accept: 'application/json',
'Content-Type': 'application/json',
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((config as any).metadata) {
diag.warn('Metadata cannot be set when using http');
}
const headersBeforeUserAgent = parseHeaders({
accept: 'application/json',
'content-type': 'application/json',
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_HEADERS
),
...parseHeaders(config.headers),
};
if (
Object.keys(headersBeforeUserAgent)
.map(key => key.toLowerCase())
.includes('user-agent')
) {
diag.warn('User-Agent header should not be set via config.');
...config.headers,
});
if (Object.keys(headersBeforeUserAgent).includes('user-agent')) {
diag.warn('Header "user-agent" should not be set by config.');
}
this._headers = Object.assign(headersBeforeUserAgent, USER_AGENT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,15 @@ export abstract class OTLPExporterNodeBase<
if ((config as any).metadata) {
diag.warn('Metadata cannot be set when using http');
}
const headersBeforeUserAgent = {
const headersBeforeUserAgent = parseHeaders({
...this.DEFAULT_HEADERS,
...baggageUtils.parseKeyPairsIntoRecord(
getEnv().OTEL_EXPORTER_OTLP_HEADERS
),
...parseHeaders(config.headers),
};
if (
Object.keys(headersBeforeUserAgent)
.map(key => key.toLowerCase())
.includes('user-agent')
) {
diag.warn('User-Agent header should not be set via config.');
...config.headers,
});
if (Object.keys(headersBeforeUserAgent).includes('user-agent')) {
diag.warn('Header "user-agent" should not be set by config.');
}
this.headers = Object.assign(headersBeforeUserAgent, USER_AGENT);
this.agent = createHttpAgent(config);
Expand Down
8 changes: 5 additions & 3 deletions experimental/packages/otlp-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const DEFAULT_EXPORT_MAX_BACKOFF = 5000;
export const DEFAULT_EXPORT_BACKOFF_MULTIPLIER = 1.5;

export const USER_AGENT = {
'User-Agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
'user-agent': `OTel-OTLP-Exporter-JavaScript/${VERSION}`,
};

/**
Expand All @@ -38,9 +38,11 @@ export function parseHeaders(
const headers: Record<string, string> = {};
Object.entries(partialHeaders).forEach(([key, value]) => {
if (typeof value !== 'undefined') {
headers[key] = String(value);
headers[key.toLowerCase()] = String(value);
} else {
diag.warn(`Header "${key}" has wrong value and will be ignored`);
diag.warn(
`Header "${key.toLowerCase()}" has wrong value and will be ignored`
);
}
});
return headers;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ export abstract class OTLPGRPCExporterNodeBase<
this.metadata.set(k, v);
}
}
if (this.metadata.get('user-agent')) {
if (this.metadata.get('User-Agent')) {
diag.warn('User-Agent header should not be set via config.');
}
this.metadata.set('user-agent', USER_AGENT);
this.metadata.set('User-Agent', USER_AGENT);
this.compression = configureCompression(config.compression);
}

Expand Down

0 comments on commit 06d5086

Please sign in to comment.