From 6836848c291ce61e9ee5483e296c0f4bcab03e5b Mon Sep 17 00:00:00 2001 From: William Chong Date: Fri, 30 Aug 2024 09:26:35 +0400 Subject: [PATCH] Do not trace events with non json metadata (#386) --- packages/opentelemetry/src/instrumentation.ts | 10 +++++-- .../src/opentelemetry/instrumentation.test.ts | 29 +++++++++++-------- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/packages/opentelemetry/src/instrumentation.ts b/packages/opentelemetry/src/instrumentation.ts index b1eb8f44..605dfa40 100644 --- a/packages/opentelemetry/src/instrumentation.ts +++ b/packages/opentelemetry/src/instrumentation.ts @@ -228,9 +228,9 @@ export class Instrumentation extends InstrumentationBase { const resolvedEvent = resolved as ResolvedEvent; const metadata = resolvedEvent?.event?.metadata; - if (!resolvedEvent.event?.isJson) return; + if (typeof metadata !== "object" || metadata === null) return resolved; - const parentContext = Instrumentation.restoreContext(metadata!); + const parentContext = Instrumentation.restoreContext(metadata); const { hostname, port } = Instrumentation.getServerAddress(uri); @@ -360,7 +360,10 @@ export class Instrumentation extends InstrumentationBase { }; } - private static restoreContext = (metadata: esdb.MetadataType): Context => { + private static restoreContext = ( + metadata: esdb.MetadataType, + isRemote = true + ): Context => { const traceId = metadata[TRACE_ID] as string; const spanId = metadata[SPAN_ID] as string; @@ -368,6 +371,7 @@ export class Instrumentation extends InstrumentationBase { traceId, spanId, traceFlags: TraceFlags.SAMPLED, + isRemote, }); return parentContext; diff --git a/packages/test/src/opentelemetry/instrumentation.test.ts b/packages/test/src/opentelemetry/instrumentation.test.ts index bb53dc26..a2622190 100644 --- a/packages/test/src/opentelemetry/instrumentation.test.ts +++ b/packages/test/src/opentelemetry/instrumentation.test.ts @@ -1,10 +1,4 @@ -import { - createTestNode, - Defer, - delay, - jsonTestEvents, - binaryTestEvents, -} from "@test-utils"; +import { createTestNode, Defer, delay, jsonTestEvents } from "@test-utils"; import { NodeTracerProvider, InMemorySpanExporter, @@ -28,7 +22,6 @@ instrumentation.disable(); import * as esdb from "@eventstore/db-client"; import { AppendToStreamOptions, - binaryEvent, ResolvedEvent, streamNameFilter, WrongExpectedVersionError, @@ -264,7 +257,7 @@ describe("instrumentation", () => { }); }); - test("non json events are not instrumented in subscription", async () => { + test.only("events with non-json metadata are not traced in subscriptions", async () => { const defer = new Defer(); const { EventStoreDBClient, jsonEvent, binaryEvent } = await import( "@eventstore/db-client" @@ -292,10 +285,16 @@ describe("instrumentation", () => { const event1 = binaryEvent({ type: "SomeType", data: Buffer.from("hello"), + metadata: { + "some-data": "some-value", + }, }); const event2 = jsonEvent({ type: "SomeType", - data: {}, + data: { + "some-data": "some-value", + }, + metadata: 2, }); await client.appendToStream(STREAM, [event1, event2]); @@ -317,22 +316,28 @@ describe("instrumentation", () => { const spans = memoryExporter.getFinishedSpans(); + const parentSpans = spans.filter( + (span) => span.name === EventStoreDBAttributes.STREAM_APPEND + ); + const childSpans = spans.filter( (span) => span.name === EventStoreDBAttributes.STREAM_SUBSCIBE ); expect(handleConfirmation).toHaveBeenCalledTimes(1); + expect(parentSpans.length).toBe(1); + expect(childSpans).toBeDefined(); expect(childSpans).toHaveLength(1); expect( childSpans[0].attributes[EventStoreDBAttributes.EVENT_STORE_EVENT_ID] - ).toBe(event2.id); + ).toBe(event1.id); expect( childSpans[0].attributes[EventStoreDBAttributes.EVENT_STORE_EVENT_TYPE] - ).toBe(event2.type); + ).toBe(event1.type); }); });