Skip to content

Commit

Permalink
feat(obs-lib): add panel overrides (#1037)
Browse files Browse the repository at this point in the history
  • Loading branch information
leeyikjiun authored Feb 19, 2025
1 parent 421f99d commit 9cc20a9
Show file tree
Hide file tree
Showing 2 changed files with 233 additions and 0 deletions.
171 changes: 171 additions & 0 deletions observability-lib/grafana/overrides.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package grafana

type Override struct {
Matcher *Matcher
Properties []*Property
}

type Matcher struct {
ID string
Options any
}

func NewByNameMatcher(name string) *Matcher {
return &Matcher{
ID: "byName",
Options: name,
}
}

type MatcherReducer string

const (
MatcherReducerLastNotNull MatcherReducer = "lastNotNull"
)

type MatcherOp string

const (
MatcherOpGTE MatcherOp = "gte"
)

type ByValueMatcherOptions struct {
Reducer MatcherReducer
Op MatcherOp
Value float64
}

func NewByValueMatcher(options *ByValueMatcherOptions) *Matcher {
return &Matcher{
ID: "byValue",
Options: map[string]any{
"reducer": options.Reducer,
"op": options.Op,
"value": options.Value,
},
}
}

type MatcherType string

const (
MatcherTypeTime MatcherType = "time"
)

func NewByTypeMatcher(t MatcherType) *Matcher {
return &Matcher{
ID: "byType",
Options: t,
}
}

func NewByRegexpMatcher(regex string) *Matcher {
return &Matcher{
ID: "byRegexp",
Options: regex,
}
}

func NewByQueryMatcher(refID string) *Matcher {
return &Matcher{
ID: "byFrameRefID",
Options: refID,
}
}

type Property struct {
ID string
Value any
}

type ColorMode string

const (
ColorModeFixed ColorMode = "fixed"
)

type ColorPropertyOptions struct {
Mode ColorMode
FixedColor string
}

func NewColorProperty(options *ColorPropertyOptions) *Property {
return &Property{
ID: "color",
Value: map[string]any{
"mode": options.Mode,
"fixedColor": options.FixedColor,
},
}
}

type UnitValue string

const (
UnitValueBlock UnitValue = "block"
)

func NewUnitProperty(value UnitValue) *Property {
return &Property{
ID: "unit",
Value: value,
}
}

type LinksPropertyOptions struct {
TargetBlank bool
Title string
URL string
}

func NewLinksProperty(options *LinksPropertyOptions) *Property {
return &Property{
ID: "links",
Value: map[string]any{
"targetBlank": options.TargetBlank,
"title": options.Title,
"url": options.URL,
},
}
}

func NewHiddenProperty(value bool) *Property {
return &Property{
ID: "custom.hidden",
Value: value,
}
}

func NewWidthProperty(value float64) *Property {
return &Property{
ID: "custom.width",
Value: value,
}
}

type CellOptionsMode string

const (
CellOptionsModeBasic CellOptionsMode = "basic"
)

type CellOptionsType string

const (
CellOptionsTypeColorBackground CellOptionsType = "color-background"
)

type CellOptionsOptions struct {
Mode CellOptionsMode
Type CellOptionsType
}

func NewCellOptions(options *CellOptionsOptions) *Property {
return &Property{
ID: "custom.cellOptions",
Value: map[string]any{
"mode": options.Mode,
"type": options.Type,
},
}
}
62 changes: 62 additions & 0 deletions observability-lib/grafana/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ func newTransform(options *TransformOptions) dashboard.DataTransformerConfig {
}
}

func newOverride(override *Override) (matcher dashboard.MatcherConfig, properties []dashboard.DynamicConfigValue) {
matcher = dashboard.MatcherConfig{
Id: override.Matcher.ID,
Options: override.Matcher.Options,
}

for _, property := range override.Properties {
properties = append(
properties,
dashboard.DynamicConfigValue{
Id: property.ID,
Value: property.Value,
},
)
}

return
}

type ToolTipOptions struct {
Mode common.TooltipDisplayMode
Sort common.SortOrder
Expand Down Expand Up @@ -138,6 +157,7 @@ type PanelOptions struct {
Query []Query
Threshold *ThresholdOptions
Transform *TransformOptions
Overrides []*Override
ColorScheme dashboard.FieldColorModeId
Interval string
}
Expand Down Expand Up @@ -267,6 +287,12 @@ func NewStatPanel(options *StatPanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.ColorScheme != "" {
newPanel.ColorScheme(dashboard.NewFieldColorBuilder().Mode(options.ColorScheme))
}
Expand Down Expand Up @@ -373,6 +399,12 @@ func NewTimeSeriesPanel(options *TimeSeriesPanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.ColorScheme != "" {
newPanel.ColorScheme(dashboard.NewFieldColorBuilder().Mode(options.ColorScheme))
}
Expand Down Expand Up @@ -454,6 +486,12 @@ func NewBarGaugePanel(options *BarGaugePanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.Orientation != "" {
newPanel.Orientation(options.Orientation)
}
Expand Down Expand Up @@ -515,6 +553,12 @@ func NewGaugePanel(options *GaugePanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

return &Panel{
gaugePanelBuilder: newPanel,
}
Expand Down Expand Up @@ -569,6 +613,12 @@ func NewTablePanel(options *TablePanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.ColorScheme != "" {
newPanel.ColorScheme(dashboard.NewFieldColorBuilder().Mode(options.ColorScheme))
}
Expand Down Expand Up @@ -648,6 +698,12 @@ func NewLogPanel(options *LogPanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.ColorScheme != "" {
newPanel.ColorScheme(dashboard.NewFieldColorBuilder().Mode(options.ColorScheme))
}
Expand Down Expand Up @@ -703,6 +759,12 @@ func NewHeatmapPanel(options *HeatmapPanelOptions) *Panel {
newPanel.WithTransformation(newTransform(options.Transform))
}

if options.Overrides != nil {
for _, override := range options.Overrides {
newPanel.WithOverride(newOverride(override))
}
}

if options.ColorScheme != "" {
newPanel.ColorScheme(dashboard.NewFieldColorBuilder().Mode(options.ColorScheme))
}
Expand Down

0 comments on commit 9cc20a9

Please sign in to comment.