Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various updates to Observability Library #1022

Merged
merged 4 commits into from
Feb 5, 2025
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
2 changes: 1 addition & 1 deletion observability-lib/api/notification-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (c *Client) DeleteNotificationTemplate(name string) (DeleteNotificationTemp
}

statusCode := resp.StatusCode()
if statusCode != 200 {
if statusCode != 204 {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if statusCode != 204 {
if statusCode != http.StatusNoContent {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great suggestion! Let me fix all the status codes in one go in another PR. cc @Atrax1

return DeleteNotificationTemplateResponse{}, resp, fmt.Errorf("error deleting notification template, received unexpected status code %d: %s", statusCode, resp.String())
}

Expand Down
5 changes: 5 additions & 0 deletions observability-lib/grafana/alerts.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package grafana

import (
"maps"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"github.com/grafana/grafana-foundation-sdk/go/cog"
"github.com/grafana/grafana-foundation-sdk/go/expr"
Expand Down Expand Up @@ -35,6 +37,7 @@ func newRuleQuery(query RuleQuery) *alerting.QueryBuilder {
RefId(query.RefID)

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

Expand Down Expand Up @@ -170,6 +173,7 @@ type AlertOptions struct {
For string
NoDataState alerting.RuleNoDataState
RuleExecErrState alerting.RuleExecErrState
Annotations map[string]string
Tags map[string]string
Query []RuleQuery
QueryRefCondition string
Expand Down Expand Up @@ -200,6 +204,7 @@ func NewAlertRule(options *AlertOptions) *alerting.RuleBuilder {
"description": options.Description,
"runbook_url": options.RunbookURL,
}
maps.Copy(annotations, options.Annotations)

if options.PanelTitle != "" {
annotations["panel_title"] = options.PanelTitle
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
}
10 changes: 7 additions & 3 deletions observability-lib/grafana/notification-template.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ package grafana
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"gopkg.in/yaml.v3"
)

func NewNotificationTemplatesFromFile(filepath string) (map[string]alerting.NotificationTemplate, error) {
func NewNotificationTemplatesFromFile(filePath string) (map[string]alerting.NotificationTemplate, error) {
fileName := strings.TrimSuffix(filepath.Base(filePath), filepath.Ext(filePath))

notificationTemplates := make(map[string]alerting.NotificationTemplate)
yamlFileToMapRes, errFileToMap := yamlFileToMap(filepath)
yamlFileToMapRes, errFileToMap := yamlFileToMap(filePath)
if errFileToMap != nil {
return nil, errFileToMap
}

for typeTemplate, template := range yamlFileToMapRes {
newTemplate, err := alerting.NewNotificationTemplateBuilder().
Name(fmt.Sprintf("chainlink-%s-notification-template", typeTemplate)).
Name(fmt.Sprintf("%s-%s-notification-template", fileName, typeTemplate)).
Template(template).Build()
if err != nil {
return nil, err
Expand Down
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
Loading