Skip to content

Commit

Permalink
feat: trace connection
Browse files Browse the repository at this point in the history
  • Loading branch information
naseemkullah committed Dec 19, 2019
1 parent b5f616f commit 3b47e26
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 8 deletions.
17 changes: 16 additions & 1 deletion packages/opentelemetry-plugin-ioredis/src/ioredis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import { BasePlugin } from '@opentelemetry/core';
import * as ioredisTypes from 'ioredis';
import * as shimmer from 'shimmer';
import { traceSendCommand } from './utils';
import { traceConnection, traceSendCommand } from './utils';

export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
static readonly COMPONENT = 'ioredis';
Expand All @@ -36,12 +36,20 @@ export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
this._patchSendCommand()
);

this._logger.debug('patching ioredis.prototype.connect');
shimmer.wrap(
this._moduleExports.prototype,
'connect',
this._patchConnection()
);

return this._moduleExports.prototype;
}

protected unpatch(): void {
if (this._moduleExports) {
shimmer.unwrap(this._moduleExports.prototype, 'sendCommand');
shimmer.unwrap(this._moduleExports.prototype, 'connect');
}
}

Expand All @@ -54,6 +62,13 @@ export class IORedisPlugin extends BasePlugin<typeof ioredisTypes> {
return traceSendCommand(tracer, original);
};
}

private _patchConnection() {
const tracer = this._tracer;
return (original: Function) => {
return traceConnection(tracer, original);
};
}
}

export const plugin = new IORedisPlugin(IORedisPlugin.COMPONENT);
30 changes: 30 additions & 0 deletions packages/opentelemetry-plugin-ioredis/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,36 @@ const endSpan = (span: Span, err: NodeJS.ErrnoException | null | undefined) => {
span.end();
};

export const traceConnection = (tracer: Tracer, original: Function) => {
return function(this: ioredisTypes.Redis & IORedisPluginClientTypes) {
const parentSpan = tracer.getCurrentSpan();
const span = tracer.startSpan('connect', {
kind: SpanKind.CLIENT,
parent: parentSpan,
attributes: {
[AttributeNames.COMPONENT]: IORedisPlugin.COMPONENT,
[AttributeNames.DB_TYPE]: IORedisPlugin.DB_TYPE,
[AttributeNames.DB_STATEMENT]: 'connect',
},
});
const { host, port } = this.options;

span.setAttributes({
[AttributeNames.PEER_HOSTNAME]: host,
[AttributeNames.PEER_PORT]: port,
[AttributeNames.PEER_ADDRESS]: `redis://${host}:${port}`,
});
try {
const client = original.apply(this, arguments);
endSpan(span, null);
return client;
} catch (error) {
endSpan(span, error);
throw error;
}
};
};

export const traceSendCommand = (tracer: Tracer, original: Function) => {
return function(
this: ioredisTypes.Redis & IORedisPluginClientTypes,
Expand Down
15 changes: 8 additions & 7 deletions packages/opentelemetry-plugin-ioredis/test/ioredis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,15 @@ describe('ioredis', () => {
let client: ioredisTypes.Redis;
const attributes = {
...DEFAULT_ATTRIBUTES,
[AttributeNames.DB_STATEMENT]: 'info',
[AttributeNames.DB_STATEMENT]: 'connect',
};
const readyHandler = () => {
const endedSpans = memoryExporter.getFinishedSpans();

assert.strictEqual(tracer.getCurrentSpan(), span);
assert.strictEqual(endedSpans.length, 1);
assert.strictEqual(endedSpans[0].name, `info`);
assert.strictEqual(endedSpans.length, 2);
assert.strictEqual(endedSpans[0].name, `connect`);
assert.strictEqual(endedSpans[1].name, `info`);
assertionUtils.assertPropagation(endedSpans[0], span);

assertionUtils.assertSpan(
Expand All @@ -107,12 +108,12 @@ describe('ioredis', () => {
okStatus
);
span.end();
assert.strictEqual(endedSpans.length, 2);
assert.strictEqual(endedSpans[1].name, `test span`);
assert.strictEqual(endedSpans.length, 3);
assert.strictEqual(endedSpans[2].name, `test span`);

client.quit(done);
assert.strictEqual(endedSpans.length, 3);
assert.strictEqual(endedSpans[2].name, `quit`);
assert.strictEqual(endedSpans.length, 4);
assert.strictEqual(endedSpans[3].name, `quit`);
};
const errorHandler = (err: Error) => {
assert.ifError(err);
Expand Down

0 comments on commit 3b47e26

Please sign in to comment.