Skip to content

Commit

Permalink
Merge branch 'main' of github.com:smartcontractkit/chainlink-common i…
Browse files Browse the repository at this point in the history
…nto INFOPLAT-1071-beholder-csa-signer-auth_2
  • Loading branch information
pkcll committed Nov 6, 2024
2 parents 5a80788 + 3b320ad commit 3e915bb
Show file tree
Hide file tree
Showing 132 changed files with 8,034 additions and 1,691 deletions.
16 changes: 16 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!--- Does this work have a corresponding ticket? Please link it here as well as one of:
- the PR title
- branch name
- commit message
[LINK-777](https://smartcontract-it.atlassian.net/browse/LINK-777)
-->

### Requires
<!--- Does this work depend on other open PRs? Please list them.
- https://github.com/smartcontractkit/libocr/pull/7777777
-->

### Supports
<!--- Does this work support other open PRs? Please list them.
- https://github.com/smartcontractkit/chainlink/pull/7777777
-->
4 changes: 3 additions & 1 deletion observability-lib/api/contact-point.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (c *Client) DeleteContactPoint(uid string) (DeleteContactPointResponse, *re
}

statusCode := resp.StatusCode()
if statusCode != 204 {
if statusCode != 202 {
return DeleteContactPointResponse{}, resp, fmt.Errorf("error deleting contact point, received unexpected status code %d: %s", statusCode, resp.String())
}

Expand All @@ -95,6 +95,7 @@ func (c *Client) PostContactPoint(contactPoint alerting.ContactPoint) (PostConta

resp, err := c.resty.R().
SetHeader("Content-Type", "application/json").
SetHeader("X-Disable-Provenance", "true").
SetBody(contactPoint).
SetResult(&grafanaResp).
Post("/api/v1/provisioning/contact-points")
Expand All @@ -119,6 +120,7 @@ func (c *Client) PutContactPoint(uid string, contactPoint alerting.ContactPoint)

resp, err := c.resty.R().
SetHeader("Content-Type", "application/json").
SetHeader("X-Disable-Provenance", "true").
SetBody(contactPoint).
SetResult(&grafanaResp).
Put(fmt.Sprintf("/api/v1/provisioning/contact-points/%s", uid))
Expand Down
2 changes: 1 addition & 1 deletion observability-lib/api/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *Client) GetDataSourceByName(name string) (*Datasource, *resty.Response,

statusCode := resp.StatusCode()
if statusCode != 200 {
return nil, resp, fmt.Errorf("error fetching datasource, received unexpected status code %d: %s", statusCode, resp.String())
return nil, resp, fmt.Errorf("error fetching datasource %s, received unexpected status code %d: %s", name, statusCode, resp.String())
}
return &grafanaResp, resp, nil
}
60 changes: 43 additions & 17 deletions observability-lib/api/notification-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,57 @@ import (
"github.com/grafana/grafana-foundation-sdk/go/alerting"
)

func objectMatchersEqual(a alerting.ObjectMatchers, b alerting.ObjectMatchers) bool {
if len(a) != len(b) {
return false
}

for i := range a {
foundMatch := false
for j := range b {
if reflect.DeepEqual(a[i], b[j]) {
foundMatch = true
break
}
}
if !foundMatch {
return false
}
}

return true
}

func policyExist(parent alerting.NotificationPolicy, newNotificationPolicy alerting.NotificationPolicy) bool {
for _, notificationPolicy := range parent.Routes {
matchersEqual := false
if notificationPolicy.ObjectMatchers != nil {
matchersEqual = objectMatchersEqual(*notificationPolicy.ObjectMatchers, *newNotificationPolicy.ObjectMatchers)
}
receiversEqual := reflect.DeepEqual(notificationPolicy.Receiver, newNotificationPolicy.Receiver)
if matchersEqual && receiversEqual {
return true
}
if notificationPolicy.Routes != nil {
policyExist(notificationPolicy, newNotificationPolicy)
}
}
return false
}

// AddNestedPolicy Add Nested Policy to Notification Policy Tree
func (c *Client) AddNestedPolicy(newNotificationPolicy alerting.NotificationPolicy) error {
notificationPolicyTree, _, err := c.GetNotificationPolicy()
if err != nil {
return err
}
updatedNotificationPolicy := notificationPolicyTree
tagsEqual := false
for key, notificationPolicy := range updatedNotificationPolicy.Routes {
if notificationPolicy.ObjectMatchers != nil {
tagsEqual = reflect.DeepEqual(notificationPolicy.ObjectMatchers, newNotificationPolicy.ObjectMatchers)
if tagsEqual {
updatedNotificationPolicy.Routes[key] = newNotificationPolicy
}
if !policyExist(alerting.NotificationPolicy(notificationPolicyTree), newNotificationPolicy) {
notificationPolicyTree.Routes = append(notificationPolicyTree.Routes, newNotificationPolicy)
_, _, errPutNotificationPolicy := c.PutNotificationPolicy(alerting.NotificationPolicy(notificationPolicyTree))
if errPutNotificationPolicy != nil {
return errPutNotificationPolicy
}
}
if !tagsEqual {
updatedNotificationPolicy.Routes = append(updatedNotificationPolicy.Routes, newNotificationPolicy)
}

_, _, errPutNotificationPolicy := c.PutNotificationPolicy(alerting.NotificationPolicy(updatedNotificationPolicy))
if errPutNotificationPolicy != nil {
return errPutNotificationPolicy
}

return nil
}

Expand Down
46 changes: 46 additions & 0 deletions observability-lib/api/notification-policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package api

import (
"testing"

"github.com/grafana/grafana-foundation-sdk/go/alerting"
"github.com/stretchr/testify/require"
)

func TestObjectMatchersEqual(t *testing.T) {
t.Run("returns true if the two object matchers are equal", func(t *testing.T) {
a := alerting.ObjectMatchers{{"team", "=", "chainlink"}}
b := alerting.ObjectMatchers{{"team", "=", "chainlink"}}

result := objectMatchersEqual(a, b)
require.True(t, result)
})

t.Run("returns true if the two object matchers with multiple matches are equal", func(t *testing.T) {
a := alerting.ObjectMatchers{
{"team", "=", "chainlink"},
{"severity", "=", "critical"},
}
b := alerting.ObjectMatchers{
{"severity", "=", "critical"},
{"team", "=", "chainlink"},
}

result := objectMatchersEqual(a, b)
require.True(t, result)
})

t.Run("returns false if the two object matchers with multiple matches are different", func(t *testing.T) {
a := alerting.ObjectMatchers{
{"team", "=", "chainlink"},
{"severity", "=", "critical"},
}
b := alerting.ObjectMatchers{
{"severity", "=", "warning"},
{"team", "=", "chainlink"},
}

result := objectMatchersEqual(a, b)
require.False(t, result)
})
}
25 changes: 25 additions & 0 deletions observability-lib/api/rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,31 @@ func (c *Client) PostAlertRule(alertRule alerting.Rule) (PostAlertRuleResponse,
return grafanaResp, resp, nil
}

type UpdateAlertRuleResponse struct{}

// UpdateAlertRule Update a specific alert rule by UID
func (c *Client) UpdateAlertRule(uid string, alertRule alerting.Rule) (UpdateAlertRuleResponse, *resty.Response, error) {
var grafanaResp UpdateAlertRuleResponse

resp, err := c.resty.R().
SetHeader("Content-Type", "application/json").
SetHeader("X-Disable-Provenance", "true").
SetBody(alertRule).
SetResult(&grafanaResp).
Put(fmt.Sprintf("/api/v1/provisioning/alert-rules/%s", uid))

if err != nil {
return UpdateAlertRuleResponse{}, resp, fmt.Errorf("error making API request: %w", err)
}

statusCode := resp.StatusCode()
if statusCode != 200 {
return UpdateAlertRuleResponse{}, resp, fmt.Errorf("error updating alert rule, received unexpected status code %d: %s", statusCode, resp.String())
}

return grafanaResp, resp, nil
}

type DeleteAlertRuleResponse struct{}

// DeleteAlertRule Delete a specific alert rule by UID
Expand Down
3 changes: 2 additions & 1 deletion observability-lib/cmd/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ type BuildOptions struct {
AlertsFilters string
}

func BuildDashboardWithType(options *BuildOptions) (*grafana.Dashboard, error) {
func BuildDashboardWithType(options *BuildOptions) (*grafana.Observability, error) {
switch options.TypeDashboard {
case TypeDashboardCoreNode:
return corenode.NewDashboard(&corenode.Props{
Name: options.Name,
Platform: options.Platform,
MetricsDataSource: options.MetricsDataSource,
LogsDataSource: options.LogsDataSource,
SlackChannel: options.SlackChannel,
SlackWebhookURL: options.SlackWebhookURL,
AlertsTags: options.AlertsTags,
Expand Down
18 changes: 11 additions & 7 deletions observability-lib/cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ var DeployCmd = &cobra.Command{
return errAlertsTags
}

metricsDataSource, errMetricsDataSource := grafana.GetDataSourceFromGrafana(
cmd.Flag("metrics-datasource").Value.String(),
cmd.Flag("grafana-url").Value.String(),
cmd.Flag("grafana-token").Value.String(),
)
var metricsDataSource *grafana.DataSource
if cmd.Flag("metrics-datasource").Value.String() != "" {
var errMetricsDataSource error
metricsDataSource, errMetricsDataSource = grafana.GetDataSourceFromGrafana(
cmd.Flag("metrics-datasource").Value.String(),
cmd.Flag("grafana-url").Value.String(),
cmd.Flag("grafana-token").Value.String(),
)

if errMetricsDataSource != nil {
return errMetricsDataSource
if errMetricsDataSource != nil {
return errMetricsDataSource
}
}

var logsDataSource *grafana.DataSource
Expand Down
9 changes: 9 additions & 0 deletions observability-lib/cmd/notification-templates.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,13 @@ slack: |-
{{ define "slack.chainlink.title" }}
{{ template "alert_severity_prefix_emoji" . }} [{{- if gt (len .Alerts.Resolved) 0}}{{ .Status | toUpper }}{{- else }}{{ .CommonLabels.severity | toUpper }}{{- end }}:{{ .Alerts | len }}] {{ .CommonLabels.alertname }}
{{ end }}
pagerduty: |-
{{ define "pagerduty.chainlink.title" }}
[{{- if gt (len .Alerts.Resolved) 0}}{{ .Status | toUpper }}{{- else }}{{ .CommonLabels.severity | toUpper }}{{- end }}:{{ .Alerts | len }}] {{ .CommonLabels.alertname }}
{{ end }}
{{ define "pagerduty.chainlink.severity" }}
{{ if .CommonLabels.severity }}{{ .CommonLabels.severity | toLower }}{{ else }}critical{{ end }}
{{ end }}
2 changes: 1 addition & 1 deletion observability-lib/dashboards/atlas-don/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/smartcontractkit/chainlink-common/observability-lib/grafana"
)

func NewDashboard(props *Props) (*grafana.Dashboard, error) {
func NewDashboard(props *Props) (*grafana.Observability, error) {
if props.Name == "" {
return nil, fmt.Errorf("Name is required")
}
Expand Down
2 changes: 1 addition & 1 deletion observability-lib/dashboards/atlas-don/component_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func TestNewDashboard(t *testing.T) {
if err != nil {
t.Errorf("Error creating dashboard: %v", err)
}
require.IsType(t, grafana.Dashboard{}, *testDashboard)
require.IsType(t, grafana.Observability{}, *testDashboard)
require.Equal(t, "DON OCR Dashboard", *testDashboard.Dashboard.Title)
json, errJSON := testDashboard.GenerateJSON()
if errJSON != nil {
Expand Down
18 changes: 9 additions & 9 deletions observability-lib/dashboards/atlas-don/test-output.json
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@
"min": 0,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -684,7 +684,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -734,7 +734,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -784,7 +784,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -834,7 +834,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -897,7 +897,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -947,7 +947,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -997,7 +997,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down Expand Up @@ -1047,7 +1047,7 @@
"decimals": 1,
"noValue": "No data",
"custom": {
"fillOpacity": 2,
"fillOpacity": 0,
"scaleDistribution": {
"type": "linear"
}
Expand Down
2 changes: 1 addition & 1 deletion observability-lib/dashboards/capabilities/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type Props struct {
}

// NewDashboard creates a Capabilities dashboard
func NewDashboard(props *Props) (*grafana.Dashboard, error) {
func NewDashboard(props *Props) (*grafana.Observability, error) {
if props.Name == "" {
return nil, fmt.Errorf("Name is required")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ func TestNewDashboard(t *testing.T) {
if err != nil {
t.Errorf("Error creating dashboard: %v", err)
}
require.IsType(t, grafana.Dashboard{}, *testDashboard)
require.IsType(t, grafana.Observability{}, *testDashboard)
require.Equal(t, "Capabilities Dashboard", *testDashboard.Dashboard.Title)
json, errJSON := testDashboard.GenerateJSON()
if errJSON != nil {
Expand Down
Loading

0 comments on commit 3e915bb

Please sign in to comment.