Skip to content

Commit 96b9e26

Browse files
feat: user agent header for traceloopId (#73)
1 parent b55a406 commit 96b9e26

File tree

2 files changed

+7
-32
lines changed

2 files changed

+7
-32
lines changed

packages/expect-opentelemetry/src/trace-loop/fetch-traces.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import { opentelemetry } from '@traceloop/otel-proto';
2+
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
23
import { setTimeout } from 'timers/promises';
34
import { httpGetBinary } from '../utils';
45

5-
const TRACELOOP_ID_REQUEST_HEADER = 'http.request.header.traceloop_id';
6-
const TRACELOOP_ID_RESPONSE_HEADER = 'http.response.header.traceloop_id';
7-
86
export interface FetchTracesConfig {
97
maxPollTime: number;
108
pollInterval: number;
@@ -22,7 +20,7 @@ export const fetchTracesConfigBase: FetchTracesConfig = {
2220
};
2321

2422
/**
25-
* Searches in the traces for a trace with the given traceLoopId contained in the attribute http.request.header.trace_loop_id
23+
* Searches in the traces for a trace with the given traceLoopId contained in the attribute http.user_agent
2624
*
2725
* @param traces all traces from the otel receiver
2826
* @param traceLoopId traceLoopId to search for
@@ -37,11 +35,9 @@ export const findTraceLoopIdMatch = (
3735
for (const span of scopeSpan.spans || []) {
3836
if (span.attributes) {
3937
for (const attribute of span.attributes) {
40-
// http: check in headers stringified json
41-
if (attribute.key === 'http.request.headers') {
42-
const matches = attribute.value?.stringValue?.match(
43-
/"traceloop_id":"(.*)"/,
44-
);
38+
if (attribute.key === SemanticAttributes.HTTP_USER_AGENT) {
39+
const matches =
40+
attribute.value?.stringValue?.match(/traceloop_id=(.*)/);
4541
if (matches?.length > 1) {
4642
if (matches[1] === traceLoopId) {
4743
return span.traceId
@@ -50,21 +46,6 @@ export const findTraceLoopIdMatch = (
5046
}
5147
}
5248
}
53-
54-
// check in specific header key
55-
if (
56-
attribute.key === TRACELOOP_ID_REQUEST_HEADER ||
57-
attribute.key === TRACELOOP_ID_RESPONSE_HEADER
58-
) {
59-
if (
60-
attribute.value?.arrayValue?.values?.[0]?.stringValue ===
61-
traceLoopId
62-
) {
63-
return span.traceId
64-
? Buffer.from(span.traceId).toString('hex')
65-
: undefined;
66-
}
67-
}
6849
}
6950
}
7051
}

packages/expect-opentelemetry/src/trace-loop/index.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,10 @@ import {
1616
byCustomAttribute,
1717
} from './filter-service-spans';
1818

19-
const TRACE_LOOP_ID_HEADER = 'traceloop_id';
20-
2119
export class TraceLoop {
2220
private readonly _traceLoopId: string;
2321
private _fetchedTrace = false;
24-
private _traceId;
22+
private _traceId: string | undefined;
2523
private _traceData: opentelemetry.proto.trace.v1.TracesData | undefined;
2624

2725
constructor() {
@@ -32,13 +30,9 @@ export class TraceLoop {
3230
return this._traceLoopId;
3331
}
3432

35-
get traceLoopHeaderName() {
36-
return TRACE_LOOP_ID_HEADER;
37-
}
38-
3933
get axiosInstance() {
4034
return axios.create({
41-
headers: { [TRACE_LOOP_ID_HEADER]: this._traceLoopId },
35+
headers: { 'User-Agent': `traceloop_id=${this._traceLoopId}` },
4236
});
4337
}
4438

0 commit comments

Comments
 (0)