ccfos / nightingale

An all-in-one observability solution which aims to combine the advantages of Prometheus and Grafana. It manages alert rules and visualizes metrics, logs, traces in a beautiful web UI.
https://flashcat.cloud/docs/
Apache License 2.0
9.42k stars 1.38k forks source link

Optimize/filter labels #2004

Closed Yziyan closed 1 month ago

Yziyan commented 2 months ago

What type of PR is this?

What this PR does / why we need it: This PR implements conditional relabeling and various relabeling actions as specified in the relabeling rules documentation. It ensures that metrics are processed according to specific conditions and transformations, improving the flexibility and functionality of our relabeling process.

Which issue(s) this PR fixes: Fixes #

Special notes for your reviewer: This PR includes the following changes:

  1. Conditional Relabeling: Added support for conditional relabeling using the if field in the configuration. This allows relabeling actions to be applied only when specific conditions are met.
  2. Relabeling Actions: Implemented multiple relabeling actions, including:
    • Adding new labels
    • Updating existing labels
    • Rewriting existing labels
    • Updating metric names
    • Removing unneeded labels
    • Removing specific label values
    • Removing unneeded metrics
    • Dropping metrics on certain conditions
    • Modifying label names
    • Constructing labels from multiple existing labels
    • Chaining relabeling rules

Example Usage:

  1. Adding new label:

    {
     "action": "replace",
     "target_label": "foo",
     "replacement": "bar"
    }

    Transforms:

    {
     "original_labels": [{"name": "job", "value": "aa"}],
     "relabelled_labels": [{"name": "job", "value": "aa"}, {"name": "foo", "value": "bar"}]
    }
  2. Updating existing label:

    {
     "action": "replace",
     "target_label": "foo",
     "replacement": "bar"
    }

    Transforms:

    {
     "original_labels": [{"name": "foo", "value": "aaaa"}],
     "relabelled_labels": [{"name": "foo", "value": "bar"}]
    }
  3. Conditional relabeling:

    {
     "action": "replace",
     "if": "label=\"x|y\"",
     "target_label": "foo",
     "replacement": "bar"
    }

    Transforms:

    {
     "original_labels": [{"name": "label", "value": "x"}],
     "relabelled_labels": [{"name": "label", "value": "x"}, {"name": "foo", "value": "bar"}]
    }

    Please review the changes and provide feedback. Detailed test cases have been included to ensure all relabeling actions work as expected.