Skip to content

Commit 674b384

Browse files
author
Naseem
authored
ioredis and redis DB semantic conventions (open-telemetry#167)
* fix: add required node types dev dependency Signed-off-by: Naseem <naseem@transit.app> * fix: conform to semantic conventions Signed-off-by: Naseem <naseem@transit.app>
1 parent 5be5217 commit 674b384

File tree

10 files changed

+59
-112
lines changed

10 files changed

+59
-112
lines changed

packages/opentelemetry-test-utils/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
},
2424
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme",
2525
"devDependencies": {
26+
"@types/node": "^14.0.27",
2627
"gts": "2.0.2",
2728
"ts-node": "8.10.2",
2829
"tslint-consistent-codestyle": "1.16.0",

plugins/node/opentelemetry-plugin-ioredis/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"devDependencies": {
4747
"@opentelemetry/context-async-hooks": "0.10.2",
4848
"@opentelemetry/node": "0.10.2",
49+
"@opentelemetry/semantic-conventions": "^0.10.2",
4950
"@opentelemetry/test-utils": "^0.9.0",
5051
"@opentelemetry/tracing": "0.10.2",
5152
"@types/ioredis": "4.17.3",

plugins/node/opentelemetry-plugin-ioredis/src/enums.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

plugins/node/opentelemetry-plugin-ioredis/src/ioredis.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ import { traceConnection, traceSendCommand } from './utils';
2222
import { VERSION } from './version';
2323

2424
export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
25-
static readonly COMPONENT = 'ioredis';
26-
static readonly DB_TYPE = 'redis';
25+
static readonly DB_SYSTEM = 'redis';
2726
readonly supportedVersions = ['>1 <5'];
2827
protected _config!: IoredisPluginConfig;
2928

@@ -74,4 +73,4 @@ export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
7473
}
7574
}
7675

77-
export const plugin = new IORedisPlugin(IORedisPlugin.COMPONENT);
76+
export const plugin = new IORedisPlugin('ioredis');

plugins/node/opentelemetry-plugin-ioredis/src/utils.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
DbStatementSerializer,
2424
} from './types';
2525
import { IORedisPlugin } from './ioredis';
26-
import { AttributeNames } from './enums';
26+
import {
27+
DatabaseAttribute,
28+
GeneralAttribute,
29+
} from '@opentelemetry/semantic-conventions';
2730

