From 885bf273a929e1f92f242f75dc27749b193e3a90 Mon Sep 17 00:00:00 2001 From: Helen Weller <38860767+helenosheaa@users.noreply.github.com> Date: Wed, 31 Mar 2021 15:08:34 -0400 Subject: [PATCH] Starlark script for renaming prometheus remote write metrics (#9074) --- plugins/parsers/prometheusremotewrite/README.md | 10 ++++++---- plugins/processors/starlark/README.md | 1 + .../testdata/rename_prometheus_remote_write.star | 16 ++++++++++++++++ 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 plugins/processors/starlark/testdata/rename_prometheus_remote_write.star diff --git a/plugins/parsers/prometheusremotewrite/README.md b/plugins/parsers/prometheusremotewrite/README.md index 1bad5bd6004ea..b523174e9184a 100644 --- a/plugins/parsers/prometheusremotewrite/README.md +++ b/plugins/parsers/prometheusremotewrite/README.md @@ -16,9 +16,7 @@ Converts prometheus remote write samples directly into Telegraf metrics. It can data_format = "prometheusremotewrite" ``` -### Example - -**Example Input** +### Example Input ``` prompb.WriteRequest{ Timeseries: []*prompb.TimeSeries{ @@ -38,7 +36,11 @@ prompb.WriteRequest{ ``` -**Example Output** +### Example Output ``` prometheus_remote_write,instance=localhost:9090,job=prometheus,quantile=0.99 go_gc_duration_seconds=4.63 1614889298859000000 ``` + +**For alignment with the [InfluxDB v1.x Prometheus Remote Write Spec](https://docs.influxdata.com/influxdb/v1.8/supported_protocols/prometheus/#how-prometheus-metrics-are-parsed-in-influxdb)** + +- Use the [Starlark processor rename prometheus remote write script](https://github.com/influxdata/telegraf/blob/master/plugins/processors/starlark/testdata/rename_prometheus_remote_write.star) to rename the measurement name to the fieldname and rename the fieldname to value. \ No newline at end of file diff --git a/plugins/processors/starlark/README.md b/plugins/processors/starlark/README.md index 62b5b85e766d4..e30ea506c13f7 100644 --- a/plugins/processors/starlark/README.md +++ b/plugins/processors/starlark/README.md @@ -237,6 +237,7 @@ def apply(metric): - [multiple metrics from json array](/plugins/processors/starlark/testdata/multiple_metrics_with_json.star) - Builds a new metric from each element of a json array then returns all the created metrics. - [custom error](/plugins/processors/starlark/testdata/fail.star) - Return a custom error with [fail](https://docs.bazel.build/versions/master/skylark/lib/globals.html#fail). - [compare with previous metric](/plugins/processors/starlark/testdata/compare_metrics.star) - Compare the current metric with the previous one using the shared state. +- [rename prometheus remote write](/plugins/processors/starlark/testdata/rename_prometheus_remote_write.star) - Rename prometheus remote write measurement name with fieldname and rename fieldname to value. [All examples](/plugins/processors/starlark/testdata) are in the testdata folder. diff --git a/plugins/processors/starlark/testdata/rename_prometheus_remote_write.star b/plugins/processors/starlark/testdata/rename_prometheus_remote_write.star new file mode 100644 index 0000000000000..cee49196c48ff --- /dev/null +++ b/plugins/processors/starlark/testdata/rename_prometheus_remote_write.star @@ -0,0 +1,16 @@ +# Specifically for prometheus remote write - renames the measurement name to the fieldname. Renames the fieldname to value. +# Assumes there is only one field as is the case for prometheus remote write. +# +# Example Input: +# prometheus_remote_write,instance=localhost:9090,job=prometheus,quantile=0.99 go_gc_duration_seconds=4.63 1614889298859000000 +# +# Example Output: +# go_gc_duration_seconds,instance=localhost:9090,job=prometheus,quantile=0.99 value=4.63 1614889299000000000 + +def apply(metric): + if metric.name == "prometheus_remote_write": + for k, v in metric.fields.items(): + metric.name = k + metric.fields["value"] = v + metric.fields.pop(k) + return metric \ No newline at end of file