Skip to content

Commit ea5594b

Browse files
committed
Use standard hashing function and updated addSpanPointer method
1 parent 16612e2 commit ea5594b

File tree

3 files changed

+52
-45
lines changed

3 files changed

+52
-45
lines changed

src/trace/listener.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ export interface TraceConfig {
7171
coldStartTraceSkipLib: string;
7272
}
7373

74+
interface SpanPointerAttributes {
75+
pointerKind: string;
76+
pointerDirection: string;
77+
pointerHash: string;
78+
}
79+
7480
export class TraceListener {
7581
private contextService: TraceContextService;
7682
private context?: Context;
@@ -81,7 +87,7 @@ export class TraceListener {
8187
private wrappedCurrentSpan?: SpanWrapper;
8288
private triggerTags?: { [key: string]: string };
8389
private lambdaSpanParentContext?: SpanContext;
84-
private spanPointerAttributesList: object[] = [];
90+
private spanPointerAttributesList: SpanPointerAttributes[] = [];
8591

8692
public get currentTraceHeaders() {
8793
return this.contextService.currentTraceHeaders;
@@ -206,7 +212,11 @@ export class TraceListener {
206212

207213
if (this.wrappedCurrentSpan) {
208214
for (const attributes of this.spanPointerAttributesList) {
209-
this.wrappedCurrentSpan.span.addSpanPointer(attributes);
215+
this.wrappedCurrentSpan.span.addSpanPointer(
216+
attributes.pointerKind,
217+
attributes.pointerDirection,
218+
attributes.pointerHash,
219+
);
210220
}
211221
}
212222
return false;

src/utils/span-pointers.spec.ts

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
import { getSpanPointerAttributes } from "./span-pointers";
22
import { eventTypes } from "../trace/trigger";
3-
import { SPAN_LINK_POINTER_KIND, S3_PTR_KIND, SPAN_POINTER_DIRECTION } from "dd-trace/packages/dd-trace/src/span_pointers";
3+
import { S3_PTR_KIND, SPAN_POINTER_DIRECTION } from "dd-trace/packages/dd-trace/src/span_pointers";
44
import * as spanPointers from "dd-trace/packages/dd-trace/src/span_pointers";
55

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

11+
interface SpanPointerAttributes {
12+
pointerKind: string;
13+
pointerDirection: string;
14+
pointerHash: string;
15+
}
16+
1117
describe("span-pointers utils", () => {
12-
const mockS3PointerHash = "mock-hash-123";
18+
const mockPointerHash = "mock-hash-123";
1319

1420
beforeEach(() => {
15-
// Mock the generateS3PointerHash function
16-
jest.spyOn(spanPointers, "generateS3PointerHash").mockReturnValue(mockS3PointerHash);
21+
jest.spyOn(spanPointers, "generatePointerHash").mockReturnValue(mockPointerHash);
1722
});
1823

1924
afterEach(() => {
@@ -47,18 +52,17 @@ describe("span-pointers utils", () => {
4752
],
4853
};
4954

50-
const expected = [
55+
const expected: SpanPointerAttributes[] = [
5156
{
52-
"ptr.kind": S3_PTR_KIND,
53-
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
54-
"ptr.hash": mockS3PointerHash,
55-
"link.kind": SPAN_LINK_POINTER_KIND,
57+
pointerKind: S3_PTR_KIND,
58+
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
59+
pointerHash: mockPointerHash,
5660
},
5761
];
5862

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

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

88-
const expected = [
92+
const expected: SpanPointerAttributes[] = [
8993
{
90-
"ptr.kind": S3_PTR_KIND,
91-
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
92-
"ptr.hash": mockS3PointerHash,
93-
"link.kind": SPAN_LINK_POINTER_KIND,
94+
pointerKind: S3_PTR_KIND,
95+
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
96+
pointerHash: mockPointerHash,
9497
},
9598
{
96-
"ptr.kind": S3_PTR_KIND,
97-
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
98-
"ptr.hash": mockS3PointerHash,
99-
"link.kind": SPAN_LINK_POINTER_KIND,
99+
pointerKind: S3_PTR_KIND,
100+
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
101+
pointerHash: mockPointerHash,
100102
},
101103
];
102104

@@ -134,12 +136,11 @@ describe("span-pointers utils", () => {
134136
],
135137
};
136138

137-
const expected = [
139+
const expected: SpanPointerAttributes[] = [
138140
{
139-
"ptr.kind": S3_PTR_KIND,
140-
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
141-
"ptr.hash": mockS3PointerHash,
142-
"link.kind": SPAN_LINK_POINTER_KIND,
141+
pointerKind: S3_PTR_KIND,
142+
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
143+
pointerHash: mockPointerHash,
143144
},
144145
];
145146

src/utils/span-pointers.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import { eventTypes } from "../trace/trigger";
22
import { logDebug } from "./log";
3-
import {
4-
SPAN_LINK_POINTER_KIND,
5-
S3_PTR_KIND,
6-
SPAN_POINTER_DIRECTION,
7-
generateS3PointerHash,
8-
} from "dd-trace/packages/dd-trace/src/span_pointers";
3+
import { S3_PTR_KIND, SPAN_POINTER_DIRECTION, generatePointerHash } from "dd-trace/packages/dd-trace/src/span_pointers";
94

105
interface SpanPointerAttributes {
11-
"ptr.kind": string;
12-
"ptr.dir": string;
13-
"ptr.hash": string;
14-
"link.kind": string;
6+
pointerKind: string;
7+
pointerDirection: string;
8+
pointerHash: string;
159
}
1610

1711
/**
@@ -40,8 +34,7 @@ export function getSpanPointerAttributes(
4034

4135
function processS3Event(event: any): SpanPointerAttributes[] {
4236
const records = event.Records || [];
43-
const spanPointerAttributesList = [];
44-
const linkKind = SPAN_LINK_POINTER_KIND;
37+
const spanPointerAttributesList: SpanPointerAttributes[] = [];
4538

4639
for (const record of records) {
4740
const eventName = record.eventName;
@@ -53,19 +46,22 @@ function processS3Event(event: any): SpanPointerAttributes[] {
5346
const s3Event = record?.s3;
5447
const bucketName = s3Event?.bucket?.name;
5548
const objectKey = s3Event?.object?.key;
56-
const eTag = s3Event?.object?.eTag;
49+
let eTag = s3Event?.object?.eTag;
5750

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

63-
const pointerHash = generateS3PointerHash(bucketName, objectKey, eTag);
64-
const spanPointerAttributes = {
65-
"ptr.kind": S3_PTR_KIND,
66-
"ptr.dir": SPAN_POINTER_DIRECTION.UPSTREAM,
67-
"ptr.hash": pointerHash,
68-
"link.kind": linkKind,
56+
// https://github.com/DataDog/dd-span-pointer-rules/blob/main/AWS/S3/Object/README.md
57+
if (eTag.startsWith('"') && eTag.endsWith('"')) {
58+
eTag = eTag.slice(1, -1);
59+
}
60+
const pointerHash = generatePointerHash([bucketName, objectKey, eTag]);
61+
const spanPointerAttributes: SpanPointerAttributes = {
62+
pointerKind: S3_PTR_KIND,
63+
pointerDirection: SPAN_POINTER_DIRECTION.UPSTREAM,
64+
pointerHash,
6965
};
7066
spanPointerAttributesList.push(spanPointerAttributes);
7167
}

0 commit comments

Comments
 (0)