Skip to content

Commit

Permalink
feat(serializers.influx): Add option to omit timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed Apr 24, 2024
1 parent bfda888 commit 6ba1473
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 5 deletions.
6 changes: 6 additions & 0 deletions plugins/serializers/influx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ for interoperability.
## integer values. Enabling this option will result in field type errors if
## existing data has been written.
influx_uint_support = false

## When true, Telegraf will omit the timestamp on data to allow InfluxDB
## to set the timestamp of the data during ingestion. This is generally NOT
## what you want as it can lead to data points captured at different times
## getting omitted due to similar data.
# influx_omit_timestamp = true
```

## Metrics
Expand Down
13 changes: 8 additions & 5 deletions plugins/serializers/influx/influx.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ func (e FieldError) Error() string {

// Serializer is a serializer for line protocol.
type Serializer struct {
MaxLineBytes int `toml:"influx_max_line_bytes"`
SortFields bool `toml:"influx_sort_fields"`
UintSupport bool `toml:"influx_uint_support"`
MaxLineBytes int `toml:"influx_max_line_bytes"`
SortFields bool `toml:"influx_sort_fields"`
UintSupport bool `toml:"influx_uint_support"`
OmitTimestamp bool `toml:"influx_omit_timestamp"`

bytesWritten int

Expand Down Expand Up @@ -153,8 +154,10 @@ func (s *Serializer) buildHeader(m telegraf.Metric) error {

func (s *Serializer) buildFooter(m telegraf.Metric) {
s.footer = s.footer[:0]
s.footer = append(s.footer, ' ')
s.footer = strconv.AppendInt(s.footer, m.Time().UnixNano(), 10)
if !s.OmitTimestamp {
s.footer = append(s.footer, ' ')
s.footer = strconv.AppendInt(s.footer, m.Time().UnixNano(), 10)
}
s.footer = append(s.footer, '\n')
}

Expand Down
18 changes: 18 additions & 0 deletions plugins/serializers/influx/influx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,24 @@ func TestSerializer(t *testing.T) {
}
}

func TestOmitTimestamp(t *testing.T) {
m := metric.New(
"cpu",
map[string]string{},
map[string]interface{}{
"value": 42.0,
},
time.Unix(1519194109, 42),
)

serializer := &Serializer{
OmitTimestamp: true,
}
output, err := serializer.Serialize(m)
require.NoError(t, err)
require.Equal(t, []byte("cpu value=42\n"), output)
}

func BenchmarkSerializer(b *testing.B) {
for _, tt := range tests {
b.Run(tt.name, func(b *testing.B) {
Expand Down
3 changes: 3 additions & 0 deletions plugins/serializers/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ type Config struct {
// Support unsigned integer output; influx format only
InfluxUintSupport bool `toml:"influx_uint_support"`

// Omit timestamp from output; influx format only
InfluxOmitTimestamp bool `toml:"influx_omit_timestamp"`

// Prefix to add to all measurements, only supports Graphite
Prefix string `toml:"prefix"`

Expand Down

0 comments on commit 6ba1473

Please sign in to comment.