|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +import { |
| 17 | + getTestSpans, |
| 18 | + registerInstrumentationTesting, |
| 19 | +} from '@opentelemetry/contrib-test-utils'; |
| 20 | +import { RedisInstrumentation } from '../../src/index'; |
| 21 | +import * as assert from 'assert'; |
| 22 | +import { context } from '@opentelemetry/api'; |
| 23 | +import { suppressTracing } from '@opentelemetry/core'; |
| 24 | +import { SpanStatusCode } from '@opentelemetry/api'; |
| 25 | +import { ATTR_DB_STATEMENT } from '../../src/semconv'; |
| 26 | +import { |
| 27 | + ATTR_DB_QUERY_TEXT, |
| 28 | + ATTR_DB_OPERATION_NAME, |
| 29 | +} from '@opentelemetry/semantic-conventions'; |
| 30 | +process.env.OTEL_SEMCONV_STABILITY_OPT_IN = 'database/dup'; |
| 31 | +registerInstrumentationTesting(new RedisInstrumentation()); |
| 32 | +import { createCluster } from 'redis'; |
| 33 | +import type { RedisClusterType } from 'redis'; |
| 34 | +const shouldTest = process.env.RUN_REDIS_CLUSTER_TESTS; |
| 35 | +const clusterRootNodes = [ |
| 36 | + { url: `redis://${process.env.OPENTELEMETRY_REDIS_CLUSTER_HOST || 'localhost'}:${process.env.OPENTELEMETRY_REDIS_CLUSTER_PORT || 6379}` }, |
| 37 | +]; |
| 38 | +describe('redis v4-v5 cluster', () => { |
| 39 | + before(function () { |
| 40 | + if (!shouldTest) { |
| 41 | + this.test!.parent!.pending = true; |
| 42 | + this.skip(); |
| 43 | + } |
| 44 | + }); |
| 45 | + let client: RedisClusterType; |
| 46 | + beforeEach(async () => { |
| 47 | + client = createCluster({ rootNodes: clusterRootNodes }); |
| 48 | + await context.with(suppressTracing(context.active()), async () => { |
| 49 | + await client.connect(); |
| 50 | + }); |
| 51 | + }); |
| 52 | + afterEach(async () => { |
| 53 | + await client?.disconnect(); |
| 54 | + }); |
| 55 | + describe('cluster multi (transaction) commands', () => { |
| 56 | + it('should produce spans for commands run inside multi().exec() on a cluster', async () => { |
| 57 | + const key = 'test-cluster-multi-key'; |
| 58 | + const [zremResult, zcardResult] = await client |
| 59 | + .multi() |
| 60 | + .ZREMRANGEBYSCORE(key, '-inf', Date.now()) |
| 61 | + .ZCARD(key) |
| 62 | + .execTyped(); |
| 63 | + assert.strictEqual(typeof zremResult, 'number'); |
| 64 | + assert.strictEqual(typeof zcardResult, 'number'); |
| 65 | + const spans = getTestSpans(); |
| 66 | + const spanNames = spans.map(s => s.name); |
| 67 | + const zremSpan = spans.find(s => s.name === 'redis-ZREMRANGEBYSCORE'); |
| 68 | + const zcardSpan = spans.find(s => s.name === 'redis-ZCARD'); |
| 69 | + assert.ok(zremSpan, `Expected redis-ZREMRANGEBYSCORE span, got: ${spanNames.join(', ')}`); |
| 70 | + assert.ok(zcardSpan, `Expected redis-ZCARD span, got: ${spanNames.join(', ')}`); |
| 71 | + assert.strictEqual(zremSpan.attributes['db.system'], 'redis'); |
| 72 | + assert.ok((zremSpan.attributes[ATTR_DB_STATEMENT] as string)?.startsWith('ZREMRANGEBYSCORE')); |
| 73 | + assert.ok((zremSpan.attributes[ATTR_DB_QUERY_TEXT] as string)?.startsWith('ZREMRANGEBYSCORE')); |
| 74 | + assert.strictEqual(zremSpan.attributes[ATTR_DB_OPERATION_NAME], 'MULTI'); |
| 75 | + assert.strictEqual(zcardSpan.attributes['db.system'], 'redis'); |
| 76 | + assert.ok((zcardSpan.attributes[ATTR_DB_STATEMENT] as string)?.startsWith('ZCARD')); |
| 77 | + assert.ok((zcardSpan.attributes[ATTR_DB_QUERY_TEXT] as string)?.startsWith('ZCARD')); |
| 78 | + assert.strictEqual(zcardSpan.attributes[ATTR_DB_OPERATION_NAME], 'MULTI'); |
| 79 | + }); |
| 80 | + it('should produce spans for commands run inside multi().exec() with generic addCommand on a cluster', async () => { |
| 81 | + const key = 'test-cluster-addcommand-key'; |
| 82 | + const [setReply] = await client |
| 83 | + .multi() |
| 84 | + .addCommand(key, false, ['SET', key, 'value']) |
| 85 | + .exec(); |
| 86 | + assert.strictEqual(setReply, 'OK'); |
| 87 | + const spans = getTestSpans(); |
| 88 | + const setSpan = spans.find(s => s.name === 'redis-SET'); |
| 89 | + assert.ok(setSpan, 'Expected redis-SET span'); |
| 90 | + assert.strictEqual(setSpan.attributes['db.system'], 'redis'); |
| 91 | + assert.ok((setSpan.attributes[ATTR_DB_STATEMENT] as string)?.startsWith('SET')); |
| 92 | + }); |
| 93 | + it('should handle errors in cluster multi commands', async () => { |
| 94 | + const key = 'test-cluster-error-key'; |
| 95 | + await client.set(key, 'string-value'); |
| 96 | + |
| 97 | + try { |
| 98 | + await client.multi().set(key, 'value').incr(key).exec(); |
| 99 | + } catch (err: any) { |
| 100 | + |
| 101 | + } |
| 102 | + const spans = getTestSpans(); |
| 103 | + const incrSpan = spans.find(s => s.name === 'redis-INCR'); |
| 104 | + assert.ok(incrSpan, 'Expected redis-INCR span'); |
| 105 | + assert.strictEqual(incrSpan.status.code, SpanStatusCode.ERROR); |
| 106 | + }); |
| 107 | + }); |
| 108 | + describe('cluster regular commands', () => { |
| 109 | + it('should produce spans for regular cluster commands', async () => { |
| 110 | + const key = 'test-cluster-regular-key'; |
| 111 | + await client.set(key, 'value'); |
| 112 | + const value = await client.get(key); |
| 113 | + assert.strictEqual(value, 'value'); |
| 114 | + const spans = getTestSpans(); |
| 115 | + const setSpan = spans.find(s => s.name === 'redis-SET'); |
| 116 | + const getSpan = spans.find(s => s.name === 'redis-GET'); |
| 117 | + assert.ok(setSpan, 'Expected redis-SET span'); |
| 118 | + assert.ok(getSpan, 'Expected redis-GET span'); |
| 119 | + assert.strictEqual(setSpan.attributes['db.system'], 'redis'); |
| 120 | + assert.strictEqual(getSpan.attributes['db.system'], 'redis'); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments