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

Commit fca954f

Browse files
author
Amir Blum
committed
feat(plugin-aws-sdk): add span kind to request hook
1 parent 38014f0 commit fca954f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

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

Lines changed: 8 additions & 4 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, Attributes } 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";
@@ -71,12 +71,14 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
7171

7272
private _startAwsSpan(
7373
request: AWS.Request<any, any>,
74-
additionalAttributes?: Attributes
74+
additionalAttributes?: Attributes,
75+
spanKind?: SpanKind
7576
): Span {
7677
const operation = (request as any).operation;
7778
const service = (request as any).service;
7879

7980
const newSpan = this._tracer.startSpan(this._getSpanName(request), {
81+
kind: spanKind,
8082
attributes: {
8183
[AttributeNames.COMPONENT]: this.moduleName,
8284
[AttributeNames.AWS_OPERATION]: operation,
@@ -147,7 +149,8 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
147149
);
148150
const span = thisPlugin._startAwsSpan(
149151
awsRequest,
150-
requestMetadata.spanAttributes
152+
requestMetadata.spanAttributes,
153+
requestMetadata.spanKind
151154
);
152155
thisPlugin._callPreRequestHooks(span, awsRequest);
153156
thisPlugin._registerCompletedEvent(span, awsRequest);
@@ -179,7 +182,8 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
179182
);
180183
const span = thisPlugin._startAwsSpan(
181184
awsRequest,
182-
requestMetadata.spanAttributes
185+
requestMetadata.spanAttributes,
186+
requestMetadata.spanKind
183187
);
184188
thisPlugin._callPreRequestHooks(span, awsRequest);
185189
thisPlugin._registerCompletedEvent(span, awsRequest);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { Span, Attributes } 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;
88
spanAttributes?: Attributes;
9+
spanKind?: SpanKind;
910
}
1011

1112
export interface ServiceExtension {
12-
// called before request is sent, and before span is created
13+
// called before request is sent, and before span is started
1314
requestHook: (request: AWS.Request<any, any>) => RequestMetadata;
1415
responseHook: (response: AWS.Response<any, any>, span: Span) => void;
1516
}

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export class SqsServiceExtension implements ServiceExtension {
3030
requestHook(request: AWS.Request<any, any>): RequestMetadata {
3131
const queueUrl = this.extractQueueUrl(request);
3232
const queueName = this.extractQueueNameFromUrl(queueUrl);
33+
let spanKind: SpanKind = SpanKind.CLIENT;
3334

3435
const spanAttributes = {
3536
[SqsAttributeNames.MESSAGING_SYSTEM]: "aws.sqs",
@@ -44,18 +45,25 @@ export class SqsServiceExtension implements ServiceExtension {
4445
switch (operation) {
4546
case "receiveMessage":
4647
isIncoming = true;
48+
spanKind = SpanKind.CONSUMER;
4749
spanAttributes[SqsAttributeNames.MESSAGING_OPERATION] = "receive";
4850
break;
51+
52+
case "sendMessage":
53+
case "sendMessageBatch":
54+
spanKind = SpanKind.PRODUCER;
55+
break;
4956
}
5057

5158
return {
5259
isIncoming,
5360
spanAttributes,
61+
spanKind,
5462
};
5563
}
5664

5765
responseHook = (response: AWS.Response<any, any>, span: Span) => {
58-
const messages: AWS.SQS.Message[] = response.data.Messages;
66+
const messages: AWS.SQS.Message[] = response?.data?.Messages;
5967
if (messages) {
6068
const queueUrl = this.extractQueueUrl((response as any)?.request);
6169
const queueName = this.extractQueueNameFromUrl(queueUrl);

0 commit comments

Comments
 (0)