-
Notifications
You must be signed in to change notification settings - Fork 539
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(instrumentation-redis-4): fix multi.exec() instrumentation for re…
…dis >=4.6.12 In [email protected] the behaviour of multi.exec() changed to *throw* a MultiErrorReply if any of the commands errored out. The individual command replies are available at 'err.replies', instead of as the promise result. This adjusts the instrumentation to generate spans as before: only setting SpanStatusCode.ERROR and calling span.recordException for the individual commands that failed. Fixes: #1874
- Loading branch information
Showing
3 changed files
with
95 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
plugins/node/opentelemetry-instrumentation-redis-4/src/internal-types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
// Error class introduced in [email protected]. | ||
// https://github.com/redis/node-redis/blob/[email protected]/packages/client/lib/errors.ts#L69-L84 | ||
export interface MultiErrorReply extends Error { | ||
replies: unknown[]; | ||
errorIndexes: Array<number>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,7 +32,7 @@ const instrumentation = registerInstrumentationTesting( | |
new RedisInstrumentation() | ||
); | ||
|
||
import { createClient, WatchError } from 'redis'; | ||
import * as redis from 'redis'; | ||
import { | ||
Span, | ||
SpanKind, | ||
|
@@ -68,7 +68,7 @@ describe('redis@^4.0.0', () => { | |
let client: any; | ||
|
||
beforeEach(async () => { | ||
client = createClient({ | ||
client = redis.createClient({ | ||
url: redisTestUrl, | ||
}); | ||
await context.with(suppressTracing(context.active()), async () => { | ||
|
@@ -187,7 +187,7 @@ describe('redis@^4.0.0', () => { | |
|
||
describe('client connect', () => { | ||
it('produces a span', async () => { | ||
const newClient = createClient({ | ||
const newClient = redis.createClient({ | ||
url: redisTestUrl, | ||
}); | ||
|
||
|
@@ -223,7 +223,7 @@ describe('redis@^4.0.0', () => { | |
const redisURL = `redis://${redisTestConfig.host}:${ | ||
redisTestConfig.port + 1 | ||
}`; | ||
const newClient = createClient({ | ||
const newClient = redis.createClient({ | ||
url: redisURL, | ||
}); | ||
|
||
|
@@ -246,7 +246,7 @@ describe('redis@^4.0.0', () => { | |
const expectAttributeConnString = `redis://${redisTestConfig.host}:${ | ||
redisTestConfig.port + 1 | ||
}`; | ||
const newClient = createClient({ | ||
const newClient = redis.createClient({ | ||
url: redisURL, | ||
}); | ||
|
||
|
@@ -273,7 +273,7 @@ describe('redis@^4.0.0', () => { | |
const expectAttributeConnString = `redis://${redisTestConfig.host}:${ | ||
redisTestConfig.port + 1 | ||
}?db=mydb`; | ||
const newClient = createClient({ | ||
const newClient = redis.createClient({ | ||
url: redisURL, | ||
}); | ||
|
||
|
@@ -377,11 +377,17 @@ describe('redis@^4.0.0', () => { | |
}); | ||
|
||
it('multi command with error', async () => { | ||
const [setReply, incrReply] = await client | ||
.multi() | ||
.set('key', 'value') | ||
.incr('key') | ||
.exec(); // ['OK', 'ReplyError'] | ||
let replies; | ||
try { | ||
replies = await client.multi().set('key', 'value').incr('key').exec(); | ||
} catch (err) { | ||
// Starting in [email protected] `multi().exec()` with *throw* a | ||
// MultiErrorReply, with `err.replies`, if any of the commands error. | ||
assert.ok(err instanceof redis.MultiErrorReply); | ||
replies = err.replies; | ||
} | ||
const [setReply, incrReply] = replies; | ||
|
||
assert.strictEqual(setReply, 'OK'); // verify we did not screw up the normal functionality | ||
assert.ok(incrReply instanceof Error); // verify we did not screw up the normal functionality | ||
|
||
|
@@ -406,7 +412,7 @@ describe('redis@^4.0.0', () => { | |
await client.multi().get(watchedKey).exec(); | ||
assert.fail('expected WatchError to be thrown and caught in try/catch'); | ||
} catch (error) { | ||
assert.ok(error instanceof WatchError); | ||
assert.ok(error instanceof redis.WatchError); | ||
} | ||
|
||
// All the multi spans' status are set to ERROR. | ||
|