-
Notifications
You must be signed in to change notification settings - Fork 29
Optimize DeployToGrafana #2359
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
Merged
Merged
Optimize DeployToGrafana #2359
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,187 @@ | ||
| package grafana_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/grafana/grafana-foundation-sdk/go/alerting" | ||
| "github.com/grafana/grafana-foundation-sdk/go/dashboard" | ||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "github.com/smartcontractkit/chainlink-common/observability-lib/grafana" | ||
| ) | ||
|
|
||
| // fakeGrafana stubs the Grafana endpoints used by DeployToGrafana and records | ||
| // how the alert-rule endpoints are called. | ||
| type fakeGrafana struct { | ||
| mu sync.Mutex | ||
|
|
||
| folderGets int | ||
| alertRuleGets int | ||
| alertRulePosts int | ||
| inFlightPosts int | ||
| maxInFlight int | ||
| } | ||
|
|
||
| func (f *fakeGrafana) handler(t *testing.T) http.Handler { | ||
| mux := http.NewServeMux() | ||
|
|
||
| mux.HandleFunc("GET /api/folders", func(w http.ResponseWriter, _ *http.Request) { | ||
| f.mu.Lock() | ||
| f.folderGets++ | ||
| f.mu.Unlock() | ||
| writeJSON(t, w, []map[string]any{{"id": 1, "uid": "folder-uid", "title": "Folder"}}) | ||
| }) | ||
| mux.HandleFunc("GET /api/search", func(w http.ResponseWriter, _ *http.Request) { | ||
| writeJSON(t, w, []map[string]any{}) | ||
| }) | ||
| mux.HandleFunc("POST /api/dashboards/db", func(w http.ResponseWriter, _ *http.Request) { | ||
| writeJSON(t, w, map[string]any{"uid": "dash-uid", "url": "/d/dash-uid"}) | ||
| }) | ||
| mux.HandleFunc("GET /api/v1/provisioning/alert-rules", func(w http.ResponseWriter, _ *http.Request) { | ||
| f.mu.Lock() | ||
| f.alertRuleGets++ | ||
| f.mu.Unlock() | ||
| writeJSON(t, w, []map[string]any{}) | ||
| }) | ||
| mux.HandleFunc("POST /api/v1/provisioning/alert-rules", func(w http.ResponseWriter, _ *http.Request) { | ||
| f.mu.Lock() | ||
| f.alertRulePosts++ | ||
| f.inFlightPosts++ | ||
| if f.inFlightPosts > f.maxInFlight { | ||
| f.maxInFlight = f.inFlightPosts | ||
| } | ||
| f.mu.Unlock() | ||
|
|
||
| // Hold the request so overlapping POSTs are observable. | ||
| time.Sleep(50 * time.Millisecond) | ||
|
|
||
| f.mu.Lock() | ||
| f.inFlightPosts-- | ||
| f.mu.Unlock() | ||
|
|
||
| w.WriteHeader(http.StatusCreated) | ||
| writeJSON(t, w, map[string]any{}) | ||
| }) | ||
| mux.HandleFunc("PUT /api/v1/provisioning/folder/{folderUID}/rule-groups/{group}", func(w http.ResponseWriter, _ *http.Request) { | ||
| writeJSON(t, w, map[string]any{}) | ||
| }) | ||
|
|
||
| return mux | ||
| } | ||
|
|
||
| func writeJSON(t *testing.T, w http.ResponseWriter, v any) { | ||
| t.Helper() | ||
| w.Header().Set("Content-Type", "application/json") | ||
| require.NoError(t, json.NewEncoder(w).Encode(v)) | ||
| } | ||
|
|
||
| func TestDeployToGrafanaFetchesRulesOnceAndWritesConcurrently(t *testing.T) { | ||
| fake := &fakeGrafana{} | ||
| server := httptest.NewServer(fake.handler(t)) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| const numAlerts = 20 | ||
|
|
||
| title := "Test Dashboard" | ||
| o := &grafana.Observability{ | ||
| Dashboard: &dashboard.Dashboard{ //nolint:staticcheck | ||
| Title: &title, | ||
| }, | ||
| Alerts: make([]alerting.Rule, numAlerts), | ||
| AlertGroups: nil, | ||
| } | ||
| for i := range o.Alerts { | ||
| o.Alerts[i] = alerting.Rule{ | ||
| Title: fmt.Sprintf("alert-%d", i), | ||
| RuleGroup: "group", | ||
| Condition: "A", | ||
| Data: []alerting.Query{}, | ||
| } | ||
| } | ||
|
|
||
| // Alert groups exercise the folder+group lookup in getAlertRules; with the | ||
| // dashboard UID lookup it must still fetch the full rule list only once. | ||
| group, err := grafana.NewAlertGroup(&grafana.AlertGroupOptions{Title: "group", Interval: 60}).Build() | ||
| require.NoError(t, err) | ||
| o.AlertGroups = []alerting.RuleGroup{group} | ||
|
|
||
| err = o.DeployToGrafana(&grafana.DeployOptions{ | ||
| GrafanaURL: server.URL, | ||
| GrafanaToken: "test-token", | ||
| FolderName: "Folder", | ||
| EnableAlerts: true, | ||
| RuleGroupFromDashboard: true, | ||
| }) | ||
| require.NoError(t, err) | ||
|
|
||
| require.Equal(t, 1, fake.alertRuleGets, "full alert rule list must be fetched exactly once per deploy") | ||
| require.Equal(t, numAlerts, fake.alertRulePosts) | ||
| require.Greater(t, fake.maxInFlight, 1, "alert rule writes should overlap") | ||
| require.LessOrEqual(t, fake.maxInFlight, 8, "alert rule writes must respect the default concurrency bound") | ||
| } | ||
|
|
||
| // deployOne deploys a single-dashboard observability with the given options, | ||
| // failing the test on any error. | ||
| func deployOne(t *testing.T, title string, opts *grafana.DeployOptions) { | ||
| t.Helper() | ||
| o := &grafana.Observability{ | ||
| Dashboard: &dashboard.Dashboard{Title: &title}, //nolint:staticcheck | ||
| Alerts: []alerting.Rule{{ | ||
| Title: "alert-" + title, | ||
| RuleGroup: "group", | ||
| Condition: "A", | ||
| Data: []alerting.Query{}, | ||
| }}, | ||
| } | ||
| group, err := grafana.NewAlertGroup(&grafana.AlertGroupOptions{Title: "group", Interval: 60}).Build() | ||
| require.NoError(t, err) | ||
| o.AlertGroups = []alerting.RuleGroup{group} | ||
|
|
||
| require.NoError(t, o.DeployToGrafana(opts)) | ||
| } | ||
|
|
||
| func TestDeployToGrafanaSharesCacheAcrossDeploys(t *testing.T) { | ||
| fake := &fakeGrafana{} | ||
| server := httptest.NewServer(fake.handler(t)) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| cache := &grafana.DeployCache{} | ||
| for _, title := range []string{"Dashboard A", "Dashboard B"} { | ||
| deployOne(t, title, &grafana.DeployOptions{ | ||
| GrafanaURL: server.URL, | ||
| GrafanaToken: "test-token", | ||
| FolderName: "Folder", | ||
| EnableAlerts: true, | ||
| RuleGroupFromDashboard: true, | ||
| Cache: cache, | ||
| }) | ||
| } | ||
|
|
||
| require.Equal(t, 1, fake.folderGets, "shared cache must resolve the folder once across deploys") | ||
| require.Equal(t, 1, fake.alertRuleGets, "shared cache must fetch the full alert rule list once across deploys") | ||
| } | ||
|
|
||
| func TestDeployToGrafanaWithoutCacheFetchesPerDeploy(t *testing.T) { | ||
| fake := &fakeGrafana{} | ||
| server := httptest.NewServer(fake.handler(t)) | ||
| t.Cleanup(server.Close) | ||
|
|
||
| for _, title := range []string{"Dashboard A", "Dashboard B"} { | ||
| deployOne(t, title, &grafana.DeployOptions{ | ||
| GrafanaURL: server.URL, | ||
| GrafanaToken: "test-token", | ||
| FolderName: "Folder", | ||
| EnableAlerts: true, | ||
| RuleGroupFromDashboard: true, | ||
| }) | ||
| } | ||
|
|
||
| require.Equal(t, 2, fake.folderGets, "without a cache each deploy resolves the folder") | ||
| require.Equal(t, 2, fake.alertRuleGets, "without a cache each deploy fetches the full alert rule list") | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.