|
| 1 | +# Grafana |
| 2 | + |
| 3 | +> [!NOTE] |
| 4 | +> Contrary to other clients, you will find Grafana client in a [separate package & go module](https://github.com/smartcontractkit/chainlink-testing-framework/tree/main/lib/grafana). |
| 5 | +
|
| 6 | +Grafana client encapsulate following functionalities: |
| 7 | +* Dashboard creation |
| 8 | +* Managing dashboard annotations (CRUD) |
| 9 | +* Checking alerts |
| 10 | + |
| 11 | +# New instance |
| 12 | +In order to create a new instance you will need: |
| 13 | +* URL |
| 14 | +* API token |
| 15 | + |
| 16 | +For example: |
| 17 | +```go |
| 18 | +url := "http://grafana.io" |
| 19 | +apiToken := "such-a-secret-1&11n" |
| 20 | +gc := NewGrafanaClient(url, apiToken) |
| 21 | +``` |
| 22 | + |
| 23 | +# Dashboard creation |
| 24 | +You can create a new dashboard defined in JSON with: |
| 25 | +```go |
| 26 | + |
| 27 | +//define your dashboard here |
| 28 | +dashboardJson := `` |
| 29 | + |
| 30 | +request := PostDashboardRequest { |
| 31 | + Dashboard: dashboardJson, |
| 32 | + FolderId: 5 // change to your folder id |
| 33 | +} |
| 34 | + |
| 35 | +dr, rawResponse, err := gc.PostDashboard(request) |
| 36 | +if err != nil { |
| 37 | + panic(err) |
| 38 | +} |
| 39 | + |
| 40 | +if rawResponse.StatusCode() != 200 { |
| 41 | + panic("response code wasn't 200, but " + rawResponse.StatusCode()) |
| 42 | +} |
| 43 | + |
| 44 | +fmt.Println("Dashboard slug is is " + *dr.Slug) |
| 45 | +``` |
| 46 | + |
| 47 | +# Posting annotations |
| 48 | +You can post annotations in a following way: |
| 49 | +```go |
| 50 | +annDetails := PostAnnotation { |
| 51 | + DashboardUID: "some-uid", |
| 52 | + Time: time.Now(), |
| 53 | + TimeEnd: time.Now().Add(1 * time.Second) |
| 54 | + Text: "my test annotation" |
| 55 | +} |
| 56 | + |
| 57 | +r, rawResponse, err := gc.PostAnnotation(annDetails) |
| 58 | +if rawResponse.StatusCode() != 200 { |
| 59 | + panic("response code wasn't 200, but " + rawResponse.StatusCode()) |
| 60 | +} |
| 61 | + |
| 62 | +fmt.Println("Created annotation with id: " + r.Id) |
| 63 | + |
| 64 | +``` |
| 65 | + |
| 66 | +# Checking alerts |
| 67 | +You can check alerts firing for a dashboard with UID: |
| 68 | +```go |
| 69 | +alerts, rawResponse, err := gc.AlertRulerClient.GetAlertsForDashboard("some-uid") |
| 70 | +if rawResponse.StatusCode() != 200 { |
| 71 | + panic("response code wasn't 200, but " + rawResponse.StatusCode()) |
| 72 | +} |
| 73 | + |
| 74 | +for name, value := range alerts { |
| 75 | + fmt.Println("Alert named " + name + "was triggered. Details: " + string(value)) |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +# Troubleshooting |
| 80 | +To enable debug mode for the underlaying HTTP client set `RESTY_DEBUG` environment variable to `true`. |
0 commit comments