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

Commit faec15b

Browse files
author
Amir Blum
committed
feat(plugin-aws-sdk): add custom attributes hook
1 parent ee0f38d commit faec15b

File tree

5 files changed

+271
-168
lines changed

5 files changed

+271
-168
lines changed

packages/plugin-aws-sdk/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,11 @@ const provider = new NodeTracerProvider({
2525
},
2626
});
2727
```
28+
29+
### aws-sdk Plugin Options
30+
31+
aws-sdk plugin has few options available to choose from. You can set the following:
32+
33+
| Options | Type | Description |
34+
| -------------- | -------------------------------------- | ----------------------------------------------------------------------------------------------- |
35+
| `preRequestHook` | `AwsSdkRequestCustomAttributeFunction` | Hook called before request send, which allow to add custom attributes to span. |

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

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@
99
callback | 1 | 2
1010
*/
1111
import { BasePlugin } from "@opentelemetry/core";
12-
import { Span } from "@opentelemetry/api";
12+
import { Span, CanonicalCode } from "@opentelemetry/api";
1313
import * as shimmer from "shimmer";
1414
import AWS from "aws-sdk";
1515
import { AttributeNames } from "./enums";
1616
import {
1717
getRequestServiceAttributes,
1818
getResponseServiceAttributes,
1919
} from "./services";
20+
import { AwsSdkPluginConfig } from "./types";
2021

2122
const VERSION = "0.0.3";
2223

2324
class AwsPlugin extends BasePlugin<typeof AWS> {
2425
readonly component: string;
26+
protected _config: AwsSdkPluginConfig;
2527
private activeRequests: Set<AWS.Request<any, any>> = new Set();
2628

2729
constructor(readonly moduleName: string) {
@@ -60,13 +62,12 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
6062

6163
private _getPatchedRequestMethod = (original: Function) => {
6264
const thisPlugin = this;
63-
6465
return function () {
6566
let span: Span | null = null;
6667
/*
67-
if the span was already started, we don't want to start a new one
68-
when Request.promise() is called
69-
*/
68+
if the span was already started, we don't want to start a new one
69+
when Request.promise() is called
70+
*/
7071

7172
if (
7273
this._asm.currentState !== "complete" &&
@@ -78,7 +79,6 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
7879
attributes: {
7980
[AttributeNames.COMPONENT]: thisPlugin.moduleName,
8081
[AttributeNames.AWS_OPERATION]: this.operation,
81-
[AttributeNames.AWS_PARAMS]: JSON.stringify(this.params),
8282
[AttributeNames.AWS_SIGNATURE_VERSION]: this.service?.config
8383
?.signatureVersion,
8484
[AttributeNames.AWS_REGION]: this.service?.config?.region,
@@ -90,6 +90,14 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
9090
},
9191
});
9292

93+
if (thisPlugin._config?.preRequestHook) {
94+
thisPlugin._safeExecute(
95+
span,
96+
() => thisPlugin._config.preRequestHook(span, this),
97+
false
98+
);
99+
}
100+
93101
(this as AWS.Request<any, any>).on("complete", (response) => {
94102
if (thisPlugin.activeRequests.has(this)) {
95103
thisPlugin.activeRequests.delete(this);
@@ -121,6 +129,33 @@ class AwsPlugin extends BasePlugin<typeof AWS> {
121129
request.operation
122130
}`;
123131
};
132+
133+
private _safeExecute<
134+
T extends (...args: unknown[]) => ReturnType<T>,
135+
K extends boolean
136+
>(
137+
span: Span,
138+
execute: T,
139+
rethrow: K
140+
): K extends true ? ReturnType<T> : ReturnType<T> | void;
141+
private _safeExecute<T extends (...args: unknown[]) => ReturnType<T>>(
142+
span: Span,
143+
execute: T,
144+
rethrow: boolean
145+
): ReturnType<T> | void {
146+
try {
147+
return execute();
148+
} catch (error) {
149+
if (rethrow) {
150+
span.setStatus({
151+
code: CanonicalCode.UNKNOWN,
152+
});
153+
span.end();
154+
throw error;
155+
}
156+
this._logger.error("caught error ", error);
157+
}
158+
}
124159
}
125160

126161
export const plugin = new AwsPlugin("aws-sdk");

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ export enum AttributeNames {
88
AWS_SERVICE_NAME = "aws.service.name",
99
AWS_SERVICE_IDENTIFIER = "aws.service.identifier",
1010
AWS_REQUEST_ID = "aws.request.id",
11-
AWS_PARAMS = "aws.params",
1211
AWS_SIGNATURE_VERSION = "aws.signature.version",
1312
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { PluginConfig, Span } from "@opentelemetry/api";
2+
import AWS from "aws-sdk";
3+
4+
export interface AwsSdkRequestCustomAttributeFunction {
5+
(span: Span, request: AWS.Request<any, any>): void;
6+
}
7+
8+
export interface AwsSdkPluginConfig extends PluginConfig {
9+
/** hook for adding custom attributes before producer message is sent */
10+
preRequestHook?: AwsSdkRequestCustomAttributeFunction;
11+
}

0 commit comments

Comments
 (0)