Skip to content

Commit ec3b9c8

Browse files
authored
fix(instrumentation-redis-4): avoid diag.error spam when configured client URL is the empty string (#2399)
Issue #2389 showed that this instrumentation would crash on a Redis client configured with {url: ''} (an empty string). The crash was fixed in #2397. This change avoids the once-crashy code, and hence the diag.error spam, by using the same guard before attempting to parse the configured client URL that the Redis client itself does, if (options?.url), Refs: #2389
1 parent 0503b66 commit ec3b9c8

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function removeCredentialsFromDBConnectionStringAttribute(
4646
diag: DiagLogger,
4747
url?: unknown
4848
): string | undefined {
49-
if (typeof url !== 'string') {
49+
if (typeof url !== 'string' || !url) {
5050
return;
5151
}
5252

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16+
17+
import { diag, DiagLogLevel } from '@opentelemetry/api';
1618
import {
1719
getTestSpans,
1820
registerInstrumentationTesting,
@@ -288,6 +290,36 @@ describe('redis@^4.0.0', () => {
288290
expectAttributeConnString
289291
);
290292
});
293+
294+
it('with empty string for client URL, there is no crash and no diag.error', async () => {
295+
// Note: This messily leaves the diag logger set for other tests.
296+
const diagErrors = [] as any;
297+
diag.setLogger(
298+
{
299+
verbose() {},
300+
debug() {},
301+
info() {},
302+
warn() {},
303+
error(...args) {
304+
diagErrors.push(args);
305+
},
306+
},
307+
DiagLogLevel.WARN
308+
);
309+
310+
const newClient = createClient({ url: '' });
311+
try {
312+
await newClient.connect();
313+
} catch (_err) {
314+
// Ignore. If the test Redis is not at the default port we expect this
315+
// to error.
316+
}
317+
await newClient.disconnect();
318+
319+
const [span] = getTestSpans();
320+
assert.strictEqual(span.name, 'redis-connect');
321+
assert.strictEqual(diagErrors.length, 0, "no diag.error's");
322+
});
291323
});
292324

293325
describe('multi (transactions) commands', () => {

0 commit comments

Comments
 (0)