How to round output values in telegraf?

Viewed 494

I'm looking for a way to round the values output from telergaf. Temperature, usage_idle, memory_usage, etc... to 14 decimals is very huge. It too much for me. It can double some of my measurements.

I haven't found a way to do it easily.

Now I have this in output:

cpu,host=nuc usage_system=1.4107433532279232,usage_idle=95.06239826368747,usage_iowait=0.37981551817639264,usage_user=2.007596310360536

and I would like that:

cpu,host=nuc usage_system=1.4,usage_idle=95.0,usage_iowait=0.4,usage_user=2.0

I guess you'd have to use a processor, but how do you do that in a simple way? starlark just to round a value ?

3 Answers

Sorry, this is a bit of an old thread, but I had the same need, and did not find any built in way to do this given that even Starlarks Math module round function will only round to whole integers.

My approach was very similar.

I wanted single digit precision after the decimal for most of my weather station data since there is no way the sensors are more accurate than that.

So I went with (inside a starlark routine): v = float(int(v*10))/10

It seemed to do what I need and should survive the 0 case.

I'm not very convinced of my solution, but I did like this... Using a starlak processor.

I haven't found a way to reach a real round function.

[[processors.starlark]]
  source = '''
def apply(metric):
  for k, v in metric.fields.items():
    if type(v) == 'float':
      f = metric.fields[k]
      if (int(f) != 0):
        metric.fields[k] = float(int(f) + float(int((f % int(f)) * 10) / 10))
      else:
        metric.fields[k] = float(int(f * 10) / 10)
  return metric
'''
Related