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

feat: add redis 4 connect span #1125

Merged
merged 15 commits into from
Sep 13, 2022
Merged
Changes from 11 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
Original file line number Diff line number Diff line change
@@ -28,13 +28,10 @@ import {
InstrumentationNodeModuleDefinition,
InstrumentationNodeModuleFile,
} from '@opentelemetry/instrumentation';
import { defaultDbStatementSerializer } from './utils';
import { defaultDbStatementSerializer, getClientAttributes } from './utils';
import { RedisInstrumentationConfig } from './types';
import { VERSION } from './version';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';

const OTEL_OPEN_SPANS = Symbol(
'opentelemetry.instruemntation.redis.open_spans'
@@ -186,6 +183,12 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
this._getPatchRedisClientSendCommand()
);

this._wrap(
redisClientPrototype,
'connect',
this._getPatchedClientConnect()
);

return moduleExports;
},
(moduleExports: any) => {
@@ -332,6 +335,48 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
};
}

private _getPatchedClientConnect() {
const plugin = this;
return function connectWrapper(original: Function) {
return function patchedConnect(this: any): Promise<void> {
const options = this.options;

const attributes = getClientAttributes(options);

const span = plugin.tracer.startSpan(
`${RedisInstrumentation.COMPONENT}-connect`,
{
kind: SpanKind.CLIENT,
attributes,
}
);

const res = context.with(trace.setSpan(context.active(), span), () => {
return original.apply(this);
});

return res
.then((result: unknown) => {
return new Promise(resolve => {
span.end();
resolve(result);
});
rauno56 marked this conversation as resolved.
Show resolved Hide resolved
})
.catch((error: Error) => {
return new Promise((_, reject) => {
span.recordException(error);
span.setStatus({
rauno56 marked this conversation as resolved.
Show resolved Hide resolved
code: SpanStatusCode.ERROR,
message: error.message,
});
span.end();
reject(error);
});
rauno56 marked this conversation as resolved.
Show resolved Hide resolved
});
};
};
}

private _traceClientCommand(
origFunction: Function,
origThis: any,
@@ -351,12 +396,7 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
const dbStatementSerializer =
this._config?.dbStatementSerializer || defaultDbStatementSerializer;

const attributes = {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.NET_PEER_NAME]: clientOptions?.socket?.host,
[SemanticAttributes.NET_PEER_PORT]: clientOptions?.socket?.port,
[SemanticAttributes.DB_CONNECTION_STRING]: clientOptions?.url,
};
const attributes = getClientAttributes(clientOptions);

try {
const dbStatement = dbStatementSerializer(commandName, commandArgs);
@@ -377,7 +417,9 @@ export class RedisInstrumentation extends InstrumentationBase<any> {
}
);

const res = origFunction.apply(origThis, origArguments);
const res = context.with(trace.setSpan(context.active(), span), () => {
return origFunction.apply(origThis, origArguments);
});
if (typeof res?.then === 'function') {
res.then(
(redisRes: unknown) => {
13 changes: 13 additions & 0 deletions plugins/node/opentelemetry-instrumentation-redis-4/src/utils.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,19 @@
* limitations under the License.
*/
import { DbStatementSerializer } from './types';
import {
DbSystemValues,
SemanticAttributes,
} from '@opentelemetry/semantic-conventions';

export const defaultDbStatementSerializer: DbStatementSerializer = cmdName =>
cmdName;

export function getClientAttributes(options: any) {
return {
[SemanticAttributes.DB_SYSTEM]: DbSystemValues.REDIS,
[SemanticAttributes.NET_PEER_NAME]: options?.socket?.host,
[SemanticAttributes.NET_PEER_PORT]: options?.socket?.port,
[SemanticAttributes.DB_CONNECTION_STRING]: options?.url,
};
}
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ import {
} from '@opentelemetry/api';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import { RedisResponseCustomAttributeFunction } from '../src/types';
import { hrTimeToMilliseconds } from '@opentelemetry/core';
import { hrTimeToMilliseconds, suppressTracing } from '@opentelemetry/core';

describe('redis@^4.0.0', () => {
before(function () {
@@ -71,7 +71,9 @@ describe('redis@^4.0.0', () => {
client = createClient({
url: redisTestUrl,
});
await client.connect();
context.with(suppressTracing(context.active()), async () => {
await client.connect();
});
});

afterEach(async () => {
@@ -183,6 +185,54 @@ describe('redis@^4.0.0', () => {
});
});

describe('client connect', () => {
it('produces a span', async () => {
const newClient = createClient({
url: redisTestUrl,
});

after(async () => {
await newClient.disconnect();
});

await newClient.connect();

const [span] = getTestSpans();

assert.strictEqual(span.name, 'redis-connect');

assert.strictEqual(
span.attributes[SemanticAttributes.DB_SYSTEM],
'redis'
);
assert.strictEqual(
span.attributes[SemanticAttributes.NET_PEER_NAME],
redisTestConfig.host
);
assert.strictEqual(
span.attributes[SemanticAttributes.NET_PEER_PORT],
redisTestConfig.port
);
assert.strictEqual(
span.attributes[SemanticAttributes.DB_CONNECTION_STRING],
redisTestUrl
);
});

it('sets error status on connection failure', async () => {
const newClient = createClient({
url: `redis://${redisTestConfig.host}:${redisTestConfig.port + 1}`,
});

await assert.rejects(newClient.connect());

const [span] = getTestSpans();

assert.strictEqual(span.name, 'redis-connect');
assert.strictEqual(span.status.code, SpanStatusCode.ERROR);
});
});

describe('multi (transactions) commands', () => {
it('multi commands', async () => {
await client.set('another-key', 'another-value');