Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For ScrapeType is 'object' extract Labels not from Child object but from Parent #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

SelAnt
Copy link

@SelAnt SelAnt commented Nov 18, 2021

Proposal - when ScrapeType is 'object' extract Labels not from Child object but from Parent as for ScrapeType is 'value'

… labels for values are extracted)

Signed-off-by: gadyag <Gleb.Gadyatskiy@pointclickcare.com>
@rustycl0ck
Copy link
Member

rustycl0ck commented Jan 18, 2022

This use case is valid. A large example of a json object, where this is required, is mentioned in #85 as well as in #132.

TL;DR -

An example json:

{
  "name": "global-counter",
  "category": "living-things",
  "beings":
  [
    {
      "kind": "animals",
      "specifics":
      [
        {
          "noun": "lion",
          "population": 123
        },
        {
          "noun": "deer",
          "population": 456
        }
      ]
    },
    {
      "kind": "fish",
      "specifics":
      [
        {
          "noun": "shark",
          "population": 789
        },
        {
          "noun": "tuna",
          "population": 890
        }
      ]
    }
  ]
}

Right now, for the population metrics, it is possible to only attach the noun label, but not possible to attach any labels from the parent objects (like kind or category).

Currently, this PR only partially allows to attach the labels from the parents. For example, it would be possible to attach the category label, but not the kind or the noun label anyhow still.
If you can make changes to address both the use cases somehow, I'd be happy to review this PR.

@iceman91176
Copy link

iceman91176 commented Nov 23, 2022

The general problem ist that the jsonpath implementation does not allow to access parent nodes (https://kubernetes.io/docs/reference/kubectl/jsonpath/) This seems to be the case with all jsonpath implementations i've been looking at.

IMHO i think that this problem can't be solved easily, BUT....

A possible solution would be transforming the JSON before extracting labels and values. This approch has been described in #132 and implemented in https://github.com/fredr/data-exporter. It would be very powerful to add a feature like transformation-steps to json_exporter.

To keep it simple start with JQ for transformations, and add more transformation-types over time

Based on your data above it would look like this

modules:
  default:
    metrics:
    - name: example_value
      type: object
      transformations:
      - type: jq
        query:  |-
           [.category as $category | .beings[] | (.kind as $kind | .specifics[] | {category: $category,kind:$kind,noun: .noun, val: .population} )]
      help: Example of sub-level value scrapes from a json
      path: '{[*]}'
      labels:
        category: '{.category}'
        kind: '{.kind}'
        noun: '{.noun}'
      values:
        count: '{.val}'

The jq-query turns your example-data into

[
  {
    "category": "living-things",
    "kind": "animals",
    "noun": "lion",
    "val": 123
  },
  {
    "category": "living-things",
    "kind": "animals",
    "noun": "deer",
    "val": 456
  },
  {
    "category": "living-things",
    "kind": "fish",
    "noun": "shark",
    "val": 789
  },
  {
    "category": "living-things",
    "kind": "fish",
    "noun": "tuna",
    "val": 890
  }
]

And the scrape returns

# HELP example_value_count Example of sub-level value scrapes from a json
# TYPE example_value_count untyped
example_value_count{category="living-things",kind="animals",noun="deer"} 456
example_value_count{category="living-things",kind="animals",noun="lion"} 123
example_value_count{category="living-things",kind="fish",noun="shark"} 789
example_value_count{category="living-things",kind="fish",noun="tuna"} 890

Unfortunately i don't know anything about golang, so this would either take ages and/or or be a complete failure if i tried to implement it. I think the transformatiosn should be added before here (

jsonMetricCollector.Data = data
)

@rustycl0ck - what do you think ? Maybe @SelAnt is willing to add this as described ?

@corpocott
Copy link

any movement on this? currently running into a problem this would solve.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants