I don’t think this is explained in the docs (and got ChatGPT completely confused), but according to the source code I can’t currently use the output from one aggregator in another. I am wondering if there’s plan to allow this?
My use case is integrating continuous voltage and current readings into energy. I want want to merge the two metrics to avoid a join operation later but I also need the basicstats plugin for down sampling.
Under the current implementation do you think it will be more efficient to use Starlark for this or loop the output back into telegraf?
@Bill_Chen Welcome to the InfluxData community!
You’re right, Telegraf doesn’t currently support chaining aggregators, so one aggregator’s output can’t feed into another. Each works independently within its own period.
If you need to perform calculations like energy = voltage × current before aggregation, the Starlark processor is a great fit. It was actually added for use cases just like this in IoT.
Here’s an example setup:
[[processors.starlark]]
namepass = ["your_measurement_name"]
source = '''
def apply(metric):
if "voltage" in metric.fields and "current" in metric.fields:
metric.fields["energy"] = metric.fields["voltage"] * metric.fields["current"]
return metric
'''
[[aggregators.basicstats]]
namepass = ["your_measurement_name"]
period = "60s"
drop_original = true
stats = ["mean", "min", "max"]
This calculates energy per data point and then aggregates it over time. Let me know if you need help adapting it to your use case!
Thank you for responding.
Sorry for not making it clear, but my current and voltage are separate metrics from the source (sampled independently), so as I understand it, the [[processors.starlark]] step you suggested won’t work.
I have looked into writing a Starlark Aggregator that does both merge and stats, but it feels like duplicating a lot of functionalities and I don’t like the poor efficiency of an interpreted language and it’s unclear to me how I would debug it.
That is why I am wondering if anyone tried to pipe the output from Telegraf back into itself (e.g. using an InfluxDB output plugin connected to an InfluxDB input plugin) and how efficient is this approach.
Thanks for the clarification, that makes sense now.
You’re right: since voltage and current are sampled independently, [[processors.starlark]] can’t help unless the metrics are merged first. Looping Telegraf output back via InfluxDB is a valid workaround, and others have used this pattern.
It does work, but expect added complexity, latency, and I/O overhead. You’ll need two Telegraf instances, one to merge and output, another to read back, calculate (via Starlark), and forward. With a local InfluxDB and short retention, performance can be decent, especially if batching is tuned.
Still, if you’re comfortable with some scripting, a Starlark aggregator might be leaner in the long term, despite the trade-offs, as it avoids extra I/O and simplifies the pipeline.
Happy to share config examples or a rough Starlark draft if helpful.