菜单
文档breadcrumb arrow Grafana 文档breadcrumb arrow 管理breadcrumb arrow 关联breadcrumb arrow 在关联中使用变量和转换
Enterprise 开源

在关联中使用变量和转换

开始之前

本示例演示了如何在测试数据源中创建链接,但相同的原理适用于任何数据源。

该示例模拟了具有两个数据源的场景:

  • 包含格式为:“2020-01-01 10:00 level=error message=error service=app1.loginService”的日志行,存储在一个名为“msg”的字段中
  • 包含在日志行的服务名称中的应用的指标(例如 app1)

以下说明展示了如何使用 provisioning 和 regex 转换来设置一个链接,该链接可以为包含在每个日志行中的主机运行指标查询。此外,还设置了一个包含服务全名查询的链接,以演示 logfmt 转换。

在 provisioning 中使用变量和转换

  1. 将以下 provisioning 配置添加到您的 Grafana 中:

    yaml
    datasources:
      - name: Target
        uid: test-target
        type: testdata
    
      - name: Source
        uid: test-source
        type: testdata
      - name: Source
        uid: test-source
        type: testdata
        correlations:
          - targetUID: test-target
            label: App metrics
            description: Application HTTP request metrics
            config:
              type: query
              target:
                scenario_id: random_walk
                alias: $${application}
              field: msg
              transformations:
                - type: regex
                  field: msg
                  expression: service=(\w+)\.\w+
                  mapValue: application
          - targetUID: test-target
            label: Service metrics
            description: Service metrics
            config:
              type: query
              target:
                scenario_id: random_walk
                alias: $${service}
              field: msg
              transformations:
                - type: logfmt
                  field: msg

    创建了两个数据源:Source(模拟日志数据源)和 Target(模拟指标数据源)

    • 创建了一个名为“App metrics”的关联,其目标数据源是 Target 数据源的 UID。
      • 标签和描述以文本形式提供
      • 每个关联都包含以下配置:
        • 必需的关联类型 (query)
        • 与测试数据源模型匹配的目标查询
      • “App metrics”关联包含以下配置:
        • 别名设置为 ${application} 变量(请注意,在 provisioning 文件中,$ 用于访问环境变量,因此需要进行转义)。
        • 创建正则表达式转换以从“msg”字段中提取值
          • 正则表达式转换用于从日志行中存储的服务全名中捕获应用程序名称。
          • 转换的输出映射到一个名为“application”的变量。
      • “Service metrics”关联以类似方式创建,但使用 logfmt 转换来分解日志消息并访问服务的全名(例如,“app1.loginService”)。
        • 例如,当输入日志行“2020-01-01 10:00 level=error message=error service=app1.loginService”时,转换将生成新变量:level、message 和 service。
        • “service”变量用作目标查询中的别名。
  2. 导航到 Explore 并打开“Source”数据源。

  3. 选择“Raw Frames”场景并提供以下数据帧以模拟返回日志行:

    json
    [
      {
        "meta": {
          "preferredVisualisationType": "logs"
        },
        "fields": [
          {
            "name": "time",
            "values": [1, 2]
          },
          {
            "name": "msg",
            "values": [
              "level=error msg=error service=app1.loginService",
              "level=debug msg=info service=app2.userProfileService"
            ]
          }
        ]
      }
    ]
  4. 运行查询,然后点击日志行打开日志详情。

    Correlation links in Logs panel
    Logs 面板中的关联链接

    “App metrics”和“Service metrics”链接显示在从日志行中通过转换提取的变量旁边

  5. 点击“App metrics”链接。

  6. 将打开一个分割视图并运行目标查询。

  7. 注意,日志行中的应用程序名称是如何作为别名属性填充到目标查询中的。

    Interpolated target query
    插值目标查询

    这允许您根据源结果运行特定的查询

    Interpolated target query results
    插值目标查询结果
  8. 返回源查询并将 raw frames 的首选可视化类型更改为“table”,以查看链接如何在表格可视化中显示。

    json
    [
      {
        "meta": {
          "preferredVisualisationType": "table"
        },
        "fields": [...]
      }
    ]
  9. 运行查询并注意链接是如何在表格单元格中创建的

    Correlations links in table
    表格中的关联链接