|
1 | | -## Observability-lib |
| 1 | +# Observability-lib |
2 | 2 |
|
3 | | -Contains the observability library to build dashboards and alerts |
| 3 | +This library enables creating Grafana dashboards and alerts with go code. |
| 4 | + |
| 5 | +It provides abstractions to create grafana resources : |
| 6 | +- [Dashboards](https://grafana.com/docs/grafana/latest/dashboards/) |
| 7 | +- [Alerts](https://grafana.com/docs/grafana/latest/alerting/) |
| 8 | +- [Contact Points](https://grafana.com/docs/grafana/latest/alerting/fundamentals/notifications/contact-points/) |
| 9 | +- [Notification Policies](https://grafana.com/docs/grafana/latest/alerting/configure-notifications/create-notification-policy/) |
| 10 | +- [Notification Templates](https://grafana.com/docs/grafana/latest/alerting/configure-notifications/template-notifications/create-notification-templates/) |
| 11 | + |
| 12 | +## Folder Structure |
| 13 | + |
| 14 | +The observability-lib is structured as follows: |
| 15 | +```shell |
| 16 | +observability-lib/ |
| 17 | + api/ # Grafana HTTP API Client to interact with resources |
| 18 | + cmd/ # CLI to interact deploy or generateJSON from dashboards defined in folder below |
| 19 | + dashboards/ # Dashboards definitions |
| 20 | + grafana/ # grafana-foundations-sdk abstraction to manipulate grafana resources |
| 21 | +``` |
| 22 | + |
| 23 | +## Documentation |
| 24 | + |
| 25 | +Godoc generated documentation is available [here](https://pkg.go.dev/github.com/goplugin/plugin-common/observability-lib) |
| 26 | + |
| 27 | +## Quickstart |
| 28 | + |
| 29 | +### Creating a dashboard |
| 30 | + |
| 31 | +<details><summary>main.go</summary> |
| 32 | + |
| 33 | +```go |
| 34 | +package main |
| 35 | + |
| 36 | +import "github.com/goplugin/plugin-common/observability-lib/grafana" |
| 37 | + |
| 38 | +func main() { |
| 39 | + builder := grafana.NewBuilder(&grafana.BuilderOptions{ |
| 40 | + Name: "Dashboard Name", |
| 41 | + Tags: []string{"tags1", "tags2"}, |
| 42 | + Refresh: "30s", |
| 43 | + TimeFrom: "now-30m", |
| 44 | + TimeTo: "now", |
| 45 | + }) |
| 46 | + |
| 47 | + builder.AddVars(grafana.NewQueryVariable(&grafana.QueryVariableOptions{ |
| 48 | + VariableOption: &grafana.VariableOption{ |
| 49 | + Label: "Environment", |
| 50 | + Name: "env", |
| 51 | + }, |
| 52 | + Datasource: "Prometheus", |
| 53 | + Query: `label_values(up, env)`, |
| 54 | + })) |
| 55 | + |
| 56 | + builder.AddRow("Summary") |
| 57 | + |
| 58 | + builder.AddPanel(grafana.NewStatPanel(&grafana.StatPanelOptions{ |
| 59 | + PanelOptions: &grafana.PanelOptions{ |
| 60 | + Datasource: "Prometheus", |
| 61 | + Title: "Uptime", |
| 62 | + Description: "instance uptime", |
| 63 | + Span: 12, |
| 64 | + Height: 4, |
| 65 | + Decimals: 2, |
| 66 | + Unit: "s", |
| 67 | + Query: []grafana.Query{ |
| 68 | + { |
| 69 | + Expr: `uptime_seconds`, |
| 70 | + Legend: `{{ pod }}`, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + ColorMode: common.BigValueColorModeNone, |
| 75 | + TextMode: common.BigValueTextModeValueAndName, |
| 76 | + Orientation: common.VizOrientationHorizontal, |
| 77 | + })) |
| 78 | + |
| 79 | + db, err := builder.Build() |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + json, err := db.GenerateJSON() |
| 84 | + if err != nil { |
| 85 | + return nil, err |
| 86 | + } |
| 87 | + fmt.Println(string(json)) |
| 88 | +} |
| 89 | +``` |
| 90 | +</details> |
| 91 | + |
| 92 | +More advanced examples can be found in the [dashboards](./dashboards) folder : |
| 93 | +- [DON OCR](./dashboards/atlas-don/component.go) |
| 94 | +- [Capabilities](./dashboards/capabilities/component.go) |
| 95 | +- [Node General](./dashboards/core-node/component.go) |
| 96 | +- [Node Components](./dashboards/core-node-components/component.go) |
| 97 | +- [Kubernetes Resources](./dashboards/k8s-resources/component.go) |
| 98 | +- [NOP OCR Health](./dashboards/nop-ocr/component.go) |
| 99 | + |
| 100 | +## Cmd Usage |
| 101 | + |
| 102 | +The CLI can be used to : |
| 103 | +- Deploy dashboards and alerts to grafana |
| 104 | +- Generate JSON from dashboards defined in the `dashboards` folder |
| 105 | + |
| 106 | +`func NewDashboard(props *Props)` in each [dashboards](./dashboards) packages is called from [cmd](./cmd/builder.go) to deploy or generate JSON from the dashboard. |
| 107 | + |
| 108 | +Example to deploy a dashboard to grafana instance using URL and token: |
| 109 | +```shell |
| 110 | +make build |
| 111 | +./observability-lib deploy \ |
| 112 | + --dashboard-name DashboardName \ |
| 113 | + --dashboard-folder FolderName \ |
| 114 | + --grafana-url $GRAFANA_URL \ |
| 115 | + --grafana-token $GRAFANA_TOKEN \ |
| 116 | + --type core-node \ |
| 117 | + --platform kubernetes \ |
| 118 | + --metrics-datasource Prometheus |
| 119 | +``` |
| 120 | +To see how to get a grafana token you can check this [page](https://grafana.com/docs/grafana/latest/administration/service-accounts/) |
| 121 | + |
| 122 | +Example to generate JSON from a dashboard defined in the `dashboards` folder: |
| 123 | +```shell |
| 124 | +make build |
| 125 | +./observability-lib generate \ |
| 126 | + --dashboard-name DashboardName \ |
| 127 | + --type core-node-components \ |
| 128 | + --platform kubernetes |
| 129 | +``` |
| 130 | + |
| 131 | +## Makefile Usage |
4 | 132 |
|
5 | | -## Usage |
6 | 133 |
|
7 | 134 | To build the observability library, run the following command: |
8 | 135 |
|
|
0 commit comments