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

✨ Experimental practice to scaffold dashboard manifest for custom metrics #2858

Conversation

Kavinjsir
Copy link
Contributor

@Kavinjsir Kavinjsir commented Aug 4, 2022

(This PR is experimental and welcome discussion)

Description

This is the experimental practice to see how possible if we can bring dashboards for custom metrics through the Grafana Plugin.

The user can add the desired metrics in grafana/custom-metrics/config.yaml:

---
customMetrics: []
  # - metric: Raw custom metric (required)
  #   type:   Metric type: counter/gauge/histogram (required)
  #   expr:   Prom_ql for the metric (optional)

(If you execute the plugin for the first time(either by init or edit), the above config template will be automatically generated.)

The plugin triggered by edit subcommand will read the config.yaml and generate grafana/custom-metrics/custom-metrics-dashboard.json, which can be directly import in the Grafana Web UI.

Story 1

Users get the config entry when initialize the project or introduce the grafana-plugin for the first time:

# Initialize a project with the plugin enabled
kubebuilder init --plugins=grafana.kubebuilder.io/v1-alpha

# Introduce the Grafana Plugin to an existing project
kubebuilder edit --plugins=grafana.kubebuilder.io/v1-alpha

A nested custom-metrics/config.yaml will be generated under grafana/.

Story 2

Users configure grafana/custom-metrics/config.yaml and run edit subcommand to get Grafana manifest:

kubebuilder edit --plugins=grafana.kubebuilder.io/v1-alpha

The grafana/custom-metrics/config.yaml will be validated and then the manifest will be generated: grafana/custom-metrics/custom-metrics-dashboard.json.

Sample

  1. Specify custom metrics in config.yaml
---
customMetrics:
  - metric: memcached_operator_reconcile_total
    type: counter
  - metric: memcached_operator_reconcile_time_seconds_bucket
    type: histogram
  1. Run kubebuilder edit --plugins=grafana.kubebuilder.io/v1-alpha1 to generate the manifest
  2. Import the manifest into Grafana Web UI:

newpre

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Aug 4, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @Kavinjsir. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

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/test-infra repository.

@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 4, 2022
@Kavinjsir Kavinjsir force-pushed the feat/cutom-metrics-experiment branch from b5194c9 to 15df8cd Compare August 4, 2022 19:57
@Kavinjsir Kavinjsir force-pushed the feat/cutom-metrics-experiment branch 2 times, most recently from 22a4e5a to 81e8ddc Compare August 16, 2022 05:18
@Kavinjsir Kavinjsir marked this pull request as ready for review August 16, 2022 05:42
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Aug 16, 2022
@Kavinjsir Kavinjsir force-pushed the feat/cutom-metrics-experiment branch from 81e8ddc to 3fb8a89 Compare August 16, 2022 05:54
@k8s-ci-robot
Copy link
Contributor

@Kavinjsir: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/test lint

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/test-infra repository.

@k8s-ci-robot
Copy link
Contributor

@Kavinjsir: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

In response to this:

/test Lint

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/test-infra repository.

@Kavinjsir
Copy link
Contributor Author

Since grafana/custom-metrics/config.yaml is exposed to customize the panels, I'm also looking for methods to test the plugin upon a sample config.

Right now the integration test will generate test data from 0 to 1. I'm wondering how we may insert a config template such as:

---
customMetrics:
  - metric: memcached_operator_reconcile_total
    type: counter
  - metric: memcached_operator_reconcile_time_seconds_bucket
    type: histogram

And run kubebuilder edit ... follow by that in the integration test?

cc: @varshaprasad96 @camilamacedo86 @rashmigottipati

Copy link
Member

@varshaprasad96 varshaprasad96 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work @Kavinjsir ! Just a few nits.

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
return false
}