2831
const endSpan = (span: Span, err: NodeJS.ErrnoException | null | undefined) => {
2932
if (err) {
@@ -42,17 +45,16 @@ export const traceConnection = (tracer: Tracer, original: Function) => {
4245
const span = tracer.startSpan('connect', {
4346
kind: SpanKind.CLIENT,
4447
attributes: {
45-
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
46-
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
47-
[AttributeNames.DB_STATEMENT]: 'connect',
48+
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
49+
[DatabaseAttribute.DB_STATEMENT]: 'connect',
4850
},
4951
});
5052
const { host, port } = this.options;
5153

5254
span.setAttributes({
53-
[AttributeNames.PEER_HOSTNAME]: host,
54-
[AttributeNames.PEER_PORT]: port,
55-
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
55+
[GeneralAttribute.NET_PEER_HOSTNAME]: host,
56+
[GeneralAttribute.NET_PEER_PORT]: port,
57+
[GeneralAttribute.NET_PEER_ADDRESS]: `redis://${host}:${port}`,
5658
});
5759
try {
5860
const client = original.apply(this, arguments);
@@ -95,9 +97,8 @@ export const traceSendCommand = (
9597
const span = tracer.startSpan(cmd.name, {
9698
kind: SpanKind.CLIENT,
9799
attributes: {
98-
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
99-
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
100-
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
100+
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
101+
[DatabaseAttribute.DB_STATEMENT]: dbStatementSerializer(
101102
cmd.name,
102103
cmd.args
103104
),
@@ -107,9 +108,9 @@ export const traceSendCommand = (
107108
const { host, port } = this.options;
108109

109110
span.setAttributes({
110-
[AttributeNames.PEER_HOSTNAME]: host,
111-
[AttributeNames.PEER_PORT]: port,
112-
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
111+
[GeneralAttribute.NET_PEER_HOSTNAME]: host,
112+
[GeneralAttribute.NET_PEER_PORT]: port,
113+
[GeneralAttribute.NET_PEER_ADDRESS]: `redis://${host}:${port}`,
113114
});
114115

115116
try {

plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,11 @@ import {
2626
import * as assert from 'assert';
2727
import * as ioredisTypes from 'ioredis';
2828
import { IORedisPlugin, plugin } from '../src';
29-
import { AttributeNames } from '../src/enums';
3029
import { IoredisPluginConfig, DbStatementSerializer } from '../src/types';
30+
import {
31+
DatabaseAttribute,
32+
GeneralAttribute,
33+
} from '@opentelemetry/semantic-conventions';
3134

3235
const memoryExporter = new InMemorySpanExporter();
3336

@@ -39,11 +42,10 @@ const CONFIG = {
3942
const URL = `redis://${CONFIG.host}:${CONFIG.port}`;
4043

4144
const DEFAULT_ATTRIBUTES = {
42-
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
43-
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
44-
[AttributeNames.PEER_HOSTNAME]: CONFIG.host,
45-
[AttributeNames.PEER_PORT]: CONFIG.port,
46-
[AttributeNames.PEER_ADDRESS]: URL,
45+
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
46+
[GeneralAttribute.NET_PEER_HOSTNAME]: CONFIG.host,
47+
[GeneralAttribute.NET_PEER_PORT]: CONFIG.port,
48+
[GeneralAttribute.NET_PEER_ADDRESS]: URL,
4749
};
4850

4951
const okStatus: Status = {
@@ -91,7 +93,7 @@ describe('ioredis', () => {
9193
});
9294

9395
it('should have correct module name', () => {
94-
assert.strictEqual(plugin.moduleName, IORedisPlugin.COMPONENT);
96+
assert.strictEqual(plugin.moduleName, 'ioredis');
9597
});
9698

9799
describe('#createClient()', () => {
@@ -100,7 +102,7 @@ describe('ioredis', () => {
100102
let client: ioredisTypes.Redis;
101103
const attributes = {
102104
...DEFAULT_ATTRIBUTES,
103-
[AttributeNames.DB_STATEMENT]: 'connect',
105+
[DatabaseAttribute.DB_STATEMENT]: 'connect',
104106
};
105107
const readyHandler = () => {
106108
const endedSpans = memoryExporter.getFinishedSpans();
@@ -194,9 +196,9 @@ describe('ioredis', () => {
194196
it(`should create a child span for cb style ${command.description}`, done => {
195197
const attributes = {
196198
...DEFAULT_ATTRIBUTES,
197-
[AttributeNames.DB_STATEMENT]: `${command.name} ${command.args.join(
198-
' '
199-
)}`,
199+
[DatabaseAttribute.DB_STATEMENT]: `${
200+
command.name
201+
} ${command.args.join(' ')}`,
200202
};
201203
const span = provider
202204
.getTracer('ioredis-test')
@@ -226,7 +228,7 @@ describe('ioredis', () => {
226228
it('should create a child span for hset promise', async () => {
227229
const attributes = {
228230
...DEFAULT_ATTRIBUTES,
229-
[AttributeNames.DB_STATEMENT]: 'hset hash random random',
231+
[DatabaseAttribute.DB_STATEMENT]: 'hset hash random random',
230232
};
231233
const span = provider.getTracer('ioredis-test').startSpan('test span');
232234
await provider.getTracer('ioredis-test').withSpan(span, async () => {
@@ -254,7 +256,7 @@ describe('ioredis', () => {
254256
it('should create a child span for streamify scanning', done => {
255257
const attributes = {
256258
...DEFAULT_ATTRIBUTES,
257-
[AttributeNames.DB_STATEMENT]: 'scan 0',
259+
[DatabaseAttribute.DB_STATEMENT]: 'scan 0',
258260
};
259261
const span = provider.getTracer('ioredis-test').startSpan('test span');
260262
provider.getTracer('ioredis-test').withSpan(span, () => {
@@ -329,7 +331,7 @@ describe('ioredis', () => {
329331

330332
const attributes = {
331333
...DEFAULT_ATTRIBUTES,
332-
[AttributeNames.DB_STATEMENT]: 'subscribe news music',
334+
[DatabaseAttribute.DB_STATEMENT]: 'subscribe news music',
333335
};
334336
testUtils.assertSpan(
335337
endedSpans[5],
@@ -348,7 +350,7 @@ describe('ioredis', () => {
348350
it('should create a child span for lua', done => {
349351
const attributes = {
350352
...DEFAULT_ATTRIBUTES,
351-
[AttributeNames.DB_STATEMENT]:
353+
[DatabaseAttribute.DB_STATEMENT]:
352354
'evalsha bfbf458525d6a0b19200bfd6db3af481156b367b 1 test',
353355
};
354356

@@ -387,7 +389,7 @@ describe('ioredis', () => {
387389
it('should create a child span for multi/transaction', done => {
388390
const attributes = {
389391
...DEFAULT_ATTRIBUTES,
390-
[AttributeNames.DB_STATEMENT]: 'multi',
392+
[DatabaseAttribute.DB_STATEMENT]: 'multi',
391393
};
392394

393395
const span = provider.getTracer('ioredis-test').startSpan('test span');
@@ -423,7 +425,7 @@ describe('ioredis', () => {
423425
it('should create a child span for pipeline', done => {
424426
const attributes = {
425427
...DEFAULT_ATTRIBUTES,
426-
[AttributeNames.DB_STATEMENT]: 'set foo bar',
428+
[DatabaseAttribute.DB_STATEMENT]: 'set foo bar',
427429
};
428430

429431
const span = provider.getTracer('ioredis-test').startSpan('test span');
@@ -457,7 +459,7 @@ describe('ioredis', () => {
457459
it('should create a child span for get promise', async () => {
458460
const attributes = {
459461
...DEFAULT_ATTRIBUTES,
460-
[AttributeNames.DB_STATEMENT]: 'get test',
462+
[DatabaseAttribute.DB_STATEMENT]: 'get test',
461463
};
462464
const span = provider.getTracer('ioredis-test').startSpan('test span');
463465
await provider.getTracer('ioredis-test').withSpan(span, async () => {
@@ -486,7 +488,7 @@ describe('ioredis', () => {
486488
it('should create a child span for del', async () => {
487489
const attributes = {
488490
...DEFAULT_ATTRIBUTES,
489-
[AttributeNames.DB_STATEMENT]: 'del test',
491+
[DatabaseAttribute.DB_STATEMENT]: 'del test',
490492
};
491493
const span = provider.getTracer('ioredis-test').startSpan('test span');
492494
await provider.getTracer('ioredis-test').withSpan(span, async () => {
@@ -541,7 +543,7 @@ describe('ioredis', () => {
541543
it(`should tag the span with a custom db.statement for cb style ${command.description}`, done => {
542544
const attributes = {
543545
...DEFAULT_ATTRIBUTES,
544-
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
546+
[DatabaseAttribute.DB_STATEMENT]: dbStatementSerializer(
545547
command.name,
546548
command.args
547549
),

plugins/node/opentelemetry-plugin-redis/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"devDependencies": {
4646
"@opentelemetry/context-async-hooks": "0.10.2",
4747
"@opentelemetry/node": "0.10.2",
48+
"@opentelemetry/semantic-conventions": "^0.10.2",
4849
"@opentelemetry/test-utils": "^0.9.0",
4950
"@opentelemetry/tracing": "0.10.2",
5051
"@types/mocha": "7.0.2",

plugins/node/opentelemetry-plugin-redis/src/enums.ts

Lines changed: 0 additions & 32 deletions
This file was deleted.

plugins/node/opentelemetry-plugin-redis/src/utils.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@ import {
2323
} from './types';
2424
import { EventEmitter } from 'events';
2525
import { RedisPlugin } from './redis';
26-
import { AttributeNames } from './enums';
26+
import {
27+
DatabaseAttribute,
28+
GeneralAttribute,
29+
} from '@opentelemetry/semantic-conventions';
2730

2831
const endSpan = (span: Span, err?: Error | null) => {
2932
if (err) {
@@ -78,21 +81,21 @@ export const getTracedInternalSendCommand = (
7881
const span = tracer.startSpan(`${RedisPlugin.COMPONENT}-${cmd.command}`, {
7982
kind: SpanKind.CLIENT,
8083
attributes: {
81-
[AttributeNames.COMPONENT]: RedisPlugin.COMPONENT,
82-
[AttributeNames.DB_STATEMENT]: cmd.command,
84+
[DatabaseAttribute.DB_SYSTEM]: RedisPlugin.COMPONENT,
85+
[DatabaseAttribute.DB_STATEMENT]: cmd.command,
8386
},
8487
});
8588

8689
// Set attributes for not explicitly typed RedisPluginClientTypes
8790
if (this.options) {
8891
span.setAttributes({
89-
[AttributeNames.PEER_HOSTNAME]: this.options.host,
90-
[AttributeNames.PEER_PORT]: this.options.port,
92+
[GeneralAttribute.NET_PEER_HOSTNAME]: this.options.host,
93+
[GeneralAttribute.NET_PEER_PORT]: this.options.port,
9194
});
9295
}
9396
if (this.address) {
9497
span.setAttribute(
95-
AttributeNames.PEER_ADDRESS,
98+
GeneralAttribute.NET_PEER_ADDRESS,
9699
`redis://${this.address}`
97100
);
98101
}

plugins/node/opentelemetry-plugin-redis/test/redis.test.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ import {
2626
import * as assert from 'assert';
2727
import * as redisTypes from 'redis';
2828
import { plugin, RedisPlugin } from '../src';
29-
import { AttributeNames } from '../src/enums';
29+
import {
30+
DatabaseAttribute,
31+
GeneralAttribute,
32+
} from '@opentelemetry/semantic-conventions';
3033

3134
const memoryExporter = new InMemorySpanExporter();
3235

@@ -38,10 +41,10 @@ const CONFIG = {
3841
const URL = `redis://${CONFIG.host}:${CONFIG.port}`;
3942

4043
const DEFAULT_ATTRIBUTES = {
41-
[AttributeNames.COMPONENT]: RedisPlugin.COMPONENT,
42-
[AttributeNames.PEER_HOSTNAME]: CONFIG.host,
43-
[AttributeNames.PEER_PORT]: CONFIG.port,
44-
[AttributeNames.PEER_ADDRESS]: URL,
44+
[DatabaseAttribute.DB_SYSTEM]: RedisPlugin.COMPONENT,
45+
[GeneralAttribute.NET_PEER_HOSTNAME]: CONFIG.host,
46+
[GeneralAttribute.NET_PEER_PORT]: CONFIG.port,
47+
[GeneralAttribute.NET_PEER_ADDRESS]: URL,
4548
};
4649

4750
const okStatus: Status = {
@@ -172,7 +175,7 @@ describe('redis@2.x', () => {
172175
it(`should create a child span for ${operation.description}`, done => {
173176
const attributes = {
174177
...DEFAULT_ATTRIBUTES,
175-
[AttributeNames.DB_STATEMENT]: operation.command,
178+
[DatabaseAttribute.DB_STATEMENT]: operation.command,
176179
};
177180
const span = tracer.startSpan('test span');
178181
tracer.withSpan(span, () => {

0 commit comments

Comments
 (0)