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

feat(observability-lib): remove consumers + refactor cmd #964

Merged
merged 2 commits into from
Dec 9, 2024
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
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
44 changes: 33 additions & 11 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,7 +57,7 @@ func policyExist(parent alerting.NotificationPolicy, newNotificationPolicy alert
return true
}
if notificationPolicy.Routes != nil {
policyExist(notificationPolicy, newNotificationPolicy)
return policyExist(notificationPolicy, newNotificationPolicy)
}
}
return false
Expand All @@ -58,7 +75,7 @@ func updateInPlace(parent *alerting.NotificationPolicy, newNotificationPolicy al
return true
}
if notificationPolicy.Routes != nil {
policyExist(notificationPolicy, newNotificationPolicy)
return updateInPlace(&parent.Routes[key], newNotificationPolicy)
}
}
return false
Expand All @@ -72,11 +89,18 @@ func deleteInPlace(parent *alerting.NotificationPolicy, newNotificationPolicy al
}
receiversEqual := reflect.DeepEqual(notificationPolicy.Receiver, newNotificationPolicy.Receiver)
if matchersEqual && receiversEqual {
parent.Routes = append(parent.Routes[:key], parent.Routes[key+1:]...)
return true
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 {
policyExist(notificationPolicy, newNotificationPolicy)
return deleteInPlace(&parent.Routes[key], newNotificationPolicy)
}
}
return false
Expand All @@ -85,16 +109,14 @@ func deleteInPlace(parent *alerting.NotificationPolicy, newNotificationPolicy al
// DeleteNestedPolicy Delete Nested Policy from Notification Policy Tree
func (c *Client) DeleteNestedPolicy(newNotificationPolicy alerting.NotificationPolicy) error {
notificationPolicyTreeResponse, _, err := c.GetNotificationPolicy()
notificationPolicyTree := alerting.NotificationPolicy(notificationPolicyTreeResponse)

if err != nil {
return err
}
if policyExist(notificationPolicyTree, newNotificationPolicy) {
deleteInPlace(&notificationPolicyTree, newNotificationPolicy)
} else {
return fmt.Errorf("policy not found")
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
Expand Down
Loading
Loading