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

Commit 40e671f

Browse files
author
Nir Hadassi
authored
feat!: Upgrade to otel 17 (#66)
BREAKING CHANGE: All plugins require opentelemetry version ^0.17.0 to work
1 parent d3efe60 commit 40e671f

File tree

24 files changed

+185
-202
lines changed

24 files changed

+185
-202
lines changed

README.md

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,21 @@
1414

1515
js extensions for the [open-telemetry](https://opentelemetry.io/) project, from [Aspecto](https://www.aspecto.io/) with :heart:
1616

17-
**Compatible with [otel v0.16.0](https://github.com/open-telemetry/opentelemetry-js/releases/tag/v0.16.0)**
17+
**Compatible with [otel v0.17.0](https://github.com/open-telemetry/opentelemetry-js/releases/tag/v0.17.0)**
1818
## Instrumentations
19-
- [opentelemetry-instrumentation-kafkajs](./packages/instrumentation-kafkajs) - auto instrumentation for [`kafkajs`](https://kafka.js.org) [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-kafkajs.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-kafkajs)
20-
- [opentelemetry-instrumentation-aws-sdk](./packages/instrumentation-aws-sdk) - auto instrumentation for [`aws-sdk`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/) [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-aws-sdk.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-aws-sdk)
21-
- [opentelemetry-instrumentation-typeorm](./packages/instrumentation-typeorm) - auto instrumentation for [`TypeORM`](https://typeorm.io/) [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-typeorm.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-typeorm)
22-
- [opentelemetry-instrumentation-sequelize](./packages/instrumentation-sequelize) - auto instrumentation for [`Sequelize`](https://sequelize.org/)
23-
[![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-sequelize.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-sequelize)
24-
- [opentelemetry-instrumentation-mongoose](./packages/instrumentation-mongoose) - auto instrumentation for [`mongoose`](https://mongoosejs.com/)
25-
[![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-mongoose.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-mongoose)
19+
| Instrumentation Package | Instrumented Lib | NPM |
20+
| --- | --- | --- |
21+
| [opentelemetry-instrumentation-kafkajs](./packages/instrumentation-kafkajs) | [`kafkajs`](https://kafka.js.org) | [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-kafkajs.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-kafkajs) |
22+
| [opentelemetry-instrumentation-aws-sdk](./packages/instrumentation-aws-sdk) | [`aws-sdk`](https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/) | [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-aws-sdk.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-aws-sdk)
23+
| [opentelemetry-instrumentation-typeorm](./packages/instrumentation-typeorm) | [`TypeORM`](https://typeorm.io/) | [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-typeorm.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-typeorm) |
24+
| [opentelemetry-instrumentation-sequelize](./packages/instrumentation-sequelize) | [`Sequelize`](https://sequelize.org/) | [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-sequelize.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-sequelize) |
25+
| [opentelemetry-instrumentation-mongoose](./packages/instrumentation-mongoose) | [`mongoose`](https://mongoosejs.com/) | [![NPM version](https://img.shields.io/npm/v/opentelemetry-instrumentation-mongoose.svg)](https://www.npmjs.com/package/opentelemetry-instrumentation-mongoose) |
2626

2727
## Compatibility with opentelemetry versions
2828
### Instrumentations in this repo are using opentelemetry [Instrumentation API](https://github.com/open-telemetry/opentelemetry-js/tree/main/packages/opentelemetry-instrumentation).
2929
For documentation using with the old [plugin](https://github.com/open-telemetry/opentelemetry-js/blob/main/packages/opentelemetry-core/src/trace/Plugin.ts) api, please go [here](https://github.com/aspecto-io/opentelemetry-ext-js/tree/4393fff108c477d05ecd02dd7d9552ea1d482853).
3030

31-
**Tested and verified against otel v0.16.0**
31+
**Tested and verified against otel v0.17.0**
32+
- Versions 0.2.x of the instrumentations are compatible with otel version v0.17.0
3233
- Versions 0.1.x of the instrumentations are compatible with otel version v0.16.0
3334
- Versions 0.0.x of the instrumentations are compatible with otel version v0.15.0

packages/instrumentation-aws-sdk/package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
"url": "https://github.com/aspecto-io/opentelemetry-ext-js/issues"
3333
},
3434
"dependencies": {
35-
"@opentelemetry/api": "^0.16.0",
36-
"@opentelemetry/instrumentation": "^0.16.0",
37-
"@opentelemetry/semantic-conventions": "^0.16.0",
35+
"@opentelemetry/api": "^0.17.0",
36+
"@opentelemetry/instrumentation": "^0.17.0",
37+
"@opentelemetry/semantic-conventions": "^0.17.0",
3838
"opentelemetry-propagation-utils": "^0.3.0"
3939
},
4040
"devDependencies": {
41-
"@opentelemetry/api": "^0.16.0",
42-
"@opentelemetry/node": "^0.16.0",
43-
"@opentelemetry/tracing": "^0.16.0",
41+
"@opentelemetry/api": "^0.17.0",
42+
"@opentelemetry/node": "^0.17.0",
43+
"@opentelemetry/tracing": "^0.17.0",
4444
"@types/mocha": "^8.2.0",
4545
"aws-sdk": "^2.780.0",
4646
"expect": "^26.6.2",

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

Lines changed: 28 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
no callback | 0 | 1
99
callback | 1 | 2
1010
*/
11-
import { Span, Attributes, SpanKind, context, setSpan, suppressInstrumentation, Context } from '@opentelemetry/api';
11+
import { Span, SpanAttributes, SpanKind, context, setSpan, suppressInstrumentation, Context, diag } from '@opentelemetry/api';
1212
import AWS from 'aws-sdk';
1313
import { AttributeNames } from './enums';
1414
import { ServicesExtensions } from './services';
@@ -37,7 +37,6 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
3737

3838
setConfig(config: Config = {}) {
3939
this._config = Object.assign({}, config);
40-
if (config.logger) this._logger = config.logger;
4140
}
4241

4342
protected init(): InstrumentationModuleDefinition<typeof AWS> {
@@ -51,9 +50,9 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
5150
}
5251

5352
protected patch(moduleExports: typeof AWS) {
54-
this.servicesExtensions = new ServicesExtensions(this.tracer, this._logger, this._config);
53+
this.servicesExtensions = new ServicesExtensions(this.tracer, this._config);
5554

56-
this._logger.debug(`applying patch to ${AwsInstrumentation.component}`);
55+
diag.debug(`applying patch to ${AwsInstrumentation.component}`);
5756
this.unpatch(moduleExports);
5857
this._wrap(moduleExports?.Request.prototype, 'send', this._getRequestSendPatch.bind(this));
5958
this._wrap(moduleExports?.Request.prototype, 'promise', this._getRequestPromisePatch.bind(this));
@@ -82,7 +81,7 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
8281

8382
private _startAwsSpan(
8483
request: AWS.Request<any, any>,
85-
additionalAttributes?: Attributes,
84+
additionalSpanAttributes?: SpanAttributes,
8685
spanKind?: SpanKind,
8786
spanName?: string
8887
): Span {
@@ -100,7 +99,7 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
10099
[AttributeNames.AWS_SERVICE_API]: service?.api?.className,
101100
[AttributeNames.AWS_SERVICE_IDENTIFIER]: service?.serviceIdentifier,
102101
[AttributeNames.AWS_SERVICE_NAME]: service?.api?.abbreviation,
103-
...additionalAttributes,
102+
...additionalSpanAttributes,
104103
},
105104
});
106105

@@ -114,7 +113,7 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
114113
() => this._config.preRequestHook(span, request),
115114
(e: Error) => {
116115
if (e)
117-
this._logger.error(`${AwsInstrumentation.component} instrumentation: preRequestHook error`, e);
116+
diag.error(`${AwsInstrumentation.component} instrumentation: preRequestHook error`, e);
118117
},
119118
true
120119
);
@@ -126,22 +125,22 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
126125
safeExecuteInTheMiddle(
127126
() => this._config.responseHook(span, response),
128127
(e: Error) => {
129-
if (e) this._logger.error(`${AwsInstrumentation.component} instrumentation: responseHook error`, e);
128+
if (e) diag.error(`${AwsInstrumentation.component} instrumentation: responseHook error`, e);
130129
},
131130
true
132131
);
133132
}
134133
}
135134

136135
private _registerCompletedEvent(span: Span, request: AWS.Request<any, any>, completedEventContext: Context) {
137-
const thisInstrumentation = this;
136+
const self = this;
138137
request.on('complete', (response) => {
139138
// read issue https://github.com/aspecto-io/opentelemetry-ext-js/issues/60
140139
context.with(completedEventContext, () => {
141-
if (!request[thisInstrumentation.REQUEST_SPAN_KEY]) {
140+
if (!request[self.REQUEST_SPAN_KEY]) {
142141
return;
143142
}
144-
request[thisInstrumentation.REQUEST_SPAN_KEY] = undefined;
143+
request[self.REQUEST_SPAN_KEY] = undefined;
145144

146145
if (response.error) {
147146
span.setAttribute(AttributeNames.AWS_ERROR, response.error);
@@ -150,28 +149,26 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
150149
this._callUserResponseHook(span, response);
151150
this.servicesExtensions.responseHook(response, span);
152151

153-
span.setAttributes({
154-
[AttributeNames.AWS_REQUEST_ID]: response.requestId,
155-
});
152+
span.setAttribute(AttributeNames.AWS_REQUEST_ID, response.requestId);
156153
span.end();
157154
});
158155
});
159156
}
160157

161158
private _getRequestSendPatch(original: (callback?: (err: any, data: any) => void) => void) {
162-
const thisInstrumentation = this;
159+
const self = this;
163160
return function (callback?: (err: any, data: any) => void) {
164161
const awsRequest: AWS.Request<any, any> = this;
165162
/*
166163
if the span was already started, we don't want to start a new one
167164
when Request.promise() is called
168165
*/
169-
if (this._asm.currentState === 'complete' || awsRequest[thisInstrumentation.REQUEST_SPAN_KEY]) {
166+
if (this._asm.currentState === 'complete' || awsRequest[self.REQUEST_SPAN_KEY]) {
170167
return original.apply(this, arguments);
171168
}
172169

173-
const requestMetadata = thisInstrumentation.servicesExtensions.requestHook(awsRequest);
174-
const span = thisInstrumentation._startAwsSpan(
170+
const requestMetadata = self.servicesExtensions.requestHook(awsRequest);
171+
const span = self._startAwsSpan(
175172
awsRequest,
176173
requestMetadata.spanAttributes,
177174
requestMetadata.spanKind,
@@ -180,44 +177,44 @@ export class AwsInstrumentation extends InstrumentationBase<typeof AWS> {
180177
const activeContextWithSpan = setSpan(context.active(), span);
181178
const callbackWithContext = context.bind(callback, activeContextWithSpan);
182179

183-
thisInstrumentation._callUserPreRequestHook(span, awsRequest);
184-
thisInstrumentation._registerCompletedEvent(span, awsRequest, activeContextWithSpan);
180+
self._callUserPreRequestHook(span, awsRequest);
181+
self._registerCompletedEvent(span, awsRequest, activeContextWithSpan);
185182

186183
return context.with(activeContextWithSpan, () => {
187-
thisInstrumentation.servicesExtensions.requestPostSpanHook(awsRequest);
188-
return thisInstrumentation._callOriginalFunction(() => original.call(awsRequest, callbackWithContext));
184+
self.servicesExtensions.requestPostSpanHook(awsRequest);
185+
return self._callOriginalFunction(() => original.call(awsRequest, callbackWithContext));
189186
});
190187
};
191188
}
192189

193190
private _getRequestPromisePatch(original: () => Promise<any>) {
194-
const thisInstrumentation = this;
191+
const self = this;
195192
return function (): Promise<any> {
196193
const awsRequest: AWS.Request<any, any> = this;
197194
// if the span was already started, we don't want to start a new one when Request.promise() is called
198-
if (this._asm.currentState === 'complete' || awsRequest[thisInstrumentation.REQUEST_SPAN_KEY]) {
195+
if (this._asm.currentState === 'complete' || awsRequest[self.REQUEST_SPAN_KEY]) {
199196
return original.apply(this, arguments);
200197
}
201198

202-
const requestMetadata = thisInstrumentation.servicesExtensions.requestHook(awsRequest);
203-
const span = thisInstrumentation._startAwsSpan(
199+
const requestMetadata = self.servicesExtensions.requestHook(awsRequest);
200+
const span = self._startAwsSpan(
204201
awsRequest,
205202
requestMetadata.spanAttributes,
206203
requestMetadata.spanKind,
207204
requestMetadata.spanName
208205
);
209206

210207
const activeContextWithSpan = setSpan(context.active(), span);
211-
thisInstrumentation._callUserPreRequestHook(span, awsRequest);
212-
thisInstrumentation._registerCompletedEvent(span, awsRequest, activeContextWithSpan);
208+
self._callUserPreRequestHook(span, awsRequest);
209+
self._registerCompletedEvent(span, awsRequest, activeContextWithSpan);
213210

214211
const origPromise: Promise<any> = context.with(activeContextWithSpan, () => {
215-
thisInstrumentation.servicesExtensions.requestPostSpanHook(awsRequest);
216-
return thisInstrumentation._callOriginalFunction(() => original.call(awsRequest, arguments));
212+
self.servicesExtensions.requestPostSpanHook(awsRequest);
213+
return self._callOriginalFunction(() => original.call(awsRequest, arguments));
217214
});
218215

219216
return requestMetadata.isIncoming
220-
? thisInstrumentation._bindPromise(origPromise, activeContextWithSpan)
217+
? self._bindPromise(origPromise, activeContextWithSpan)
221218
: origPromise;
222219
};
223220
}

packages/instrumentation-aws-sdk/src/services/ServiceExtension.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { Span, Attributes, SpanKind } from '@opentelemetry/api';
1+
import { Span, SpanAttributes, SpanKind } from '@opentelemetry/api';
22

33
export interface RequestMetadata {
44
// isIncoming - if true, then the operation callback / promise should be bind with the operation's span
55
isIncoming: boolean;
6-
spanAttributes?: Attributes;
6+
spanAttributes?: SpanAttributes;
77
spanKind?: SpanKind;
88
spanName?: string;
99
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Tracer, Span, Logger } from '@opentelemetry/api';
1+
import { Tracer, Span } from '@opentelemetry/api';
22
import { ServiceExtension, RequestMetadata } from './ServiceExtension';
33
import { SqsServiceExtension } from './sqs';
44
import * as AWS from 'aws-sdk';
@@ -8,8 +8,8 @@ import { DynamodbServiceExtension } from './dynamodb';
88
export class ServicesExtensions implements ServiceExtension {
99
services: Map<string, ServiceExtension> = new Map();
1010

11-
constructor(tracer: Tracer, logger: Logger, instrumentationConfig: AwsSdkInstrumentationConfig) {
12-
this.services.set('sqs', new SqsServiceExtension(tracer, logger, instrumentationConfig?.sqsProcessHook));
11+
constructor(tracer: Tracer, instrumentationConfig: AwsSdkInstrumentationConfig) {
12+
this.services.set('sqs', new SqsServiceExtension(tracer, instrumentationConfig?.sqsProcessHook));
1313
this.services.set('dynamodb', new DynamodbServiceExtension());
1414
}
1515

packages/instrumentation-aws-sdk/src/services/dynamodb.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Logger, SpanKind } from '@opentelemetry/api';
1+
import { SpanKind } from '@opentelemetry/api';
22
import { RequestMetadata, ServiceExtension } from './ServiceExtension';
33
import { DatabaseAttribute } from '@opentelemetry/semantic-conventions';
44

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
SpanKind,
44
Span,
55
propagation,
6-
Logger,
6+
diag,
77
TextMapGetter,
88
TextMapSetter,
99
setSpan,
@@ -61,7 +61,6 @@ const sqsContextGetter = new SqsContextGetter();
6161
export class SqsServiceExtension implements ServiceExtension {
6262
constructor(
6363
private tracer: Tracer,
64-
private logger: Logger,
6564
private sqsProcessHook: AwsSdkSqsProcessCustomAttributeFunction
6665
) {}
6766

@@ -180,7 +179,7 @@ export class SqsServiceExtension implements ServiceExtension {
180179
if (Object.keys(attributes).length < SQS_MAX_MESSAGE_ATTRIBUTES) {
181180
propagation.inject(context.active(), attributes, sqsContextSetter);
182181
} else {
183-
this.logger.warn(
182+
diag.warn(
184183
'OpenTelemetry aws-sdk instrumentation cannot set context propagation on SQS message due to maximum amount of MessageAttributes'
185184
);
186185
}

packages/instrumentation-aws-sdk/test/aws-sdk.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'mocha';
22
import { AwsInstrumentation } from '../src';
33
import { InMemorySpanExporter, SimpleSpanProcessor, ReadableSpan, Span } from '@opentelemetry/tracing';
4-
import { context, StatusCode } from '@opentelemetry/api';
4+
import { context, SpanStatusCode } from '@opentelemetry/api';
55
import { NodeTracerProvider } from '@opentelemetry/node';
66
import { ContextManager } from '@opentelemetry/context-base';
77
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
@@ -259,7 +259,7 @@ describe('instrumentation-aws-sdk', () => {
259259
s3.createBucket({ Bucket: bucketName }, async function (err, data) {
260260
const awsSpans = getAwsSpans();
261261
expect(awsSpans.length).toBe(1);
262-
expect(awsSpans[0].status.code).toStrictEqual(StatusCode.UNSET);
262+
expect(awsSpans[0].status.code).toStrictEqual(SpanStatusCode.UNSET);
263263
done();
264264
});
265265
});

packages/instrumentation-aws-sdk/test/sqs.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import 'mocha';
22
import { AwsInstrumentation } from '../src';
33
import { NodeTracerProvider } from '@opentelemetry/node';
44
import { ContextManager } from '@opentelemetry/context-base';
5-
import { context, SpanKind, StatusCode } from '@opentelemetry/api';
5+
import { context, SpanKind, SpanStatusCode } from '@opentelemetry/api';
66
import { InMemorySpanExporter, SimpleSpanProcessor, ReadableSpan, Span } from '@opentelemetry/tracing';
77
import { AsyncHooksContextManager } from '@opentelemetry/context-async-hooks';
88
import { mockAwsSend } from './testing-utils';
@@ -383,8 +383,8 @@ describe('sqs', () => {
383383
.getFinishedSpans()
384384
.filter((s) => s.attributes[SqsAttributeNames.MESSAGING_OPERATION] === 'process');
385385
expect(processSpans.length).toBe(2);
386-
expect(processSpans[0].status.code).toStrictEqual(StatusCode.UNSET);
387-
expect(processSpans[1].status.code).toStrictEqual(StatusCode.UNSET);
386+
expect(processSpans[0].status.code).toStrictEqual(SpanStatusCode.UNSET);
387+
expect(processSpans[1].status.code).toStrictEqual(SpanStatusCode.UNSET);
388388
});
389389
});
390390
});

packages/instrumentation-kafkajs/package.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@
3232
"url": "https://github.com/aspecto-io/opentelemetry-ext-js/issues"
3333
},
3434
"dependencies": {
35-
"@opentelemetry/api": "^0.16.0",
36-
"@opentelemetry/instrumentation": "^0.16.0",
37-
"@opentelemetry/semantic-conventions": "^0.16.0"
35+
"@opentelemetry/api": "^0.17.0",
36+
"@opentelemetry/instrumentation": "^0.17.0",
37+
"@opentelemetry/semantic-conventions": "^0.17.0"
3838
},
3939
"devDependencies": {
40-
"@opentelemetry/node": "^0.16.0",
41-
"@opentelemetry/tracing": "^0.16.0",
40+
"@opentelemetry/node": "^0.17.0",
41+
"@opentelemetry/tracing": "^0.17.0",
4242
"@types/mocha": "^8.2.0",
4343
"expect": "^26.6.2",
4444
"kafkajs": "^1.12.0",

0 commit comments

Comments
 (0)