grafana / alloy

OpenTelemetry Collector distribution with programmable pipelines
https://grafana.com/oss/alloy
Apache License 2.0
1.32k stars 181 forks source link

loki.process option to flatten structured_metadata for otel attributes #1173

Open nferch opened 3 months ago

nferch commented 3 months ago

Request

in loki.process, structured_metadata should optionally flatten a nested value, using the keys of the nested value as metadata keys.

For example:

An inbound message structured like this:

{
  "body": "log message",
  "attributes": {
      "foo": "bar",
      "abc": "xyz"
  }
}

Using configuration like this:

loki.process "logs_service" {
        stage.json {
                expressions = {"body" = "body", "attributes" = "attributes"}
        }

        stage.structured_metadata {
                values = {
                        attributes = "attributes",
                }
                flatten = true
        }

        stage.output {
                source = "body"
        }

Should produce output like this:

{ "line": "log message", "fields": { "foo": "bar", "abc": "xyz"}}

Alternatively, if there was a different value to set the loki.format hint to that would use the body as the log payload and set other specified keys as metadata, that would work as well.

Use case

I am using the otelcol.receiver to forward logs to Loki using otelcol.exporter.loki. I am using the loki.resource.labels hint to set certain resource attributes as Loki labels. I would like to set resource and log attributes as Loki log metadata.

Out of the box, with the default json setting of json the otelcol.exporter.loki will pass the Otel Log as something like the following, which is not really well suited for my purposes:

{
  "body": "log body",
  "attributes: {
        "logline_attribute": "a"
  },
   "resources": {
         "resource_attribute_a": "val"
  }
}

I can set the otelcol.exporter.loki hint to raw, which will send the body as the log payload, which is closer to what I want, but I lose the resource and log attributes in the process.

What is the closest to idea is the following:

loki.process "logs_service" {
        stage.json {
                expressions = {"body" = "body", "attributes" = "attributes", "resources" = "resources", "instrumentation_scope" = "i
nstrumentation_scope"}
        }

        stage.structured_metadata {
                values = {
                        attributes = "attributes",
                        resources  = "resources",
                }
        }

        stage.output {
                source = "body"
        }

This sets the log and resource attributes as values to metadata values, which is better than nothing, but I'm not aware of a way in LogQL to parse metadata fields as JSON.

github-actions[bot] commented 2 months ago

This issue has not had any activity in the past 30 days, so the needs-attention label has been added to it. If the opened issue is a bug, check to see if a newer release fixed your issue. If it is no longer relevant, please feel free to close this issue. The needs-attention label signals to maintainers that something has fallen through the cracks. No action is needed by you; your issue will be kept open and you do not have to respond to this comment. The label will be removed the next time this job runs if there is new activity. Thank you for your contributions!