Skip to content

Commit

Permalink
Various updates to Observability Library
Browse files Browse the repository at this point in the history
- Fix alert query type not instant when the query is instant
- Add disable resolve message and uid to contact point
- Add stacking mode to time series panel
- Add multi and include all to custom variable
- Use csv when the variable key and value is the same
  • Loading branch information
leeyikjiun committed Feb 4, 2025
1 parent aea9294 commit 6d6c525
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions observability-lib/grafana/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ func newRuleQuery(query RuleQuery) *alerting.QueryBuilder {
RefId(query.RefID)

if query.Instant {
model.QueryType("instant")
model.Instant()
}

Expand Down
19 changes: 14 additions & 5 deletions observability-lib/grafana/contact-point.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@ package grafana
import "github.com/grafana/grafana-foundation-sdk/go/alerting"

type ContactPointOptions struct {
Name string
Type alerting.ContactPointType
Settings map[string]interface{}
Name string
Type alerting.ContactPointType
Settings map[string]interface{}
DisableResolveMessage bool
Uid string
}

func NewContactPoint(options *ContactPointOptions) *alerting.ContactPointBuilder {
return alerting.NewContactPointBuilder().
builder := alerting.NewContactPointBuilder().
Name(options.Name).
Type(options.Type).
Settings(options.Settings)
Settings(options.Settings).
DisableResolveMessage(options.DisableResolveMessage)

if options.Uid != "" {
builder.Uid(options.Uid)
}

return builder
}
11 changes: 10 additions & 1 deletion observability-lib/grafana/panels.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ type TimeSeriesPanelOptions struct {
ToolTipOptions *ToolTipOptions
ThresholdStyle common.GraphThresholdsStyleMode
DrawStyle common.GraphDrawStyle
StackingMode common.StackingMode
}

func NewTimeSeriesPanel(options *TimeSeriesPanelOptions) *Panel {
Expand All @@ -300,6 +301,10 @@ func NewTimeSeriesPanel(options *TimeSeriesPanelOptions) *Panel {
options.ToolTipOptions = &ToolTipOptions{}
}

if options.StackingMode == "" {
options.StackingMode = common.StackingModeNone
}

newPanel := timeseries.NewPanelBuilder().
Datasource(datasourceRef(options.Datasource)).
Title(*options.Title).
Expand All @@ -315,7 +320,11 @@ func NewTimeSeriesPanel(options *TimeSeriesPanelOptions) *Panel {
ScaleDistribution(common.NewScaleDistributionConfigBuilder().
Type(options.ScaleDistribution),
).
Tooltip(newToolTip(options.ToolTipOptions))
Tooltip(newToolTip(options.ToolTipOptions)).
// Time Series Panel Options
Stacking(common.NewStackingConfigBuilder().
Mode(options.StackingMode),
)

if options.Decimals != nil {
newPanel.Decimals(*options.Decimals)
Expand Down
14 changes: 11 additions & 3 deletions observability-lib/grafana/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ type VariableOption struct {

type CustomVariableOptions struct {
*VariableOption
Values map[string]any
Values map[string]any
Multi bool
IncludeAll bool
}

func NewCustomVariable(options *CustomVariableOptions) *dashboard.CustomVariableBuilder {
Expand All @@ -38,7 +40,9 @@ func NewCustomVariable(options *CustomVariableOptions) *dashboard.CustomVariable
Selected: cog.ToPtr[bool](true),
Text: dashboard.StringOrArrayOfString{String: cog.ToPtr(options.CurrentText)},
Value: dashboard.StringOrArrayOfString{String: cog.ToPtr(options.CurrentValue)},
})
}).
Multi(options.Multi).
IncludeAll(options.IncludeAll)

optionsList := []dashboard.VariableOption{
{
Expand All @@ -63,7 +67,11 @@ func NewCustomVariable(options *CustomVariableOptions) *dashboard.CustomVariable
// Escape commas and colons in the value which are reserved characters for values string
cleanValue := strings.ReplaceAll(value.(string), ",", "\\,")
cleanValue = strings.ReplaceAll(cleanValue, ":", "\\:")
valuesString += key + " : " + cleanValue + " , "
valuesString += key
if key != cleanValue {
valuesString += " : " + cleanValue
}
valuesString += ", "
}
variable.Values(dashboard.StringOrMap{String: cog.ToPtr(strings.TrimSuffix(valuesString, ", "))})

Expand Down

0 comments on commit 6d6c525

Please sign in to comment.