Skip to content

Commit 3ded60f

Browse files
committed
review comments
1 parent e8f2d99 commit 3ded60f

File tree

4 files changed

+40
-28
lines changed

4 files changed

+40
-28
lines changed

dev-packages/node-integration-tests/suites/tracing/redis-cache/test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ describe('redis cache auto instrumentation', () => {
9191
}),
9292
// MGET
9393
expect.objectContaining({
94-
description: 'test-key,ioredis-cache:test-key,ioredis-cache:unavailable-data',
94+
description: 'test-key, ioredis-cache:test-key, ioredis-cache:unavailable-data',
9595
op: 'cache.get',
9696
origin: 'auto.db.otel.redis',
9797
data: expect.objectContaining({
9898
'sentry.origin': 'auto.db.otel.redis',
9999
'db.statement': 'mget [3 other arguments]',
100100
'cache.hit': true,
101-
'cache.key': 'test-key,ioredis-cache:test-key,ioredis-cache:unavailable-data',
101+
'cache.key': 'test-key, ioredis-cache:test-key, ioredis-cache:unavailable-data',
102102
'network.peer.address': 'localhost',
103103
'network.peer.port': 6379,
104104
}),

packages/node/src/integrations/tracing/redis.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,26 @@ export const instrumentRedis = generateInstrumentOnce(INTEGRATION_NAME, () => {
5050

5151
const cacheOperation = getCacheOperation(redisCommand);
5252

53-
if (!cacheOperation) return; // redis command unsupported as cache operation
53+
if (!cacheOperation) {
54+
// redis command unsupported as cache operation
55+
return;
56+
}
5457

5558
const cacheItemSize = calculateCacheItemSize(response);
56-
if (cacheItemSize) span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_ITEM_SIZE, cacheItemSize);
59+
if (cacheItemSize) {
60+
span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_ITEM_SIZE, cacheItemSize);
61+
}
5762

58-
if (cacheOperation === 'cache.get' && cacheItemSize !== undefined)
63+
if (cacheOperation === 'cache.get' && cacheItemSize !== undefined) {
5964
span.setAttribute(SEMANTIC_ATTRIBUTE_CACHE_HIT, cacheItemSize > 0);
65+
}
6066

6167
span.setAttributes({
6268
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: cacheOperation,
6369
[SEMANTIC_ATTRIBUTE_CACHE_KEY]: safeKey,
6470
});
6571

66-
span.updateName(`${safeKey.substring(0, 1024)}...`);
72+
span.updateName(safeKey.length > 1024 ? `${safeKey.substring(0, 1024)}...` : safeKey);
6773
},
6874
});
6975
});

packages/node/src/utils/redisCache.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,23 +32,20 @@ export function getCacheKeySafely(cmdArgs: IORedisCommandArgs): string {
3232

3333
const keys: string[] = [];
3434

35-
cmdArgs.forEach(arg => {
36-
if (typeof arg === 'string' || typeof arg === 'number') {
37-
keys.push(arg.toString());
38-
} else if (Buffer.isBuffer(arg)) {
35+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
36+
const processArg = (arg: string | Buffer | number | any[]): void => {
37+
if (typeof arg === 'string' || typeof arg === 'number' || Buffer.isBuffer(arg)) {
3938
keys.push(arg.toString());
4039
} else if (Array.isArray(arg)) {
41-
arg.forEach(subArg => {
42-
if (typeof subArg === 'string' || typeof subArg === 'number') {
43-
keys.push(subArg.toString());
44-
} else if (Buffer.isBuffer(subArg)) {
45-
keys.push(subArg.toString());
46-
}
47-
});
40+
arg.forEach(processArg);
41+
} else {
42+
keys.push('<unknown>');
4843
}
49-
});
44+
};
45+
46+
cmdArgs.forEach(processArg);
5047

51-
return keys.join(',');
48+
return keys.join(', ');
5249
} catch (e) {
5350
return '';
5451
}

packages/node/test/integrations/tracing/redis.test.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ describe('Redis', () => {
2222
it('should return a comma-separated string for multiple arguments', () => {
2323
const cmdArgs = ['key1', 'key2', 'key3'];
2424
const result = getCacheKeySafely(cmdArgs);
25-
expect(result).toBe('key1,key2,key3');
25+
expect(result).toBe('key1, key2, key3');
2626
});
2727

2828
it('should handle number arguments', () => {
2929
const cmdArgs = [1, 2, 3];
3030
const result = getCacheKeySafely(cmdArgs);
31-
expect(result).toBe('1,2,3');
31+
expect(result).toBe('1, 2, 3');
3232
});
3333

3434
it('should handle Buffer arguments', () => {
3535
const cmdArgs = [Buffer.from('key1'), Buffer.from('key2')];
3636
const result = getCacheKeySafely(cmdArgs);
37-
expect(result).toBe('key1,key2');
37+
expect(result).toBe('key1, key2');
3838
});
3939

4040
it('should handle array arguments', () => {
@@ -43,21 +43,30 @@ describe('Redis', () => {
4343
['key3', 'key4'],
4444
];
4545
const result = getCacheKeySafely(cmdArgs);
46-
expect(result).toBe('key1,key2,key3,key4');
46+
expect(result).toBe('key1, key2, key3, key4');
4747
});
4848

4949
it('should handle mixed type arguments', () => {
50-
const cmdArgs = [Buffer.from('key1'), ['key2', 'key3'], [Buffer.from('key4'), 'key5', 'key6', 7]];
50+
const cmdArgs = [Buffer.from('key1'), ['key2', 'key3'], [Buffer.from('key4'), 'key5', 'key6', 7, ['key8']]];
5151
const result = getCacheKeySafely(cmdArgs);
52-
expect(result).toBe('key1,key2,key3,key4,key5,key6,7');
52+
expect(result).toBe('key1, key2, key3, key4, key5, key6, 7, key8');
5353
});
5454

55-
it('should return an empty string if an error occurs', () => {
56-
const cmdArgs = [Symbol('key1')]; // Symbols cannot be converted to a string
55+
it('should handle nested arrays in arguments', () => {
56+
const cmdArgs = [
57+
['key1', 'key2'],
58+
['key3', 'key4', ['key5', ['key6']]],
59+
];
60+
const result = getCacheKeySafely(cmdArgs);
61+
expect(result).toBe('key1, key2, key3, key4, key5, key6');
62+
});
63+
64+
it('should return <unknown> if the arg type is not supported', () => {
65+
const cmdArgs = [Symbol('key1')];
5766
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
5867
// @ts-ignore
5968
const result = getCacheKeySafely(cmdArgs);
60-
expect(result).toBe('');
69+
expect(result).toBe('<unknown>');
6170
});
6271
});
6372

0 commit comments

Comments
 (0)