I am collecting metrics using telegraf and trying to send that metrics in json format to an API using outputs.http.I have a tag key called resource_uri whose value is subscriptions/value i.e resource_uri=subscriptions/value .I wanted to add a forward_slash in the starting of this value i.e /subscriptions/value so I have used processor called starlark and added some python code in it to achieve this.
[[processors.starlark]]
source = '''
def apply(metric):
forward_slash = "/"
uri = metric.tags.get("resource_uri")
if uri != None:
metric.tags["resource_uri"] = forward_slash+uri
return metric
'''
When I am running the plugin file in test mode using telegraf -config promitor.conf --test I am getting the desired output:-
resource_uri=/subscriptions/value
But when I restart the telegraf service using systemctl restart telegraf.service and check the logs of my API which is collecting this metrics,in that logs I am receiving the data in json format and I am getting the resource_uri key as {resource_uri’: '///subscriptions/value}
Can you please tell me how can we fix this issue ? How can I remove the 2 extra forward slashes
It will really help me
Please share you full config. You may be running a processor multiple times because you are running an aggregator as well. If this is the case, you could update your script to also check if there is already a leading slash, which would probably be a good idea as well.
If I have a simple example I only see one leading slash:
[agent]
debug = true
[[outputs.file]]
[[inputs.exec]]
command = "echo metric,resource_uri=fake/uri value=42"
data_format = "influx"
[[inputs.exec]]
command = "echo metric,resource_uri=/full/uri value=42"
data_format = "influx"
[[processors.starlark]]
source = '''
def apply(metric):
forward_slash = "/"
uri = metric.tags.get("resource_uri")
if uri != None and not uri.startswith("/"):
metric.tags["resource_uri"] = "/"+uri
return metric
'''
❯ ./telegraf --config config.toml --once
2023-05-25T17:39:42Z I! Loading config: config.toml
2023-05-25T17:39:42Z I! Starting Telegraf 1.27.0-bbe30f76
2023-05-25T17:39:42Z I! Available plugins: 235 inputs, 9 aggregators, 28 processors, 23 parsers, 58 outputs, 4 secret-stores
2023-05-25T17:39:42Z I! Loaded inputs: exec (2x)
2023-05-25T17:39:42Z I! Loaded aggregators:
2023-05-25T17:39:42Z I! Loaded processors: starlark
2023-05-25T17:39:42Z I! Loaded secretstores:
2023-05-25T17:39:42Z I! Loaded outputs: file
2023-05-25T17:39:42Z I! Tags enabled: host=ryzen
2023-05-25T17:39:42Z D! [agent] Initializing plugins
2023-05-25T17:39:42Z D! [agent] Connecting outputs
2023-05-25T17:39:42Z D! [agent] Attempting connection to [outputs.file]
2023-05-25T17:39:42Z D! [agent] Successfully connected to outputs.file
2023-05-25T17:39:42Z D! [agent] Starting service inputs
2023-05-25T17:39:42Z D! [agent] Stopping service inputs
2023-05-25T17:39:42Z D! [agent] Input channel closed
2023-05-25T17:39:42Z D! [agent] Processor channel closed
2023-05-25T17:39:42Z I! [agent] Hang on, flushing any cached metrics before shutdown
metric,host=ryzen,resource_uri=/full/uri value=42 1685036382000000000
metric,host=ryzen,resource_uri=/fake/uri value=42 1685036382000000000
2023-05-25T17:39:42Z D! [outputs.file] Wrote batch of 2 metrics in 16.11µs
2023-05-25T17:39:42Z D! [outputs.file] Buffer fullness: 0 / 10000 metrics
2023-05-25T17:39:42Z I! [agent] Stopping running outputs
2023-05-25T17:39:42Z D! [agent] Stopped Successfully