@chainlink/external-adapter-framework 0.0.10 → 0.0.12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.c8rc.json +3 -0
- package/.eslintignore +9 -0
- package/.eslintrc.js +96 -0
- package/.github/README.MD +17 -0
- package/.github/actions/setup/action.yaml +13 -0
- package/.github/workflows/main.yaml +39 -0
- package/.github/workflows/publish.yaml +20 -0
- package/.prettierignore +13 -0
- package/.yarnrc +0 -0
- package/README.md +103 -0
- package/dist/src/adapter.d.ts +135 -0
- package/dist/src/adapter.js +145 -0
- package/dist/src/background-executor.d.ts +11 -0
- package/{background-executor.js → dist/src/background-executor.js} +0 -0
- package/{cache → dist/src/cache}/factory.d.ts +0 -0
- package/dist/src/cache/factory.js +55 -0
- package/dist/src/cache/index.d.ts +94 -0
- package/dist/src/cache/index.js +173 -0
- package/{cache → dist/src/cache}/local.d.ts +0 -0
- package/{cache → dist/src/cache}/local.js +0 -0
- package/{cache → dist/src/cache}/metrics.d.ts +0 -0
- package/{cache → dist/src/cache}/metrics.js +0 -0
- package/{cache → dist/src/cache}/redis.d.ts +0 -0
- package/dist/src/cache/redis.js +100 -0
- package/dist/src/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
- package/dist/src/config/index.d.ts +214 -0
- package/dist/src/config/index.js +380 -0
- package/{config → dist/src/config}/provider-limits.d.ts +0 -0
- package/{config → dist/src/config}/provider-limits.js +1 -1
- package/dist/src/examples/bank-frick/accounts.d.ts +39 -0
- package/dist/src/examples/bank-frick/accounts.js +192 -0
- package/dist/src/examples/bank-frick/config/index.d.ts +4 -0
- package/dist/src/examples/bank-frick/config/index.js +54 -0
- package/dist/src/examples/bank-frick/index.d.ts +2 -0
- package/dist/src/examples/bank-frick/index.js +15 -0
- package/dist/src/examples/bank-frick/util.d.ts +4 -0
- package/dist/src/examples/bank-frick/util.js +39 -0
- package/dist/src/examples/coingecko/batch-warming.d.ts +7 -0
- package/dist/src/examples/coingecko/batch-warming.js +53 -0
- package/dist/src/examples/coingecko/index.d.ts +2 -0
- package/dist/src/examples/coingecko/index.js +11 -0
- package/dist/src/examples/coingecko/rest.d.ts +12 -0
- package/dist/src/examples/coingecko/rest.js +51 -0
- package/{examples → dist/src/examples}/ncfx/config/index.d.ts +0 -0
- package/{examples → dist/src/examples}/ncfx/config/index.js +0 -0
- package/dist/src/examples/ncfx/index.d.ts +13 -0
- package/dist/src/examples/ncfx/index.js +11 -0
- package/dist/src/examples/ncfx/websocket.d.ts +47 -0
- package/dist/src/examples/ncfx/websocket.js +73 -0
- package/dist/src/index.d.ts +11 -0
- package/dist/src/index.js +128 -0
- package/{metrics → dist/src/metrics}/constants.d.ts +0 -0
- package/{metrics → dist/src/metrics}/constants.js +0 -0
- package/{metrics → dist/src/metrics}/index.d.ts +0 -0
- package/dist/src/metrics/index.js +122 -0
- package/dist/src/metrics/util.d.ts +7 -0
- package/dist/src/metrics/util.js +9 -0
- package/{adapter.d.ts → dist/src/package/adapter.d.ts} +0 -0
- package/{adapter.js → dist/src/package/adapter.js} +0 -0
- package/{background-executor.d.ts → dist/src/package/background-executor.d.ts} +0 -0
- package/dist/src/package/background-executor.js +45 -0
- package/dist/src/package/cache/factory.d.ts +6 -0
- package/{cache → dist/src/package/cache}/factory.js +0 -0
- package/{cache → dist/src/package/cache}/index.d.ts +0 -0
- package/{cache → dist/src/package/cache}/index.js +0 -0
- package/dist/src/package/cache/local.d.ts +23 -0
- package/dist/src/package/cache/local.js +83 -0
- package/dist/src/package/cache/metrics.d.ts +27 -0
- package/dist/src/package/cache/metrics.js +120 -0
- package/dist/src/package/cache/redis.d.ts +16 -0
- package/{cache → dist/src/package/cache}/redis.js +0 -0
- package/{config → dist/src/package/config}/index.d.ts +0 -0
- package/{config → dist/src/package/config}/index.js +0 -0
- package/dist/src/package/config/provider-limits.d.ts +31 -0
- package/dist/src/package/config/provider-limits.js +76 -0
- package/{examples → dist/src/package/examples}/coingecko/batch-warming.d.ts +0 -0
- package/{examples → dist/src/package/examples}/coingecko/batch-warming.js +0 -0
- package/{examples → dist/src/package/examples}/coingecko/index.d.ts +0 -0
- package/{examples → dist/src/package/examples}/coingecko/index.js +0 -0
- package/{examples → dist/src/package/examples}/coingecko/rest.d.ts +0 -0
- package/{examples → dist/src/package/examples}/coingecko/rest.js +0 -0
- package/dist/src/package/examples/ncfx/config/index.d.ts +12 -0
- package/dist/src/package/examples/ncfx/config/index.js +15 -0
- package/{examples → dist/src/package/examples}/ncfx/index.d.ts +0 -0
- package/{examples → dist/src/package/examples}/ncfx/index.js +0 -0
- package/{examples → dist/src/package/examples}/ncfx/websocket.d.ts +0 -0
- package/{examples → dist/src/package/examples}/ncfx/websocket.js +0 -0
- package/{index.d.ts → dist/src/package/index.d.ts} +0 -0
- package/{index.js → dist/src/package/index.js} +0 -0
- package/dist/src/package/metrics/constants.d.ts +16 -0
- package/dist/src/package/metrics/constants.js +25 -0
- package/dist/src/package/metrics/index.d.ts +15 -0
- package/{metrics → dist/src/package/metrics}/index.js +0 -0
- package/{metrics → dist/src/package/metrics}/util.d.ts +0 -0
- package/{metrics → dist/src/package/metrics}/util.js +0 -0
- package/dist/src/package/package.json +72 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.d.ts +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.js +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/index.d.ts +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/index.js +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/metrics.d.ts +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/metrics.js +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.d.ts +0 -0
- package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.js +0 -0
- package/{test.d.ts → dist/src/package/test.d.ts} +0 -0
- package/{test.js → dist/src/package/test.js} +0 -0
- package/{transports → dist/src/package/transports}/batch-warming.d.ts +0 -0
- package/{transports → dist/src/package/transports}/batch-warming.js +0 -0
- package/{transports → dist/src/package/transports}/index.d.ts +0 -0
- package/{transports → dist/src/package/transports}/index.js +0 -0
- package/{transports → dist/src/package/transports}/metrics.d.ts +0 -0
- package/{transports → dist/src/package/transports}/metrics.js +0 -0
- package/{transports → dist/src/package/transports}/rest.d.ts +0 -0
- package/{transports → dist/src/package/transports}/rest.js +0 -0
- package/{transports → dist/src/package/transports}/util.d.ts +0 -0
- package/{transports → dist/src/package/transports}/util.js +0 -0
- package/{transports → dist/src/package/transports}/websocket.d.ts +0 -0
- package/{transports → dist/src/package/transports}/websocket.js +0 -0
- package/{util → dist/src/package/util}/expiring-sorted-set.d.ts +0 -0
- package/{util → dist/src/package/util}/expiring-sorted-set.js +0 -0
- package/{util → dist/src/package/util}/index.d.ts +0 -0
- package/{util → dist/src/package/util}/index.js +0 -0
- package/{util → dist/src/package/util}/logger.d.ts +0 -0
- package/{util → dist/src/package/util}/logger.js +0 -0
- package/{util → dist/src/package/util}/request.d.ts +0 -0
- package/{util → dist/src/package/util}/request.js +0 -0
- package/{validation → dist/src/package/validation}/error.d.ts +0 -0
- package/{validation → dist/src/package/validation}/error.js +0 -0
- package/{validation → dist/src/package/validation}/index.d.ts +0 -0
- package/{validation → dist/src/package/validation}/index.js +0 -0
- package/{validation → dist/src/package/validation}/input-params.d.ts +0 -0
- package/{validation → dist/src/package/validation}/input-params.js +0 -0
- package/{validation → dist/src/package/validation}/override-functions.d.ts +0 -0
- package/{validation → dist/src/package/validation}/override-functions.js +0 -0
- package/{validation → dist/src/package/validation}/preset-tokens.json +0 -0
- package/{validation → dist/src/package/validation}/validator.d.ts +0 -0
- package/{validation → dist/src/package/validation}/validator.js +0 -0
- package/dist/src/package.json +72 -0
- package/dist/src/rate-limiting/background/fixed-frequency.d.ts +11 -0
- package/dist/src/rate-limiting/background/fixed-frequency.js +35 -0
- package/dist/src/rate-limiting/index.d.ts +55 -0
- package/dist/src/rate-limiting/index.js +63 -0
- package/dist/src/rate-limiting/metrics.d.ts +3 -0
- package/dist/src/rate-limiting/metrics.js +44 -0
- package/dist/src/rate-limiting/request/simple-counting.d.ts +21 -0
- package/dist/src/rate-limiting/request/simple-counting.js +62 -0
- package/dist/src/test.d.ts +1 -0
- package/dist/src/test.js +6 -0
- package/dist/src/transports/batch-warming.d.ts +35 -0
- package/dist/src/transports/batch-warming.js +101 -0
- package/dist/src/transports/index.d.ts +70 -0
- package/dist/src/transports/index.js +87 -0
- package/dist/src/transports/metrics.d.ts +22 -0
- package/dist/src/transports/metrics.js +105 -0
- package/dist/src/transports/rest.d.ts +44 -0
- package/dist/src/transports/rest.js +131 -0
- package/dist/src/transports/util.d.ts +8 -0
- package/dist/src/transports/util.js +85 -0
- package/dist/src/transports/websocket.d.ts +80 -0
- package/dist/src/transports/websocket.js +166 -0
- package/dist/src/util/expiring-sorted-set.d.ts +21 -0
- package/dist/src/util/expiring-sorted-set.js +47 -0
- package/dist/src/util/index.d.ts +12 -0
- package/dist/src/util/index.js +35 -0
- package/dist/src/util/logger.d.ts +42 -0
- package/dist/src/util/logger.js +62 -0
- package/dist/src/util/request.d.ts +57 -0
- package/dist/src/util/request.js +2 -0
- package/dist/src/util/subscription-set/expiring-sorted-set.d.ts +22 -0
- package/dist/src/util/subscription-set/expiring-sorted-set.js +47 -0
- package/dist/src/util/subscription-set/subscription-set.d.ts +18 -0
- package/dist/src/util/subscription-set/subscription-set.js +19 -0
- package/dist/src/util/test-payload-loader.d.ts +25 -0
- package/dist/src/util/test-payload-loader.js +83 -0
- package/dist/src/validation/error.d.ts +50 -0
- package/dist/src/validation/error.js +79 -0
- package/dist/src/validation/index.d.ts +5 -0
- package/dist/src/validation/index.js +91 -0
- package/dist/src/validation/input-params.d.ts +15 -0
- package/dist/src/validation/input-params.js +30 -0
- package/dist/src/validation/override-functions.d.ts +3 -0
- package/dist/src/validation/override-functions.js +40 -0
- package/dist/src/validation/preset-tokens.json +23 -0
- package/dist/src/validation/validator.d.ts +47 -0
- package/dist/src/validation/validator.js +303 -0
- package/docker-compose.yaml +35 -0
- package/env.sh +54 -0
- package/env2.sh +55 -0
- package/package.json +5 -3
- package/publish.sh +0 -0
- package/src/adapter.ts +263 -0
- package/src/background-executor.ts +52 -0
- package/src/cache/factory.ts +26 -0
- package/src/cache/index.ts +258 -0
- package/src/cache/local.ts +73 -0
- package/src/cache/metrics.ts +112 -0
- package/src/cache/redis.ts +93 -0
- package/src/config/index.ts +517 -0
- package/src/config/provider-limits.ts +130 -0
- package/src/examples/bank-frick/README.MD +10 -0
- package/src/examples/bank-frick/accounts.ts +246 -0
- package/src/examples/bank-frick/config/index.ts +53 -0
- package/src/examples/bank-frick/index.ts +13 -0
- package/src/examples/bank-frick/types.d.ts +38 -0
- package/src/examples/bank-frick/util.ts +55 -0
- package/src/examples/coingecko/batch-warming.ts +78 -0
- package/src/examples/coingecko/index.ts +9 -0
- package/src/examples/coingecko/rest.ts +77 -0
- package/src/examples/ncfx/config/index.ts +12 -0
- package/src/examples/ncfx/index.ts +9 -0
- package/src/examples/ncfx/websocket.ts +99 -0
- package/src/index.ts +149 -0
- package/src/metrics/constants.ts +23 -0
- package/src/metrics/index.ts +115 -0
- package/src/metrics/util.ts +18 -0
- package/src/rate-limiting/background/fixed-frequency.ts +45 -0
- package/src/rate-limiting/index.ts +100 -0
- package/src/rate-limiting/metrics.ts +18 -0
- package/src/rate-limiting/request/simple-counting.ts +76 -0
- package/src/test.ts +5 -0
- package/src/transports/batch-warming.ts +122 -0
- package/src/transports/index.ts +152 -0
- package/src/transports/metrics.ts +95 -0
- package/src/transports/rest.ts +164 -0
- package/src/transports/util.ts +63 -0
- package/src/transports/websocket.ts +245 -0
- package/src/util/index.ts +22 -0
- package/src/util/logger.ts +69 -0
- package/src/util/request.ts +117 -0
- package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
- package/src/util/subscription-set/subscription-set.ts +35 -0
- package/src/util/test-payload-loader.ts +87 -0
- package/src/validation/error.ts +116 -0
- package/src/validation/index.ts +110 -0
- package/src/validation/input-params.ts +45 -0
- package/src/validation/override-functions.ts +44 -0
- package/src/validation/preset-tokens.json +23 -0
- package/src/validation/validator.ts +384 -0
- package/test/adapter.test.ts +27 -0
- package/test/background-executor.test.ts +108 -0
- package/test/cache/cache-key.test.ts +114 -0
- package/test/cache/helper.ts +100 -0
- package/test/cache/local.test.ts +54 -0
- package/test/cache/redis.test.ts +89 -0
- package/test/correlation.test.ts +114 -0
- package/test/index.test.ts +37 -0
- package/test/metrics/feed-id.test.ts +38 -0
- package/test/metrics/helper.ts +14 -0
- package/test/metrics/labels.test.ts +36 -0
- package/test/metrics/metrics.test.ts +267 -0
- package/test/metrics/redis-metrics.test.ts +113 -0
- package/test/metrics/warmer-metrics.test.ts +192 -0
- package/test/metrics/ws-metrics.test.ts +225 -0
- package/test/rate-limit-config.test.ts +242 -0
- package/test/smoke.test.ts +166 -0
- package/test/transports/batch.test.ts +465 -0
- package/test/transports/rest.test.ts +242 -0
- package/test/transports/websocket.test.ts +183 -0
- package/test/tsconfig.json +5 -0
- package/test/util.ts +77 -0
- package/test/validation.test.ts +178 -0
- package/test-payload-fail.json +3 -0
- package/test-payload.js +22 -0
- package/test-payload.json +7 -0
- package/test.sh +20 -0
- package/test2.sh +2 -0
- package/tsconfig.json +25 -0
- package/typedoc.json +6 -0
- package/webpack.config.js +23 -0
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import * as client from 'prom-client'
|
|
2
|
+
|
|
3
|
+
interface CacheMetricsLabels {
|
|
4
|
+
participant_id: string
|
|
5
|
+
feed_id: string
|
|
6
|
+
cache_type: string
|
|
7
|
+
// Is_from_ws?: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const cacheGet = (label: CacheMetricsLabels, value: unknown, staleness: number) => {
|
|
11
|
+
if (typeof value === 'number' || typeof value === 'string') {
|
|
12
|
+
const parsedValue = Number(value)
|
|
13
|
+
if (!Number.isNaN(parsedValue) && Number.isFinite(parsedValue)) {
|
|
14
|
+
cacheDataGetValues.labels(label).set(parsedValue)
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
cacheDataGetCount.labels(label).inc()
|
|
18
|
+
cacheDataStalenessSeconds.labels(label).set(staleness)
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const cacheSet = (label: CacheMetricsLabels, maxAge: number) => {
|
|
22
|
+
cacheDataSetCount.labels(label).inc()
|
|
23
|
+
cacheDataMaxAge.labels(label).set(maxAge)
|
|
24
|
+
cacheDataStalenessSeconds.labels(label).set(0)
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export const cacheMetricsLabel = (cacheKey: string, feedId: string, cacheType: string) => ({
|
|
28
|
+
participant_id: cacheKey,
|
|
29
|
+
feed_id: feedId,
|
|
30
|
+
cache_type: cacheType,
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
export enum CacheTypes {
|
|
34
|
+
Redis = 'redis',
|
|
35
|
+
Local = 'local',
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export enum CMD_SENT_STATUS {
|
|
39
|
+
TIMEOUT = 'TIMEOUT',
|
|
40
|
+
FAIL = 'FAIL',
|
|
41
|
+
SUCCESS = 'SUCCESS',
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const baseLabels = [
|
|
45
|
+
'feed_id',
|
|
46
|
+
'participant_id',
|
|
47
|
+
'cache_type',
|
|
48
|
+
'is_from_ws',
|
|
49
|
+
'experimental',
|
|
50
|
+
] as const
|
|
51
|
+
|
|
52
|
+
// Skipping this metrics for v3
|
|
53
|
+
// const cache_execution_duration_seconds = new client.Histogram({
|
|
54
|
+
// name: 'cache_execution_duration_seconds',
|
|
55
|
+
// help: 'A histogram bucket of the distribution of cache execution durations',
|
|
56
|
+
// labelNames: [...baseLabels, 'cache_hit'] as const,
|
|
57
|
+
// buckets: [0.01, 0.1, 1, 10],
|
|
58
|
+
// })
|
|
59
|
+
|
|
60
|
+
const cacheDataGetCount = new client.Counter({
|
|
61
|
+
name: 'cache_data_get_count',
|
|
62
|
+
help: 'A counter that increments every time a value is fetched from the cache',
|
|
63
|
+
labelNames: baseLabels,
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const cacheDataGetValues = new client.Gauge({
|
|
67
|
+
name: 'cache_data_get_values',
|
|
68
|
+
help: 'A gauge keeping track of values being fetched from cache',
|
|
69
|
+
labelNames: baseLabels,
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const cacheDataMaxAge = new client.Gauge({
|
|
73
|
+
name: 'cache_data_max_age',
|
|
74
|
+
help: 'A gauge tracking the max age of stored values in the cache',
|
|
75
|
+
labelNames: baseLabels,
|
|
76
|
+
})
|
|
77
|
+
|
|
78
|
+
const cacheDataSetCount = new client.Counter({
|
|
79
|
+
name: 'cache_data_set_count',
|
|
80
|
+
help: 'A counter that increments every time a value is set to the cache',
|
|
81
|
+
labelNames: [...baseLabels, 'status_code'],
|
|
82
|
+
})
|
|
83
|
+
|
|
84
|
+
const cacheDataStalenessSeconds = new client.Gauge({
|
|
85
|
+
name: 'cache_data_staleness_seconds',
|
|
86
|
+
help: 'Observes the staleness of the data returned',
|
|
87
|
+
labelNames: baseLabels,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
// Redis Metrics
|
|
91
|
+
export const redisConnectionsOpen = new client.Counter({
|
|
92
|
+
name: 'redis_connections_open',
|
|
93
|
+
help: 'The number of redis connections that are open',
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
export const redisRetriesCount = new client.Counter({
|
|
97
|
+
name: 'redis_retries_count',
|
|
98
|
+
help: 'The number of retries that have been made to establish a redis connection',
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
export const redisCommandsSentCount = new client.Counter({
|
|
102
|
+
name: 'redis_commands_sent_count',
|
|
103
|
+
help: 'The number of redis commands sent',
|
|
104
|
+
labelNames: ['status', 'function_name'],
|
|
105
|
+
})
|
|
106
|
+
|
|
107
|
+
// Cache Warmer Metrics
|
|
108
|
+
export const cacheWarmerCount = new client.Gauge({
|
|
109
|
+
name: 'cache_warmer_get_count',
|
|
110
|
+
help: 'The number of cache warmers running',
|
|
111
|
+
labelNames: ['isBatched'] as const,
|
|
112
|
+
})
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import Redis from 'ioredis'
|
|
2
|
+
import { AdapterResponse, makeLogger } from '../util'
|
|
3
|
+
import { Cache, CacheEntry } from './index'
|
|
4
|
+
import * as cacheMetrics from './metrics'
|
|
5
|
+
|
|
6
|
+
const logger = makeLogger('RedisCache')
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Redis implementation of a Cache. It uses a simple js Object, storing entries with both
|
|
10
|
+
* a value and an expiration timestamp. Expired entries are deleted on reads (i.e. no background gc/upkeep).
|
|
11
|
+
*
|
|
12
|
+
* @typeParam T - the type for the entries' values
|
|
13
|
+
*/
|
|
14
|
+
export class RedisCache<T = unknown> implements Cache<T> {
|
|
15
|
+
constructor(private client: Redis) {}
|
|
16
|
+
|
|
17
|
+
async get(key: string): Promise<T | undefined> {
|
|
18
|
+
logger.trace(`Getting key ${key}`)
|
|
19
|
+
const value = await this.client.get(key)
|
|
20
|
+
|
|
21
|
+
// Record get command sent to Redis
|
|
22
|
+
cacheMetrics.redisCommandsSentCount
|
|
23
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'get' })
|
|
24
|
+
.inc()
|
|
25
|
+
|
|
26
|
+
if (!value) {
|
|
27
|
+
logger.debug(`No entry in redis cache for key "${key}", returning undefined`)
|
|
28
|
+
return undefined
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return JSON.parse(value) as T
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async delete(key: string): Promise<void> {
|
|
35
|
+
logger.trace(`Deleting key ${key}`)
|
|
36
|
+
await this.client.del(key)
|
|
37
|
+
|
|
38
|
+
// Record delete command sent to Redis
|
|
39
|
+
cacheMetrics.redisCommandsSentCount
|
|
40
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'delete' })
|
|
41
|
+
.inc()
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async set(key: string, value: T, ttl: number): Promise<void> {
|
|
45
|
+
logger.trace(`Setting key ${key}`)
|
|
46
|
+
await this.client.set(key, JSON.stringify(value), 'PX', ttl)
|
|
47
|
+
|
|
48
|
+
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
49
|
+
const feedId = (value as unknown as AdapterResponse).meta?.metrics?.feedId
|
|
50
|
+
if (feedId) {
|
|
51
|
+
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
52
|
+
const label = cacheMetrics.cacheMetricsLabel(key, feedId, cacheMetrics.CacheTypes.Redis)
|
|
53
|
+
cacheMetrics.cacheSet(label, ttl)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// Record set command sent to Redis
|
|
57
|
+
cacheMetrics.redisCommandsSentCount
|
|
58
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'set' })
|
|
59
|
+
.inc()
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async setMany(entries: CacheEntry<T>[], ttl: number): Promise<void> {
|
|
63
|
+
logger.trace(`Setting a bunch of keys`)
|
|
64
|
+
// Unfortunately, there's no ttl for mset
|
|
65
|
+
let chain = this.client.multi()
|
|
66
|
+
|
|
67
|
+
for (const entry of entries) {
|
|
68
|
+
chain = chain.set(entry.key, JSON.stringify(entry.value), 'PX', ttl)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
await chain.exec()
|
|
72
|
+
|
|
73
|
+
// Loop again, but this time to record these in metrics
|
|
74
|
+
for (const entry of entries) {
|
|
75
|
+
// Only record metrics if feed Id is present, otherwise assuming value is not adapter response to record
|
|
76
|
+
const feedId = (entry.value as unknown as AdapterResponse).meta?.metrics?.feedId
|
|
77
|
+
if (feedId) {
|
|
78
|
+
// Record cache set count, max age, and staleness (set to 0 for cache set)
|
|
79
|
+
const label = cacheMetrics.cacheMetricsLabel(
|
|
80
|
+
entry.key,
|
|
81
|
+
feedId,
|
|
82
|
+
cacheMetrics.CacheTypes.Redis,
|
|
83
|
+
)
|
|
84
|
+
cacheMetrics.cacheSet(label, ttl)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Record setMany command sent to Redis
|
|
89
|
+
cacheMetrics.redisCommandsSentCount
|
|
90
|
+
.labels({ status: cacheMetrics.CMD_SENT_STATUS.SUCCESS, function_name: 'exec' })
|
|
91
|
+
.inc()
|
|
92
|
+
}
|
|
93
|
+
}
|