Skip to content

Commit fce12fd

Browse files
committed
test(dev): cover spanCount noise-filtering; clarify hex-id helper name
Follow-ups from #2043 review (Gitika, Harrison): - Add a TraceStore.list spanCount test with transport-noise spans (1 agent + 4 http-send -> "1"), guarding the post-filter count. - Rename hexFromB64OrString -> hexFromBase64OrHex; both reviewers misread B64.
1 parent 4b80241 commit fce12fd

3 files changed

Lines changed: 25 additions & 15 deletions

File tree

src/core/dev/otel/store.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,16 @@ describe("TraceStore", () => {
7070
expect(traces[0]!.spanCount).toBe("2");
7171
});
7272

73+
test("spanCount reflects the rendered waterfall, not filtered transport noise", async () => {
74+
const trace = payload(TRACE_A);
75+
const spans = trace.resourceSpans![0]!.scopeSpans![0]!.spans!;
76+
// 1 meaningful agent span + 4 "http send" spans the inspector filters out.
77+
for (let i = 0; i < 4; i++) spans.push({ ...spans[0]!, name: "GET / http send" });
78+
await store.append(trace);
79+
80+
expect((await store.list())[0]!.spanCount).toBe("1");
81+
});
82+
7383
test("payloads without a trace id are dropped", async () => {
7484
await store.append({ resourceSpans: [] });
7585
expect(await store.list()).toEqual([]);

src/core/dev/otel/transforms.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
extractAnyValue,
55
extractTraceMeta,
66
flattenAttributes,
7-
hexFromB64OrString,
7+
hexFromBase64OrHex,
88
nanoToMs,
99
partitionByTraceId,
1010
} from "./transforms";
@@ -199,10 +199,10 @@ describe("helpers", () => {
199199
expect(nanoToMs(undefined)).toBe(0);
200200
});
201201

202-
test("hexFromB64OrString accepts hex, base64, and empty", () => {
203-
expect(hexFromB64OrString(TRACE_ID_HEX.toUpperCase())).toBe(TRACE_ID_HEX);
204-
expect(hexFromB64OrString(TRACE_ID_B64)).toBe(TRACE_ID_HEX);
205-
expect(hexFromB64OrString(undefined)).toBe("");
202+
test("hexFromBase64OrHex accepts hex, base64, and empty", () => {
203+
expect(hexFromBase64OrHex(TRACE_ID_HEX.toUpperCase())).toBe(TRACE_ID_HEX);
204+
expect(hexFromBase64OrHex(TRACE_ID_B64)).toBe(TRACE_ID_HEX);
205+
expect(hexFromBase64OrHex(undefined)).toBe("");
206206
});
207207

208208
test("flattenAttributes handles typed values, arrays, and kvlist, empty for none", () => {

src/core/dev/otel/transforms.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export function extractTraceMeta(
2828
if (service) services.add(service);
2929
for (const scopeSpan of resourceSpan.scopeSpans ?? []) {
3030
for (const span of scopeSpan.spans ?? []) {
31-
meta.traceId ??= hexFromB64OrString(span.traceId) || undefined;
31+
meta.traceId ??= hexFromBase64OrHex(span.traceId) || undefined;
3232
widenTimeBounds(meta, nanoToMs(span.startTimeUnixNano));
3333
widenTimeBounds(meta, nanoToMs(span.endTimeUnixNano));
3434
meta.sessionId ??=
@@ -43,7 +43,7 @@ export function extractTraceMeta(
4343
if (service) services.add(service);
4444
for (const scopeLog of resourceLog.scopeLogs ?? []) {
4545
for (const record of scopeLog.logRecords ?? []) {
46-
meta.traceId ??= hexFromB64OrString(record.traceId) || undefined;
46+
meta.traceId ??= hexFromBase64OrHex(record.traceId) || undefined;
4747
widenTimeBounds(
4848
meta,
4949
nanoToMs(record.timeUnixNano) || nanoToMs(record.observedTimeUnixNano),
@@ -79,7 +79,7 @@ export function partitionByTraceId(payload: OtlpPayload): Map<string, OtlpPayloa
7979

8080
for (const resourceSpan of payload.resourceSpans ?? []) {
8181
for (const scopeSpan of resourceSpan.scopeSpans ?? []) {
82-
const byTrace = groupBy(scopeSpan.spans ?? [], (span) => hexFromB64OrString(span.traceId));
82+
const byTrace = groupBy(scopeSpan.spans ?? [], (span) => hexFromBase64OrHex(span.traceId));
8383
for (const [traceId, spans] of byTrace) {
8484
(partition(traceId).resourceSpans ??= []).push({
8585
resource: resourceSpan.resource,
@@ -92,7 +92,7 @@ export function partitionByTraceId(payload: OtlpPayload): Map<string, OtlpPayloa
9292
for (const resourceLog of payload.resourceLogs ?? []) {
9393
for (const scopeLog of resourceLog.scopeLogs ?? []) {
9494
const byTrace = groupBy(scopeLog.logRecords ?? [], (record) =>
95-
hexFromB64OrString(record.traceId),
95+
hexFromBase64OrHex(record.traceId),
9696
);
9797
for (const [traceId, logRecords] of byTrace) {
9898
(partition(traceId).resourceLogs ??= []).push({
@@ -137,9 +137,9 @@ export function buildTraceDetail(
137137
spans: scopeSpan.spans
138138
?.map((span) => ({
139139
...span,
140-
traceId: hexFromB64OrString(span.traceId),
141-
spanId: hexFromB64OrString(span.spanId),
142-
parentSpanId: hexFromB64OrString(span.parentSpanId),
140+
traceId: hexFromBase64OrHex(span.traceId),
141+
spanId: hexFromBase64OrHex(span.spanId),
142+
parentSpanId: hexFromBase64OrHex(span.parentSpanId),
143143
attributes: flattenAttributes(span.attributes),
144144
}))
145145
.filter((span) => isMeaningfulSpan(span)),
@@ -157,8 +157,8 @@ export function buildTraceDetail(
157157
scope: scopeLog.scope,
158158
logRecords: scopeLog.logRecords?.map((record) => ({
159159
...record,
160-
traceId: hexFromB64OrString(record.traceId),
161-
spanId: hexFromB64OrString(record.spanId),
160+
traceId: hexFromBase64OrHex(record.traceId),
161+
spanId: hexFromBase64OrHex(record.spanId),
162162
body: record.body === undefined ? undefined : extractAnyValue(record.body),
163163
attributes: flattenAttributes(record.attributes),
164164
})),
@@ -224,7 +224,7 @@ export function nanoToMs(nano: string | undefined): number {
224224
* Normalize a trace/span id that may be base64 (protobuf JSON conversion) or
225225
* already hex (JSON ingest) into lowercase hex.
226226
*/
227-
export function hexFromB64OrString(value: string | undefined): string {
227+
export function hexFromBase64OrHex(value: string | undefined): string {
228228
if (!value) return "";
229229
if (/^[0-9a-f]+$/i.test(value) && (value.length === 32 || value.length === 16))
230230
return value.toLowerCase();

0 commit comments

Comments
 (0)