telegraf icon indicating copy to clipboard operation
telegraf copied to clipboard

Json serializer converts float to int when decimal part is 0 e.g. 1.0

Open ganeshdudwadkar opened this issue 3 years ago • 6 comments

Relevant telegraf.conf

[[inputs.execd]]
  command = ["/Users/test/tick/count_float.sh"]
[[outputs.file]]
  ## Files to write to, "stdout" is a specially handled file.
  files = [ "/tmp/metrics.out"]
  data_format = "json"

Logs from Telegraf

./telegraf --config test_conf1 --debug
2023-10-19T16:23:29Z I! Loading config: test_conf1
2023-10-19T16:23:29Z I! Starting Telegraf 1.26.2
2023-10-19T16:23:29Z I! Available plugins: 235 inputs, 9 aggregators, 27 processors, 22 parsers, 57 outputs, 2 secret-stores
2023-10-19T16:23:29Z I! Loaded inputs: execd
2023-10-19T16:23:29Z I! Loaded aggregators:
2023-10-19T16:23:29Z I! Loaded processors:
2023-10-19T16:23:29Z I! Loaded secretstores:
2023-10-19T16:23:29Z I! Loaded outputs: file
2023-10-19T16:23:29Z I! Tags enabled: host=TEST
2023-10-19T16:23:29Z I! [agent] Config: Interval:10s, Quiet:false, Hostname:"TEST", Flush Interval:10s
2023-10-19T16:23:29Z D! [agent] Initializing plugins
2023-10-19T16:23:29Z D! [agent] Connecting outputs
2023-10-19T16:23:29Z D! [agent] Attempting connection to [outputs.file]
2023-10-19T16:23:29Z D! [agent] Successfully connected to outputs.file
2023-10-19T16:23:29Z D! [agent] Starting service inputs
2023-10-19T16:23:29Z I! [inputs.execd] Starting process: /Users/test/tick/count_float.sh []
2023-10-19T16:23:39Z D! [outputs.file] Wrote batch of 1 metrics in 10.157333ms
2023-10-19T16:23:39Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:23:49Z D! [outputs.file] Wrote batch of 1 metrics in 4.001583ms
2023-10-19T16:23:49Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:23:59Z D! [outputs.file] Wrote batch of 1 metrics in 1.639583ms
2023-10-19T16:23:59Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:09Z D! [outputs.file] Wrote batch of 1 metrics in 2.666417ms
2023-10-19T16:24:09Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:19Z D! [outputs.file] Wrote batch of 1 metrics in 3.056083ms
2023-10-19T16:24:19Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:29Z D! [outputs.file] Wrote batch of 1 metrics in 1.120666ms
2023-10-19T16:24:29Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:39Z D! [outputs.file] Wrote batch of 1 metrics in 564µs
2023-10-19T16:24:39Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:49Z D! [outputs.file] Wrote batch of 1 metrics in 2.307166ms
2023-10-19T16:24:49Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:24:59Z D! [outputs.file] Wrote batch of 1 metrics in 488.625µs
2023-10-19T16:24:59Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:26:01Z D! [agent] Stopping service inputs
2023-10-19T16:26:01Z I! [inputs.execd] Process /Users/test/tick/count_float.sh shut down
2023-10-19T16:26:01Z D! [agent] Input channel closed
2023-10-19T16:26:01Z I! [agent] Hang on, flushing any cached metrics before shutdown
2023-10-19T16:26:01Z D! [outputs.file] Wrote batch of 1 metrics in 167.125µs
2023-10-19T16:26:01Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-10-19T16:26:01Z I! [agent] Stopping running outputs
2023-10-19T16:26:01Z D! [agent] Stopped Successfully

System info

Telegraf 1.26.2 (git: HEAD@a3a884a1), Mac Pro

Docker

No response

Steps to reproduce

  1. Use given telegraf config & Test script
  2. Start telegraf using the given config
  3. Check file o/p ...

Expected behavior

{"fields":{"count":0.0},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732609} {"fields":{"count":0.1},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732619} {"fields":{"count":0.2},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732629} {"fields":{"count":0.3},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732639} {"fields":{"count":0.4},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732649} {"fields":{"count":0.5},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732659} {"fields":{"count":0.6},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732669} {"fields":{"count":1.0},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732679} {"fields":{"count":2.0},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732689}

Actual behavior

{"fields":{"count":0},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732609} {"fields":{"count":0.1},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732619} {"fields":{"count":0.2},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732629} {"fields":{"count":0.3},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732639} {"fields":{"count":0.4},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732649} {"fields":{"count":0.5},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732659} {"fields":{"count":0.6},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732669} {"fields":{"count":1},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732679} {"fields":{"count":2},"name":"counter_bash","tags":{"host":"TEST"},"timestamp":1697732689}

Additional info

When input field value is like 0.0, 1.0, 2.0 without any decimal part, JSON serializer drops the precision of float and it writes it as an int. This breaks schema of downstream systems which consumes this payload.

contents of the script ->

#!/bin/bash

for counter in 0.0 0.1 0.2 0.3 0.4 0.5 0.6 1.0 2.0
do
    echo "counter_bash count=${counter}"
    sleep 10
done

ganeshdudwadkar avatar Oct 19 '23 16:10 ganeshdudwadkar

@ganeshdudwadkar there is no integer type in JSON. All numeric values are "numbers" (see RFC8259 section 6) and the format is perfectly valid.

srebhan avatar Oct 19 '23 17:10 srebhan

Thanks. But what can be done to write field value 1.0 as 1.0 in json o/p?

ganeshdudwadkar avatar Oct 19 '23 17:10 ganeshdudwadkar

This seems to be json marshaling issue rather than telegraf issue. I posted the question on stackoverflow regarding this -> https://stackoverflow.com/questions/77326018/go-json-marshaling-drops-precision-of-float-numbers

But leaving this open here looking for solutions/workaround from the community. Thanks

ganeshdudwadkar avatar Oct 19 '23 17:10 ganeshdudwadkar

@ganeshdudwadkar I strongly recommend to fix the receiving side if the specification-conform behavior of Telegraf causes issues. You cannot infer the "integer" datatype from a missing decimal separator in JSON! If you can convince the Golang developers to add an option to json.Marshal forcing the indicator I'm happy to use make that available in Telegraf but I guess your chances are slim...

For support questions like this, you should use the forums and/or our Slack channel. Closing this as it is not a bug and nothing Telegraf can influence.

srebhan avatar Oct 20 '23 09:10 srebhan

Well, I understand its Go behind the scene causing the problem but that cannot be an excuse. In influx line protocol, it appends "f" to float value to convince InfluxDB as its a float value. I don't expect to append "f" in json but it can be formatted to print it as float since telegraf knows for sure its a float value. It can be added as configurable option to print float precision for json serializer. Now its up to you guys to entertain this or not. Thanks

https://stackoverflow.com/questions/52446730/stop-json-marshal-from-stripping-trailing-zero-from-floating-point-number

ganeshdudwadkar avatar Oct 20 '23 15:10 ganeshdudwadkar

@ganeshdudwadkar please do not blame JSON spec-specific things on Telegraf! The JSON specification does not distinguish between float and integer!!!!!!!! So what Golang does is perfectly aligned with the spec! If you don't like it, try to convince people to change the JSON spec to e.g. transport integers as numbers and always add a dot to floats with trailing zeros or something! You do have my full support for this.

Telegraf cannot and will not work around issues in the JSON spec. If your downstream parser relies on the behavior you describe it is just wrong!

srebhan avatar Oct 31 '23 09:10 srebhan