Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions observability-lib/grafana/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (b *Builder) AddVars(items ...cog.Builder[dashboard.VariableModel]) {
func (b *Builder) AddRow(title string, options ...RowOptions) {
row := dashboard.NewRowBuilder(title)
for _, o := range options {
if o.Collapsed {
row.Collapsed(true)
}
row.Collapsed(o.Collapsed)
}
b.dashboardBuilder.WithRow(row)
}
Expand Down
18 changes: 18 additions & 0 deletions observability-lib/grafana/panels.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package grafana

import (
"regexp"
"strconv"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"github.com/grafana/grafana-foundation-sdk/go/bargauge"
"github.com/grafana/grafana-foundation-sdk/go/cog"
Expand Down Expand Up @@ -29,6 +32,7 @@ const (
type Query struct {
Expr string
Legend string
Interval string // i.e. 30s, 2m, a lower limit for the interval that will be sent to datasource and used by $__interval/range variables
Instant bool
Min float64
Format prometheus.PromQueryFormat
Expand All @@ -41,6 +45,10 @@ func newQuery(query Query) *prometheus.DataqueryBuilder {
LegendFormat(query.Legend).
Format(query.Format)

if isValidDuration(query.Interval) {
res.Interval(query.Interval)
}

if query.Instant {
res.Instant()
}
Expand All @@ -51,6 +59,16 @@ func newQuery(query Query) *prometheus.DataqueryBuilder {
return res
}

func isValidDuration(interval string) bool {
var grafanaDurationRegexp = regexp.MustCompile(`^(\d+)(ms|[smhdwMy])$`) // ms, s, m, h, d, w, M, y
m := grafanaDurationRegexp.FindStringSubmatch(interval)
if m == nil {
return false
}
n, _ := strconv.ParseInt(m[1], 10, 64)
return n > 0
}

type LegendOptions struct {
Placement common.LegendPlacement
DisplayMode common.LegendDisplayMode
Expand Down
Loading