Skip to content

Commit 4d252a9

Browse files
move util to utils file
1 parent d91383a commit 4d252a9

File tree

3 files changed

+39
-38
lines changed

3 files changed

+39
-38
lines changed

packages/node-opentelemetry/src/__tests__/parseHeaders.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { parseOtlpHeaders } from '../otel';
1+
import { parseOtlpHeaders } from '../utils';
22

33
describe('Parse OTLP Headers', () => {
44
it('should return an empty object when no headers string is provided', () => {

packages/node-opentelemetry/src/otel.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import { getHyperDXMetricReader } from './metrics';
4949
import { MutableAsyncLocalStorageContextManager } from './MutableAsyncLocalStorageContextManager';
5050
import { Logger as OtelLogger } from './otel-logger';
5151
import HyperDXSpanProcessor from './spanProcessor';
52+
import { parseOtlpHeaders } from './utils';
5253

5354
const UI_LOG_PREFIX = '[⚡HyperDX]';
5455

@@ -146,43 +147,6 @@ const hrtimeToMs = (hrtime: [number, number]) => {
146147
return hrtime[0] * 1e3 + hrtime[1] / 1e6;
147148
};
148149

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-
export const parseOtlpHeaders = (
154-
headersString?: string,
155-
): Record<string, string> => {
156-
if (!headersString) {
157-
return {};
158-
}
159-
160-
const headers: Record<string, string> = {};
161-
const pairs = headersString.split(',');
162-
163-
for (const pair of pairs) {
164-
const trimmedPair = pair.trim();
165-
if (!trimmedPair) {
166-
continue;
167-
}
168-
169-
const equalIndex = trimmedPair.indexOf('=');
170-
if (equalIndex === -1) {
171-
// Skip malformed pairs without '='
172-
continue;
173-
}
174-
175-
const key = trimmedPair.substring(0, equalIndex).trim();
176-
const value = trimmedPair.substring(equalIndex + 1).trim();
177-
178-
if (key) {
179-
headers[key] = value;
180-
}
181-
}
182-
183-
return headers;
184-
};
185-
186150
const healthCheckUrl = async (
187151
ui: ora.Ora,
188152
url: string,

packages/node-opentelemetry/src/utils.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,40 @@ export const stringToBoolean = (stringValue: string | undefined) => {
2626
return undefined;
2727
}
2828
};
29+
30+
/**
31+
* Parses OTEL_EXPORTER_OTLP_HEADERS environment variable into a structured headers object.
32+
* Format: "key1=value1,key2=value2" -> { key1: "value1", key2: "value2" }
33+
*/
34+
export const parseOtlpHeaders = (
35+
headersString?: string,
36+
): Record<string, string> => {
37+
if (!headersString) {
38+
return {};
39+
}
40+
41+
const headers: Record<string, string> = {};
42+
const pairs = headersString.split(',');
43+
44+
for (const pair of pairs) {
45+
const trimmedPair = pair.trim();
46+
if (!trimmedPair) {
47+
continue;
48+
}
49+
50+
const equalIndex = trimmedPair.indexOf('=');
51+
if (equalIndex === -1) {
52+
// Skip malformed pairs without '='
53+
continue;
54+
}
55+
56+
const key = trimmedPair.substring(0, equalIndex).trim();
57+
const value = trimmedPair.substring(equalIndex + 1).trim();
58+
59+
if (key) {
60+
headers[key] = value;
61+
}
62+
}
63+
64+
return headers;
65+
};

0 commit comments

Comments
 (0)