From f147a460cacbf7241899af04e9b161358deadb04 Mon Sep 17 00:00:00 2001 From: daidokoro Date: Sun, 24 Sep 2023 17:33:56 +0200 Subject: [PATCH] reamme --- processor/starlarktransform/README.md | 40 +++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 6 deletions(-) diff --git a/processor/starlarktransform/README.md b/processor/starlarktransform/README.md index 442bfeed1b25..751f3013b8e3 100644 --- a/processor/starlarktransform/README.md +++ b/processor/starlarktransform/README.md @@ -26,16 +26,21 @@ To configure starlarktransform, you add your code using the `code` option in the processors: starlarktransform: code: | - def transform(event): + def transform(event): event = json.decode(event) - - - return event + return event ``` You must define a function called `transform` that accepts a single argument, `event`. This function is called by the processor and is passed the telemetry event. The function **must return** the modified, json decoded event. +## Why? + +While there are a number of transform processors, most notably, the main OTTL [transform processor](https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/transformprocessor), this processor aims to grant users more flexibility by allowing them to manipulate telemetry data using a familiar syntax. + +Python is a popular, well known language, even among non-developers. By allowing Starlark code to be used as an option to transform telemetry data, users can leverage their existing knowledge of Python. + + ## How it works The processor uses the [Starlark-Go](https://github.com/google/starlark-go) interpreter, this allows you to run this processor without having to install a Starlark language interpreter on the host. @@ -382,7 +387,7 @@ processors: starlarktransform/metrics: code: | def transform(event): - print("received event", event) + print("received event", event) event = json.decode(event) for md in event['resourceMetrics']: # if resources are empty @@ -463,4 +468,27 @@ service: ### Warnings -The starlarktransform processor allows you to modify all aspects of your telemetry data. This can result in invalid or bad data being propogated if you are not careful. It is your responsibility to inspect the data and ensure it is valid. \ No newline at end of file +The starlarktransform processor allows you to modify all aspects of your telemetry data. This can result in invalid or bad data being propogated if you are not careful. It is your responsibility to inspect the data and ensure it is valid. + + +```yaml + +```yaml +processors: + starlarktransform: + code: | + def transform(event): + e = json.decode(event) + for r in e["resourceLogs"]: + for sl in r["scopeLogs"]: + for lr in sl["logRecords"]: + if lr["body"]["stringValue"] == "operationA": + lr["attributes"].append({ + "key": "test", + "value": {"stringValue": "pass"} + }) + return e + +``` + +``` \ No newline at end of file