Skip to content

Commit 2930b0e

Browse files
committed
Force DuplicatePolicy for Redis TS created programmatically by Cosmotech API.
Depending on target deployment environment, Redis TS can have DUPLICATE_POLICY set to BLOCK. Until now, Redis TS created by Cosmotech API relied on target environment (without override the default value) As we log every API calls, when API is flooded, it can lead to HTTP errors 500 when Redis is deployed with DUPLICATE_POLICY set to BLOCK (https://redis.io/docs/latest/develop/data-types/timeseries/configuration/#duplicate_policy). To prevent that, every Redis TS will be created now with DUPLICATE_POLICY set to MAX. Doing that, will not lead to HTTP 500 error and if X Redis TS will be created with the same timestamp, only the TS with the maximum value will be kept. Note: You should delete all previously created one. A Redis TS cannot be changed/upgraded
1 parent 9d05cdf commit 2930b0e

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

metrics/src/main/kotlin/com/cosmotech/metrics/MetricsServiceImpl.kt

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import org.slf4j.LoggerFactory
1010
import org.springframework.context.event.EventListener
1111
import org.springframework.stereotype.Service
1212
import redis.clients.jedis.UnifiedJedis
13+
import redis.clients.jedis.timeseries.DuplicatePolicy
1314
import redis.clients.jedis.timeseries.TSAlterParams
1415
import redis.clients.jedis.timeseries.TSCreateParams
1516

@@ -58,7 +59,12 @@ class MetricsServiceImpl(
5859
val metricLabels = getMetricLabels(commonLabels)
5960
logger.debug(
6061
"Creating Redis TS: $key with retention: $metricRetention and ${metricLabels.count()} labels")
61-
unifiedJedis.tsCreate(key, TSCreateParams().retention(metricRetention).labels(metricLabels))
62+
unifiedJedis.tsCreate(
63+
key,
64+
TSCreateParams()
65+
.retention(metricRetention)
66+
.labels(metricLabels)
67+
.duplicatePolicy(DuplicatePolicy.MAX))
6268
if (metric.downSampling || csmPlatformProperties.metrics.downSamplingDefaultEnabled) {
6369
val downSamplingKey = getDownSamplingKey(metric)
6470
val downSamplingMetricLabels = getDownSamplingMetricLabels(commonLabels)
@@ -69,7 +75,10 @@ class MetricsServiceImpl(
6975
"and ${downSamplingMetricLabels.count()} labels")
7076
unifiedJedis.tsCreate(
7177
downSamplingKey,
72-
TSCreateParams().retention(downSamplingRetention).labels(downSamplingMetricLabels))
78+
TSCreateParams()
79+
.retention(downSamplingRetention)
80+
.labels(downSamplingMetricLabels)
81+
.duplicatePolicy(DuplicatePolicy.MAX))
7382
logger.debug(
7483
"Creating Redis DownSampling TS rule: from $key to $downSamplingKey, " +
7584
"aggregation: ${metric.downSamplingAggregation.value}, bucketDuration: $downSamplingBucketDuration")
@@ -89,7 +98,12 @@ class MetricsServiceImpl(
8998
logger.debug(
9099
"Redis TS library cannot get current labels so it is not possible to check if labels changed")
91100
val metricLabels = getMetricLabels(commonLabels)
92-
unifiedJedis.tsAlter(key, TSAlterParams().retention(metricRetention).labels(metricLabels))
101+
unifiedJedis.tsAlter(
102+
key,
103+
TSAlterParams()
104+
.retention(metricRetention)
105+
.labels(metricLabels)
106+
.duplicatePolicy(DuplicatePolicy.MAX))
93107
} else {}
94108
}
95109
}

0 commit comments

Comments
 (0)