Skip to content

Commit fe3dc37

Browse files
authored
chore(instrumentation-redis): update semconv usage to ATTR_ exports (#3064)
1 parent f69757f commit fe3dc37

File tree

6 files changed

+204
-121
lines changed

6 files changed

+204
-121
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
* The database statement being executed.
36+
*
37+
* @example SELECT * FROM wuser_table
38+
* @example SET mykey "WuValue"
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.query.text`.
43+
*/
44+
export const ATTR_DB_STATEMENT = 'db.statement' as const;
45+
46+
/**
47+
* Deprecated, use `db.system.name` instead.
48+
*
49+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
50+
*
51+
* @deprecated Replaced by `db.system.name`.
52+
*/
53+
export const ATTR_DB_SYSTEM = 'db.system' as const;
54+
55+
/**
56+
* Deprecated, use `server.address` on client spans and `client.address` on server spans.
57+
*
58+
* @example example.com
59+
*
60+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
61+
*
62+
* @deprecated Replaced by `server.address` on client spans and `client.address` on server spans.
63+
*/
64+
export const ATTR_NET_PEER_NAME = 'net.peer.name' as const;
65+
66+
/**
67+
* Deprecated, use `server.port` on client spans and `client.port` on server spans.
68+
*
69+
* @example 8080
70+
*
71+
* @experimental This attribute is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
72+
*
73+
* @deprecated Replaced by `server.port` on client spans and `client.port` on server spans.
74+
*/
75+
export const ATTR_NET_PEER_PORT = 'net.peer.port' as const;
76+
77+
/**
78+
* Enum value "redis" for attribute {@link ATTR_DB_SYSTEM_NAME}.
79+
*
80+
* [Redis](https://redis.io/)
81+
*
82+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
83+
*/
84+
export const DB_SYSTEM_NAME_VALUE_REDIS = 'redis' as const;
85+
86+
/**
87+
* Enum value "redis" for attribute {@link ATTR_DB_SYSTEM}.
88+
*
89+
* Redis
90+
*
91+
* @experimental This enum value is experimental and is subject to breaking changes in minor releases of `@opentelemetry/semantic-conventions`.
92+
*/
93+
export const DB_SYSTEM_VALUE_REDIS = 'redis' as const;

packages/instrumentation-redis/src/v2-v3/instrumentation.ts

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,21 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from '../version';
3333
import type { RedisCommand, RedisPluginClientTypes } from './internal-types';
3434
import { Attributes, SpanKind, context, trace } from '@opentelemetry/api';
3535
import {
36-
DBSYSTEMVALUES_REDIS,
37-
SEMATTRS_DB_CONNECTION_STRING,
38-
SEMATTRS_DB_STATEMENT,
39-
SEMATTRS_DB_SYSTEM,
40-
SEMATTRS_NET_PEER_NAME,
41-
SEMATTRS_NET_PEER_PORT,
4236
ATTR_DB_SYSTEM_NAME,
4337
ATTR_DB_QUERY_TEXT,
4438
ATTR_DB_OPERATION_NAME,
4539
ATTR_SERVER_ADDRESS,
4640
ATTR_SERVER_PORT,
4741
} from '@opentelemetry/semantic-conventions';
42+
import {
43+
ATTR_DB_CONNECTION_STRING,
44+
ATTR_DB_STATEMENT,
45+
ATTR_DB_SYSTEM,
46+
ATTR_NET_PEER_NAME,
47+
ATTR_NET_PEER_PORT,
48+
DB_SYSTEM_NAME_VALUE_REDIS,
49+
DB_SYSTEM_VALUE_REDIS,
50+
} from '../semconv';
4851
import { defaultDbStatementSerializer } from '@opentelemetry/redis-common';
4952

5053
export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrumentationConfig> {
@@ -156,17 +159,14 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
156159

157160
if (instrumentation._semconvStability & SemconvStability.OLD) {
158161
Object.assign(attributes, {
159-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
160-
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(
161-
cmd.command,
162-
cmd.args
163-
),
162+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
163+
[ATTR_DB_STATEMENT]: dbStatementSerializer(cmd.command, cmd.args),
164164
});
165165
}
166166

167167
if (instrumentation._semconvStability & SemconvStability.STABLE) {
168168
Object.assign(attributes, {
169-
[ATTR_DB_SYSTEM_NAME]: 'redis',
169+
[ATTR_DB_SYSTEM_NAME]: DB_SYSTEM_NAME_VALUE_REDIS,
170170
[ATTR_DB_OPERATION_NAME]: cmd.command,
171171
[ATTR_DB_QUERY_TEXT]: dbStatementSerializer(cmd.command, cmd.args),
172172
});
@@ -186,8 +186,8 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
186186

187187
if (instrumentation._semconvStability & SemconvStability.OLD) {
188188
Object.assign(connectionAttributes, {
189-
[SEMATTRS_NET_PEER_NAME]: this.connection_options.host,
190-
[SEMATTRS_NET_PEER_PORT]: this.connection_options.port,
189+
[ATTR_NET_PEER_NAME]: this.connection_options.host,
190+
[ATTR_NET_PEER_PORT]: this.connection_options.port,
191191
});
192192
}
193193

@@ -206,7 +206,7 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
206206
instrumentation._semconvStability & SemconvStability.OLD
207207
) {
208208
span.setAttribute(
209-
SEMATTRS_DB_CONNECTION_STRING,
209+
ATTR_DB_CONNECTION_STRING,
210210
`redis://${this.address}`
211211
);
212212
}

