Skip to content

feat(redis): Support setex command in caching functionality #12380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ async function run() {
await redis.set('test-key', 'test-value');
await redis.set('ioredis-cache:test-key', 'test-value');

await redis.set('ioredis-cache:test-key-set-EX', 'test-value', 'EX', 10);
await redis.setex('ioredis-cache:test-key-setex', 10, 'test-value');

await redis.get('test-key');
await redis.get('ioredis-cache:test-key');
await redis.get('ioredis-cache:unavailable-data');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,34 @@ describe('redis cache auto instrumentation', () => {
'network.peer.port': 6379,
}),
}),
// SET (with EX)
expect.objectContaining({
description: 'ioredis-cache:test-key-set-EX',
op: 'cache.put',
origin: 'auto.db.otel.redis',
data: expect.objectContaining({
'sentry.origin': 'auto.db.otel.redis',
'db.statement': 'set ioredis-cache:test-key-set-EX [3 other arguments]',
'cache.key': ['ioredis-cache:test-key-set-EX'],
'cache.item_size': 2,
'network.peer.address': 'localhost',
'network.peer.port': 6379,
}),
}),
// SETEX
expect.objectContaining({
description: 'ioredis-cache:test-key-setex',
op: 'cache.put',
origin: 'auto.db.otel.redis',
data: expect.objectContaining({
'sentry.origin': 'auto.db.otel.redis',
'db.statement': 'setex ioredis-cache:test-key-setex [2 other arguments]',
'cache.key': ['ioredis-cache:test-key-setex'],
'cache.item_size': 2,
'network.peer.address': 'localhost',
'network.peer.port': 6379,
}),
}),
// GET
expect.objectContaining({
description: 'ioredis-cache:test-key',
Expand Down
3 changes: 1 addition & 2 deletions packages/node/src/integrations/tracing/redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@ let _redisOptions: RedisOptions = {};
export const instrumentRedis = generateInstrumentOnce(INTEGRATION_NAME, () => {
return new IORedisInstrumentation({
responseHook: (span, redisCommand, cmdArgs, response) => {
const safeKey = getCacheKeySafely(redisCommand, cmdArgs);

span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.db.otel.redis');

const safeKey = getCacheKeySafely(redisCommand, cmdArgs);
const cacheOperation = getCacheOperation(redisCommand);

if (
Expand Down
13 changes: 8 additions & 5 deletions packages/node/src/utils/redisCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { flatten } from '@sentry/utils';
const SINGLE_ARG_COMMANDS = ['get', 'set', 'setex'];

export const GET_COMMANDS = ['get', 'mget'];
export const SET_COMMANDS = ['set' /* todo: 'setex' */];
export const SET_COMMANDS = ['set', 'setex'];
// todo: del, expire

/** Determine cache operation based on redis statement */
Expand Down Expand Up @@ -56,14 +56,17 @@ export function getCacheKeySafely(redisCommand: string, cmdArgs: IORedisCommandA

/** Determines whether a redis operation should be considered as "cache operation" by checking if a key is prefixed.
* We only support certain commands (such as 'set', 'get', 'mget'). */
export function shouldConsiderForCache(redisCommand: string, key: string[], prefixes: string[]): boolean {
export function shouldConsiderForCache(redisCommand: string, keys: string[], prefixes: string[]): boolean {
if (!getCacheOperation(redisCommand)) {
return false;
}

return key.reduce((prev, key) => {
return prev || keyHasPrefix(key, prefixes);
}, false);
for (const key of keys) {
if (keyHasPrefix(key, prefixes)) {
return true;
}
}
return false;
}

/** Calculates size based on the cache response value */
Expand Down
2 changes: 1 addition & 1 deletion packages/opentelemetry/src/utils/parseSpanDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function parseSpanDescription(span: AbstractSpan): SpanDescription {

// If db.type exists then this is a database call span
// If the Redis DB is used as a cache, the span description should not be changed
if (dbSystem && (!attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP] || !opIsCache)) {
if (dbSystem && !opIsCache) {
return descriptionForDbSystem({ attributes, name });
}

Expand Down
Loading