Skip to content

Commit b8f799e

Browse files
committed
chore: lint fix
1 parent b9b7116 commit b8f799e

File tree

3 files changed

+126
-124
lines changed

3 files changed

+126
-124
lines changed

plugins/node/opentelemetry-instrumentation-redis/src/instrumentation.ts

Lines changed: 95 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
safeExecuteInTheMiddle,
2222
} from '@opentelemetry/instrumentation';
2323
import {
24-
endSpan,
24+
endSpan,
2525
getTracedCreateClient,
2626
getTracedCreateStreamTrace,
2727
} from './utils';
@@ -31,15 +31,15 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from './version';
3131
import { RedisPluginClientTypes } from './internal-types';
3232
import { SpanKind, context, trace } from '@opentelemetry/api';
3333
import {
34-
DBSYSTEMVALUES_REDIS,
35-
SEMATTRS_DB_CONNECTION_STRING,
36-
SEMATTRS_DB_STATEMENT,
37-
SEMATTRS_DB_SYSTEM,
38-
SEMATTRS_NET_PEER_NAME,
39-
SEMATTRS_NET_PEER_PORT,
40-
} from '@opentelemetry/semantic-conventions';
34+
DBSYSTEMVALUES_REDIS,
35+
SEMATTRS_DB_CONNECTION_STRING,
36+
SEMATTRS_DB_STATEMENT,
37+
SEMATTRS_DB_SYSTEM,
38+
SEMATTRS_NET_PEER_NAME,
39+
SEMATTRS_NET_PEER_PORT,
40+
} from '@opentelemetry/semantic-conventions';
4141
import { defaultDbStatementSerializer } from '@opentelemetry/redis-common';
42-
42+
4343
const DEFAULT_CONFIG: RedisInstrumentationConfig = {
4444
requireParentSpan: false,
4545
};
@@ -115,91 +115,97 @@ export class RedisInstrumentation extends InstrumentationBase<RedisInstrumentati
115115
private _getPatchInternalSendCommand() {
116116
const instrumentation = this;
117117
return function internal_send_command(original: Function) {
118-
return function internal_send_command_trace(
119-
this: RedisPluginClientTypes,
120-
cmd?: RedisCommand
121-
) {
122-
// Versions of redis (2.4+) use a single options object
123-
// instead of named arguments
124-
if (arguments.length !== 1 || typeof cmd !== 'object') {
125-
// We don't know how to trace this call, so don't start/stop a span
126-
return original.apply(this, arguments);
127-
}
118+
return function internal_send_command_trace(
119+
this: RedisPluginClientTypes,
120+
cmd?: RedisCommand
121+
) {
122+
// Versions of redis (2.4+) use a single options object
123+
// instead of named arguments
124+
if (arguments.length !== 1 || typeof cmd !== 'object') {
125+
// We don't know how to trace this call, so don't start/stop a span
126+
return original.apply(this, arguments);
127+
}
128128

129-
const config = instrumentation.getConfig();
130-
131-
const hasNoParentSpan = trace.getSpan(context.active()) === undefined;
132-
if (config.requireParentSpan === true && hasNoParentSpan) {
133-
return original.apply(this, arguments);
134-
}
135-
136-
const dbStatementSerializer =
137-
config?.dbStatementSerializer || defaultDbStatementSerializer;
138-
const span = instrumentation.tracer.startSpan(
139-
`${RedisInstrumentation.COMPONENT}-${cmd.command}`,
140-
{
141-
kind: SpanKind.CLIENT,
142-
attributes: {
143-
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
144-
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(cmd.command, cmd.args),
129+
const config = instrumentation.getConfig();
130+
131+
const hasNoParentSpan = trace.getSpan(context.active()) === undefined;
132+
if (config.requireParentSpan === true && hasNoParentSpan) {
133+
return original.apply(this, arguments);
134+
}
135+
136+
const dbStatementSerializer =
137+
config?.dbStatementSerializer || defaultDbStatementSerializer;
138+
const span = instrumentation.tracer.startSpan(
139+
`${RedisInstrumentation.COMPONENT}-${cmd.command}`,
140+
{
141+
kind: SpanKind.CLIENT,
142+
attributes: {
143+
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
144+
[SEMATTRS_DB_STATEMENT]: dbStatementSerializer(
145+
cmd.command,
146+
cmd.args
147+
),
148+
},
149+
}
150+
);
151+
152+
// Set attributes for not explicitly typed RedisPluginClientTypes
153+
if (this.connection_options) {
154+
span.setAttributes({
155+
[SEMATTRS_NET_PEER_NAME]: this.connection_options.host,
156+
[SEMATTRS_NET_PEER_PORT]: this.connection_options.port,
157+
});
158+
}
159+
if (this.address) {
160+
span.setAttribute(
161+
SEMATTRS_DB_CONNECTION_STRING,
162+
`redis://${this.address}`
163+
);
164+
}
165+
166+
const originalCallback = arguments[0].callback;
167+
if (originalCallback) {
168+
const originalContext = context.active();
169+
(arguments[0] as RedisCommand).callback = function callback<T>(
170+
this: unknown,
171+
err: Error | null,
172+
reply: T
173+
) {
174+
if (config?.responseHook) {
175+
const responseHook = config.responseHook;
176+
safeExecuteInTheMiddle(
177+
() => {
178+
responseHook(span, cmd.command, cmd.args, reply);
145179
},
146-
}
147-
);
148-
149-
// Set attributes for not explicitly typed RedisPluginClientTypes
150-
if (this.connection_options) {
151-
span.setAttributes({
152-
[SEMATTRS_NET_PEER_NAME]: this.connection_options.host,
153-
[SEMATTRS_NET_PEER_PORT]: this.connection_options.port,
154-
});
155-
}
156-
if (this.address) {
157-
span.setAttribute(
158-
SEMATTRS_DB_CONNECTION_STRING,
159-
`redis://${this.address}`
180+
err => {
181+
if (err) {
182+
instrumentation._diag.error(
183+
'Error executing responseHook',
184+
err
185+
);
186+
}
187+
},
188+
true
160189
);
161190
}
162-
163-
const originalCallback = arguments[0].callback;
164-
if (originalCallback) {
165-
const originalContext = context.active();
166-
(arguments[0] as RedisCommand).callback = function callback<T>(
167-
this: unknown,
168-
err: Error | null,
169-
reply: T
170-
) {
171-
if (config?.responseHook) {
172-
const responseHook = config.responseHook;
173-
safeExecuteInTheMiddle(
174-
() => {
175-
responseHook(span, cmd.command, cmd.args, reply);
176-
},
177-
err => {
178-
if (err) {
179-
instrumentation._diag.error('Error executing responseHook', err);
180-
}
181-
},
182-
true
183-
);
184-
}
185-
186-
endSpan(span, err);
187-
return context.with(
188-
originalContext,
189-
originalCallback,
190-
this,
191-
...arguments
192-
);
193-
};
194-
}
195-
try {
196-
// Span will be ended in callback
197-
return original.apply(this, arguments);
198-
} catch (rethrow: any) {
199-
endSpan(span, rethrow);
200-
throw rethrow; // rethrow after ending span
201-
}
191+
192+
endSpan(span, err);
193+
return context.with(
194+
originalContext,
195+
originalCallback,
196+
this,
197+
...arguments
198+
);
202199
};
200+
}
201+
try {
202+
// Span will be ended in callback
203+
return original.apply(this, arguments);
204+
} catch (rethrow: any) {
205+
endSpan(span, rethrow);
206+
throw rethrow; // rethrow after ending span
207+
}
208+
};
203209
};
204210
}
205211

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,7 @@
1515
*/
1616

1717
import type * as redisTypes from 'redis';
18-
import {
19-
context,
20-
Span,
21-
SpanStatusCode,
22-
} from '@opentelemetry/api';
18+
import { context, Span, SpanStatusCode } from '@opentelemetry/api';
2319
import { EventEmitter } from 'events';
2420

2521
export const endSpan = (span: Span, err?: Error | null) => {
@@ -39,9 +35,7 @@ export const getTracedCreateClient = (original: Function) => {
3935
};
4036
};
4137

42-
export const getTracedCreateStreamTrace = (
43-
original: Function
44-
) => {
38+
export const getTracedCreateStreamTrace = (original: Function) => {
4539
return function create_stream_trace(this: redisTypes.RedisClient) {
4640
if (!Object.prototype.hasOwnProperty.call(this, 'stream')) {
4741
Object.defineProperty(this, 'stream', {

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

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -144,30 +144,30 @@ describe('redis@2.x', () => {
144144
expectedDbStatement: string;
145145
method: (cb: redisTypes.Callback<unknown>) => unknown;
146146
}> = [
147-
{
148-
description: 'insert',
149-
command: 'hset',
150-
args: ['hash', 'random', 'random'],
151-
expectedDbStatement: 'hash random [1 other arguments]',
152-
method: (cb: redisTypes.Callback<number>) =>
153-
client.hset('hash', 'random', 'random', cb),
154-
},
155-
{
156-
description: 'get',
157-
command: 'get',
158-
args: ['test'],
159-
expectedDbStatement: 'test',
160-
method: (cb: redisTypes.Callback<string | null>) =>
161-
client.get('test', cb),
162-
},
163-
{
164-
description: 'delete',
165-
command: 'del',
166-
args: ['test'],
167-
expectedDbStatement: 'test',
168-
method: (cb: redisTypes.Callback<number>) => client.del('test', cb),
169-
},
170-
];
147+
{
148+
description: 'insert',
149+
command: 'hset',
150+
args: ['hash', 'random', 'random'],
151+
expectedDbStatement: 'hash random [1 other arguments]',
152+
method: (cb: redisTypes.Callback<number>) =>
153+
client.hset('hash', 'random', 'random', cb),
154+
},
155+
{
156+
description: 'get',
157+
command: 'get',
158+
args: ['test'],
159+
expectedDbStatement: 'test',
160+
method: (cb: redisTypes.Callback<string | null>) =>
161+
client.get('test', cb),
162+
},
163+
{
164+
description: 'delete',
165+
command: 'del',
166+
args: ['test'],
167+
expectedDbStatement: 'test',
168+
method: (cb: redisTypes.Callback<number>) => client.del('test', cb),
169+
},
170+
];
171171

172172
before(done => {
173173
client = redis.createClient(URL);
@@ -402,7 +402,9 @@ describe('redis@2.x', () => {
402402

403403
it('should use new tracer provider after setTracerProvider is called', done => {
404404
const testSpecificMemoryExporter = new InMemorySpanExporter();
405-
const spanProcessor = new SimpleSpanProcessor(testSpecificMemoryExporter);
405+
const spanProcessor = new SimpleSpanProcessor(
406+
testSpecificMemoryExporter
407+
);
406408
const tracerProvider = new NodeTracerProvider({
407409
spanProcessors: [spanProcessor],
408410
});
@@ -411,14 +413,14 @@ describe('redis@2.x', () => {
411413
// new spans use it.
412414
instrumentation.setTracerProvider(tracerProvider);
413415

414-
client.set('foo', 'bar-value-from-test', (err) => {
416+
client.set('test', 'value-with-new-tracer-provider', err => {
415417
assert.ifError(err);
416418
// assert that the span was exported by the new tracer provider
417419
// which is using the test specific span processor
418420
const spans = testSpecificMemoryExporter.getFinishedSpans();
419421
assert.strictEqual(spans.length, 1);
420422
done();
421-
})
423+
});
422424
});
423425
});
424426
});

0 commit comments

Comments
 (0)