packages/instrumentation-redis/src/v4-v5/instrumentation.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from '../version';
3737
import {
3838
ATTR_DB_OPERATION_NAME,
3939
ATTR_DB_QUERY_TEXT,
40-
SEMATTRS_DB_STATEMENT,
4140
} from '@opentelemetry/semantic-conventions';
41+
import { ATTR_DB_STATEMENT } from '../semconv';
4242
import type { MultiErrorReply } from './internal-types';
4343

4444
const OTEL_OPEN_SPANS = Symbol(
@@ -417,7 +417,7 @@ export class RedisInstrumentationV4_V5 extends InstrumentationBase<RedisInstrume
417417
const dbStatement = dbStatementSerializer(commandName, commandArgs);
418418
if (dbStatement != null) {
419419
if (this._semconvStability & SemconvStability.OLD) {
420-
attributes[SEMATTRS_DB_STATEMENT] = dbStatement;
420+
attributes[ATTR_DB_STATEMENT] = dbStatement;
421421
}
422422
if (this._semconvStability & SemconvStability.STABLE) {
423423
attributes[ATTR_DB_QUERY_TEXT] = dbStatement;

packages/instrumentation-redis/src/v4-v5/utils.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,18 @@
1515
*/
1616
import { Attributes, DiagLogger } from '@opentelemetry/api';
1717
import {
18-
SEMATTRS_DB_SYSTEM,
19-
SEMATTRS_DB_CONNECTION_STRING,
20-
SEMATTRS_NET_PEER_NAME,
21-
SEMATTRS_NET_PEER_PORT,
22-
DBSYSTEMVALUES_REDIS,
2318
ATTR_DB_SYSTEM_NAME,
2419
ATTR_SERVER_ADDRESS,
2520
ATTR_SERVER_PORT,
2621
} from '@opentelemetry/semantic-conventions';
22+
import {
23+
ATTR_DB_SYSTEM,
24+
ATTR_DB_CONNECTION_STRING,
25+
ATTR_NET_PEER_NAME,
26+
ATTR_NET_PEER_PORT,
27+
DB_SYSTEM_VALUE_REDIS,
28+
DB_SYSTEM_NAME_VALUE_REDIS,
29+
} from '../semconv';
2730
import { SemconvStability } from '@opentelemetry/instrumentation';
2831

2932
export function getClientAttributes(
@@ -35,17 +38,17 @@ export function getClientAttributes(
3538

3639
if (semconvStability & SemconvStability.OLD) {
3740
Object.assign(attributes, {
38-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
39-
[SEMATTRS_NET_PEER_NAME]: options?.socket?.host,
40-
[SEMATTRS_NET_PEER_PORT]: options?.socket?.port,
41-
[SEMATTRS_DB_CONNECTION_STRING]:
41+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
42+
[ATTR_NET_PEER_NAME]: options?.socket?.host,
43+
[ATTR_NET_PEER_PORT]: options?.socket?.port,
44+
[ATTR_DB_CONNECTION_STRING]:
4245
removeCredentialsFromDBConnectionStringAttribute(diag, options?.url),
4346
});
4447
}
4548

4649
if (semconvStability & SemconvStability.STABLE) {
4750
Object.assign(attributes, {
48-
[ATTR_DB_SYSTEM_NAME]: 'redis',
51+
[ATTR_DB_SYSTEM_NAME]: DB_SYSTEM_NAME_VALUE_REDIS,
4952
[ATTR_SERVER_ADDRESS]: options?.socket?.host,
5053
[ATTR_SERVER_PORT]: options?.socket?.port,
5154
});

packages/instrumentation-redis/test/v2-v3/redis.test.ts

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,20 @@ import * as testUtils from '@opentelemetry/contrib-test-utils';
2727
import * as assert from 'assert';
2828
import { RedisInstrumentation } from '../../src';
2929
import {
30-
DBSYSTEMVALUES_REDIS,
31-
SEMATTRS_DB_CONNECTION_STRING,
32-
SEMATTRS_DB_STATEMENT,
33-
SEMATTRS_DB_SYSTEM,
34-
SEMATTRS_NET_PEER_NAME,
35-
SEMATTRS_NET_PEER_PORT,
3630
ATTR_DB_SYSTEM_NAME,
3731
ATTR_DB_OPERATION_NAME,
3832
ATTR_DB_QUERY_TEXT,
3933
ATTR_SERVER_ADDRESS,
4034
ATTR_SERVER_PORT,
4135
} from '@opentelemetry/semantic-conventions';
36+
import {
37+
ATTR_DB_CONNECTION_STRING,
38+
ATTR_DB_STATEMENT,
39+
ATTR_DB_SYSTEM,
40+
ATTR_NET_PEER_NAME,
41+
ATTR_NET_PEER_PORT,
42+
DB_SYSTEM_VALUE_REDIS,
43+
} from '../../src/semconv';
4244
import { SemconvStability } from '@opentelemetry/instrumentation';
4345

4446
process.env.OTEL_SEMCONV_STABILITY_OPT_IN = 'database/dup';
@@ -60,10 +62,10 @@ const DEFAULT_ATTRIBUTES = {
6062
[ATTR_DB_SYSTEM_NAME]: 'redis',
6163
[ATTR_SERVER_ADDRESS]: CONFIG.host,
6264
[ATTR_SERVER_PORT]: CONFIG.port,
63-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
64-
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
65-
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
66-
[SEMATTRS_DB_CONNECTION_STRING]: URL,
65+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
66+
[ATTR_NET_PEER_NAME]: CONFIG.host,
67+
[ATTR_NET_PEER_PORT]: CONFIG.port,
68+
[ATTR_DB_CONNECTION_STRING]: URL,
6769
};
6870

6971
const unsetStatus: SpanStatus = {
@@ -187,9 +189,9 @@ describe('redis v2-v3', () => {
187189
instrumentation.setConfig({ semconvStability: SemconvStability.OLD });
188190

189191
recordSpanForOperation(operation, span => {
190-
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], 'redis');
192+
assert.strictEqual(span.attributes[ATTR_DB_SYSTEM], 'redis');
191193
assert.strictEqual(
192-
span.attributes[SEMATTRS_DB_STATEMENT],
194+
span.attributes[ATTR_DB_STATEMENT],
193195
`${operation.command} ${operation.expectedDbStatementOld}`
194196
);
195197

@@ -198,17 +200,14 @@ describe('redis v2-v3', () => {
198200
assert.ok(!(ATTR_DB_OPERATION_NAME in span.attributes));
199201

200202
assert.strictEqual(
201-
span.attributes[SEMATTRS_NET_PEER_NAME],
203+
span.attributes[ATTR_NET_PEER_NAME],
202204
CONFIG.host
203205
);
204206
assert.strictEqual(
205-
span.attributes[SEMATTRS_NET_PEER_PORT],
207+
span.attributes[ATTR_NET_PEER_PORT],
206208
CONFIG.port
207209
);
208-
assert.strictEqual(
209-
span.attributes[SEMATTRS_DB_CONNECTION_STRING],
210-
URL
211-
);
210+
assert.strictEqual(span.attributes[ATTR_DB_CONNECTION_STRING], URL);
212211

213212
assert.ok(!(ATTR_SERVER_ADDRESS in span.attributes));
214213
assert.ok(!(ATTR_SERVER_PORT in span.attributes));
@@ -232,18 +231,18 @@ describe('redis v2-v3', () => {
232231
operation.command
233232
);
234233

235-
assert.ok(!(SEMATTRS_DB_SYSTEM in span.attributes));
236-
assert.ok(!(SEMATTRS_DB_STATEMENT in span.attributes));
234+
assert.ok(!(ATTR_DB_SYSTEM in span.attributes));
235+
assert.ok(!(ATTR_DB_STATEMENT in span.attributes));
237236

238237
assert.strictEqual(
239238
span.attributes[ATTR_SERVER_ADDRESS],
240239
CONFIG.host
241240
);
242241
assert.strictEqual(span.attributes[ATTR_SERVER_PORT], CONFIG.port);
243242

244-
assert.ok(!(SEMATTRS_NET_PEER_NAME in span.attributes));
245-
assert.ok(!(SEMATTRS_NET_PEER_PORT in span.attributes));
246-
assert.ok(!(SEMATTRS_DB_CONNECTION_STRING in span.attributes));
243+
assert.ok(!(ATTR_NET_PEER_NAME in span.attributes));
244+
assert.ok(!(ATTR_NET_PEER_PORT in span.attributes));
245+
assert.ok(!(ATTR_DB_CONNECTION_STRING in span.attributes));
247246
done();
248247
});
249248
});
@@ -254,9 +253,9 @@ describe('redis v2-v3', () => {
254253
});
255254

256255
recordSpanForOperation(operation, span => {
257-
assert.strictEqual(span.attributes[SEMATTRS_DB_SYSTEM], 'redis');
256+
assert.strictEqual(span.attributes[ATTR_DB_SYSTEM], 'redis');
258257
assert.strictEqual(
259-
span.attributes[SEMATTRS_DB_STATEMENT],
258+
span.attributes[ATTR_DB_STATEMENT],
260259
`${operation.command} ${operation.expectedDbStatementOld}`
261260
);
262261
assert.strictEqual(span.attributes[ATTR_DB_SYSTEM_NAME], 'redis');
@@ -270,22 +269,19 @@ describe('redis v2-v3', () => {
270269
);
271270

272271
assert.strictEqual(
273-
span.attributes[SEMATTRS_NET_PEER_NAME],
272+
span.attributes[ATTR_NET_PEER_NAME],
274273
CONFIG.host
275274
);
276275
assert.strictEqual(
277-
span.attributes[SEMATTRS_NET_PEER_PORT],
276+
span.attributes[ATTR_NET_PEER_PORT],
278277
CONFIG.port
279278
);
280279
assert.strictEqual(
281280
span.attributes[ATTR_SERVER_ADDRESS],
282281
CONFIG.host
283282
);
284283
assert.strictEqual(span.attributes[ATTR_SERVER_PORT], CONFIG.port);
285-
assert.strictEqual(
286-
span.attributes[SEMATTRS_DB_CONNECTION_STRING],
287-
URL
288-
);
284+
assert.strictEqual(span.attributes[ATTR_DB_CONNECTION_STRING], URL);
289285
done();
290286
});
291287
});
@@ -299,7 +295,7 @@ describe('redis v2-v3', () => {
299295
...DEFAULT_ATTRIBUTES,
300296
[ATTR_DB_OPERATION_NAME]: operation.command,
301297
[ATTR_DB_QUERY_TEXT]: operation.expectedDbStatementStable,
302-
[SEMATTRS_DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatementOld}`,
298+
[ATTR_DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatementOld}`,
303299
};
304300
const span = tracer.startSpan('test span');
305301
context.with(trace.setSpan(context.active(), span), () => {
@@ -367,7 +363,7 @@ describe('redis v2-v3', () => {
367363
operation.args
368364
);
369365
assert.strictEqual(
370-
endedSpans[0].attributes[SEMATTRS_DB_STATEMENT],
366+
endedSpans[0].attributes[ATTR_DB_STATEMENT],
371367
expectedStatement
372368
);
373369

0 commit comments

Comments
 (0)