Skip to content

Commit d68bdde

Browse files
authored
chore(instrumentation-mongodb): update semconv usage to ATTR_ exports (#3057)
Refs: #2377
1 parent a914e34 commit d68bdde

File tree

7 files changed

+167
-48
lines changed

7 files changed

+167
-48
lines changed

packages/instrumentation-mongodb/package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@
7272
"typescript": "5.0.4"
7373
},
7474
"dependencies": {
75-
"@opentelemetry/instrumentation": "^0.205.0",
76-
"@opentelemetry/semantic-conventions": "^1.27.0"
75+
"@opentelemetry/instrumentation": "^0.205.0"
7776
},
7877
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-mongodb#readme"
7978
}

packages/instrumentation-mongodb/src/instrumentation.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ import {
2828
safeExecuteInTheMiddle,
2929
} from '@opentelemetry/instrumentation';
3030
import {
31-
DBSYSTEMVALUES_MONGODB,
32-
SEMATTRS_DB_CONNECTION_STRING,
33-
SEMATTRS_DB_MONGODB_COLLECTION,
34-
SEMATTRS_DB_NAME,
35-
SEMATTRS_DB_OPERATION,
36-
SEMATTRS_DB_STATEMENT,
37-
SEMATTRS_DB_SYSTEM,
38-
SEMATTRS_NET_PEER_NAME,
39-
SEMATTRS_NET_PEER_PORT,
40-
} from '@opentelemetry/semantic-conventions';
31+
ATTR_DB_CONNECTION_STRING,
32+
ATTR_DB_MONGODB_COLLECTION,
33+
ATTR_DB_NAME,
34+
ATTR_DB_OPERATION,
35+
ATTR_DB_STATEMENT,
36+
ATTR_DB_SYSTEM,
37+
ATTR_NET_PEER_NAME,
38+
ATTR_NET_PEER_PORT,
39+
DB_SYSTEM_VALUE_MONGODB,
40+
} from './semconv';
4141
import { MongoDBInstrumentationConfig, CommandResult } from './types';
4242
import {
4343
CursorState,
@@ -913,18 +913,18 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
913913
) {
914914
// add database related attributes
915915
span.setAttributes({
916-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_MONGODB,
917-
[SEMATTRS_DB_NAME]: dbName,
918-
[SEMATTRS_DB_MONGODB_COLLECTION]: dbCollection,
919-
[SEMATTRS_DB_OPERATION]: operation,
920-
[SEMATTRS_DB_CONNECTION_STRING]: `mongodb://${host}:${port}/${dbName}`,
916+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_MONGODB,
917+
[ATTR_DB_NAME]: dbName,
918+
[ATTR_DB_MONGODB_COLLECTION]: dbCollection,
919+
[ATTR_DB_OPERATION]: operation,
920+
[ATTR_DB_CONNECTION_STRING]: `mongodb://${host}:${port}/${dbName}`,
921921
});
922922

923923
if (host && port) {
924-
span.setAttribute(SEMATTRS_NET_PEER_NAME, host);
924+
span.setAttribute(ATTR_NET_PEER_NAME, host);
925925
const portNumber = parseInt(port, 10);
926926
if (!isNaN(portNumber)) {
927-
span.setAttribute(SEMATTRS_NET_PEER_PORT, portNumber);
927+
span.setAttribute(ATTR_NET_PEER_PORT, portNumber);
928928
}
929929
}
930930
if (!commandObj) return;
@@ -939,7 +939,7 @@ export class MongoDBInstrumentation extends InstrumentationBase<MongoDBInstrumen
939939
safeExecuteInTheMiddle(
940940
() => {
941941
const query = dbStatementSerializer(commandObj);
942-
span.setAttribute(SEMATTRS_DB_STATEMENT, query);
942+
span.setAttribute(ATTR_DB_STATEMENT, query);
943943
},
944944
err => {
945945
if (err) {
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 `server.address`, `server.port` attributes instead.
25+
*
26+
* @example "Server=(localdb)\\v11.0;Integrated Security=true;"
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 `server.address` and `server.port`.
31+
*/
32+
export const ATTR_DB_CONNECTION_STRING = 'db.connection_string' as const;
33+
34+
/**
35+
* Deprecated, use `db.collection.name` instead.
36+
*
37+
* @example "mytable"
38+
*
39+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
40+
*
41+
* @deprecated Replaced by `db.collection.name`.
42+
*/
43+
export const ATTR_DB_MONGODB_COLLECTION = 'db.mongodb.collection' as const;
44+
45+
/**
46+
* Deprecated, use `db.namespace` instead.
47+
*
48+
* @example customers
49+
* @example main
50+
*
51+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
52+
*
53+
* @deprecated Replaced by `db.namespace`.
54+
*/
55+
export const ATTR_DB_NAME = 'db.name' as const;
56+
57+
/**
58+
* Deprecated, use `db.operation.name` instead.
59+
*
60+
* @example findAndModify
61+
* @example HMSET
62+
* @example SELECT
63+
*
64+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
65+
*
66+
* @deprecated Replaced by `db.operation.name`.
67+
*/
68+
export const ATTR_DB_OPERATION = 'db.operation' as const;
69+
70+
/**
71+
* The database statement being executed.
72+
*
73+
* @example SELECT * FROM wuser_table
74+
* @example SET mykey "WuValue"
75+
*
76+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
77+
*
78+
* @deprecated Replaced by `db.query.text`.
79+
*/
80+
export const ATTR_DB_STATEMENT = 'db.statement' as const;
81+
82+
/**
83+
* Deprecated, use `db.system.name` instead.
84+
*
85+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
86+
*
87+
* @deprecated Replaced by `db.system.name`.
88+
*/
89+
export const ATTR_DB_SYSTEM = 'db.system' as const;
90+
91+
/**
92+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
93+
*
94+
* @example example.com
95+
*
96+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
97+
*
98+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
99+
*/
100+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
101+
102+
/**
103+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
104+
*
105+
* @example 8080
106+
*
107+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
108+
*
109+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
110+
*/
111+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;
112+
113+
/**
114+
* Enum value "mongodb" for attribute {@link ATTR_DB_SYSTEM}.
115+
*
116+
* MongoDB
117+
*
118+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
119+
*/
120+
export const DB_SYSTEM_VALUE_MONGODB = 'mongodb' as const;

packages/instrumentation-mongodb/test/mongodb-v3.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ const instrumentation = registerInstrumentationTesting(
3434
import type { MongoClient, Collection } from 'mongodb';
3535
import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils';
3636
import {
37-
SEMATTRS_DB_STATEMENT,
38-
SEMATTRS_NET_PEER_NAME,
39-
SEMATTRS_NET_PEER_PORT,
40-
} from '@opentelemetry/semantic-conventions';
37+
ATTR_DB_STATEMENT,
38+
ATTR_NET_PEER_NAME,
39+
ATTR_NET_PEER_PORT,
40+
} from '../src/semconv';
4141

4242
// We can't use @ts-expect-error because it will fail depending on the used mongodb version on tests
4343
/* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -329,7 +329,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => {
329329
);
330330
const mongoSpan = spans.find(s => s.name === operationName);
331331
const dbStatement = JSON.parse(
332-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
332+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
333333
);
334334
assert.strictEqual(dbStatement[key], '?');
335335
done();
@@ -364,7 +364,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => {
364364
);
365365
const mongoSpan = spans.find(s => s.name === operationName);
366366
const dbStatement = JSON.parse(
367-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
367+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
368368
);
369369
assert.deepEqual(dbStatement, {
370370
aggregate: '?',
@@ -417,7 +417,7 @@ describe('MongoDBInstrumentation-Tracing-v3', () => {
417417
);
418418
const mongoSpan = spans.find(s => s.name === operationName);
419419
const dbStatement = JSON.parse(
420-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
420+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
421421
);
422422
assert.strictEqual(dbStatement[key], value);
423423
done();
@@ -656,11 +656,11 @@ describe('MongoDBInstrumentation-Tracing-v3', () => {
656656
(err, address) => {
657657
if (err) return done(err);
658658
assert.strictEqual(
659-
mongoSpan.attributes[SEMATTRS_NET_PEER_NAME],
659+
mongoSpan.attributes[ATTR_NET_PEER_NAME],
660660
address
661661
);
662662
assert.strictEqual(
663-
mongoSpan.attributes[SEMATTRS_NET_PEER_PORT],
663+
mongoSpan.attributes[ATTR_NET_PEER_PORT],
664664
process.env.MONGODB_PORT
665665
? parseInt(process.env.MONGODB_PORT)
666666
: 27017

packages/instrumentation-mongodb/test/mongodb-v4.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const instrumentation = registerInstrumentationTesting(
3535

3636
import type { MongoClient, Collection } from 'mongodb';
3737
import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils';
38-
import { SEMATTRS_DB_STATEMENT } from '@opentelemetry/semantic-conventions';
38+
import { ATTR_DB_STATEMENT } from '../src/semconv';
3939

4040
// We can't use @ts-expect-error because it will fail depending on the used mongodb version on tests
4141
/* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -365,7 +365,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {
365365
);
366366
const mongoSpan = spans.find(s => s.name === operationName);
367367
const dbStatement = JSON.parse(
368-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
368+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
369369
);
370370
assert.strictEqual(dbStatement[key], '?');
371371
done();
@@ -400,7 +400,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {
400400
);
401401
const mongoSpan = spans.find(s => s.name === operationName);
402402
const dbStatement = JSON.parse(
403-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
403+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
404404
);
405405
assert.deepEqual(dbStatement, {
406406
aggregate: '?',
@@ -453,7 +453,7 @@ describe('MongoDBInstrumentation-Tracing-v4', () => {
453453
);
454454
const mongoSpan = spans.find(s => s.name === operationName);
455455
const dbStatement = JSON.parse(
456-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
456+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
457457
);
458458
assert.strictEqual(dbStatement[key], value);
459459
done();

packages/instrumentation-mongodb/test/mongodb-v5-v6.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ let instrumentation: MongoDBInstrumentation;
4444

4545
import type { MongoClient, Collection } from 'mongodb';
4646
import { assertSpans, accessCollection, DEFAULT_MONGO_HOST } from './utils';
47-
import { SEMATTRS_DB_STATEMENT } from '@opentelemetry/semantic-conventions';
47+
import { ATTR_DB_STATEMENT } from '../src/semconv';
4848

4949
// We can't use @ts-expect-error because it will fail depending on the used mongodb version on tests
5050
/* eslint-disable @typescript-eslint/ban-ts-comment */
@@ -368,7 +368,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
368368
);
369369
const mongoSpan = spans.find(s => s.name === operationName);
370370
const dbStatement = JSON.parse(
371-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
371+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
372372
);
373373
assert.strictEqual(dbStatement[key], '?');
374374
done();
@@ -403,7 +403,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
403403
);
404404
const mongoSpan = spans.find(s => s.name === operationName);
405405
const dbStatement = JSON.parse(
406-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
406+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
407407
);
408408
assert.deepEqual(dbStatement, {
409409
aggregate: '?',
@@ -456,7 +456,7 @@ describe('MongoDBInstrumentation-Tracing-v5', () => {
456456
);
457457
const mongoSpan = spans.find(s => s.name === operationName);
458458
const dbStatement = JSON.parse(
459-
mongoSpan!.attributes[SEMATTRS_DB_STATEMENT] as string
459+
mongoSpan!.attributes[ATTR_DB_STATEMENT] as string
460460
);
461461
assert.strictEqual(dbStatement[key], value);
462462
done();

packages/instrumentation-mongodb/test/utils.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
import { SpanKind, SpanStatusCode } from '@opentelemetry/api';
1818
import {
19-
SEMATTRS_DB_CONNECTION_STRING,
20-
SEMATTRS_DB_OPERATION,
21-
SEMATTRS_DB_STATEMENT,
22-
SEMATTRS_DB_SYSTEM,
23-
SEMATTRS_NET_PEER_NAME,
24-
} from '@opentelemetry/semantic-conventions';
19+
ATTR_DB_CONNECTION_STRING,
20+
ATTR_DB_OPERATION,
21+
ATTR_DB_STATEMENT,
22+
ATTR_DB_SYSTEM,
23+
ATTR_NET_PEER_NAME,
24+
} from '../src/semconv';
2525
import { ReadableSpan } from '@opentelemetry/sdk-trace-base';
2626
import * as assert from 'assert';
2727
import type { MongoClient, MongoClientOptions, Collection } from 'mongodb';
@@ -99,25 +99,25 @@ export function assertSpans(
9999
assert.strictEqual(mongoSpan.name, expectedName);
100100
assert.strictEqual(mongoSpan.kind, expectedKind);
101101
assert.strictEqual(
102-
mongoSpan.attributes[SEMATTRS_DB_OPERATION],
102+
mongoSpan.attributes[ATTR_DB_OPERATION],
103103
expectedOperation
104104
);
105-
assert.strictEqual(mongoSpan.attributes[SEMATTRS_DB_SYSTEM], 'mongodb');
105+
assert.strictEqual(mongoSpan.attributes[ATTR_DB_SYSTEM], 'mongodb');
106106
assert.strictEqual(
107-
mongoSpan.attributes[SEMATTRS_NET_PEER_NAME],
107+
mongoSpan.attributes[ATTR_NET_PEER_NAME],
108108
process.env.MONGODB_HOST || DEFAULT_MONGO_HOST
109109
);
110110
assert.strictEqual(mongoSpan.status.code, SpanStatusCode.UNSET);
111111
if (expectedConnString) {
112112
assert.strictEqual(
113-
mongoSpan.attributes[SEMATTRS_DB_CONNECTION_STRING],
113+
mongoSpan.attributes[ATTR_DB_CONNECTION_STRING],
114114
expectedConnString
115115
);
116116
}
117117

118118
if (isEnhancedDatabaseReportingEnabled) {
119119
const dbStatement = JSON.parse(
120-
mongoSpan.attributes[SEMATTRS_DB_STATEMENT] as string
120+
mongoSpan.attributes[ATTR_DB_STATEMENT] as string
121121
);
122122
for (const key in dbStatement) {
123123
assert.notStrictEqual(dbStatement[key], '?');

0 commit comments

Comments
 (0)