Skip to content

Commit a914e34

Browse files
authored
chore(instrumentation-ioredis): update semconv usage to ATTR_ exports (#3059)
Refs: #2377
1 parent 8e2f6b4 commit a914e34

File tree

4 files changed

+140
-54
lines changed

4 files changed

+140
-54
lines changed

packages/instrumentation-ioredis/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"@opentelemetry/contrib-test-utils": "^0.51.0",
5959
"@opentelemetry/sdk-trace-base": "^2.0.0",
6060
"@opentelemetry/sdk-trace-node": "^2.0.0",
61+
"@opentelemetry/semantic-conventions": "^1.27.0",
6162
"@types/ioredis4": "npm:@types/[email protected]",
6263
"@types/mocha": "10.0.10",
6364
"@types/node": "18.18.14",
@@ -72,8 +73,7 @@
7273
},
7374
"dependencies": {
7475
"@opentelemetry/instrumentation": "^0.205.0",
75-
"@opentelemetry/redis-common": "^0.38.0",
76-
"@opentelemetry/semantic-conventions": "^1.27.0"
76+
"@opentelemetry/redis-common": "^0.38.0"
7777
},
7878
"homepage": "https://github.com/open-telemetry/opentelemetry-js-contrib/tree/main/packages/instrumentation-ioredis#readme"
7979
}

packages/instrumentation-ioredis/src/instrumentation.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import {
2323
import { IORedisInstrumentationConfig } from './types';
2424
import { IORedisCommand, RedisInterface } from './internal-types';
2525
import {
26-
DBSYSTEMVALUES_REDIS,
27-
SEMATTRS_DB_CONNECTION_STRING,
28-
SEMATTRS_DB_STATEMENT,
29-
SEMATTRS_DB_SYSTEM,
30-
SEMATTRS_NET_PEER_NAME,
31-
SEMATTRS_NET_PEER_PORT,
32-
} from '@opentelemetry/semantic-conventions';
26+
DB_SYSTEM_VALUE_REDIS,
27+
ATTR_DB_CONNECTION_STRING,
28+
ATTR_DB_STATEMENT,
29+
ATTR_DB_SYSTEM,
30+
ATTR_NET_PEER_NAME,
31+
ATTR_NET_PEER_PORT,
32+
} from './semconv';
3333
import { safeExecuteInTheMiddle } from '@opentelemetry/instrumentation';
3434
import { endSpan } from './utils';
3535
import { defaultDbStatementSerializer } from '@opentelemetry/redis-common';
@@ -123,8 +123,8 @@ export class IORedisInstrumentation extends InstrumentationBase<IORedisInstrumen
123123
const span = instrumentation.tracer.startSpan(cmd.name, {
124124
kind: SpanKind.CLIENT,
125125
attributes: {
126-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
127-
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(cmd.name, cmd.args),
126+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
127+
[ATTR_DB_STATEMENT]: dbStatementSerializer(cmd.name, cmd.args),
128128
},
129129
});
130130

@@ -149,9 +149,9 @@ export class IORedisInstrumentation extends InstrumentationBase<IORedisInstrumen
149149
const { host, port } = this.options;
150150

151151
span.setAttributes({
152-
[SEMATTRS_NET_PEER_NAME]: host,
153-
[SEMATTRS_NET_PEER_PORT]: port,
154-
[SEMATTRS_DB_CONNECTION_STRING]: `redis://${host}:${port}`,
152+
[ATTR_NET_PEER_NAME]: host,
153+
[ATTR_NET_PEER_PORT]: port,
154+
[ATTR_DB_CONNECTION_STRING]: `redis://${host}:${port}`,
155155
});
156156

