Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ioredis and redis DB semantic conventions #167

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/opentelemetry-test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"homepage": "https://github.com/open-telemetry/opentelemetry-js#readme",
"devDependencies": {
"@types/node": "^14.0.27",
"gts": "2.0.2",
"ts-node": "8.10.2",
"tslint-consistent-codestyle": "1.16.0",
Expand Down
1 change: 1 addition & 0 deletions plugins/node/opentelemetry-plugin-ioredis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"devDependencies": {
"@opentelemetry/context-async-hooks": "0.10.2",
"@opentelemetry/node": "0.10.2",
"@opentelemetry/semantic-conventions": "^0.10.2",
"@opentelemetry/test-utils": "^0.9.0",
"@opentelemetry/tracing": "0.10.2",
"@types/ioredis": "4.17.3",
Expand Down
32 changes: 0 additions & 32 deletions plugins/node/opentelemetry-plugin-ioredis/src/enums.ts

This file was deleted.

5 changes: 2 additions & 3 deletions plugins/node/opentelemetry-plugin-ioredis/src/ioredis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ import { traceConnection, traceSendCommand } from './utils';
import { VERSION } from './version';

export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
static readonly COMPONENT = 'ioredis';
static readonly DB_TYPE = 'redis';
static readonly DB_SYSTEM = 'redis';
readonly supportedVersions = ['>1 <5'];
protected _config!: IoredisPluginConfig;

Expand Down Expand Up @@ -74,4 +73,4 @@ export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
}
}

export const plugin = new IORedisPlugin(IORedisPlugin.COMPONENT);
export const plugin = new IORedisPlugin('ioredis');
27 changes: 14 additions & 13 deletions plugins/node/opentelemetry-plugin-ioredis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
DbStatementSerializer,
} from './types';
import { IORedisPlugin } from './ioredis';
import { AttributeNames } from './enums';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

