How does state = {} work in telegraf aggregator

I am defining “state = {“critical”:,“metrics”:}” in [[aggregators.starlark]], but when printing state it is giving me output as “{}”. If any one give any pointers on this.

Hello @snehal520,
Welcome!
When using Telegraf it’s helpful to you use the debug=true in the agent portion of the config. Can you please share the logs as well as the rest of your telegraf conf?

That syntax looks fine.

I find these resources helpful too:

@Anaisdg I am trying to use starlark aggregator in telegraf. how state variable is getting shared across metrics.
consider the below snippet

[[aggregators.starlark]]
namepass = [“m1”,“m2”]
source = ‘’’
load(“logging.star”, “log”)
state = {}

def add(metric):
log.info("Aggregator Start "+metric.name)
log.info("Check in ADD "+ str(state))
if metric.name == “m1”:
if “m1” in state:
state[“m1”].append(metric)
else:
state[“m1”] =
else:
if “m2” in state:
state[“m2”].append(metric)
else:
state[“m2”] =

def push():
log.info(“PUSH CALLED”)
return None

def reset():
log.info(“RESET”)
state.clear()
‘’’

Output of above snippet

2023-11-24T09:34:00Z I! [aggregators.starlark] PUSH CALLED
2023-11-24T09:34:00Z I! [aggregators.starlark] RESET
2023-11-24T09:34:00Z I! [aggregators.starlark] Aggregator Start m1
2023-11-24T09:34:00Z I! [aggregators.starlark] Check in ADD {}
2023-11-24T09:34:00Z I! [aggregators.starlark] Aggregator Start m1
2023-11-24T09:34:00Z I! [aggregators.starlark] Check in ADD {“m1”: }
2023-11-24T09:34:00Z I! [aggregators.starlark] Aggregator Start m1
2023-11-24T09:34:00Z I! [aggregators.starlark] Check in ADD {“m1”: [Metric(“m1”, tags={“t1”: “true”}, fields={“name”: “apple”, “type”: “fruits”}, time=1700818440000000000)]}
2023-11-24T09:34:00Z I! [aggregators.starlark] Aggregator Start m1
2023-11-24T09:34:00Z I! [aggregators.starlark] Check in ADD {“m1”: [Metric(“m1”, tags={“t1”: “true”}, fields={“type”: “fruits”, “name”: “banana”}, time=1700818440000000000), Metric(“m1”, tags={“t1”: “true”}, fields={“type”: " fruits", “name”: “banana”}, time=1700818440000000000)]}

In 2nd output fruit name “apple” should also be present, but it is getting replaced to only have two entries with banana.

Please give any inputs on this.

@snehal520 what are you trying to achieve?

@jpowers I am trying to store all the values of metric m1 and m2 which I receive within a minute into state.

With the goal of capturing what?

@jpowers In push method, will do some computation on the basis of metrics value and push the result.

This is what I would use, note the use of deepcopy.

[[aggregators.starlark]]
    source = '''
load("logging.star", "log")
state = {"m1": [], "m2": []}

def add(metric):
    if metric.name == "m1":
        state["m1"].append(deepcopy(metric))
    elif metric.name == "m2":
        state["m2"].append(deepcopy(metric))
    else:
      log.info("unknown metric")

    log.info(str(state))


def push():
    return None

def reset():
    state.clear()
    state["m1"] = []
    state["m2"] = []
'''
1 Like