func fillMissingExpr(item templates.CustomMetricItem) templates.CustomMetricItem {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this func adds a default expression when user does not provide it?
If so, is it compatible with all the three kinds of prometheus metrics like gauge, counter etc.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this func adds a default expression when user does not provide it?

Yes, the current design is the plugin will check if the user provides expr, if missed, then filled a basic one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it compatible with all the three kinds of prometheus metrics like gauge, counter etc.

Yes, very basic though:

  • counter: sum(rate(custom_metric{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)
  • histogram: histogram_quantile(0.90, sum by(instance, le) (rate(custom_metric{job=\"$job\", namespace=\"$namespace\"}[5m])))
  • gauge: the custom_metric itself (show the value directly)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would be glad if we have better choices of expr, especially for gauge.
(Not quite confident to bring further prom_ql for gauge since it seems such fluctuating values might be observed in various perspectives..)

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
@k8s-ci-robot k8s-ci-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Aug 25, 2022
@Kavinjsir Kavinjsir force-pushed the feat/cutom-metrics-experiment branch 8 times, most recently from 3d40a17 to 9180448 Compare August 26, 2022 19:27
@camilamacedo86
Copy link
Member

/test pull-kubebuilder-test

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-18-20

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-19-16

@Kavinjsir
Copy link
Contributor Author

/test pull-kubebuilder-test

@Kavinjsir Kavinjsir force-pushed the feat/cutom-metrics-experiment branch 3 times, most recently from ca98b1a to c1fcb7e Compare September 3, 2022 18:05
@Kavinjsir
Copy link
Contributor Author

/test pull-kubebuilder-e2e-k8s-1-18-20

@Kavinjsir
Copy link
Contributor Author

/test pull-kubebuilder-e2e-k8s-1-19-16

@Kavinjsir
Copy link
Contributor Author

/test pull-kubebuilder-test

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-e2e-k8s-1-19-16
/test pull-kubebuilder-test

@camilamacedo86
Copy link
Member

/test pull-kubebuilder-test

1 similar comment
@camilamacedo86
Copy link
Member

/test pull-kubebuilder-test

@Kavinjsir
Copy link
Contributor Author

See that the two test cases remain erroring. Would you like me to fix it by additional coding?
@varshaprasad96 @camilamacedo86

}
]
},
"unit": "s"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Temporary set the unit for all panels to be seconds. This would affect the dimension of the lines shown in each panel:

Not sure if this is acceptable to left for users OR maybe we could make this configurable through config.yaml?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

configurable units would be much appreciated!

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
return false
}

func fillMissingExpr(item templates.CustomMetricItem) templates.CustomMetricItem {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this func adds a default expression when user does not provide it?

Yes, the current design is the plugin will check if the user provides expr, if missed, then filled a basic one.

return false
}

func fillMissingExpr(item templates.CustomMetricItem) templates.CustomMetricItem {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it compatible with all the three kinds of prometheus metrics like gauge, counter etc.

Yes, very basic though:

  • counter: sum(rate(custom_metric{job=\"$job\", namespace=\"$namespace\"}[5m])) by (instance, pod)
  • histogram: histogram_quantile(0.90, sum by(instance, le) (rate(custom_metric{job=\"$job\", namespace=\"$namespace\"}[5m])))
  • gauge: the custom_metric itself (show the value directly)

pkg/plugins/optional/grafana/v1alpha/scaffolds/edit.go Outdated Show resolved Hide resolved
docs/book/src/plugins/grafana-v1-alpha.md Show resolved Hide resolved
customMetrics:
# - metric: # Raw custom metric (required)
# type: # Metric type: counter/gauge/histogram (required)
# expr: # Prom_ql for the metric (optional)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that makes sense. I'm not sure how could it be better. Right now, I just append an example following the template. Would this be good?

)

var _ plugins.Scaffolder = &editScaffolder{}

const ConfigFilePath = "grafana/custom-metrics/config.yaml"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!


BeforeEach(func() {
var err error
kbc, err = utils.NewTestContext(pluginutil.KubebuilderBinName, "GO111MODULE=on")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test error may come from here. Since such NewTestContext would call kubectl that assumes an existing kind cluster.

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
Copy link
Member

@camilamacedo86 camilamacedo86 Sep 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kavinjsir we should not add a new file here.
In this case, we need only in the Kubebuilder Makefile to call the test.

We would only need to execute this test once. However, to make simple let's call it with the others and a TODO here to sort it out after.

That means:
a) Remove this file plugin.sh
b) Add the new test in : https://github.com/kubernetes-sigs/kubebuilder/blob/master/test/e2e/setup.sh#L65
c) Revert the changes made in the Kubebuilder Makefile
d) Add a comment in the setup.sh and in the new e2e test like TODO: in this case, we do not need the cluster and we would not need to test against many k8s versions. So that it can be improved in the future.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

c/c @varshaprasad96

/hold

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good to me. I've updated the code based on the steps and it looks work well.

@k8s-ci-robot k8s-ci-robot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 5, 2022
Co-authored-by: Varsha <varshaprasad96@gmail.com> Camila Macedo <camilamacedo86@gmail.com>

- Scaffold `config.yaml` template
- Load configured `config.yaml` to create manifest
- Add test cases for custom metrics manifests
- Documentation

Go template syntax ref: https://stackoverflow.com/a/22375000
Copy link
Member

@camilamacedo86 camilamacedo86 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/hold cancel

@k8s-ci-robot k8s-ci-robot removed the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label Sep 6, 2022
@camilamacedo86
Copy link
Member

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Sep 6, 2022
@camilamacedo86 camilamacedo86 merged commit 46f36a3 into kubernetes-sigs:master Sep 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants