Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit baafa1b

Browse files
author
Amir Blum
authored
Merge pull request #15 from aspecto-io/request-hook-refactor
feat(plugin-aws-sdk): request hook enhancment
2 parents 7ec8663 + 74412a3 commit baafa1b

File tree

4 files changed

+61
-20
lines changed

4 files changed

+61
-20
lines changed

packages/plugin-aws-sdk/src/aws-sdk.ts

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
callback | 1 | 2
1010
*/
1111
import { BasePlugin } from "@opentelemetry/core";
12-
import { Span, CanonicalCode } from "@opentelemetry/api";
12+
import { Span, CanonicalCode, Attributes, SpanKind } from "@opentelemetry/api";
1313
import * as shimmer from "shimmer";
1414
import AWS from "aws-sdk";
1515
import { AttributeNames } from "./enums";
@@ -69,11 +69,18 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
6969
return target;
7070
}
7171

72-
private _startAwsSpan(request: AWS.Request<any, any>): Span {
72+
private _startAwsSpan(
73+
request: AWS.Request<any, any>,
74+
additionalAttributes?: Attributes,
75+
spanKind?: SpanKind,
76+
spanName?: string
77+
): Span {
7378
const operation = (request as any).operation;
7479
const service = (request as any).service;
80+
const name = spanName ?? this._getSpanName(request);
7581

76-
const newSpan = this._tracer.startSpan(this._getSpanName(request), {
82+
const newSpan = this._tracer.startSpan(name, {
83+
kind: spanKind,
7784
attributes: {
7885
[AttributeNames.COMPONENT]: this.moduleName,
7986
[AttributeNames.AWS_OPERATION]: operation,
@@ -83,6 +90,7 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
8390
[AttributeNames.AWS_SERVICE_API]: service?.api?.className,
8491
[AttributeNames.AWS_SERVICE_IDENTIFIER]: service?.serviceIdentifier,
8592
[AttributeNames.AWS_SERVICE_NAME]: service?.api?.abbreviation,
93+
...additionalAttributes,
8694
},
8795
});
8896

@@ -138,8 +146,15 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
138146
return original.apply(this, arguments);
139147
}
140148

141-
const span = thisPlugin._startAwsSpan(awsRequest);
142-
thisPlugin.servicesExtensions.requestHook(awsRequest, span);
149+
const requestMetadata = thisPlugin.servicesExtensions.requestHook(
150+
awsRequest
151+
);
152+
const span = thisPlugin._startAwsSpan(
153+
awsRequest,
154+
requestMetadata.spanAttributes,
155+
requestMetadata.spanKind,
156+
requestMetadata.spanName
157+
);
143158
thisPlugin._callPreRequestHooks(span, awsRequest);
144159
thisPlugin._registerCompletedEvent(span, awsRequest);
145160

@@ -165,10 +180,14 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
165180
return original.apply(this, arguments);
166181
}
167182

168-
const span = thisPlugin._startAwsSpan(awsRequest);
169183
const requestMetadata = thisPlugin.servicesExtensions.requestHook(
184+
awsRequest
185+
);
186+
const span = thisPlugin._startAwsSpan(
170187
awsRequest,
171-
span
188+
requestMetadata.spanAttributes,
189+
requestMetadata.spanKind,
190+
requestMetadata.spanName
172191
);
173192
thisPlugin._callPreRequestHooks(span, awsRequest);
174193
thisPlugin._registerCompletedEvent(span, awsRequest);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
1-
import { Span } from "@opentelemetry/api";
1+
import { Span, Attributes, SpanKind } from "@opentelemetry/api";
22

33
/*
44
isIncoming - if true, then the operation callback / promise should be bind with the operation's span
55
*/
66
export interface RequestMetadata {
77
isIncoming: boolean;
8+
spanAttributes?: Attributes;
9+
spanKind?: SpanKind;
10+
spanName?: string;
811
}
912

1013
export interface ServiceExtension {
11-
requestHook: (request: AWS.Request<any, any>, span: Span) => RequestMetadata;
14+
// called before request is sent, and before span is started
15+
requestHook: (request: AWS.Request<any, any>) => RequestMetadata;
1216
responseHook: (response: AWS.Response<any, any>, span: Span) => void;
1317
}

packages/plugin-aws-sdk/src/services/ServicesExtensions.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,14 @@ export class ServicesExtensions implements ServiceExtension {
1010
this.services.set("sqs", new SqsServiceExtension(tracer));
1111
}
1212

13-
requestHook(request: AWS.Request<any, any>, span: Span): RequestMetadata {
13+
requestHook(request: AWS.Request<any, any>): RequestMetadata {
1414
const serviceId = (request as any)?.service?.serviceIdentifier;
1515
const serviceExtension = this.services.get(serviceId);
16-
if (!serviceExtension) return { isIncoming: false };
17-
return serviceExtension.requestHook(request, span);
16+
if (!serviceExtension)
17+
return {
18+
isIncoming: false,
19+
};
20+
return serviceExtension.requestHook(request);
1821
}
1922

2023
responseHook(response: AWS.Response<any, any>, span: Span) {

packages/plugin-aws-sdk/src/services/sqs.ts

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,32 +27,47 @@ export class SqsServiceExtension implements ServiceExtension {
2727
this.tracer = tracer;
2828
}
2929

30-
requestHook(request: AWS.Request<any, any>, span: Span): RequestMetadata {
30+
requestHook(request: AWS.Request<any, any>): RequestMetadata {
3131
const queueUrl = this.extractQueueUrl(request);
3232
const queueName = this.extractQueueNameFromUrl(queueUrl);
33-
34-
span.setAttribute(SqsAttributeNames.MESSAGING_SYSTEM, "aws.sqs");
35-
span.setAttribute(SqsAttributeNames.MESSAGING_DESTINATIONKIND, "queue");
36-
span.setAttribute(SqsAttributeNames.MESSAGING_DESTINATION, queueName);
37-
span.setAttribute(SqsAttributeNames.MESSAGING_URL, queueUrl);
33+
let spanKind: SpanKind = SpanKind.CLIENT;
34+
let spanName: string;
35+
36+
const spanAttributes = {
37+
[SqsAttributeNames.MESSAGING_SYSTEM]: "aws.sqs",
38+
[SqsAttributeNames.MESSAGING_DESTINATIONKIND]: "queue",
39+
[SqsAttributeNames.MESSAGING_DESTINATION]: queueName,
40+
[SqsAttributeNames.MESSAGING_URL]: queueUrl,
41+
};
3842

3943
let isIncoming = false;
4044

4145
const operation = (request as any)?.operation;
4246
switch (operation) {
4347
case "receiveMessage":
4448
isIncoming = true;
45-
span.setAttribute(SqsAttributeNames.MESSAGING_OPERATION, "receive");
49+
spanKind = SpanKind.CONSUMER;
50+
spanName = queueName;
51+
spanAttributes[SqsAttributeNames.MESSAGING_OPERATION] = "receive";
52+
break;
53+
54+
case "sendMessage":
55+
case "sendMessageBatch":
56+
spanKind = SpanKind.PRODUCER;
57+
spanName = queueName;
4658
break;
4759
}
4860

4961
return {
5062
isIncoming,
63+
spanAttributes,
64+
spanKind,
65+
spanName,
5166
};
5267
}
5368

5469
responseHook = (response: AWS.Response<any, any>, span: Span) => {
55-
const messages: AWS.SQS.Message[] = response.data.Messages;
70+
const messages: AWS.SQS.Message[] = response?.data?.Messages;
5671
if (messages) {
5772
const queueUrl = this.extractQueueUrl((response as any)?.request);
5873
const queueName = this.extractQueueNameFromUrl(queueUrl);

0 commit comments

Comments
 (0)