Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add loki.enrich component #2882

Open
wants to merge 21 commits into
base: main
Choose a base branch
from
Open

Add loki.enrich component #2882

wants to merge 21 commits into from

Conversation

v-zhuravlev
Copy link
Contributor

@v-zhuravlev v-zhuravlev commented Mar 2, 2025

PR Description

This PR adds a new loki.enrich component that enriches Loki logs with labels from discovery.* components.
The component matches a label from incoming logs against a label from discovered targets, and copies specified labels from the matched target to the log entry.

Should solve #810 .

Features:

  • Copy selected labels from matched targets to logs
  • Support for any discovery component (file, DNS, HTTP, etc.)

Example use cases to ensure best logs-metrics correlation:

  • Enrich Network devices syslog messages with device metadata(as labels) from IP address management (IPAM)/Data center infrastructure management(DCIM) like Netbox (https://github.com/FlxPeters/netbox-plugin-prometheus-sd). It could be same labels as used by prometheus.exporter.snmp or other metric scrapers.
  • Ensure metrics and logs use same labelset (gathered from same discovery.* component) even if metrics are scraped from prometheus endpoints and logs are received via loki.source.api.

Notes to the Reviewer

  • loki.enrich forwards logs unchanged if no match is found
  • loki.enrich should work with any discovery.* component. Best combined with discovery.relabel to rename hidden labels if present.
  • The integration test demonstrates:
    • Sample log pushed via HTTP API and then additional labels added using file-based discovery sample

PR Checklist

  • CHANGELOG.md updated
  • Documentation added
  • Tests updated
  • Config converters updated

Copy link
Contributor

github-actions bot commented Mar 2, 2025

💻 Deploy preview deleted.

@v-zhuravlev v-zhuravlev changed the title Add loki.enricher component (WIP) Add loki.enrich component (WIP) Mar 3, 2025
@v-zhuravlev v-zhuravlev changed the title Add loki.enrich component (WIP) Add loki.enrich component Mar 4, 2025
@v-zhuravlev v-zhuravlev marked this pull request as ready for review March 4, 2025 22:54
@v-zhuravlev v-zhuravlev requested review from clayton-cornell and a team as code owners March 4, 2025 22:54
@v-zhuravlev v-zhuravlev requested a review from wildum March 4, 2025 23:05
@v-zhuravlev v-zhuravlev closed this Mar 5, 2025
@v-zhuravlev v-zhuravlev reopened this Mar 5, 2025
@v-zhuravlev
Copy link
Contributor Author

Similar functionality, but not applicable to syslog as of now: https://grafana.com/docs/alloy/latest/reference/stdlib/array/#arraycombine_maps

@v-zhuravlev v-zhuravlev mentioned this pull request Mar 5, 2025
Copy link
Collaborator

@ptodev ptodev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@v-zhuravlev thank you, this looks interesting! Could you please edit the description that this solves #810?

Btw we also have a similar component for traces - otelcol.processor.discovery.

source_label = "LABEL"

// List of labels to copy from discovered targets to logs
target_labels = ["LABEL", ...]
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can think of a more descriptive name. target_labels could be labels that we got from the discovered targets just for matching but not for addition.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about labels_to_copy ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

edited description.

Copy link
Collaborator

@ptodev ptodev Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like labels_to_copy! It's more descriptive.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i'll change it then

Comment on lines +72 to +73
// Initialize the cache with provided targets
c.refreshCacheFromTargets(args.Targets)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if the discovery component will just return an empty map if it hasn't discovered anything yet? Then loki.enrich would still not work for the first few seconds until the discovery starts working?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, possibly. Is there any way to force discovery before component is initialized?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so.


func init() {
component.Register(component.Registration{
Name: "loki.enrich",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

loki.enrich sounds like a good name. Initially I thought there should be a reference to discovery or targets in the name, but I gave up on that thought because in theory the targets don't have to come from a discovery component.

@v-zhuravlev v-zhuravlev requested a review from ptodev March 12, 2025 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants