Skip to content

Commit 972fbd5

Browse files
committed
Add otel headers to health checks
1 parent ddbead4 commit 972fbd5

File tree

1 file changed

+45
-9
lines changed
  • packages/node-opentelemetry/src

1 file changed

+45
-9
lines changed

packages/node-opentelemetry/src/otel.ts

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,41 @@ const hrtimeToMs = (hrtime: [number, number]) => {
146146
return hrtime[0] * 1e3 + hrtime[1] / 1e6;
147147
};
148148

149+
/**
150+
* Parses OTEL_EXPORTER_OTLP_HEADERS environment variable into a structured headers object.
151+
* Format: "key1=value1,key2=value2" -> { key1: "value1", key2: "value2" }
152+
*/
153+
const parseOtlpHeaders = (headersString?: string): Record<string, string> => {
154+
if (!headersString) {
155+
return {};
156+
}
157+
158+
const headers: Record<string, string> = {};
159+
const pairs = headersString.split(',');
160+
161+
for (const pair of pairs) {
162+
const trimmedPair = pair.trim();
163+
if (!trimmedPair) {
164+
continue;
165+
}
166+
167+
const equalIndex = trimmedPair.indexOf('=');
168+
if (equalIndex === -1) {
169+
// Skip malformed pairs without '='
170+
continue;
171+
}
172+
173+
const key = trimmedPair.substring(0, equalIndex).trim();
174+
const value = trimmedPair.substring(equalIndex + 1).trim();
175+
176+
if (key) {
177+
headers[key] = value;
178+
}
179+
}
180+
181+
return headers;
182+
};
183+
149184
const healthCheckUrl = async (
150185
ui: ora.Ora,
151186
url: string,
@@ -215,6 +250,13 @@ export const initSDK = (config: SDKConfig) => {
215250
});
216251
ui.succeed('Set default otel envs');
217252

253+
// Parse OTLP headers from environment variable
254+
const otlpHeaders = parseOtlpHeaders(env.OTEL_EXPORTER_OTLP_HEADERS);
255+
const healthCheckHeaders = {
256+
'Content-Type': 'application/json',
257+
...otlpHeaders,
258+
};
259+
218260
const stopOnTerminationSignals =
219261
config.stopOnTerminationSignals ??
220262
DEFAULT_HDX_NODE_STOP_ON_TERMINATION_SIGNALS; // Stop by default
@@ -244,23 +286,17 @@ export const initSDK = (config: SDKConfig) => {
244286
Promise.all([
245287
healthCheckUrl(ui, DEFAULT_OTEL_TRACES_EXPORTER_URL, {
246288
method: 'POST',
247-
headers: {
248-
'Content-Type': 'application/json',
249-
},
289+
headers: healthCheckHeaders,
250290
body: JSON.stringify({}),
251291
}),
252292
healthCheckUrl(ui, _logger.getExporterUrl(), {
253293
method: 'POST',
254-
headers: {
255-
'Content-Type': 'application/json',
256-
},
294+
headers: healthCheckHeaders,
257295
body: JSON.stringify({}),
258296
}),
259297
healthCheckUrl(ui, DEFAULT_OTEL_METRICS_EXPORTER_URL, {
260298
method: 'POST',
261-
headers: {
262-
'Content-Type': 'application/json',
263-
},
299+
headers: healthCheckHeaders,
264300
body: JSON.stringify({}),
265301
}),
266302
]);

0 commit comments

Comments
 (0)