Skip to content

Commit 8198baa

Browse files
authored
chore(instrumentation-mongoose): update semconv usage to ATTR_ exports (#3062)
Refs: #2377
1 parent 35ac722 commit 8198baa

File tree

8 files changed

+191
-86
lines changed

8 files changed

+191
-86
lines changed

packages/instrumentation-mongoose/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,7 @@
6969
},
7070
"dependencies": {
7171
"@opentelemetry/core": "^2.0.0",
72-
"@opentelemetry/instrumentation": "^0.205.0",
73-
"@opentelemetry/semantic-conventions": "^1.27.0"
72+
"@opentelemetry/instrumentation": "^0.205.0"
7473
},
7574
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-mongoose#readme"
7675
}

packages/instrumentation-mongoose/src/mongoose.ts

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ import {
3030
/** @knipignore */
3131
import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
3232
import {
33-
SEMATTRS_DB_OPERATION,
34-
SEMATTRS_DB_STATEMENT,
35-
SEMATTRS_DB_SYSTEM,
36-
} from '@opentelemetry/semantic-conventions';
33+
ATTR_DB_OPERATION,
34+
ATTR_DB_STATEMENT,
35+
ATTR_DB_SYSTEM,
36+
} from './semconv';
3737

3838
const contextCaptureFunctionsCommon = [
3939
'deleteOne',
@@ -210,13 +210,10 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
210210
const attributes: Attributes = {};
211211
const { dbStatementSerializer } = self.getConfig();
212212
if (dbStatementSerializer) {
213-
attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer(
214-
'aggregate',
215-
{
216-
options: this.options,
217-
aggregatePipeline: this._pipeline,
218-
}
219-
);
213+
attributes[ATTR_DB_STATEMENT] = dbStatementSerializer('aggregate', {
214+
options: this.options,
215+
aggregatePipeline: this._pipeline,
216+
});
220217
}
221218

222219
const span = self._startSpan(
@@ -254,7 +251,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
254251
const attributes: Attributes = {};
255252
const { dbStatementSerializer } = self.getConfig();
256253
if (dbStatementSerializer) {
257-
attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer(this.op, {
254+
attributes[ATTR_DB_STATEMENT] = dbStatementSerializer(this.op, {
258255
condition: this._conditions,
259256
updates: this._update,
260257
options: this.options,
@@ -299,7 +296,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
299296
const attributes: Attributes = {};
300297
const { dbStatementSerializer } = self.getConfig();
301298
if (dbStatementSerializer) {
302-
attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer(
299+
attributes[ATTR_DB_STATEMENT] = dbStatementSerializer(
303300
op,
304301
serializePayload
305302
);
@@ -367,7 +364,7 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
367364
const attributes: Attributes = {};
368365
const { dbStatementSerializer } = self.getConfig();
369366
if (dbStatementSerializer) {
370-
attributes[SEMATTRS_DB_STATEMENT] = dbStatementSerializer(
367+
attributes[ATTR_DB_STATEMENT] = dbStatementSerializer(
371368
op,
372369
serializePayload
373370
);
@@ -436,8 +433,8 @@ export class MongooseInstrumentation extends InstrumentationBase<MongooseInstrum
436433
attributes: {
437434
...attributes,
438435
...getAttributesFromCollection(collection),
439-
[SEMATTRS_DB_OPERATION]: operation,
440-
[SEMATTRS_DB_SYSTEM]: 'mongoose',
436+
[ATTR_DB_OPERATION]: operation,
437+
[ATTR_DB_SYSTEM]: 'mongoose',
441438
},
442439
},
443440
parentSpan ? trace.setSpan(context.active(), parentSpan) : undefined
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/*
18+
* This file contains a copy of unstable semantic convention definitions
19+
* used by this package.
20+
* @see https://github.com/open-telemetry/opentelemetry-js/tree/main/semantic-conventions#unstable-semconv
21+
*/
22+
23+
/**
24+
* Deprecated, use `db.collection.name` instead.
25+
*
26+
* @example "mytable"
27+
*
28+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
29+
*
30+
* @deprecated Replaced by `db.collection.name`.
31+
*/
32+
export const ATTR_DB_MONGODB_COLLECTION = 'db.mongodb.collection' as const;
33+
34+
/**
35+
* Deprecated, use `db.namespace` instead.
36+
*
37+
* @example customers
38+
* @example main
39+
*
40+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
41+
*
42+
* @deprecated Replaced by `db.namespace`.
43+
*/
44+
export const ATTR_DB_NAME = 'db.name' as const;
45+
46+
/**
47+
* Deprecated, use `db.operation.name` instead.
48+
*
49+
* @example findAndModify
50+
* @example HMSET
51+
* @example SELECT
52+
*
53+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
54+
*
55+
* @deprecated Replaced by `db.operation.name`.
56+
*/
57+
export const ATTR_DB_OPERATION = 'db.operation' as const;
58+
59+
/**
60+
* The database statement being executed.
61+
*
62+
* @example SELECT * FROM wuser_table
63+
* @example SET mykey "WuValue"
64+
*
65+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
66+
*
67+
* @deprecated Replaced by `db.query.text`.
68+
*/
69+
export const ATTR_DB_STATEMENT = 'db.statement' as const;
70+
71+
/**
72+
* Deprecated, use `db.system.name` instead.
73+
*
74+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
75+
*
76+
* @deprecated Replaced by `db.system.name`.
77+
*/
78+
export const ATTR_DB_SYSTEM = 'db.system' as const;
79+
80+
/**
81+
* Deprecated, no replacement at this time.
82+
*
83+
* @example readonly_user
84+
* @example reporting_user
85+
*
86+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
87+
*
88+
* @deprecated Removed, no replacement at this time.
89+
*/
90+
export const ATTR_DB_USER = 'db.user' as const;
91+
92+
/**
93+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
94+
*
95+
* @example example.com
96+
*
97+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
98+
*
99+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
100+
*/
101+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
102+
103+
/**
104+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
105+
*
106+
* @example 8080
107+
*
108+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
109+
*
110+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
111+
*/
112+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;

packages/instrumentation-mongoose/src/utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,22 @@ import type { Collection } from 'mongoose';
1818
import { MongooseResponseCustomAttributesFunction } from './types';
1919
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
2020
import {
21-
SEMATTRS_DB_MONGODB_COLLECTION,
22-
SEMATTRS_DB_NAME,
23-
SEMATTRS_DB_USER,
24-
SEMATTRS_NET_PEER_NAME,
25-
SEMATTRS_NET_PEER_PORT,
26-
} from '@opentelemetry/semantic-conventions';
21+
ATTR_DB_MONGODB_COLLECTION,
22+
ATTR_DB_NAME,
23+
ATTR_DB_USER,
24+
ATTR_NET_PEER_NAME,
25+
ATTR_NET_PEER_PORT,
26+
} from './semconv';
2727

2828
export function getAttributesFromCollection(
2929
collection: Collection
3030
): Attributes {
3131
return {
32-
[SEMATTRS_DB_MONGODB_COLLECTION]: collection.name,
33-
[SEMATTRS_DB_NAME]: collection.conn.name,
34-
[SEMATTRS_DB_USER]: collection.conn.user,
35-
[SEMATTRS_NET_PEER_NAME]: collection.conn.host,
36-
[SEMATTRS_NET_PEER_PORT]: collection.conn.port,
32+
[ATTR_DB_MONGODB_COLLECTION]: collection.name,
33+
[ATTR_DB_NAME]: collection.conn.name,
34+
[ATTR_DB_USER]: collection.conn.user,
35+
[ATTR_NET_PEER_NAME]: collection.conn.host,
36+
[ATTR_NET_PEER_PORT]: collection.conn.port,
3737
};
3838
}
3939

packages/instrumentation-mongoose/test/asserts.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,28 @@
1616
import { expect } from 'expect';
1717
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
1818
import {
19-
SEMATTRS_DB_MONGODB_COLLECTION,
20-
SEMATTRS_DB_NAME,
21-
SEMATTRS_DB_STATEMENT,
22-
SEMATTRS_DB_SYSTEM,
23-
SEMATTRS_NET_PEER_NAME,
24-
SEMATTRS_NET_PEER_PORT,
25-
} from '@opentelemetry/semantic-conventions';
19+
ATTR_DB_MONGODB_COLLECTION,
20+
ATTR_DB_NAME,
21+
ATTR_DB_STATEMENT,
22+
ATTR_DB_SYSTEM,
23+
ATTR_NET_PEER_NAME,
24+
ATTR_NET_PEER_PORT,
25+
} from '../src/semconv';
2626
import { SpanStatusCode } from '@opentelemetry/api';
2727
import { SerializerPayload } from '../src';
2828
import { DB_NAME, MONGO_HOST, MONGO_PORT } from './config';
2929
import User from './user';
3030

3131
export const assertSpan = (span: ReadableSpan) => {
3232
expect(span.status.code).toBe(SpanStatusCode.UNSET);
33-
expect(span.attributes[SEMATTRS_DB_SYSTEM]).toEqual('mongoose');
34-
expect(span.attributes[SEMATTRS_DB_MONGODB_COLLECTION]).toEqual(
33+
expect(span.attributes[ATTR_DB_SYSTEM]).toEqual('mongoose');
34+
expect(span.attributes[ATTR_DB_MONGODB_COLLECTION]).toEqual(
3535
User.collection.name
3636
);
37-
expect(span.attributes[SEMATTRS_DB_NAME]).toEqual(DB_NAME);
38-
expect(span.attributes[SEMATTRS_NET_PEER_NAME]).toEqual(MONGO_HOST);
39-
expect(span.attributes[SEMATTRS_NET_PEER_PORT]).toEqual(MONGO_PORT);
37+
expect(span.attributes[ATTR_DB_NAME]).toEqual(DB_NAME);
38+
expect(span.attributes[ATTR_NET_PEER_NAME]).toEqual(MONGO_HOST);
39+
expect(span.attributes[ATTR_NET_PEER_PORT]).toEqual(MONGO_PORT);
4040
};
4141

4242
export const getStatement = (span: ReadableSpan): SerializerPayload =>
43-
JSON.parse(span.attributes[SEMATTRS_DB_STATEMENT] as string);
43+
JSON.parse(span.attributes[ATTR_DB_STATEMENT] as string);

0 commit comments

Comments
 (0)