157157
try {
@@ -202,16 +202,16 @@ export class IORedisInstrumentation extends InstrumentationBase<IORedisInstrumen
202202
const span = instrumentation.tracer.startSpan('connect', {
203203
kind: SpanKind.CLIENT,
204204
attributes: {
205-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
206-
[SEMATTRS_DB_STATEMENT]: 'connect',
205+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
206+
[ATTR_DB_STATEMENT]: 'connect',
207207
},
208208
});
209209
const { host, port } = this.options;
210210

211211
span.setAttributes({
212-
[SEMATTRS_NET_PEER_NAME]: host,
213-
[SEMATTRS_NET_PEER_PORT]: port,
214-
[SEMATTRS_DB_CONNECTION_STRING]: `redis://${host}:${port}`,
212+
[ATTR_NET_PEER_NAME]: host,
213+
[ATTR_NET_PEER_PORT]: port,
214+
[ATTR_DB_CONNECTION_STRING]: `redis://${host}:${port}`,
215215
});
216216
try {
217217
const client = original.apply(this, arguments);
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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}.
79+
*
80+
* Redis
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_VALUE_REDIS = 'redis' as const;

packages/instrumentation-ioredis/test/ioredis.test.ts

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ import {
4040
IORedisRequestHookInformation,
4141
} from '../src/types';
4242
import {
43-
DBSYSTEMVALUES_REDIS,
44-
SEMATTRS_DB_CONNECTION_STRING,
45-
SEMATTRS_DB_STATEMENT,
46-
SEMATTRS_DB_SYSTEM,
47-
SEMATTRS_EXCEPTION_MESSAGE,
48-
SEMATTRS_EXCEPTION_STACKTRACE,
49-
SEMATTRS_EXCEPTION_TYPE,
50-
SEMATTRS_NET_PEER_NAME,
51-
SEMATTRS_NET_PEER_PORT,
43+
ATTR_EXCEPTION_MESSAGE,
44+
ATTR_EXCEPTION_STACKTRACE,
45+
ATTR_EXCEPTION_TYPE,
5246
} from '@opentelemetry/semantic-conventions';
47+
import {
48+
DB_SYSTEM_VALUE_REDIS,
49+
ATTR_DB_CONNECTION_STRING,
50+
ATTR_DB_STATEMENT,
51+
ATTR_DB_SYSTEM,
52+
ATTR_NET_PEER_NAME,
53+
ATTR_NET_PEER_PORT,
54+
} from '../src/semconv';
5355

5456
const memoryExporter = new InMemorySpanExporter();
5557

@@ -61,10 +63,10 @@ const CONFIG = {
6163
const REDIS_URL = `redis://${CONFIG.host}:${CONFIG.port}`;
6264

6365
const DEFAULT_ATTRIBUTES = {
64-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
65-
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
66-
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
67-
[SEMATTRS_DB_CONNECTION_STRING]: REDIS_URL,
66+
[ATTR_DB_SYSTEM]: DB_SYSTEM_VALUE_REDIS,
67+
[ATTR_NET_PEER_NAME]: CONFIG.host,
68+
[ATTR_NET_PEER_PORT]: CONFIG.port,
69+
[ATTR_DB_CONNECTION_STRING]: REDIS_URL,
6870
};
6971

7072
const unsetStatus: SpanStatus = {
@@ -76,8 +78,8 @@ const predictableStackTrace =
7678
const sanitizeEventForAssertion = (span: ReadableSpan) => {
7779
span.events.forEach(e => {
7880
// stack trace includes data such as /user/{userName}/repos/{projectName}
79-
if (e.attributes?.[SEMATTRS_EXCEPTION_STACKTRACE]) {
80-
e.attributes[SEMATTRS_EXCEPTION_STACKTRACE] = predictableStackTrace;
81+
if (e.attributes?.[ATTR_EXCEPTION_STACKTRACE]) {
82+
e.attributes[ATTR_EXCEPTION_STACKTRACE] = predictableStackTrace;
8183
}
8284

8385
// since time will change on each test invocation, it is being replaced to predicable value
@@ -130,7 +132,7 @@ describe('ioredis', () => {
130132
let client: ioredisTypes.Redis;
131133
const attributes = {
132134
...DEFAULT_ATTRIBUTES,
133-
[SEMATTRS_DB_STATEMENT]: 'connect',
135+
[ATTR_DB_STATEMENT]: 'connect',
134136
};
135137
const readyHandler = () => {
136138
const endedSpans = memoryExporter.getFinishedSpans();
@@ -240,7 +242,7 @@ describe('ioredis', () => {
240242
it(`should create a child span for cb style ${command.description}`, done => {
241243
const attributes = {
242244
...DEFAULT_ATTRIBUTES,
243-
[SEMATTRS_DB_STATEMENT]: `${command.name} ${command.expectedDbStatement}`,
245+
[ATTR_DB_STATEMENT]: `${command.name} ${command.expectedDbStatement}`,
244246
};
245247
const span = provider
246248
.getTracer('ioredis-test')
@@ -270,7 +272,7 @@ describe('ioredis', () => {
270272
it('should create a child span for hset promise', async () => {
271273
const attributes = {
272274
...DEFAULT_ATTRIBUTES,
273-
[SEMATTRS_DB_STATEMENT]: `hset ${hashKeyName} random [1 other arguments]`,
275+
[ATTR_DB_STATEMENT]: `hset ${hashKeyName} random [1 other arguments]`,
274276
};
275277
const span = provider.getTracer('ioredis-test').startSpan('test span');
276278
await context.with(trace.setSpan(context.active(), span), async () => {
@@ -312,15 +314,15 @@ describe('ioredis', () => {
312314
const exceptionEvent = ioredisSpan.events[0];
313315
assert.strictEqual(exceptionEvent.name, 'exception');
314316
assert.strictEqual(
315-
exceptionEvent.attributes?.[SEMATTRS_EXCEPTION_MESSAGE],
317+
exceptionEvent.attributes?.[ATTR_EXCEPTION_MESSAGE],
316318
ex.message
317319
);
318320
assert.strictEqual(
319-
exceptionEvent.attributes?.[SEMATTRS_EXCEPTION_STACKTRACE],
321+
exceptionEvent.attributes?.[ATTR_EXCEPTION_STACKTRACE],
320322
ex.stack
321323
);
322324
assert.strictEqual(
323-
exceptionEvent.attributes?.[SEMATTRS_EXCEPTION_TYPE],
325+
exceptionEvent.attributes?.[ATTR_EXCEPTION_TYPE],
324326
ex.name
325327
);
326328
}
@@ -330,7 +332,7 @@ describe('ioredis', () => {
330332
it('should create a child span for streamify scanning', done => {
331333
const attributes = {
332334
...DEFAULT_ATTRIBUTES,
333-
[SEMATTRS_DB_STATEMENT]: 'scan 0 MATCH test-* COUNT 1000',
335+
[ATTR_DB_STATEMENT]: 'scan 0 MATCH test-* COUNT 1000',
334336
};
335337
const span = provider.getTracer('ioredis-test').startSpan('test span');
336338
context.with(trace.setSpan(context.active(), span), () => {
@@ -406,7 +408,7 @@ describe('ioredis', () => {
406408

407409
const attributes = {
408410
...DEFAULT_ATTRIBUTES,
409-
[SEMATTRS_DB_STATEMENT]: 'subscribe news music',
411+
[ATTR_DB_STATEMENT]: 'subscribe news music',
410412
};
411413
testUtils.assertSpan(
412414
endedSpans[4],
@@ -425,7 +427,7 @@ describe('ioredis', () => {
425427
it('should create a child span for multi/transaction', done => {
426428
const attributes = {
427429
...DEFAULT_ATTRIBUTES,
428-
[SEMATTRS_DB_STATEMENT]: 'multi',
430+
[ATTR_DB_STATEMENT]: 'multi',
429431
};
430432

431433
const span = provider.getTracer('ioredis-test').startSpan('test span');
@@ -461,7 +463,7 @@ describe('ioredis', () => {
461463
it('should create a child span for pipeline', done => {
462464
const attributes = {
463465
...DEFAULT_ATTRIBUTES,
464-
[SEMATTRS_DB_STATEMENT]: 'set foo [1 other arguments]',
466+
[ATTR_DB_STATEMENT]: 'set foo [1 other arguments]',
465467
};
466468

467469
const span = provider.getTracer('ioredis-test').startSpan('test span');
@@ -495,7 +497,7 @@ describe('ioredis', () => {
495497
it('should create a child span for get promise', async () => {
496498
const attributes = {
497499
...DEFAULT_ATTRIBUTES,
498-
[SEMATTRS_DB_STATEMENT]: `get ${testKeyName}`,
500+
[ATTR_DB_STATEMENT]: `get ${testKeyName}`,
499501
};
500502
const span = provider.getTracer('ioredis-test').startSpan('test span');
501503
await context.with(trace.setSpan(context.active(), span), async () => {
@@ -524,7 +526,7 @@ describe('ioredis', () => {
524526
it('should create a child span for del', async () => {
525527
const attributes = {
526528
...DEFAULT_ATTRIBUTES,
527-
[SEMATTRS_DB_STATEMENT]: `del ${testKeyName}`,
529+
[ATTR_DB_STATEMENT]: `del ${testKeyName}`,
528530
};
529531
const span = provider.getTracer('ioredis-test').startSpan('test span');
530532
await context.with(trace.setSpan(context.active(), span), async () => {
@@ -558,7 +560,7 @@ describe('ioredis', () => {
558560

559561
const attributes = {
560562
...DEFAULT_ATTRIBUTES,
561-
[SEMATTRS_DB_STATEMENT]: `evalsha bfbf458525d6a0b19200bfd6db3af481156b367b 1 ${testKeyName}`,
563+
[ATTR_DB_STATEMENT]: `evalsha bfbf458525d6a0b19200bfd6db3af481156b367b 1 ${testKeyName}`,
562564
};
563565

564566
const span = provider.getTracer('ioredis-test').startSpan('test span');
@@ -591,10 +593,10 @@ describe('ioredis', () => {
591593
[
592594
{
593595
attributes: {
594-
[SEMATTRS_EXCEPTION_MESSAGE]:
596+
[ATTR_EXCEPTION_MESSAGE]:
595597
'NOSCRIPT No matching script. Please use EVAL.',
596-
[SEMATTRS_EXCEPTION_STACKTRACE]: predictableStackTrace,
597-
[SEMATTRS_EXCEPTION_TYPE]: 'ReplyError',
598+
[ATTR_EXCEPTION_STACKTRACE]: predictableStackTrace,
599+
[ATTR_EXCEPTION_TYPE]: 'ReplyError',
598600
},
599601
name: 'exception',
600602
time: [0, 0],
@@ -665,7 +667,7 @@ describe('ioredis', () => {
665667
SpanKind.CLIENT,
666668
{
667669
...DEFAULT_ATTRIBUTES,
668-
[SEMATTRS_DB_STATEMENT]: `set ${testKeyName} [1 other arguments]`,
670+
[ATTR_DB_STATEMENT]: `set ${testKeyName} [1 other arguments]`,
669671
},
670672
[],
671673
unsetStatus
@@ -691,7 +693,7 @@ describe('ioredis', () => {
691693
SpanKind.CLIENT,
692694
{
693695
...DEFAULT_ATTRIBUTES,
694-
[SEMATTRS_DB_STATEMENT]: 'connect',
696+
[ATTR_DB_STATEMENT]: 'connect',
695697
},
696698
[],
697699
unsetStatus
@@ -740,7 +742,7 @@ describe('ioredis', () => {
740742
it(`should tag the span with a custom db.statement for cb style ${command.description}`, done => {
741743
const attributes = {
742744
...DEFAULT_ATTRIBUTES,
743-
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(
745+
[ATTR_DB_STATEMENT]: dbStatementSerializer(
744746
command.name,
745747
command.args
746748
),
@@ -983,7 +985,7 @@ describe('ioredis', () => {
983985
operation.args
984986
);
985987
assert.strictEqual(
986-
endedSpans[0].attributes[SEMATTRS_DB_STATEMENT],
988+
endedSpans[0].attributes[ATTR_DB_STATEMENT],
987989
expectedStatement
988990
);
989991
done();

0 commit comments

Comments
 (0)