Skip to content

Commit 1d263e5

Browse files
chore(instrumentation-amqp): bump @types/amqplib and remove unnecessary casts (#2960)
Co-authored-by: David Luna <[email protected]>
1 parent bd9e782 commit 1d263e5

File tree

6 files changed

+28
-40
lines changed

6 files changed

+28
-40
lines changed

package-lock.json

Lines changed: 9 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/instrumentation-amqplib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@
5858
"devDependencies": {
5959
"@opentelemetry/api": "^1.3.0",
6060
"@opentelemetry/contrib-test-utils": "^0.49.0",
61-
"@types/amqplib": "^0.5.17",
61+
"@types/amqplib": "^0.10.7",
6262
"@types/lodash": "4.14.199",
6363
"@types/mocha": "10.0.10",
6464
"@types/node": "18.18.14",

packages/instrumentation-amqplib/src/amqplib.ts

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,9 @@ import {
6565
CONNECTION_ATTRIBUTES,
6666
getConnectionAttributesFromServer,
6767
getConnectionAttributesFromUrl,
68+
InstrumentationConnection,
6869
InstrumentationConsumeChannel,
70+
InstrumentationConsumeMessage,
6971
InstrumentationMessage,
7072
InstrumentationPublishChannel,
7173
isConfirmChannelTracing,
@@ -253,18 +255,11 @@ export class AmqplibInstrumentation extends InstrumentationBase<AmqplibInstrumen
253255
this,
254256
url,
255257
socketOptions,
256-
function (this: unknown, err, conn: Connection) {
258+
function (this: unknown, err, conn: InstrumentationConnection) {
257259
if (err == null) {
258260
const urlAttributes = getConnectionAttributesFromUrl(url);
259-
// the type of conn in @types/amqplib is amqp.Connection, but in practice the library send the
260-
// `serverProperties` on the `conn` and not in a property `connection`.
261-
// I don't have capacity to debug it currently but it should probably be fixed in @types or
262-
// in the package itself
263-
// currently setting as any to calm typescript
264-
const serverAttributes = getConnectionAttributesFromServer(
265-
conn as any
266-
);
267-
(conn as any)[CONNECTION_ATTRIBUTES] = {
261+
const serverAttributes = getConnectionAttributesFromServer(conn);
262+
conn[CONNECTION_ATTRIBUTES] = {
268263
...urlAttributes,
269264
...serverAttributes,
270265
};
@@ -402,7 +397,7 @@ export class AmqplibInstrumentation extends InstrumentationBase<AmqplibInstrumen
402397

403398
const patchedOnMessage = function (
404399
this: unknown,
405-
msg: InstrumentationMessage | null
400+
msg: InstrumentationConsumeMessage | null
406401
) {
407402
// msg is expected to be null for signaling consumer cancel notification
408403
// https://www.rabbitmq.com/consumer-cancel.html
@@ -724,7 +719,7 @@ export class AmqplibInstrumentation extends InstrumentationBase<AmqplibInstrumen
724719

725720
private callConsumeEndHook(
726721
span: Span,
727-
msg: ConsumeMessage,
722+
msg: InstrumentationMessage,
728723
rejected: boolean | null,
729724
endOperation: EndOperation
730725
) {

packages/instrumentation-amqplib/src/utils.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,15 @@ export const CONNECTION_ATTRIBUTES: unique symbol = Symbol(
4545
'opentelemetry.amqplib.connection.attributes'
4646
);
4747

48+
export type InstrumentationConnection = amqp.Connection & {
49+
[CONNECTION_ATTRIBUTES]?: Attributes;
50+
};
4851
export type InstrumentationPublishChannel = (
4952
| amqp.Channel
5053
| amqp.ConfirmChannel
51-
) & { connection: { [CONNECTION_ATTRIBUTES]: Attributes } };
54+
) & { connection: InstrumentationConnection };
5255
export type InstrumentationConsumeChannel = amqp.Channel & {
53-
connection: { [CONNECTION_ATTRIBUTES]: Attributes };
56+
connection: InstrumentationConnection;
5457
[CHANNEL_SPANS_NOT_ENDED]?: {
5558
msg: amqp.ConsumeMessage;
5659
timeOfConsume: HrTime;
@@ -60,6 +63,9 @@ export type InstrumentationConsumeChannel = amqp.Channel & {
6063
export type InstrumentationMessage = amqp.Message & {
6164
[MESSAGE_STORED_SPAN]?: Span;
6265
};
66+
export type InstrumentationConsumeMessage = amqp.ConsumeMessage & {
67+
[MESSAGE_STORED_SPAN]?: Span;
68+
};
6369

6470
const IS_CONFIRM_CHANNEL_CONTEXT_KEY: symbol = createContextKey(
6571
'opentelemetry.amqplib.channel.is-confirm-channel'
@@ -117,7 +123,7 @@ const extractConnectionAttributeOrLog = (
117123
};
118124

119125
export const getConnectionAttributesFromServer = (
120-
conn: amqp.Connection['connection']
126+
conn: amqp.Connection
121127
): Attributes => {
122128
const product = conn.serverProperties.product?.toLowerCase?.();
123129
if (product) {

packages/instrumentation-amqplib/test/amqplib-promise.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ const CHANNEL_CLOSED_IN_TEST = Symbol(
6868
);
6969

7070
describe('amqplib instrumentation promise model', () => {
71-
let conn: amqp.Connection;
71+
let conn: amqp.ChannelModel;
7272
before(async function () {
7373
if (!shouldTest) {
7474
this.skip();

packages/instrumentation-amqplib/test/utils.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import { rabbitMqUrl } from './config';
3333

3434
describe('utils', () => {
3535
describe('getConnectionAttributesFromServer', () => {
36-
let conn: amqp.Connection;
36+
let conn: amqp.ChannelModel;
3737
before(async function () {
3838
if (!shouldTest) {
3939
this.skip();

0 commit comments

Comments
 (0)