Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(otlp-exporter): support user-agent headers in web exporters #3811

Closed
Closed
Changes from 1 commit
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(otlp-exporter): normalize http headers to lower case
llc1123 committed May 22, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit 96009bd5e3e16c9abbbe6ff4638b365d6a9d4012
Original file line number Diff line number Diff line change
@@ -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);
}
Original file line number Diff line number Diff line change
@@ -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);
8 changes: 5 additions & 3 deletions experimental/packages/otlp-exporter-base/src/util.ts
Original file line number Diff line number Diff line change
@@ -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}`,
};

/**
@@ -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;
Original file line number Diff line number Diff line change
@@ -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);
}