Skip to content

Commit

Permalink
Merge branch 'main' into rm-old-ccip-methods
Browse files Browse the repository at this point in the history
  • Loading branch information
RensR authored Dec 12, 2024
2 parents 75fde6d + 0b03fa3 commit 69d3bbe
Show file tree
Hide file tree
Showing 154 changed files with 5,687 additions and 17,871 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/golangci_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ jobs:
- name: golangci-lint
uses: smartcontractkit/.github/actions/ci-lint-go@2ac9d97a83a5edded09af7fcf4ea5bce7a4473a4 # v0.2.6
with:
golangci-lint-version: v1.61.0
golangci-lint-version: v1.62.2

4 changes: 2 additions & 2 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
golang 1.22.7
golang 1.23.3
protoc 25.1
protoc-gen-go-grpc 1.3.0
golangci-lint 1.61.0
golangci-lint 1.62.2
mockery 2.43.2
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ generate: mockery install-protoc gomods
mockery

.PHONY: lint-workspace lint
GOLANGCI_LINT_VERSION := 1.60.1
GOLANGCI_LINT_VERSION := 1.62.2
GOLANGCI_LINT_COMMON_OPTS := --max-issues-per-linter 0 --max-same-issues 0
GOLANGCI_LINT_DIRECTORY := ./golangci-lint

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
module github.com/smartcontractkit/chainlink-common

go 1.22.0

toolchain go1.22.7
go 1.23.3

