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

Does Custom Resource State Metrics support deep nested objects? #2589

Open
pbxqdown opened this issue Jan 8, 2025 · 3 comments
Open

Does Custom Resource State Metrics support deep nested objects? #2589

pbxqdown opened this issue Jan 8, 2025 · 3 comments
Labels
kind/feature Categorizes issue or PR as related to a new feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.

Comments

@pbxqdown
Copy link

pbxqdown commented Jan 8, 2025

We have a k8s object as follows.

status:
    resourceCount:
      tenantA:
        hostGroupA:
          example.com/type1: 2
          example.com/type2: 2
          example.com/type3: 20

We would like to generate a metrics like resourceCount{tenant=tenantA, hostGroup=hostGroupA, resourceType=type2} 2.
Initially I tried to use labelFromKey, but it seems only supports one level of nesting. I have to statically specify path, and in the last level use labelFromKey to generate metrics by resourceType.

metrics:
            - name: resourceCount
              each:
                type: Gauge
                gauge:
                  path: [status, resourceCount, tenantA, hostGroupA]
                  labelFromKey: resourceType

Wondering whether we can use labelFromKey for nested objects.

@pbxqdown pbxqdown added the kind/feature Categorizes issue or PR as related to a new feature. label Jan 8, 2025
@k8s-ci-robot
Copy link
Contributor

This issue is currently awaiting triage.

If kube-state-metrics contributors determine this is a relevant issue, they will accept it by applying the triage/accepted label and provide further guidance.

The triage/accepted label can be added by org members by writing /triage accepted in a comment.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one. label Jan 8, 2025
@ringerc
Copy link

ringerc commented Jan 20, 2025

If I understand correctly, you want the "parent" keys in the CR to be added as labels.

This seems interesting but hard to generalize. How would this work if there are multiple keys with different structures? And when faced with sub-trees containing arrays, how would it know what key to use as the label name and value, and which key to traverse for further values? It seems like you would need something like a JSONPath expression or a series of them, so you could express to k-s-m a generic pattern for traversing your CR to discover unique time-series and values for them.

Otherwise you won't be able to handle something like:

status:
    resourceCount:
      - name: tenantA:
        hosts:
         - hostname: hostGroupA
           entries:
             - type: example.com/type1:
               count: 2
             - type: example.com/type2
               count: 2
             - type: example.com/type3
               count: 20

... which is the other common pattern for expressing these sorts of structures.

@pbxqdown
Copy link
Author

pbxqdown commented Jan 23, 2025

Thanks for response @ringerc. Yes I think we need something like JSONPath to extract arbitrary custom info from k8s object. A kubectl request may look like:

kubectl get <resource> -o=jsonpath='{range .status.resourceCount[*]}{range @[*]}{range @}["{.}", "{@}"]{"\n"}{end}{end}{end}'

If there is a general solution for this, kube-state-metrics will be much more powerful. But it is understandable this might be challenging.
We end up with defining prometheus metrics directly inside source code. It feels easier than configuring kube-state-metrics to expose highly customized objects.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/feature Categorizes issue or PR as related to a new feature. needs-triage Indicates an issue or PR lacks a `triage/foo` label and requires one.
Projects
None yet
Development

No branches or pull requests

3 participants