Skip to content

Commit

Permalink
feat: user agent header for traceloopId (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomer-friedman authored May 31, 2023
1 parent b55a406 commit 96b9e26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
29 changes: 5 additions & 24 deletions packages/expect-opentelemetry/src/trace-loop/fetch-traces.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { opentelemetry } from '@traceloop/otel-proto';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { setTimeout } from 'timers/promises';
import { httpGetBinary } from '../utils';

const TRACELOOP_ID_REQUEST_HEADER = 'http.request.header.traceloop_id';
const TRACELOOP_ID_RESPONSE_HEADER = 'http.response.header.traceloop_id';

export interface FetchTracesConfig {
maxPollTime: number;
pollInterval: number;
Expand All @@ -22,7 +20,7 @@ export const fetchTracesConfigBase: FetchTracesConfig = {
};

/**
* Searches in the traces for a trace with the given traceLoopId contained in the attribute http.request.header.trace_loop_id
* Searches in the traces for a trace with the given traceLoopId contained in the attribute http.user_agent
*
* @param traces all traces from the otel receiver
* @param traceLoopId traceLoopId to search for
Expand All @@ -37,11 +35,9 @@ export const findTraceLoopIdMatch = (
for (const span of scopeSpan.spans || []) {
if (span.attributes) {
for (const attribute of span.attributes) {
// http: check in headers stringified json
if (attribute.key === 'http.request.headers') {
const matches = attribute.value?.stringValue?.match(
/"traceloop_id":"(.*)"/,
);
if (attribute.key === SemanticAttributes.HTTP_USER_AGENT) {
const matches =
attribute.value?.stringValue?.match(/traceloop_id=(.*)/);
if (matches?.length > 1) {
if (matches[1] === traceLoopId) {
return span.traceId
Expand All @@ -50,21 +46,6 @@ export const findTraceLoopIdMatch = (
}
}
}

// check in specific header key
if (
attribute.key === TRACELOOP_ID_REQUEST_HEADER ||
attribute.key === TRACELOOP_ID_RESPONSE_HEADER
) {
if (
attribute.value?.arrayValue?.values?.[0]?.stringValue ===
traceLoopId
) {
return span.traceId
? Buffer.from(span.traceId).toString('hex')
: undefined;
}
}
}
}
}
Expand Down
10 changes: 2 additions & 8 deletions packages/expect-opentelemetry/src/trace-loop/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@ import {
byCustomAttribute,
} from './filter-service-spans';

const TRACE_LOOP_ID_HEADER = 'traceloop_id';

export class TraceLoop {
private readonly _traceLoopId: string;
private _fetchedTrace = false;
private _traceId;
private _traceId: string | undefined;
private _traceData: opentelemetry.proto.trace.v1.TracesData | undefined;

constructor() {
Expand All @@ -32,13 +30,9 @@ export class TraceLoop {
return this._traceLoopId;
}

get traceLoopHeaderName() {
return TRACE_LOOP_ID_HEADER;
}

get axiosInstance() {
return axios.create({
headers: { [TRACE_LOOP_ID_HEADER]: this._traceLoopId },
headers: { 'User-Agent': `traceloop_id=${this._traceLoopId}` },
});
}

Expand Down

0 comments on commit 96b9e26

Please sign in to comment.