@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.
Files changed (269) hide show
  1. package/.c8rc.json +3 -0
  2. package/.eslintignore +9 -0
  3. package/.eslintrc.js +96 -0
  4. package/.github/README.MD +17 -0
  5. package/.github/actions/setup/action.yaml +13 -0
  6. package/.github/workflows/main.yaml +39 -0
  7. package/.github/workflows/publish.yaml +20 -0
  8. package/.prettierignore +13 -0
  9. package/.yarnrc +0 -0
  10. package/README.md +103 -0
  11. package/dist/src/adapter.d.ts +135 -0
  12. package/dist/src/adapter.js +145 -0
  13. package/dist/src/background-executor.d.ts +11 -0
  14. package/{background-executor.js → dist/src/background-executor.js} +0 -0
  15. package/{cache → dist/src/cache}/factory.d.ts +0 -0
  16. package/dist/src/cache/factory.js +55 -0
  17. package/dist/src/cache/index.d.ts +94 -0
  18. package/dist/src/cache/index.js +173 -0
  19. package/{cache → dist/src/cache}/local.d.ts +0 -0
  20. package/{cache → dist/src/cache}/local.js +0 -0
  21. package/{cache → dist/src/cache}/metrics.d.ts +0 -0
  22. package/{cache → dist/src/cache}/metrics.js +0 -0
  23. package/{cache → dist/src/cache}/redis.d.ts +0 -0
  24. package/dist/src/cache/redis.js +100 -0
  25. package/dist/src/chainlink-external-adapter-framework-0.0.6.tgz +0 -0
  26. package/dist/src/config/index.d.ts +214 -0
  27. package/dist/src/config/index.js +380 -0
  28. package/{config → dist/src/config}/provider-limits.d.ts +0 -0
  29. package/{config → dist/src/config}/provider-limits.js +1 -1
  30. package/dist/src/examples/bank-frick/accounts.d.ts +39 -0
  31. package/dist/src/examples/bank-frick/accounts.js +192 -0
  32. package/dist/src/examples/bank-frick/config/index.d.ts +4 -0
  33. package/dist/src/examples/bank-frick/config/index.js +54 -0
  34. package/dist/src/examples/bank-frick/index.d.ts +2 -0
  35. package/dist/src/examples/bank-frick/index.js +15 -0
  36. package/dist/src/examples/bank-frick/util.d.ts +4 -0
  37. package/dist/src/examples/bank-frick/util.js +39 -0
  38. package/dist/src/examples/coingecko/batch-warming.d.ts +7 -0
  39. package/dist/src/examples/coingecko/batch-warming.js +53 -0
  40. package/dist/src/examples/coingecko/index.d.ts +2 -0
  41. package/dist/src/examples/coingecko/index.js +11 -0
  42. package/dist/src/examples/coingecko/rest.d.ts +12 -0
  43. package/dist/src/examples/coingecko/rest.js +51 -0
  44. package/{examples → dist/src/examples}/ncfx/config/index.d.ts +0 -0
  45. package/{examples → dist/src/examples}/ncfx/config/index.js +0 -0
  46. package/dist/src/examples/ncfx/index.d.ts +13 -0
  47. package/dist/src/examples/ncfx/index.js +11 -0
  48. package/dist/src/examples/ncfx/websocket.d.ts +47 -0
  49. package/dist/src/examples/ncfx/websocket.js +73 -0
  50. package/dist/src/index.d.ts +11 -0
  51. package/dist/src/index.js +128 -0
  52. package/{metrics → dist/src/metrics}/constants.d.ts +0 -0
  53. package/{metrics → dist/src/metrics}/constants.js +0 -0
  54. package/{metrics → dist/src/metrics}/index.d.ts +0 -0
  55. package/dist/src/metrics/index.js +122 -0
  56. package/dist/src/metrics/util.d.ts +7 -0
  57. package/dist/src/metrics/util.js +9 -0
  58. package/{adapter.d.ts → dist/src/package/adapter.d.ts} +0 -0
  59. package/{adapter.js → dist/src/package/adapter.js} +0 -0
  60. package/{background-executor.d.ts → dist/src/package/background-executor.d.ts} +0 -0
  61. package/dist/src/package/background-executor.js +45 -0
  62. package/dist/src/package/cache/factory.d.ts +6 -0
  63. package/{cache → dist/src/package/cache}/factory.js +0 -0
  64. package/{cache → dist/src/package/cache}/index.d.ts +0 -0
  65. package/{cache → dist/src/package/cache}/index.js +0 -0
  66. package/dist/src/package/cache/local.d.ts +23 -0
  67. package/dist/src/package/cache/local.js +83 -0
  68. package/dist/src/package/cache/metrics.d.ts +27 -0
  69. package/dist/src/package/cache/metrics.js +120 -0
  70. package/dist/src/package/cache/redis.d.ts +16 -0
  71. package/{cache → dist/src/package/cache}/redis.js +0 -0
  72. package/{config → dist/src/package/config}/index.d.ts +0 -0
  73. package/{config → dist/src/package/config}/index.js +0 -0
  74. package/dist/src/package/config/provider-limits.d.ts +31 -0
  75. package/dist/src/package/config/provider-limits.js +76 -0
  76. package/{examples → dist/src/package/examples}/coingecko/batch-warming.d.ts +0 -0
  77. package/{examples → dist/src/package/examples}/coingecko/batch-warming.js +0 -0
  78. package/{examples → dist/src/package/examples}/coingecko/index.d.ts +0 -0
  79. package/{examples → dist/src/package/examples}/coingecko/index.js +0 -0
  80. package/{examples → dist/src/package/examples}/coingecko/rest.d.ts +0 -0
  81. package/{examples → dist/src/package/examples}/coingecko/rest.js +0 -0
  82. package/dist/src/package/examples/ncfx/config/index.d.ts +12 -0
  83. package/dist/src/package/examples/ncfx/config/index.js +15 -0
  84. package/{examples → dist/src/package/examples}/ncfx/index.d.ts +0 -0
  85. package/{examples → dist/src/package/examples}/ncfx/index.js +0 -0
  86. package/{examples → dist/src/package/examples}/ncfx/websocket.d.ts +0 -0
  87. package/{examples → dist/src/package/examples}/ncfx/websocket.js +0 -0
  88. package/{index.d.ts → dist/src/package/index.d.ts} +0 -0
  89. package/{index.js → dist/src/package/index.js} +0 -0
  90. package/dist/src/package/metrics/constants.d.ts +16 -0
  91. package/dist/src/package/metrics/constants.js +25 -0
  92. package/dist/src/package/metrics/index.d.ts +15 -0
  93. package/{metrics → dist/src/package/metrics}/index.js +0 -0
  94. package/{metrics → dist/src/package/metrics}/util.d.ts +0 -0
  95. package/{metrics → dist/src/package/metrics}/util.js +0 -0
  96. package/dist/src/package/package.json +72 -0
  97. package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.d.ts +0 -0
  98. package/{rate-limiting → dist/src/package/rate-limiting}/background/fixed-frequency.js +0 -0
  99. package/{rate-limiting → dist/src/package/rate-limiting}/index.d.ts +0 -0
  100. package/{rate-limiting → dist/src/package/rate-limiting}/index.js +0 -0
  101. package/{rate-limiting → dist/src/package/rate-limiting}/metrics.d.ts +0 -0
  102. package/{rate-limiting → dist/src/package/rate-limiting}/metrics.js +0 -0
  103. package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.d.ts +0 -0
  104. package/{rate-limiting → dist/src/package/rate-limiting}/request/simple-counting.js +0 -0
  105. package/{test.d.ts → dist/src/package/test.d.ts} +0 -0
  106. package/{test.js → dist/src/package/test.js} +0 -0
  107. package/{transports → dist/src/package/transports}/batch-warming.d.ts +0 -0
  108. package/{transports → dist/src/package/transports}/batch-warming.js +0 -0
  109. package/{transports → dist/src/package/transports}/index.d.ts +0 -0
  110. package/{transports → dist/src/package/transports}/index.js +0 -0
  111. package/{transports → dist/src/package/transports}/metrics.d.ts +0 -0
  112. package/{transports → dist/src/package/transports}/metrics.js +0 -0
  113. package/{transports → dist/src/package/transports}/rest.d.ts +0 -0
  114. package/{transports → dist/src/package/transports}/rest.js +0 -0
  115. package/{transports → dist/src/package/transports}/util.d.ts +0 -0
  116. package/{transports → dist/src/package/transports}/util.js +0 -0
  117. package/{transports → dist/src/package/transports}/websocket.d.ts +0 -0
  118. package/{transports → dist/src/package/transports}/websocket.js +0 -0
  119. package/{util → dist/src/package/util}/expiring-sorted-set.d.ts +0 -0
  120. package/{util → dist/src/package/util}/expiring-sorted-set.js +0 -0
  121. package/{util → dist/src/package/util}/index.d.ts +0 -0
  122. package/{util → dist/src/package/util}/index.js +0 -0
  123. package/{util → dist/src/package/util}/logger.d.ts +0 -0
  124. package/{util → dist/src/package/util}/logger.js +0 -0
  125. package/{util → dist/src/package/util}/request.d.ts +0 -0
  126. package/{util → dist/src/package/util}/request.js +0 -0
  127. package/{validation → dist/src/package/validation}/error.d.ts +0 -0
  128. package/{validation → dist/src/package/validation}/error.js +0 -0
  129. package/{validation → dist/src/package/validation}/index.d.ts +0 -0
  130. package/{validation → dist/src/package/validation}/index.js +0 -0
  131. package/{validation → dist/src/package/validation}/input-params.d.ts +0 -0
  132. package/{validation → dist/src/package/validation}/input-params.js +0 -0
  133. package/{validation → dist/src/package/validation}/override-functions.d.ts +0 -0
  134. package/{validation → dist/src/package/validation}/override-functions.js +0 -0
  135. package/{validation → dist/src/package/validation}/preset-tokens.json +0 -0
  136. package/{validation → dist/src/package/validation}/validator.d.ts +0 -0
  137. package/{validation → dist/src/package/validation}/validator.js +0 -0
  138. package/dist/src/package.json +72 -0
  139. package/dist/src/rate-limiting/background/fixed-frequency.d.ts +11 -0
  140. package/dist/src/rate-limiting/background/fixed-frequency.js +35 -0
  141. package/dist/src/rate-limiting/index.d.ts +55 -0
  142. package/dist/src/rate-limiting/index.js +63 -0
  143. package/dist/src/rate-limiting/metrics.d.ts +3 -0
  144. package/dist/src/rate-limiting/metrics.js +44 -0
  145. package/dist/src/rate-limiting/request/simple-counting.d.ts +21 -0
  146. package/dist/src/rate-limiting/request/simple-counting.js +62 -0
  147. package/dist/src/test.d.ts +1 -0
  148. package/dist/src/test.js +6 -0
  149. package/dist/src/transports/batch-warming.d.ts +35 -0
  150. package/dist/src/transports/batch-warming.js +101 -0
  151. package/dist/src/transports/index.d.ts +70 -0
  152. package/dist/src/transports/index.js +87 -0
  153. package/dist/src/transports/metrics.d.ts +22 -0
  154. package/dist/src/transports/metrics.js +105 -0
  155. package/dist/src/transports/rest.d.ts +44 -0
  156. package/dist/src/transports/rest.js +131 -0
  157. package/dist/src/transports/util.d.ts +8 -0
  158. package/dist/src/transports/util.js +85 -0
  159. package/dist/src/transports/websocket.d.ts +80 -0
  160. package/dist/src/transports/websocket.js +166 -0
  161. package/dist/src/util/expiring-sorted-set.d.ts +21 -0
  162. package/dist/src/util/expiring-sorted-set.js +47 -0
  163. package/dist/src/util/index.d.ts +12 -0
  164. package/dist/src/util/index.js +35 -0
  165. package/dist/src/util/logger.d.ts +42 -0
  166. package/dist/src/util/logger.js +62 -0
  167. package/dist/src/util/request.d.ts +57 -0
  168. package/dist/src/util/request.js +2 -0
  169. package/dist/src/util/subscription-set/expiring-sorted-set.d.ts +22 -0
  170. package/dist/src/util/subscription-set/expiring-sorted-set.js +47 -0
  171. package/dist/src/util/subscription-set/subscription-set.d.ts +18 -0
  172. package/dist/src/util/subscription-set/subscription-set.js +19 -0
  173. package/dist/src/util/test-payload-loader.d.ts +25 -0
  174. package/dist/src/util/test-payload-loader.js +83 -0
  175. package/dist/src/validation/error.d.ts +50 -0
  176. package/dist/src/validation/error.js +79 -0
  177. package/dist/src/validation/index.d.ts +5 -0
  178. package/dist/src/validation/index.js +91 -0
  179. package/dist/src/validation/input-params.d.ts +15 -0
  180. package/dist/src/validation/input-params.js +30 -0
  181. package/dist/src/validation/override-functions.d.ts +3 -0
  182. package/dist/src/validation/override-functions.js +40 -0
  183. package/dist/src/validation/preset-tokens.json +23 -0
  184. package/dist/src/validation/validator.d.ts +47 -0
  185. package/dist/src/validation/validator.js +303 -0
  186. package/docker-compose.yaml +35 -0
  187. package/env.sh +54 -0
  188. package/env2.sh +55 -0
  189. package/package.json +5 -3
  190. package/publish.sh +0 -0
  191. package/src/adapter.ts +263 -0
  192. package/src/background-executor.ts +52 -0
  193. package/src/cache/factory.ts +26 -0
  194. package/src/cache/index.ts +258 -0
  195. package/src/cache/local.ts +73 -0
  196. package/src/cache/metrics.ts +112 -0
  197. package/src/cache/redis.ts +93 -0
  198. package/src/config/index.ts +517 -0
  199. package/src/config/provider-limits.ts +130 -0
  200. package/src/examples/bank-frick/README.MD +10 -0
  201. package/src/examples/bank-frick/accounts.ts +246 -0
  202. package/src/examples/bank-frick/config/index.ts +53 -0
  203. package/src/examples/bank-frick/index.ts +13 -0
  204. package/src/examples/bank-frick/types.d.ts +38 -0
  205. package/src/examples/bank-frick/util.ts +55 -0
  206. package/src/examples/coingecko/batch-warming.ts +78 -0
  207. package/src/examples/coingecko/index.ts +9 -0
  208. package/src/examples/coingecko/rest.ts +77 -0
  209. package/src/examples/ncfx/config/index.ts +12 -0
  210. package/src/examples/ncfx/index.ts +9 -0
  211. package/src/examples/ncfx/websocket.ts +99 -0
  212. package/src/index.ts +149 -0
  213. package/src/metrics/constants.ts +23 -0
  214. package/src/metrics/index.ts +115 -0
  215. package/src/metrics/util.ts +18 -0
  216. package/src/rate-limiting/background/fixed-frequency.ts +45 -0
  217. package/src/rate-limiting/index.ts +100 -0
  218. package/src/rate-limiting/metrics.ts +18 -0
  219. package/src/rate-limiting/request/simple-counting.ts +76 -0
  220. package/src/test.ts +5 -0
  221. package/src/transports/batch-warming.ts +122 -0
  222. package/src/transports/index.ts +152 -0
  223. package/src/transports/metrics.ts +95 -0
  224. package/src/transports/rest.ts +164 -0
  225. package/src/transports/util.ts +63 -0
  226. package/src/transports/websocket.ts +245 -0
  227. package/src/util/index.ts +22 -0
  228. package/src/util/logger.ts +69 -0
  229. package/src/util/request.ts +117 -0
  230. package/src/util/subscription-set/expiring-sorted-set.ts +54 -0
  231. package/src/util/subscription-set/subscription-set.ts +35 -0
  232. package/src/util/test-payload-loader.ts +87 -0
  233. package/src/validation/error.ts +116 -0
  234. package/src/validation/index.ts +110 -0
  235. package/src/validation/input-params.ts +45 -0
  236. package/src/validation/override-functions.ts +44 -0
  237. package/src/validation/preset-tokens.json +23 -0
  238. package/src/validation/validator.ts +384 -0
  239. package/test/adapter.test.ts +27 -0
  240. package/test/background-executor.test.ts +108 -0
  241. package/test/cache/cache-key.test.ts +114 -0
  242. package/test/cache/helper.ts +100 -0
  243. package/test/cache/local.test.ts +54 -0
  244. package/test/cache/redis.test.ts +89 -0
  245. package/test/correlation.test.ts +114 -0
  246. package/test/index.test.ts +37 -0
  247. package/test/metrics/feed-id.test.ts +38 -0
  248. package/test/metrics/helper.ts +14 -0
  249. package/test/metrics/labels.test.ts +36 -0
  250. package/test/metrics/metrics.test.ts +267 -0
  251. package/test/metrics/redis-metrics.test.ts +113 -0
  252. package/test/metrics/warmer-metrics.test.ts +192 -0
  253. package/test/metrics/ws-metrics.test.ts +225 -0
  254. package/test/rate-limit-config.test.ts +242 -0
  255. package/test/smoke.test.ts +166 -0
  256. package/test/transports/batch.test.ts +465 -0
  257. package/test/transports/rest.test.ts +242 -0
  258. package/test/transports/websocket.test.ts +183 -0
  259. package/test/tsconfig.json +5 -0
  260. package/test/util.ts +77 -0
  261. package/test/validation.test.ts +178 -0
  262. package/test-payload-fail.json +3 -0
  263. package/test-payload.js +22 -0
  264. package/test-payload.json +7 -0
  265. package/test.sh +20 -0
  266. package/test2.sh +2 -0
  267. package/tsconfig.json +25 -0
  268. package/typedoc.json +6 -0
  269. 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
+ }