【问题标题】:problem when parsing JSON with Go JSONPath使用 Go JSONPath 解析 JSON 时的问题
【发布时间】:2021-09-18 15:15:34
【问题描述】:

我使用代码将 json 数据从 Yarn REST API 转换为 Prometheus 数据类型: https://github.com/prometheus-community/json_exporter。 但是,它打印错误:

level=error ts=2021-07-08T06:31:03.712Z caller=collector.go:83 msg="无法提取指标值" path={.capacity} err="未找到容量"指标="Desc{fqName: "queues_capacity", help: "队列信息", constLabels: {}, variableLabels: [type]}"

我想知道是我的 YAML 文件配置有问题(例如嵌套 json 方面)还是只是代码的原因。

我的 yaml 配置是:

metrics:
- name: queues
  path: "{ .scheduler.schedulerInfo.queues.queue }"
  help: information on queues
  type: object
  labels:
       type: '{.type}'
  values:
       capacity: '{.capacity}'

而部分json文件是:

{
"scheduler": {
"schedulerInfo": {
"type": "capacityScheduler",
"capacity": 100,
"usedCapacity": 1.0526316,
"maxCapacity": 100,
"queueName": "root",
"queues": {
"queue": [
{
"type": "capacitySchedulerLeafQueueInfo",
"capacity": 10,
"usedCapacity": 10.526316,
"maxCapacity": 100,

【问题讨论】:

  • 会不会是响应json中的field key容量不一致,有些情况下json对象中根本没有返回?
  • 我查了一下,队列对象一共有五个,每个都有容量字段

标签: json go jsonpath


【解决方案1】:

使用 [*] 首先获取所有对象,所以应该是:

  path: "{ .scheduler.schedulerInfo.queues.queue[*] }"

据此:https://kubernetes.io/docs/reference/kubectl/jsonpath/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-01-27
    • 2022-01-03
    • 2017-02-05
    • 1970-01-01
    • 2015-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多