const endSpan = (span: Span, err: NodeJS.ErrnoException | null | undefined) => {
if (err) {
Expand All @@ -42,17 +45,16 @@ export const traceConnection = (tracer: Tracer, original: Function) => {
const span = tracer.startSpan('connect', {
kind: SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.DB_STATEMENT]: 'connect',
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
[DatabaseAttribute.DB_STATEMENT]: 'connect',
},
});
const { host, port } = this.options;

span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: host,
[AttributeNames.PEER_PORT]: port,
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
[GeneralAttribute.NET_PEER_HOSTNAME]: host,
[GeneralAttribute.NET_PEER_PORT]: port,
[GeneralAttribute.NET_PEER_ADDRESS]: `redis://${host}:${port}`,
});
try {
const client = original.apply(this, arguments);
Expand Down Expand Up @@ -95,9 +97,8 @@ export const traceSendCommand = (
const span = tracer.startSpan(cmd.name, {
kind: SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
[DatabaseAttribute.DB_STATEMENT]: dbStatementSerializer(
cmd.name,
cmd.args
),
Expand All @@ -107,9 +108,9 @@ export const traceSendCommand = (
const { host, port } = this.options;

span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: host,
[AttributeNames.PEER_PORT]: port,
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
[GeneralAttribute.NET_PEER_HOSTNAME]: host,
[GeneralAttribute.NET_PEER_PORT]: port,
[GeneralAttribute.NET_PEER_ADDRESS]: `redis://${host}:${port}`,
});

try {
Expand Down
42 changes: 22 additions & 20 deletions plugins/node/opentelemetry-plugin-ioredis/test/ioredis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import {
import * as assert from 'assert';
import * as ioredisTypes from 'ioredis';
import { IORedisPlugin, plugin } from '../src';
import { AttributeNames } from '../src/enums';
import { IoredisPluginConfig, DbStatementSerializer } from '../src/types';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

const memoryExporter = new InMemorySpanExporter();

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

const DEFAULT_ATTRIBUTES = {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.PEER_HOSTNAME]: CONFIG.host,
[AttributeNames.PEER_PORT]: CONFIG.port,
[AttributeNames.PEER_ADDRESS]: URL,
[DatabaseAttribute.DB_SYSTEM]: IORedisPlugin.DB_SYSTEM,
[GeneralAttribute.NET_PEER_HOSTNAME]: CONFIG.host,
[GeneralAttribute.NET_PEER_PORT]: CONFIG.port,
[GeneralAttribute.NET_PEER_ADDRESS]: URL,
};

const okStatus: Status = {
Expand Down Expand Up @@ -91,7 +93,7 @@ describe('ioredis', () => {
});

it('should have correct module name', () => {
assert.strictEqual(plugin.moduleName, IORedisPlugin.COMPONENT);
assert.strictEqual(plugin.moduleName, 'ioredis');
});

describe('#createClient()', () => {
Expand All @@ -100,7 +102,7 @@ describe('ioredis', () => {
let client: ioredisTypes.Redis;
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'connect',
[DatabaseAttribute.DB_STATEMENT]: 'connect',
};
const readyHandler = () => {
const endedSpans = memoryExporter.getFinishedSpans();
Expand Down Expand Up @@ -194,9 +196,9 @@ describe('ioredis', () => {
it(`should create a child span for cb style ${command.description}`, done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: `${command.name} ${command.args.join(
' '
)}`,
[DatabaseAttribute.DB_STATEMENT]: `${
command.name
} ${command.args.join(' ')}`,
};
const span = provider
.getTracer('ioredis-test')
Expand Down Expand Up @@ -226,7 +228,7 @@ describe('ioredis', () => {
it('should create a child span for hset promise', async () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'hset hash random random',
[DatabaseAttribute.DB_STATEMENT]: 'hset hash random random',
};
const span = provider.getTracer('ioredis-test').startSpan('test span');
await provider.getTracer('ioredis-test').withSpan(span, async () => {
Expand Down Expand Up @@ -254,7 +256,7 @@ describe('ioredis', () => {
it('should create a child span for streamify scanning', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'scan 0',
[DatabaseAttribute.DB_STATEMENT]: 'scan 0',
};
const span = provider.getTracer('ioredis-test').startSpan('test span');
provider.getTracer('ioredis-test').withSpan(span, () => {
Expand Down Expand Up @@ -329,7 +331,7 @@ describe('ioredis', () => {

const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'subscribe news music',
[DatabaseAttribute.DB_STATEMENT]: 'subscribe news music',
};
testUtils.assertSpan(
endedSpans[5],
Expand All @@ -348,7 +350,7 @@ describe('ioredis', () => {
it('should create a child span for lua', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]:
[DatabaseAttribute.DB_STATEMENT]:
'evalsha bfbf458525d6a0b19200bfd6db3af481156b367b 1 test',
};

Expand Down Expand Up @@ -387,7 +389,7 @@ describe('ioredis', () => {
it('should create a child span for multi/transaction', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'multi',
[DatabaseAttribute.DB_STATEMENT]: 'multi',
};

const span = provider.getTracer('ioredis-test').startSpan('test span');
Expand Down Expand Up @@ -423,7 +425,7 @@ describe('ioredis', () => {
it('should create a child span for pipeline', done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'set foo bar',
[DatabaseAttribute.DB_STATEMENT]: 'set foo bar',
};

const span = provider.getTracer('ioredis-test').startSpan('test span');
Expand Down Expand Up @@ -457,7 +459,7 @@ describe('ioredis', () => {
it('should create a child span for get promise', async () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'get test',
[DatabaseAttribute.DB_STATEMENT]: 'get test',
};
const span = provider.getTracer('ioredis-test').startSpan('test span');
await provider.getTracer('ioredis-test').withSpan(span, async () => {
Expand Down Expand Up @@ -486,7 +488,7 @@ describe('ioredis', () => {
it('should create a child span for del', async () => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'del test',
[DatabaseAttribute.DB_STATEMENT]: 'del test',
};
const span = provider.getTracer('ioredis-test').startSpan('test span');
await provider.getTracer('ioredis-test').withSpan(span, async () => {
Expand Down Expand Up @@ -541,7 +543,7 @@ describe('ioredis', () => {
it(`should tag the span with a custom db.statement for cb style ${command.description}`, done => {
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: dbStatementSerializer(
[DatabaseAttribute.DB_STATEMENT]: dbStatementSerializer(
command.name,
command.args
),
Expand Down
1 change: 1 addition & 0 deletions plugins/node/opentelemetry-plugin-redis/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"devDependencies": {
"@opentelemetry/context-async-hooks": "0.10.2",
"@opentelemetry/node": "0.10.2",
"@opentelemetry/semantic-conventions": "^0.10.2",
"@opentelemetry/test-utils": "^0.9.0",
"@opentelemetry/tracing": "0.10.2",
"@types/mocha": "7.0.2",
Expand Down
32 changes: 0 additions & 32 deletions plugins/node/opentelemetry-plugin-redis/src/enums.ts

This file was deleted.

15 changes: 9 additions & 6 deletions plugins/node/opentelemetry-plugin-redis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import {
} from './types';
import { EventEmitter } from 'events';
import { RedisPlugin } from './redis';
import { AttributeNames } from './enums';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

const endSpan = (span: Span, err?: Error | null) => {
if (err) {
Expand Down Expand Up @@ -78,21 +81,21 @@ export const getTracedInternalSendCommand = (
const span = tracer.startSpan(`${RedisPlugin.COMPONENT}-${cmd.command}`, {
kind: SpanKind.CLIENT,
attributes: {
[AttributeNames.COMPONENT]: RedisPlugin.COMPONENT,
[AttributeNames.DB_STATEMENT]: cmd.command,
[DatabaseAttribute.DB_SYSTEM]: RedisPlugin.COMPONENT,
[DatabaseAttribute.DB_STATEMENT]: cmd.command,
},
});

// Set attributes for not explicitly typed RedisPluginClientTypes
if (this.options) {
span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: this.options.host,
[AttributeNames.PEER_PORT]: this.options.port,
[GeneralAttribute.NET_PEER_HOSTNAME]: this.options.host,
[GeneralAttribute.NET_PEER_PORT]: this.options.port,
});
}
if (this.address) {
span.setAttribute(
AttributeNames.PEER_ADDRESS,
GeneralAttribute.NET_PEER_ADDRESS,
`redis://${this.address}`
);
}
Expand Down
15 changes: 9 additions & 6 deletions plugins/node/opentelemetry-plugin-redis/test/redis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ import {
import * as assert from 'assert';
import * as redisTypes from 'redis';
import { plugin, RedisPlugin } from '../src';
import { AttributeNames } from '../src/enums';
import {
DatabaseAttribute,
GeneralAttribute,
} from '@opentelemetry/semantic-conventions';

const memoryExporter = new InMemorySpanExporter();

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

const DEFAULT_ATTRIBUTES = {
[AttributeNames.COMPONENT]: RedisPlugin.COMPONENT,
[AttributeNames.PEER_HOSTNAME]: CONFIG.host,
[AttributeNames.PEER_PORT]: CONFIG.port,
[AttributeNames.PEER_ADDRESS]: URL,
[DatabaseAttribute.DB_SYSTEM]: RedisPlugin.COMPONENT,
[GeneralAttribute.NET_PEER_HOSTNAME]: CONFIG.host,
[GeneralAttribute.NET_PEER_PORT]: CONFIG.port,
[GeneralAttribute.NET_PEER_ADDRESS]: URL,
};

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