Skip to content

Commit 55813cb

Browse files
authored
Add queryOptions.Inreval that may be used by interval/range vars (#1957)
* Add query options Inreval to that may be used by interval/range vars * Extend grafana interval validation; fix row.Collapsed option usage
1 parent 48154c0 commit 55813cb

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

observability-lib/grafana/builder.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ func (b *Builder) AddVars(items ...cog.Builder[dashboard.VariableModel]) {
8080
func (b *Builder) AddRow(title string, options ...RowOptions) {
8181
row := dashboard.NewRowBuilder(title)
8282
for _, o := range options {
83-
if o.Collapsed {
84-
row.Collapsed(true)
85-
}
83+
row.Collapsed(o.Collapsed)
8684
}
8785
b.dashboardBuilder.WithRow(row)
8886
}

observability-lib/grafana/panels.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package grafana
22

33
import (
4+
"regexp"
5+
"strconv"
6+
47
"github.com/grafana/grafana-foundation-sdk/go/alerting"
58
"github.com/grafana/grafana-foundation-sdk/go/bargauge"
69
"github.com/grafana/grafana-foundation-sdk/go/cog"
@@ -29,6 +32,7 @@ const (
2932
type Query struct {
3033
Expr string
3134
Legend string
35+
Interval string // i.e. 30s, 2m, a lower limit for the interval that will be sent to datasource and used by $__interval/range variables
3236
Instant bool
3337
Min float64
3438
Format prometheus.PromQueryFormat
@@ -41,6 +45,10 @@ func newQuery(query Query) *prometheus.DataqueryBuilder {
4145
LegendFormat(query.Legend).
4246
Format(query.Format)
4347

48+
if isValidDuration(query.Interval) {
49+
res.Interval(query.Interval)
50+
}
51+
4452
if query.Instant {
4553
res.Instant()
4654
}
@@ -51,6 +59,16 @@ func newQuery(query Query) *prometheus.DataqueryBuilder {
5159
return res
5260
}
5361

62+
func isValidDuration(interval string) bool {
63+
var grafanaDurationRegexp = regexp.MustCompile(`^(\d+)(ms|[smhdwMy])$`) // ms, s, m, h, d, w, M, y
64+
m := grafanaDurationRegexp.FindStringSubmatch(interval)
65+
if m == nil {
66+
return false
67+
}
68+
n, _ := strconv.ParseInt(m[1], 10, 64)
69+
return n > 0
70+
}
71+
5472
type LegendOptions struct {
5573
Placement common.LegendPlacement
5674
DisplayMode common.LegendDisplayMode

0 commit comments

Comments
 (0)