Skip to content

Commit

Permalink
fix(instrumentation-redis-4): avoid diag.error spam when configured c…
Browse files Browse the repository at this point in the history
…lient 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
  • Loading branch information
trentm authored Sep 4, 2024
1 parent 0503b66 commit ec3b9c8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ function removeCredentialsFromDBConnectionStringAttribute(
diag: DiagLogger,
url?: unknown
): string | undefined {
if (typeof url !== 'string') {
if (typeof url !== 'string' || !url) {
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { diag, DiagLogLevel } from '@opentelemetry/api';
import {
getTestSpans,
registerInstrumentationTesting,
Expand Down Expand Up @@ -288,6 +290,36 @@ describe('redis@^4.0.0', () => {
expectAttributeConnString
);
});

it('with empty string for client URL, there is no crash and no diag.error', async () => {
// Note: This messily leaves the diag logger set for other tests.
const diagErrors = [] as any;
diag.setLogger(
{
verbose() {},
debug() {},
info() {},
warn() {},
error(...args) {
diagErrors.push(args);
},
},
DiagLogLevel.WARN
);

const newClient = createClient({ url: '' });
try {
await newClient.connect();
} catch (_err) {
// Ignore. If the test Redis is not at the default port we expect this
// to error.
}
await newClient.disconnect();

const [span] = getTestSpans();
assert.strictEqual(span.name, 'redis-connect');
assert.strictEqual(diagErrors.length, 0, "no diag.error's");
});
});

describe('multi (transactions) commands', () => {
Expand Down

0 comments on commit ec3b9c8

Please sign in to comment.