Skip to content

Commit 9d6f509

Browse files
committed
test(redis): add semconvStability tests and override setConfig
1 parent c7ceada commit 9d6f509

File tree

5 files changed

+240
-6
lines changed

5 files changed

+240
-6
lines changed

packages/instrumentation-redis/src/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
*/
1616

1717
import { Span } from '@opentelemetry/api';
18-
import { InstrumentationConfig } from '@opentelemetry/instrumentation';
18+
import {
19+
InstrumentationConfig,
20+
SemconvStability,
21+
} from '@opentelemetry/instrumentation';
1922

2023
/**
2124
* Function that can be used to serialize db.statement tag
@@ -56,4 +59,7 @@ export interface RedisInstrumentationConfig extends InstrumentationConfig {
5659

5760
/** Require parent to create redis span, default when unset is false */
5861
requireParentSpan?: boolean;
62+
63+
/** Controls which semantic convention attributes are emitted on spans. */
64+
semconvStability?: SemconvStability;
5965
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ export class RedisInstrumentationV2_V3 extends InstrumentationBase<RedisInstrume
5959
);
6060
}
6161

62+
override setConfig(config: RedisInstrumentationConfig = {}) {
63+
super.setConfig(config);
64+
this._semconvStability = config.semconvStability
65+
? config.semconvStability
66+
: semconvStabilityFromStr(
67+
'database',
68+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
69+
);
70+
}
71+
6272
protected init() {
6373
return [
6474
new InstrumentationNodeModuleDefinition(

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ export class RedisInstrumentationV4_V5 extends InstrumentationBase<RedisInstrume
6767
);
6868
}
6969

70+
override setConfig(config: RedisInstrumentationConfig = {}) {
71+
super.setConfig(config);
72+
this._semconvStability = config.semconvStability
73+
? config.semconvStability
74+
: semconvStabilityFromStr(
75+
'database',
76+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN
77+
);
78+
}
79+
7080
protected init() {
7181
// @node-redis/client is a new package introduced and consumed by 'redis 4.0.x'
7282
// on [email protected] it was changed to @redis/client.

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

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,14 @@ import {
3333
SEMATTRS_DB_SYSTEM,
3434
SEMATTRS_NET_PEER_NAME,
3535
SEMATTRS_NET_PEER_PORT,
36+
ATTR_DB_SYSTEM_NAME,
37+
ATTR_DB_OPERATION_NAME,
38+
ATTR_DB_QUERY_TEXT,
39+
ATTR_SERVER_ADDRESS,
40+
ATTR_SERVER_PORT,
3641
} from '@opentelemetry/semantic-conventions';
3742

43+
process.env.OTEL_SEMCONV_STABILITY_OPT_IN = 'database/dup';
3844
const instrumentation = testUtils.registerInstrumentationTesting(
3945
new RedisInstrumentation()
4046
);
@@ -50,6 +56,9 @@ const CONFIG = {
5056
const URL = `redis://${CONFIG.host}:${CONFIG.port}`;
5157

5258
const DEFAULT_ATTRIBUTES = {
59+
[ATTR_DB_SYSTEM_NAME]: 'redis',
60+
[ATTR_SERVER_ADDRESS]: CONFIG.host,
61+
[ATTR_SERVER_PORT]: CONFIG.port,
5362
[SEMATTRS_DB_SYSTEM]: DBSYSTEMVALUES_REDIS,
5463
[SEMATTRS_NET_PEER_NAME]: CONFIG.host,
5564
[SEMATTRS_NET_PEER_PORT]: CONFIG.port,
@@ -105,29 +114,33 @@ describe('redis v2-v3', () => {
105114
description: string;
106115
command: string;
107116
args: string[];
108-
expectedDbStatement: string;
117+
expectedDbStatementOld: string;
118+
expectedDbStatementStable: string;
109119
method: (cb: Callback<unknown>) => unknown;
110120
}> = [
111121
{
112122
description: 'insert',
113123
command: 'hset',
114124
args: ['hash', 'random', 'random'],
115-
expectedDbStatement: 'hash random [1 other arguments]',
125+
expectedDbStatementOld: 'hash random [1 other arguments]',
126+
expectedDbStatementStable: 'hset hash random [1 other arguments]',
116127
method: (cb: Callback<number>) =>
117128
client.hset('hash', 'random', 'random', cb),
118129
},
119130
{
120131
description: 'get',
121132
command: 'get',
122133
args: ['test'],
123-
expectedDbStatement: 'test',
134+
expectedDbStatementOld: 'test',
135+
expectedDbStatementStable: 'get test',
124136
method: (cb: Callback<string | null>) => client.get('test', cb),
125137
},
126138
{
127139
description: 'delete',
128140
command: 'del',
129141
args: ['test'],
130-
expectedDbStatement: 'test',
142+
expectedDbStatementOld: 'test',
143+
expectedDbStatementStable: 'del test',
131144
method: (cb: Callback<number>) => client.del('test', cb),
132145
},
133146
];
@@ -163,7 +176,9 @@ describe('redis v2-v3', () => {
163176
it(`should create a child span for ${operation.description}`, done => {
164177
const attributes = {
165178
...DEFAULT_ATTRIBUTES,
166-
[SEMATTRS_DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatement}`,
179+
[ATTR_DB_OPERATION_NAME]: operation.command,
180+
[ATTR_DB_QUERY_TEXT]: operation.expectedDbStatementStable,
181+
[SEMATTRS_DB_STATEMENT]: `${operation.command} ${operation.expectedDbStatementOld}`,
167182
};
168183
const span = tracer.startSpan('test span');
169184
context.with(trace.setSpan(context.active(), span), () => {
@@ -177,6 +192,7 @@ describe('redis v2-v3', () => {
177192
endedSpans[0].name,
178193
`redis-${operation.command}`
179194
);
195+
180196
testUtils.assertSpan(
181197
endedSpans[0],
182198
SpanKind.CLIENT,
@@ -233,6 +249,11 @@ describe('redis v2-v3', () => {
233249
endedSpans[0].attributes[SEMATTRS_DB_STATEMENT],
234250
expectedStatement
235251
);
252+
253+
assert.strictEqual(
254+
endedSpans[0].attributes[ATTR_DB_QUERY_TEXT],
255+
expectedStatement
256+
);
236257
done();
237258
});
238259
});

0 commit comments

Comments
 (0)