require (
github.com/andybalholm/brotli v1.1.0
Expand Down
74 changes: 43 additions & 31 deletions observability-lib/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ The observability-lib is structured as follows:
```shell
observability-lib/
api/ # Grafana HTTP API Client to interact with resources
cmd/ # CLI to interact deploy or generateJSON from dashboards defined in folder below
dashboards/ # Dashboards definitions
cmd/ # CLI
grafana/ # grafana-foundations-sdk abstraction to manipulate grafana resources
```

Expand Down Expand Up @@ -89,43 +88,56 @@ func main() {
```
</details>

More advanced examples can be found in the [dashboards](./dashboards) folder :
- [DON OCR](./dashboards/atlas-don/component.go)
- [Capabilities](./dashboards/capabilities/component.go)
- [Node General](./dashboards/core-node/component.go)
- [Node Components](./dashboards/core-node-components/component.go)
- [Kubernetes Resources](./dashboards/k8s-resources/component.go)
- [NOP OCR Health](./dashboards/nop-ocr/component.go)

## Cmd Usage

The CLI can be used to :
- Deploy dashboards and alerts to grafana
- Generate JSON from dashboards defined in the `dashboards` folder
CLI to manipulate grafana resources

### Contact Point

`func NewDashboard(props *Props)` in each [dashboards](./dashboards) packages is called from [cmd](./cmd/builder.go) to deploy or generate JSON from the dashboard.
#### List

Example to deploy a dashboard to grafana instance using URL and token:
```shell
make build
./observability-lib deploy \
--dashboard-name DashboardName \
--dashboard-folder FolderName \
--grafana-url $GRAFANA_URL \
--grafana-token $GRAFANA_TOKEN \
--type core-node \
--platform kubernetes \
--metrics-datasource Prometheus
./observability-lib api contact-point list \
--grafana-url http://localhost:3000 \
--grafana-token <token>
```
To see how to get a grafana token you can check this [page](https://grafana.com/docs/grafana/latest/administration/service-accounts/)

Example to generate JSON from a dashboard defined in the `dashboards` folder:
#### Delete

```shell
make build
./observability-lib generate \
--dashboard-name DashboardName \
--type core-node-components \
--platform kubernetes
./observability-lib api contact-point delete <name> \
--grafana-url http://localhost:3000 \
--grafana-token <token>
```

### Dashboard

#### Delete

```shell
./observability-lib api dashboard delete <name> \
--grafana-url http://localhost:3000 \
--grafana-token <token>
```

### Notification Policy

#### List

```shell
./observability-lib api notification-policy list \
--grafana-url http://localhost:3000 \
--grafana-token <token>
```

#### Delete

```shell
./observability-lib api notification-policy delete <receiverName> \
--grafana-url http://localhost:3000 \
--grafana-token <token> \
--matchers key,=,value \
--matchers key2,=,value2
```

## Makefile Usage
Expand Down
96 changes: 89 additions & 7 deletions observability-lib/api/notification-policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ func objectMatchersEqual(a alerting.ObjectMatchers, b alerting.ObjectMatchers) b
return true
}

func PrintPolicyTree(policy alerting.NotificationPolicy, depth int) {
if depth == 0 {
fmt.Printf("| Root Policy | Receiver: %s\n", *policy.Receiver)
}

for _, notificationPolicy := range policy.Routes {
for i := 0; i < depth; i++ {
fmt.Print("--")
}
fmt.Printf("| Matchers %s | Receiver: %s\n", *notificationPolicy.ObjectMatchers, *notificationPolicy.Receiver)

if notificationPolicy.Routes != nil {
PrintPolicyTree(notificationPolicy, depth+1)
}
}
}

func policyExist(parent alerting.NotificationPolicy, newNotificationPolicy alerting.NotificationPolicy) bool {
for _, notificationPolicy := range parent.Routes {
matchersEqual := false
Expand All @@ -40,24 +57,89 @@ func policyExist(parent alerting.NotificationPolicy, newNotificationPolicy alert
return true
}
if notificationPolicy.Routes != nil {
policyExist(notificationPolicy, newNotificationPolicy)
return policyExist(notificationPolicy, newNotificationPolicy)
}
}
return false
}

func updateInPlace(parent *alerting.NotificationPolicy, newNotificationPolicy alerting.NotificationPolicy) bool {
for key, 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 {
parent.Routes[key] = newNotificationPolicy
return true
}
if notificationPolicy.Routes != nil {
return updateInPlace(&parent.Routes[key], newNotificationPolicy)
}
}
return false
}

func deleteInPlace(parent *alerting.NotificationPolicy, newNotificationPolicy alerting.NotificationPolicy) bool {
for key, 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 {
if len(parent.Routes) == 1 {
parent.Routes = nil
return true
} else if len(parent.Routes) > 1 {
parent.Routes = append(parent.Routes[:key], parent.Routes[key+1:]...)
return true
} else {
return false
}
}
if notificationPolicy.Routes != nil {
return deleteInPlace(&parent.Routes[key], newNotificationPolicy)
}
}
return false
}

// DeleteNestedPolicy Delete Nested Policy from Notification Policy Tree
func (c *Client) DeleteNestedPolicy(newNotificationPolicy alerting.NotificationPolicy) error {
notificationPolicyTreeResponse, _, err := c.GetNotificationPolicy()
if err != nil {
return err
}
notificationPolicyTree := alerting.NotificationPolicy(notificationPolicyTreeResponse)
if !policyExist(notificationPolicyTree, newNotificationPolicy) {
return fmt.Errorf("notification policy not found")
}
deleteInPlace(&notificationPolicyTree, newNotificationPolicy)
_, _, errPutNotificationPolicy := c.PutNotificationPolicy(notificationPolicyTree)
if errPutNotificationPolicy != nil {
return errPutNotificationPolicy
}
return nil
}

// AddNestedPolicy Add Nested Policy to Notification Policy Tree
func (c *Client) AddNestedPolicy(newNotificationPolicy alerting.NotificationPolicy) error {
notificationPolicyTree, _, err := c.GetNotificationPolicy()
notificationPolicyTreeResponse, _, err := c.GetNotificationPolicy()
notificationPolicyTree := alerting.NotificationPolicy(notificationPolicyTreeResponse)

if err != nil {
return err
}
if !policyExist(alerting.NotificationPolicy(notificationPolicyTree), newNotificationPolicy) {
if !policyExist(notificationPolicyTree, newNotificationPolicy) {
notificationPolicyTree.Routes = append(notificationPolicyTree.Routes, newNotificationPolicy)
_, _, errPutNotificationPolicy := c.PutNotificationPolicy(alerting.NotificationPolicy(notificationPolicyTree))
if errPutNotificationPolicy != nil {
return errPutNotificationPolicy
}
} else {
updateInPlace(&notificationPolicyTree, newNotificationPolicy)
}
_, _, errPutNotificationPolicy := c.PutNotificationPolicy(notificationPolicyTree)
if errPutNotificationPolicy != nil {
return errPutNotificationPolicy
}
return nil
}
Expand Down
Loading

0 comments on commit 69d3bbe

Please sign in to comment.