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

Need help - Grafana agent flow - Set label based on filepath #13196

Closed
DamienCH opened this issue Jun 11, 2024 · 1 comment
Closed

Need help - Grafana agent flow - Set label based on filepath #13196

DamienCH opened this issue Jun 11, 2024 · 1 comment

Comments

@DamienCH
Copy link

Hello folks,

I need to figure how to dynamically assign labels based on the filepath where logs are collected.
Logs will be collected from a bunch of subfolders under "logspath":
C:\folder01\logspath\customer01\app01\somelog.log
I have to assign a "customer" and a "app" label with the values extracted from the path.

This is done using Grafana Agent Flow (latest version).

I found someone who was able to achieve that, but their config file syntax doesn't look at all like what I see in the documentation.

Your help would be greatly appreciated.

Code block I came up with (but doesn't work at all):

`loki.relabel "fixed_labels" {
forward_to = [loki.process.fields_to_label.receiver]

rule {
    action = "replace"
    target_label = "customer"
    source_labels = ["__path__"]
    regex = "c:\\folder01\\logspath\\(?P<customer>.*)\\.*\\.*"
}

`

  1. Is that even the right process ? Relabel ?
  2. is that the right flow ? (regex runs against the source_labels, selects the named group "customer" and that value "replaces" the target label "customer".) Or do I get all that wrong?
  3. Even the regex is difficult to work with (works fine in regex101, but not in the agent config). Do you see what's wrong with it ?

Any help would be greatly appreciated.

@DamienCH
Copy link
Author

DamienCH commented Jun 12, 2024

Ok this can be dismissed, I found the solution.

This is a sample of the config that worked for me:
_loki.relabel "dynamic_labels" {
forward_to = [loki.relabel.fixed_labels.receiver]

rule {
    action          = "replace"
    source_labels   = ["filename"]
    target_label    = "customer"
    regex           = `c:\\ems\\logspath\\(?P<customer>.*)\\.*\\.*`
    replacement     = "$1"
}
rule {
    action          = "replace"
    source_labels   = ["filename"]
    target_label    = "app"
    regex           = `c:\\ems\\logspath\\.*\\(?P<app>.*)\\.*`
    replacement     = "$1"
}_

Leaving this here, cause I've found I wasn't the only one struggling with that. Hopefully that helps someone.

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

No branches or pull requests

1 participant