Repository navigation
fix(metrics): show the latest complete data point with --latest #165
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鈥檒l occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7b5165f
fix(metrics): show the latest complete data point with --latest
pjcdawkins f57036a
fix(metrics): only compare the last 3 points for --latest
pjcdawkins 22a079c
fix(metrics): bound --latest lookback by time instead of point count
pjcdawkins 0997ba4
fix(metrics): judge --latest points by start time relative to now
pjcdawkins f531f96
test(metrics): avoid minute-boundary flake in --latest test
pjcdawkins 84d669e
test(metrics): generate --latest test data at request time
pjcdawkins 3e627fd
test(metrics): keep settled-point case within the mocked range
pjcdawkins 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| package tests | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/upsun/cli/pkg/mockapi" | ||
| ) | ||
|
|
||
| func TestMetricsLatest(t *testing.T) { | ||
| authServer := mockapi.NewAuthServer(t) | ||
| defer authServer.Close() | ||
|
|
||
| apiHandler := mockapi.NewHandler(t) | ||
| apiServer := httptest.NewServer(apiHandler) | ||
| defer apiServer.Close() | ||
|
|
||
| projectID := mockapi.ProjectID() | ||
| apiHandler.SetProjects([]*mockapi.Project{{ | ||
| ID: projectID, | ||
| Links: mockapi.MakeHALLinks("self=/projects/"+projectID, | ||
| "environments=/projects/"+projectID+"/environments"), | ||
| DefaultBranch: "main", | ||
| }}) | ||
|
|
||
| envPath := "/projects/" + projectID + "/environments/main" | ||
| main := makeEnv(projectID, "main", "production", "active", nil) | ||
| main.Links["#observability-pipeline"] = mockapi.HALLink{HREF: apiServer.URL + envPath + "/observability"} | ||
| main.SetCurrentDeployment(&mockapi.Deployment{ | ||
| WebApps: map[string]mockapi.App{"app": {Name: "app", Type: "golang:1.23", Size: "AUTO"}}, | ||
| Services: map[string]mockapi.App{"db": {Name: "db", Type: "mariadb:11.4", Size: "AUTO"}}, | ||
| Workers: map[string]mockapi.Worker{}, | ||
| Routes: map[string]any{}, | ||
| Links: mockapi.MakeHALLinks("self=" + envPath + "/deployment/current"), | ||
| }) | ||
| apiHandler.SetEnvironments([]*mockapi.Environment{main}) | ||
|
|
||
| cpu := func(used, limit float64) map[string]any { | ||
| return map[string]any{"cpu_used": map[string]any{"avg": used}, "cpu_limit": map[string]any{"max": limit}} | ||
| } | ||
| // Timestamps are relative to the request time, as recent points are treated differently. | ||
| var ( | ||
| mu sync.Mutex | ||
| now time.Time | ||
| data func() []map[string]any | ||
| ) | ||
| ts := func(minutesAgo int) time.Time { return now.Add(-time.Duration(minutesAgo) * time.Minute) } | ||
| point := func(minutesAgo int, services map[string]any) map[string]any { | ||
| p := map[string]any{"timestamp": ts(minutesAgo).Unix()} | ||
| if services != nil { | ||
| p["services"] = services | ||
| } | ||
| return p | ||
| } | ||
| row := func(minutesAgo int, rest string) string { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| return ts(minutesAgo).Format("2006-01-02T15:04:05+00:00") + "\t" + rest | ||
| } | ||
|
|
||
| // Modeled on the API: recent points lack services that have not reported yet, | ||
| // and the in-progress point has no "services" key at all. | ||
| data = func() []map[string]any { | ||
| return []map[string]any{ | ||
| point(3, map[string]any{"app": cpu(0.1, 1), "db": cpu(0.1, 1), "router": cpu(0.01, 0.1)}), | ||
| point(2, map[string]any{"app": cpu(0.2, 1), "db": cpu(0.3, 1), "router": cpu(0.02, 0.1)}), | ||
| point(1, map[string]any{"db": cpu(0.4, 1)}), | ||
| point(0, nil), | ||
| } | ||
| } | ||
| setData := func(d func() []map[string]any) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| data = d | ||
| } | ||
| apiHandler.Get(envPath+"/observability/resources/overview", func(w http.ResponseWriter, _ *http.Request) { | ||
| mu.Lock() | ||
| defer mu.Unlock() | ||
| now = time.Now().UTC() | ||
| _ = json.NewEncoder(w).Encode(map[string]any{ | ||
| "_grain": 60, | ||
| "_from": ts(10).Unix(), | ||
| "_to": now.Unix(), | ||
| "data": data(), | ||
| }) | ||
| }) | ||
|
|
||
| f := newCommandFactory(t, apiServer.URL, authServer.URL) | ||
| latest := func() string { | ||
| return f.Run("metrics:cpu", "-p", projectID, "-e", "main", "--latest", "--format", "tsv", "--no-header") | ||
| } | ||
|
|
||
| out := latest() | ||
| assertTrimmed(t, row(2, "app\t0.2\t1\t20.0%")+"\n"+ | ||
| row(2, "db\t0.3\t1\t30.0%")+"\n"+ | ||
| row(2, "router\t0.02\t0.1\t20.0%"), out) | ||
|
|
||
| out = f.Run("metrics:cpu", "-p", projectID, "-e", "main", "--format", "tsv") | ||
| assert.Contains(t, out, row(1, "db\t0.4\t1\t40.0%")) | ||
|
|
||
| // A service that stopped reporting before the recent points is ignored. | ||
| setData(func() []map[string]any { | ||
| return []map[string]any{ | ||
| point(4, map[string]any{"app": cpu(0.1, 1), "db": cpu(0.1, 1)}), | ||
| point(3, map[string]any{"app": cpu(0.2, 1)}), | ||
| point(2, map[string]any{"app": cpu(0.3, 1)}), | ||
| point(1, map[string]any{"app": cpu(0.4, 1)}), | ||
| } | ||
| }) | ||
| out = latest() | ||
| assertTrimmed(t, row(1, "app\t0.4\t1\t40.0%"), out) | ||
|
|
||
| // Older points are not skipped. | ||
| setData(func() []map[string]any { | ||
| return []map[string]any{ | ||
| point(4, map[string]any{"app": cpu(0.1, 1), "db": cpu(0.1, 1)}), | ||
| point(3, map[string]any{"app": cpu(0.2, 1)}), | ||
| } | ||
| }) | ||
| out = latest() | ||
| assertTrimmed(t, row(3, "app\t0.2\t1\t20.0%"), out) | ||
| } |
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
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.