@merkl/api 0.10.151 → 0.10.152
Sign up to get free protection for your applications and to get access to all the features.
@@ -1,10 +1,43 @@
|
|
1
|
-
import { REDIS_READ_TIMEOUT, REDIS_RETRIES, redisClient } from "../../../cache/redis";
|
1
|
+
import { REDIS_READ_TIMEOUT, REDIS_RETRIES, REDIS_WRITE_TIMEOUT, redisClient } from "../../../cache/redis";
|
2
|
+
import { record } from "@elysiajs/opentelemetry";
|
2
3
|
import { withRetry, withTimeout } from "@sdk";
|
3
4
|
export class CacheRepository {
|
4
5
|
static async set(ttl, key, value) {
|
5
|
-
await
|
6
|
+
await record("redis.set", async (span) => {
|
7
|
+
span.setAttribute("db.system", "redis");
|
8
|
+
span.setAttribute("net.transport", "ip_tcp");
|
9
|
+
span.setAttribute("db.statement", `set ${key} ${value} ${ttl}`);
|
10
|
+
const startTime = Date.now();
|
11
|
+
try {
|
12
|
+
const result = await withRetry(withTimeout, [redisClient.set(key, value, { EX: ttl }), REDIS_WRITE_TIMEOUT], REDIS_RETRIES);
|
13
|
+
span.setAttribute("db.redis.response_time", Date.now() - startTime);
|
14
|
+
return result;
|
15
|
+
}
|
16
|
+
catch (error) {
|
17
|
+
span.setStatus({ code: 2 }); // OpenTelemetry StatusCode.ERROR
|
18
|
+
span.setAttribute("error.message", error.message);
|
19
|
+
span.recordException(error);
|
20
|
+
throw error;
|
21
|
+
}
|
22
|
+
});
|
6
23
|
}
|
7
24
|
static async get(key) {
|
8
|
-
return await
|
25
|
+
return await record("redis.get", async (span) => {
|
26
|
+
span.setAttribute("db.system", "redis");
|
27
|
+
span.setAttribute("net.transport", "ip_tcp");
|
28
|
+
span.setAttribute("db.statement", `get ${key}`);
|
29
|
+
const startTime = Date.now();
|
30
|
+
try {
|
31
|
+
const result = await withRetry(withTimeout, [redisClient.get(key), REDIS_READ_TIMEOUT], REDIS_RETRIES);
|
32
|
+
span.setAttribute("db.redis.response_time", Date.now() - startTime);
|
33
|
+
return result;
|
34
|
+
}
|
35
|
+
catch (error) {
|
36
|
+
span.setStatus({ code: 2 }); // OpenTelemetry StatusCode.ERROR
|
37
|
+
span.setAttribute("error.message", error.message);
|
38
|
+
span.recordException(error);
|
39
|
+
throw error;
|
40
|
+
}
|
41
|
+
});
|
9
42
|
}
|
10
43
|
}
|