Skip to content

Commit 3f9521a

Browse files
committed
fix(redis): apply small review fixes
1 parent a7b48ca commit 3f9521a

File tree

4 files changed

+24
-46
lines changed

4 files changed

+24
-46
lines changed

packages/instrumentation-redis/src/types.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ export interface RedisInstrumentationConfig extends InstrumentationConfig {
6060
/** Require parent to create redis span, default when unset is false */
6161
requireParentSpan?: boolean;
6262

63-
/** Controls which semantic convention attributes are emitted on spans. */
63+
/**
64+
* Controls which semantic-convention attributes are emitted on spans.
65+
* Default: 'OLD'.
66+
* When this option is set, it takes precedence over any value provided via
67+
* the OTEL_SEMCONV_STABILITY_OPT_IN environment variable.
68+
*/
6469
semconvStability?: SemconvStability;
6570
}

packages/instrumentation-redis/src/v2-v3/instrumentation.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import { RedisInstrumentationConfig } from '../types';
3131
/** @knipignore */
3232
import { PACKAGE_NAME, PACKAGE_VERSION } from '../version';
3333
import type { RedisCommand, RedisPluginClientTypes } from './internal-types';
34-
import { SpanKind, context, trace } from '@opentelemetry/api';
34+
import { Attributes, SpanKind, context, trace } from '@opentelemetry/api';
3535
import {
3636
DBSYSTEMVALUES_REDIS,
3737
SEMATTRS_DB_CONNECTION_STRING,
@@ -53,10 +53,12 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
5353

5454
constructor(config: RedisInstrumentationConfig = {}) {
5555
super(PACKAGE_NAME, PACKAGE_VERSION, config);
56-
this._semconvStability = semconvStabilityFromStr(
57-
'database',
58-
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
59-
);
56+
this._semconvStability = config.semconvStability
57+
? config.semconvStability
58+
: semconvStabilityFromStr(
59+
'database',
60+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
61+
);
6062
}
6163

6264
override setConfig(config: RedisInstrumentationConfig = {}) {
@@ -150,7 +152,7 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
150152
const dbStatementSerializer =
151153
config?.dbStatementSerializer || defaultDbStatementSerializer;
152154

153-
const attributes: { [key: string]: any } = {};
155+
const attributes: Attributes = {};
154156

155157
if (instrumentation._semconvStability & SemconvStability.OLD) {
156158
Object.assign(attributes, {
@@ -180,7 +182,7 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
180182

181183
// Set attributes for not explicitly typed RedisPluginClientTypes
182184
if (this.connection_options) {
183-
const connectionAttributes: { [key: string]: any } = {};
185+
const connectionAttributes: Attributes = {};
184186

185187
if (instrumentation._semconvStability & SemconvStability.OLD) {
186188
Object.assign(connectionAttributes, {

packages/instrumentation-redis/src/v4-v5/instrumentation.ts

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import { PACKAGE_NAME, PACKAGE_VERSION } from '../version';
3737
import {
3838
ATTR_DB_OPERATION_NAME,
3939
ATTR_DB_QUERY_TEXT,
40-
ATTR_DB_OPERATION_BATCH_SIZE,
4140
SEMATTRS_DB_STATEMENT,
4241
} from '@opentelemetry/semantic-conventions';
4342
import type { MultiErrorReply } from './internal-types';
@@ -61,10 +60,12 @@ export class RedisInstrumentationV4_V5 extends InstrumentationBase<RedisInstrume
6160

6261
constructor(config: RedisInstrumentationConfig = {}) {
6362
super(PACKAGE_NAME, PACKAGE_VERSION, config);
64-
this._semconvStability = semconvStabilityFromStr(
65-
'database',
66-
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
67-
);
63+
this._semconvStability = config.semconvStability
64+
? config.semconvStability
65+
: semconvStabilityFromStr(
66+
'database',
67+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
68+
);
6869
}
6970

7071
override setConfig(config: RedisInstrumentationConfig = {}) {
@@ -409,11 +410,10 @@ export class RedisInstrumentationV4_V5 extends InstrumentationBase<RedisInstrume
409410
clientOptions,
410411
this._semconvStability
411412
);
412-
413+
if (this._semconvStability & SemconvStability.STABLE) {
414+
attributes[ATTR_DB_OPERATION_NAME] = commandName;
415+
}
413416
try {
414-
if (this._semconvStability & SemconvStability.STABLE) {
415-
attributes[ATTR_DB_OPERATION_NAME] = commandName;
416-
}
417417
const dbStatement = dbStatementSerializer(commandName, commandArgs);
418418
if (dbStatement != null) {
419419
if (this._semconvStability & SemconvStability.OLD) {
@@ -487,12 +487,6 @@ export class RedisInstrumentationV4_V5 extends InstrumentationBase<RedisInstrume
487487

488488
for (let i = 0; i < openSpans.length; i++) {
489489
const { span, commandName, commandArgs } = openSpans[i];
490-
if (
491-
openSpans.length >= 2 &&
492-
this._semconvStability & SemconvStability.STABLE
493-
) {
494-
span.setAttribute(ATTR_DB_OPERATION_BATCH_SIZE, openSpans.length);
495-
}
496490
const currCommandRes = replies[i];
497491
const [res, err] =
498492
currCommandRes instanceof Error

packages/instrumentation-redis/test/v4-v5/redis.test.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ import {
5252
ATTR_SERVER_ADDRESS,
5353
ATTR_SERVER_PORT,
5454
ATTR_EXCEPTION_MESSAGE,
55-
ATTR_DB_OPERATION_BATCH_SIZE,
5655
} from '@opentelemetry/semantic-conventions';
5756
import { RedisResponseCustomAttributeFunction } from '../../src/types';
5857
import { hrTimeToMilliseconds, suppressTracing } from '@opentelemetry/core';
@@ -663,28 +662,6 @@ describe('redis v4-v5', () => {
663662
});
664663
});
665664

666-
describe('extra coverage batch size and URL sanitization', () => {
667-
const stableCfg = { semconvStability: SemconvStability.STABLE };
668-
669-
afterEach(() => {
670-
instrumentation.setConfig({});
671-
});
672-
673-
it('should set ATTR_DB_OPERATION_BATCH_SIZE when multi has 3 commands', async () => {
674-
instrumentation.setConfig(stableCfg);
675-
const multi = client.multi();
676-
multi.set('covKey1', 'v1');
677-
multi.get('covKey1');
678-
multi.set('covKey2', 'v2');
679-
await multi.exec();
680-
681-
const spanWithBatch = getTestSpans().find(
682-
s => s.attributes[ATTR_DB_OPERATION_BATCH_SIZE] === 3
683-
);
684-
assert.ok(spanWithBatch, 'No span had batch size 3');
685-
});
686-
});
687-
688665
describe('config', () => {
689666
describe('dbStatementSerializer', () => {
690667
it('custom dbStatementSerializer', async () => {

0 commit comments

Comments
 (0)