Skip to content

Commit

Permalink
Do not trace events with non json metadata (#386)
Browse files Browse the repository at this point in the history
  • Loading branch information
w1am authored Aug 30, 2024
1 parent c54e366 commit 6836848
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
10 changes: 7 additions & 3 deletions packages/opentelemetry/src/instrumentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -360,14 +360,18 @@ 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;

const parentContext = trace.setSpanContext(context.active(), {
traceId,
spanId,
traceFlags: TraceFlags.SAMPLED,
isRemote,
});

return parentContext;
Expand Down
29 changes: 17 additions & 12 deletions packages/test/src/opentelemetry/instrumentation.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
createTestNode,
Defer,
delay,
jsonTestEvents,
binaryTestEvents,
} from "@test-utils";
import { createTestNode, Defer, delay, jsonTestEvents } from "@test-utils";
import {
NodeTracerProvider,
InMemorySpanExporter,
Expand All @@ -28,7 +22,6 @@ instrumentation.disable();
import * as esdb from "@eventstore/db-client";
import {
AppendToStreamOptions,
binaryEvent,
ResolvedEvent,
streamNameFilter,
WrongExpectedVersionError,
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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]);
Expand All @@ -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);
});
});

Expand Down

0 comments on commit 6836848

Please sign in to comment.