【问题标题】:Vector dev how to use hypenated key names?Vector dev如何使用hypenated键名?
【发布时间】:2022-11-01 20:16:05
【问题描述】:

有时,在处理向量中的日志事件时,日志源可能有连字符作为键名 - 例如 json 结构化日志。假设它来自第三方并且无法更改它们,我们如何处理这些密钥?

示例日志消息(为演示而设计)是:

{
  "labels":{"no_hypens":"normal field","this-has-hypens":"this is a test"},
  "message":"a message",
  "timestamp":"2022-11-01T12:03:00.941866394Z"
}

注意那里的labels.this-has-hyphens 字段。

【问题讨论】:

    标签: vector-vrl


    【解决方案1】:

    我设法整理了一个测试用例,并找出了语法,既可以提供这样的测试数据,也可以提取 VRL 中的数据:

    测试用例

    ---
    tests:
      - name: hypens
        inputs:
          - insert_at: hypens
            type: log
            log_fields:
              labels."this-has-hypens": "this is a test"
              labels.no_hypens: "normal field"
        outputs:
          - extract_from: hypens
            conditions:
              - type: vrl
                source: |
                  assert_eq!("normal field", .no_hypens)
                  assert_eq!("this is a test", .output_without_hypens)
    

    这将插入并检查输入数据中的两个字段。请注意,需要引用带连字符的关键段。

    接下来是变换中的 VRL:

    ---
    transforms:
      hypens:
        type: remap
        inputs:
          - route
        source: |
          log(., "error")
    
          . = {
            "no_hypens": .labels.no_hypens,
            "output_without_hypens": .labels."this-has-hypens",
          }
    

    日志消息在那里,因为在调试它时,我必须弄清楚测试连字符字段甚至没有到达转换,直到我在它周围加上引号。 然后字段引用本身也需要有引号,在点之后(不是方括号)。

    这将通过测试,并输出正确的数据。

    【讨论】:

      猜你喜欢
      • 2021-03-04
      • 2013-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      • 1970-01-01
      • 2019-07-11
      • 1970-01-01
      相关资源
      最近更新 更多