'In Telegraf how to include only specific values of a tag

I am using a prometheus plugin in Telegraf to get the data from promitor and push it to InfluxDB. However, as per my requirement there is one tag named as "resource_name" and it contains multiple values let's say ["A", "B", "C", "D", "E", "F", "G", "H"]. Out of these values I only want ["A", "B", "C", "D", "E"] and these only values should be inserted into InfluxDB.

To achieve my requirement I am using below plugin and using tagpass to allow only specific values.

[[inputs.prometheus]]
  metric_version = 2
  name_suffix = "_promitor_abcd"
  urls = ["http://IP:Port/metrics"]
  tagexclude = [ "host", "url" ]
  [inputs.prometheus.tagpass]
    resource_name = [ "A", "B", "C", "D", "E" ]

After using this when I run this configuration file I am still able to see all the values in InfluxDB under "resource_name" tag or column and not the values which I specified above in my configuration file.

Can anybody help me to understand what went wrong here and how to push only specific values in influxDB?



Solution 1:[1]

The tagpass parameter is one of the available metric filters in telegraf. tagpass is specific to the tag key itself. Metrics that contain keys specified by tagpass are emitted. So if you have a metric as follows:

metric,color=red,height=3 value=2
metric,color=red,width=4 value=2

With the above, a tagpass=color would emit both metrics, while a tagpass=height would only emit the first.

In your case, you are wanting to filter based on the value of a specific tag. In this case, I would use the starlark processor to create a custom function that checks the value of the tag for the values you are after.

expected = ["A", "B", "C", "D", "E"]

def apply(metric):
    if metric.tags["resource_name"] in expected:
        return metric

I am doing the above from memory so I apologize in advance if it is not quite correct.

Thanks!

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 powersj