Skip to content

Commit 771ae54

Browse files
committed
Add option to expire values
1 parent 716be18 commit 771ae54

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

plugins/outputs/redistimeseries/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ to use them.
4141
## Timeout for operations such as ping or sending metrics
4242
# timeout = "10s"
4343

44+
## Send an expire command to redis following the metric
45+
## Effectively causing redis to forget old values after a specific duration
46+
## Omit this field to never expire entries
47+
# expire = "300s"
48+
4449
## Enable attempt to convert string fields to numeric values
4550
## If "false" or in case the string value cannot be converted the string
4651
## field will be dropped.

plugins/outputs/redistimeseries/redistimeseries.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,14 @@ import (
2222
var sampleConfig string
2323

2424
type RedisTimeSeries struct {
25-
Address string `toml:"address"`
26-
Username config.Secret `toml:"username"`
27-
Password config.Secret `toml:"password"`
28-
Database int `toml:"database"`
29-
ConvertStringFields bool `toml:"convert_string_fields"`
30-
Timeout config.Duration `toml:"timeout"`
31-
Log telegraf.Logger `toml:"-"`
25+
Address string `toml:"address"`
26+
Username config.Secret `toml:"username"`
27+
Password config.Secret `toml:"password"`
28+
Database int `toml:"database"`
29+
ConvertStringFields bool `toml:"convert_string_fields"`
30+
Timeout config.Duration `toml:"timeout"`
31+
Expire *config.Duration `toml:"expire"`
32+
Log telegraf.Logger `toml:"-"`
3233
tls.ClientConfig
3334
client *redis.Client
3435
}
@@ -104,8 +105,14 @@ func (r *RedisTimeSeries) Write(metrics []telegraf.Metric) error {
104105
}
105106
}
106107

107-
resp := r.client.TSAddWithArgs(ctx, key, m.Time().UnixMilli(), value, &redis.TSOptions{Labels: m.Tags()})
108-
if err := resp.Err(); err != nil {
108+
var err error
109+
pipe := r.client.Pipeline()
110+
pipe.TSAddWithArgs(ctx, key, m.Time().UnixMilli(), value, &redis.TSOptions{Labels: m.Tags()})
111+
if r.Expire != nil {
112+
pipe.Expire(ctx, key, time.Duration(*r.Expire))
113+
}
114+
_, err = pipe.Exec(ctx)
115+
if err != nil {
109116
return fmt.Errorf("adding sample %q failed: %w", key, err)
110117
}
111118
}

plugins/outputs/redistimeseries/sample.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
## Timeout for operations such as ping or sending metrics
1212
# timeout = "10s"
1313

14+
## Send an expire command to redis following the metric
15+
## Effectively causing redis to forget old values after a specific duration
16+
## Omit this field to never expire values
17+
# expire = "300s"
18+
1419
## Enable attempt to convert string fields to numeric values
1520
## If "false" or in case the string value cannot be converted the string
1621
## field will be dropped.

0 commit comments

Comments
 (0)