Skip to content

Commit

Permalink
Add configurable top
Browse files Browse the repository at this point in the history
  • Loading branch information
jotak committed Apr 15, 2024
1 parent 796064e commit 5b02dd1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
6 changes: 6 additions & 0 deletions apis/flowmetrics/v1alpha1/flowmetric_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,12 @@ type Query struct {
// label such as: `sum(rate($METRIC[2m])) by (Label1, Label2)`, you may write as the legend: `Label1={{ Label1 }}, Label2={{ Label2 }}`.
// +required
Legend string `json:"legend"`

// Top N series to display per timestamp. Does not apply to `SingleStat` chart type.
// +kubebuilder:default:=7
// +kubebuilder:validation:Minimum=1
// +required
Top int `json:"top"`
}

// FlowMetricStatus defines the observed state of FlowMetric
Expand Down
7 changes: 7 additions & 0 deletions bundle/manifests/flows.netobserv.io_flowmetrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ spec:
You can use `$METRIC` to refer to the metric defined in this resource. For example: `sum(rate($METRIC[2m]))`.
To learn more about `promQL`, refer to the Prometheus documentation: https://prometheus.io/docs/prometheus/latest/querying/basics/
type: string
top:
default: 7
description: Top N series to display per timestamp. Does
not apply to `SingleStat` chart type.
minimum: 1
type: integer
required:
- legend
- promQL
- top
type: object
type: array
sectionName:
Expand Down
7 changes: 7 additions & 0 deletions config/crd/bases/flows.netobserv.io_flowmetrics.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,16 @@ spec:
You can use `$METRIC` to refer to the metric defined in this resource. For example: `sum(rate($METRIC[2m]))`.
To learn more about `promQL`, refer to the Prometheus documentation: https://prometheus.io/docs/prometheus/latest/querying/basics/
type: string
top:
default: 7
description: Top N series to display per timestamp. Does
not apply to `SingleStat` chart type.
minimum: 1
type: integer
required:
- legend
- promQL
- top
type: object
type: array
sectionName:
Expand Down
6 changes: 5 additions & 1 deletion pkg/dashboards/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,12 @@ func createSingleStatPanels(c *chart) []Panel {
func createGraphPanel(c *chart) Panel {
var targets []Target
for _, q := range c.Queries {
top := 7
if q.Top > 0 {
top = q.Top
}
query := strings.ReplaceAll(q.PromQL, "$METRIC", "netobserv_"+c.mptr.Spec.MetricName)
query = fmt.Sprintf("topk(7, %s)", query)
query = fmt.Sprintf("topk(%d, %s)", top, query)
targets = append(targets, NewTarget(query, q.Legend))
}
return NewPanel(c.Title, c.Type, c.Unit, 4, targets...)
Expand Down
3 changes: 2 additions & 1 deletion pkg/dashboards/dashboard_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func TestCreateCustomDashboard(t *testing.T) {
{
PromQL: `sum(rate($METRIC{label="foo"}[5m])) by (lbl1,lbl2)`,
Legend: "{{lbl1}}: {{lbl2}}",
Top: 10,
},
},
},
Expand Down Expand Up @@ -269,7 +270,7 @@ func TestCreateCustomDashboard(t *testing.T) {
Format: "Bps",
Targets: []Target{
{
Expr: "topk(7, sum(rate(netobserv_my_metric{label=\"foo\"}[5m])) by (lbl1,lbl2))",
Expr: "topk(10, sum(rate(netobserv_my_metric{label=\"foo\"}[5m])) by (lbl1,lbl2))",
LegendFormat: "{{lbl1}}: {{lbl2}}",
},
},
Expand Down

0 comments on commit 5b02dd1

Please sign in to comment.