合并 promtail 配置中两个标签的值

发布于 2025-01-14 22:49:28 字数 683 浏览 4 评论 0原文

如何在 promtail 配置中添加多个标签的值并将它们分配给另一个标签?

scrape_configs:
  - job_name: journal
    journal:
      max_age: 12h
    relabel_configs:
    - source_labels: ['__journal__machine_id']
      target_label: 'HostId'
    - source_labels: ['__journal__hostname']
      target_label: 'HostName'
    - source_labels: ['__journal_syslog_identifier']
      target_label: 'ApplicationName'
    pipeline_stages:
    - match:
        selector: '{ApplicationName="test-app"}'
        stages:
        - static_labels:
            OriginId: //here I want to asign HostId+HostName+ApplicationName

最后,我预计标签 OriginId 的值为 HostId+HostName+ApplicationName

How to add the values of multiple labels and assign them to another label in promtail config?

scrape_configs:
  - job_name: journal
    journal:
      max_age: 12h
    relabel_configs:
    - source_labels: ['__journal__machine_id']
      target_label: 'HostId'
    - source_labels: ['__journal__hostname']
      target_label: 'HostName'
    - source_labels: ['__journal_syslog_identifier']
      target_label: 'ApplicationName'
    pipeline_stages:
    - match:
        selector: '{ApplicationName="test-app"}'
        stages:
        - static_labels:
            OriginId: //here I want to asign HostId+HostName+ApplicationName

In the end, I expect the value of label OriginId would be HostId+HostName+ApplicationName

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨落□心尘 2025-01-21 22:49:28

static_labels 只允许向标签集中添加静态标签,即不能使用其他标签的值。由于您已经有了 relabel_configs 部分,也许您可​​以直接从重新标记步骤生成 OriginId ?类似于:

- source_labels: ['__journal__machine_id', '__journal__hostname', '__journal_syslog_identifier']
  separator: '_'
  target_label: 'OriginId'

在这种情况下,如果输入标签集如下所示:

__journal__machine_id: "machine-id-1"
__journal__hostname: "host1"
__journal_syslog_identifier: "abcde-123"

OriginId 最终的值为:machine-id-1_host1_abcde-123。默认分隔符(如果配置中未指定分隔符,则为;)。

static_labels only allows adding a static label to the label set, i.e you cannot use the value of other labels. Since you already have a relabel_configs section maybe you can generate the OriginId directly from the relabeling step? Something like:

- source_labels: ['__journal__machine_id', '__journal__hostname', '__journal_syslog_identifier']
  separator: '_'
  target_label: 'OriginId'

In this case if the input label set looks like:

__journal__machine_id: "machine-id-1"
__journal__hostname: "host1"
__journal_syslog_identifier: "abcde-123"

OriginId would end up with the value: machine-id-1_host1_abcde-123. The default separator (if none is specified in the configuration is ;).

揪着可爱 2025-01-21 22:49:28

您可以在 relabel_config 中使用带有 separatorreplace 操作。

这是一个例子:

...
- action: replace
  separator: "+"
  source_labels:
    - source_labels: 
      - __journal__machine_id
      - __journal__hostname
      - __journal_syslog_identifier
      target_label: 'OriginId'
...

我认为这应该适合你。

You can use the replace action with a separator in a relabel_config.

Here's an example:

...
- action: replace
  separator: "+"
  source_labels:
    - source_labels: 
      - __journal__machine_id
      - __journal__hostname
      - __journal_syslog_identifier
      target_label: 'OriginId'
...

I think that should work for you.

你的笑 2025-01-21 22:49:28

这可以通过模板阶段来实现。

下面的示例从 filename 映射 teamspace 以创建一个名为 namespace 的新标签。

scrape_configs:
- job_name: logs
  static_configs:
  - targets:
      - localhost
    labels:
      hostname: ${HOSTNAME}
      job: logs
      __path__: /logs/**/**/*.{log,txt,out}
  pipeline_stages:
    - match:
        selector: '{job="logs"}'
        stages:
        - regex:
            source: filename
            expression: "/logs/(?P<team>.+)/(?P<space>.+)/(?P<pod>.+)/.+"
        - template:
            source: namespace
            template: '{{ .team }}-{{ .space }}'
        - labels:
            team:
            namespace:
            pod:

This can be achieved via template stage.

Below example maps team and space from filename to create a new label called namespace.

scrape_configs:
- job_name: logs
  static_configs:
  - targets:
      - localhost
    labels:
      hostname: ${HOSTNAME}
      job: logs
      __path__: /logs/**/**/*.{log,txt,out}
  pipeline_stages:
    - match:
        selector: '{job="logs"}'
        stages:
        - regex:
            source: filename
            expression: "/logs/(?P<team>.+)/(?P<space>.+)/(?P<pod>.+)/.+"
        - template:
            source: namespace
            template: '{{ .team }}-{{ .space }}'
        - labels:
            team:
            namespace:
            pod:
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文