resmoio / kubernetes-event-exporter

Export Kubernetes events to multiple destinations with routing and filtering
Apache License 2.0
753 stars 149 forks source link

Feature request for Loki sink: Use event values as labels #185

Open bbreijer opened 3 months ago

bbreijer commented 3 months ago

currently it is only possible to use event values (like namespace) in the layout of a Loki body, it would be very nice to be able to use the event data (link kind, namespace, etc.) in the streamLabels.

I am not familiar with Go, nor do i know how to test this, therefore i did not make a PR.

But looking at the code: Might this be the addition we are looking for?

In pkg/sinks/loki.go, change:

    eventBody, err := serializeEventWithLayout(l.cfg.Layout, ev)
    if err != nil {
        return err
    }
    timestamp := generateTimestamp()
    a := LokiMsg{
        Streams: []promtailStream{{
            Stream: l.cfg.StreamLabels,
            Values: [][]string{{timestamp, string(eventBody)}},
        }},
    }

into

    eventBody, err := serializeEventWithLayout(l.cfg.Layout, ev)
    if err != nil {
        return err
    }
    streamLabels, err := serializeEventWithLayout(l.cfg.StreamLabels, ev)
    if err != nil {
        return err
    }
    timestamp := generateTimestamp()
    a := LokiMsg{
        Streams: []promtailStream{{
            Stream: string(streamLabels),
            Values: [][]string{{timestamp, string(eventBody)}},
        }},
    }

Could you consider this improvement?

mi-fabien-cayre commented 2 weeks ago

Would be useful for sure

mi-fabien-cayre commented 2 weeks ago

Wrote a PR including some tests (I also need this feature to be added)