Skip to content

Commit

Permalink
Use standard hashing function and updated addSpanPointer method
Browse files Browse the repository at this point in the history
  • Loading branch information
nhulston committed Nov 15, 2024
1 parent 16612e2 commit ea5594b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 45 deletions.
14 changes: 12 additions & 2 deletions src/trace/listener.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,12 @@ export interface TraceConfig {
coldStartTraceSkipLib: string;
}

interface SpanPointerAttributes {
pointerKind: string;
pointerDirection: string;
pointerHash: string;
}

export class TraceListener {
private contextService: TraceContextService;
private context?: Context;
Expand All @@ -81,7 +87,7 @@ export class TraceListener {
private wrappedCurrentSpan?: SpanWrapper;
private triggerTags?: { [key: string]: string };
private lambdaSpanParentContext?: SpanContext;
private spanPointerAttributesList: object[] = [];
private spanPointerAttributesList: SpanPointerAttributes[] = [];

public get currentTraceHeaders() {
return this.contextService.currentTraceHeaders;
Expand Down Expand Up @@ -206,7 +212,11 @@ export class TraceListener {

if (this.wrappedCurrentSpan) {
for (const attributes of this.spanPointerAttributesList) {
this.wrappedCurrentSpan.span.addSpanPointer(attributes);
this.wrappedCurrentSpan.span.addSpanPointer(
attributes.pointerKind,
attributes.pointerDirection,
attributes.pointerHash,
);
}
}
return false;
Expand Down
49 changes: 25 additions & 24 deletions src/utils/span-pointers.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { getSpanPointerAttributes } from "./span-pointers";
import { eventTypes } from "../trace/trigger";
import { SPAN_LINK_POINTER_KIND, S3_PTR_KIND, SPAN_POINTER_DIRECTION } from "dd-trace/packages/dd-trace/src/span_pointers";
import { S3_PTR_KIND, SPAN_POINTER_DIRECTION } from "dd-trace/packages/dd-trace/src/span_pointers";
import * as spanPointers from "dd-trace/packages/dd-trace/src/span_pointers";

// Mock the external dependencies
jest.mock("./log", () => ({
logDebug: jest.fn(),
}));

interface SpanPointerAttributes {
pointerKind: string;
pointerDirection: string;
pointerHash: string;
}

describe("span-pointers utils", () => {
const mockS3PointerHash = "mock-hash-123";
const mockPointerHash = "mock-hash-123";

beforeEach(() => {
// Mock the generateS3PointerHash function
jest.spyOn(spanPointers, "generateS3PointerHash").mockReturnValue(mockS3PointerHash);
jest.spyOn(spanPointers, "generatePointerHash").mockReturnValue(mockPointerHash);
});

afterEach(() => {
Expand Down Expand Up @@ -47,18 +52,17 @@ describe("span-pointers utils", () => {
],
};

const expected = [
const expected: SpanPointerAttributes[] = [
{
"ptr.kind": S3_PTR_KIND,
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
"ptr.hash": mockS3PointerHash,
"link.kind": SPAN_LINK_POINTER_KIND,
pointerKind: S3_PTR_KIND,
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
pointerHash: mockPointerHash,
},
];

const result = getSpanPointerAttributes(eventTypes.s3, event);
expect(result).toEqual(expected);
expect(spanPointers.generateS3PointerHash).toHaveBeenCalledWith("test-bucket", "test-key", "test-etag");
expect(spanPointers.generatePointerHash).toHaveBeenCalledWith(["test-bucket", "test-key", "test-etag"]);
});

it("processes multiple S3 records correctly", () => {
Expand All @@ -85,18 +89,16 @@ describe("span-pointers utils", () => {
],
};

const expected = [
const expected: SpanPointerAttributes[] = [
{
"ptr.kind": S3_PTR_KIND,
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
"ptr.hash": mockS3PointerHash,
"link.kind": SPAN_LINK_POINTER_KIND,
pointerKind: S3_PTR_KIND,
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
pointerHash: mockPointerHash,
},
{
"ptr.kind": S3_PTR_KIND,
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
"ptr.hash": mockS3PointerHash,
"link.kind": SPAN_LINK_POINTER_KIND,
pointerKind: S3_PTR_KIND,
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
pointerHash: mockPointerHash,
},
];

Expand Down Expand Up @@ -134,12 +136,11 @@ describe("span-pointers utils", () => {
],
};

const expected = [
const expected: SpanPointerAttributes[] = [
{
"ptr.kind": S3_PTR_KIND,
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
"ptr.hash": mockS3PointerHash,
"link.kind": SPAN_LINK_POINTER_KIND,
pointerKind: S3_PTR_KIND,
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
pointerHash: mockPointerHash,
},
];

Expand Down
34 changes: 15 additions & 19 deletions src/utils/span-pointers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { eventTypes } from "../trace/trigger";
import { logDebug } from "./log";
import {
SPAN_LINK_POINTER_KIND,
S3_PTR_KIND,
SPAN_POINTER_DIRECTION,
generateS3PointerHash,
} from "dd-trace/packages/dd-trace/src/span_pointers";
import { S3_PTR_KIND, SPAN_POINTER_DIRECTION, generatePointerHash } from "dd-trace/packages/dd-trace/src/span_pointers";

Check failure on line 3 in src/utils/span-pointers.ts

View workflow job for this annotation

GitHub Actions / unit-test (16.14)

Cannot find module 'dd-trace/packages/dd-trace/src/span_pointers' or its corresponding type declarations.

Check failure on line 3 in src/utils/span-pointers.ts

View workflow job for this annotation

GitHub Actions / unit-test (18.12)

Cannot find module 'dd-trace/packages/dd-trace/src/span_pointers' or its corresponding type declarations.

Check failure on line 3 in src/utils/span-pointers.ts

View workflow job for this annotation

GitHub Actions / unit-test (20.9)

Cannot find module 'dd-trace/packages/dd-trace/src/span_pointers' or its corresponding type declarations.

interface SpanPointerAttributes {
"ptr.kind": string;
"ptr.dir": string;
"ptr.hash": string;
"link.kind": string;
pointerKind: string;
pointerDirection: string;
pointerHash: string;
}

/**
Expand Down Expand Up @@ -40,8 +34,7 @@ export function getSpanPointerAttributes(

function processS3Event(event: any): SpanPointerAttributes[] {
const records = event.Records || [];
const spanPointerAttributesList = [];
const linkKind = SPAN_LINK_POINTER_KIND;
const spanPointerAttributesList: SpanPointerAttributes[] = [];

for (const record of records) {
const eventName = record.eventName;
Expand All @@ -53,19 +46,22 @@ function processS3Event(event: any): SpanPointerAttributes[] {
const s3Event = record?.s3;
const bucketName = s3Event?.bucket?.name;
const objectKey = s3Event?.object?.key;
const eTag = s3Event?.object?.eTag;
let eTag = s3Event?.object?.eTag;

if (!bucketName || !objectKey || !eTag) {
logDebug("Unable to calculate span pointer hash because of missing parameters.");
continue;
}

const pointerHash = generateS3PointerHash(bucketName, objectKey, eTag);
const spanPointerAttributes = {
"ptr.kind": S3_PTR_KIND,
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
"ptr.hash": pointerHash,
"link.kind": linkKind,
// https://github.com/DataDog/dd-span-pointer-rules/blob/main/AWS/S3/Object/README.md
if (eTag.startsWith('"') && eTag.endsWith('"')) {
eTag = eTag.slice(1, -1);
}
const pointerHash = generatePointerHash([bucketName, objectKey, eTag]);
const spanPointerAttributes: SpanPointerAttributes = {
pointerKind: S3_PTR_KIND,
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
pointerHash,
};
spanPointerAttributesList.push(spanPointerAttributes);
}
Expand Down

0 comments on commit ea5594b

Please sign in to comment.