google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.3k stars 209 forks source link

Error in plugin: key "...." not in Fields #324

Closed hung-ngo closed 3 years ago

hung-ngo commented 3 years ago

My Telerfaf config has:

[[inputs.snmp]] agents = ["udp://127.0.0.1:161"] name = "Local VM" : [[inputs.snmp]] agents = ["udp://192.168.9.1:161,udp://192.168.9.1:162"] name = "remote VM" : [[processors.starlark]] script = "/home/hn1961/starlark/mine.star"

The starlark script 'mine.star' has the following:

def apply(metric): allocatedMem = metric.fields['Allocated Virtual Memory'] usedMem = metric.fields['Used Virtual Memory'] metric.fields['freeMem']= allocatedMem - usedMem return metric

The goal To have a new metric 'freeMem' with the desirable result.

The environment:

The problem Telegraf gives errors 2020-12-02T18:58:07Z E! [processors.starlark] Error in plugin: key "Allocated Virtual Memory" not in Fields Chronograf shows:

I tried adding IF-ELSE statement in the starlark script so that the calculation are done for the 'Local VM' measurement. However, I am unable to determine the Condition parameter, for instance, def apply(metric): if MEASUREMENT['Name'] == "Local VM": allocatedMem = metric.fields['Allocated Virtual Memory'] usedMem = metric.fields['Used Virtual Memory'] metric.fields['freeMem']= allocatedMem - usedMem return metric else: return metric

I get errors: 2020-12-02T19:08:56Z E! [telegraf] Error running agent: could not initialize processor starlark: /home/hn1961/starlark/sam.star:2:6: undefined: MEASUREMENT

Next Steps Any pointer is greatly appreciated.

alandonovan commented 3 years ago

This repo holds the Go implementation of Starlark, but your question relates to the Telegraf application (https://github.com/influxdata/telegraf/blob/master/plugins/processors/starlark/README.md). Try reporting it here: https://github.com/influxdata/telegraf/issues

hung-ngo commented 3 years ago

Thank you.