-
Notifications
You must be signed in to change notification settings - Fork 159
feat(otel): add ate.workerpool.workers metric with e2e suite #492
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
Julian Gutierrez Oschmann (juli4n)
merged 5 commits into
agent-substrate:main
from
krisztianfekete:feat/metrics-ateattr
Jul 23, 2026
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ae9b3a0
feat(otel): add platform-metric label keys to the shared registry
krisztianfekete 77968c8
feat(otel): rework into worker-pool saturation metric with e2e suite
krisztianfekete 1823f3f
address review comments
krisztianfekete c259781
remove unused helper
krisztianfekete ef8a3c2
scope metrics assertions to ate.workerpool.workers in e2e tests
krisztianfekete 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,87 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/agent-substrate/substrate/internal/ateattr" | ||
| atev1alpha1 "github.com/agent-substrate/substrate/pkg/api/v1alpha1" | ||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| "go.opentelemetry.io/otel/metric" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| ) | ||
|
|
||
| const workerpoolWorkersMetric = "ate.workerpool.workers" | ||
|
|
||
| // RegisterWorkerCount wires the ate.workerpool.workers observable against workers | ||
| // (workercache.Cache.Workers in prod) and listPools (a WorkerPool lister's List, | ||
| // used to seed zero-valued series). Worker counts are spatially summable (over | ||
| // states = pool size, over pools = fleet), which is the UpDownCounter contract; a | ||
| // gauge would be wrong for a value meant to be summed. | ||
| func RegisterWorkerCount(meter metric.Meter, workers func() ([]*ateapipb.Worker, error), listPools func(labels.Selector) ([]*atev1alpha1.WorkerPool, error)) error { | ||
| counter, err := meter.Int64ObservableUpDownCounter( | ||
| workerpoolWorkersMetric, | ||
| metric.WithUnit("{worker}"), | ||
| metric.WithDescription("Number of workers by pool, worker state, and sandbox class."), | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("create %s updowncounter: %w", workerpoolWorkersMetric, err) | ||
| } | ||
|
|
||
| _, err = meter.RegisterCallback(func(_ context.Context, o metric.Observer) error { | ||
| ws, err := workers() | ||
| if err != nil { | ||
| // Worker cache unavailable (warmup/reconnect): skip the whole observation. | ||
| return nil | ||
| } | ||
| type key struct{ pool, state, class string } | ||
| tally := make(map[key]int64) | ||
| // Seed both states at 0 for every known pool so a saturated or empty pool | ||
| // reports 0, not an absent series that breaks idle==0 alerts. | ||
| if listPools != nil { | ||
| if pools, err := listPools(labels.Everything()); err == nil { | ||
| for _, p := range pools { | ||
| class := string(p.Spec.SandboxClass) | ||
| if class == "" { | ||
| class = string(atev1alpha1.SandboxClassGvisor) | ||
| } | ||
| tally[key{p.Name, ateattr.WorkerStateIdle, class}] = 0 | ||
| tally[key{p.Name, ateattr.WorkerStateAssigned, class}] = 0 | ||
| } | ||
| } | ||
| } | ||
| for _, w := range ws { | ||
| state := ateattr.WorkerStateIdle | ||
| if w.GetAssignment() != nil { | ||
| state = ateattr.WorkerStateAssigned | ||
| } | ||
| tally[key{w.GetWorkerPool(), state, w.GetSandboxClass()}]++ | ||
| } | ||
| for k, n := range tally { | ||
| o.ObserveInt64(counter, n, metric.WithAttributes( | ||
| ateattr.WorkerPoolNameKey.String(k.pool), | ||
| ateattr.WorkerStateKey.String(k.state), | ||
| ateattr.SandboxClassKey.String(k.class), | ||
| )) | ||
| } | ||
| return nil | ||
| }, counter) | ||
| if err != nil { | ||
| return fmt.Errorf("register %s callback: %w", workerpoolWorkersMetric, err) | ||
| } | ||
| return nil | ||
| } | ||
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,184 @@ | ||
| // Copyright 2026 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package controlapi | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/agent-substrate/substrate/internal/ateattr" | ||
| atev1alpha1 "github.com/agent-substrate/substrate/pkg/api/v1alpha1" | ||
| "github.com/agent-substrate/substrate/pkg/proto/ateapipb" | ||
| sdkmetric "go.opentelemetry.io/otel/sdk/metric" | ||
| "go.opentelemetry.io/otel/sdk/metric/metricdata" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/labels" | ||
| ) | ||
|
|
||
| // newWorkerCountReader registers ate.workerpool.workers against a local | ||
| // ManualReader-backed provider so tests stay parallel-safe and never touch the | ||
| // global meter provider. | ||
| func newWorkerCountReader(t *testing.T, workers func() ([]*ateapipb.Worker, error), listPools func(labels.Selector) ([]*atev1alpha1.WorkerPool, error)) *sdkmetric.ManualReader { | ||
| t.Helper() | ||
| reader := sdkmetric.NewManualReader() | ||
| mp := sdkmetric.NewMeterProvider(sdkmetric.WithReader(reader)) | ||
| if err := RegisterWorkerCount(mp.Meter("ateapi"), workers, listPools); err != nil { | ||
| t.Fatalf("RegisterWorkerCount: %v", err) | ||
| } | ||
| return reader | ||
| } | ||
|
|
||
| func noWorkers() ([]*ateapipb.Worker, error) { return nil, nil } | ||
|
|
||
| func noPools(labels.Selector) ([]*atev1alpha1.WorkerPool, error) { return nil, nil } | ||
|
|
||
| func collectMetric(t *testing.T, reader *sdkmetric.ManualReader, name string) (metricdata.Metrics, bool) { | ||
| t.Helper() | ||
| var rm metricdata.ResourceMetrics | ||
| if err := reader.Collect(context.Background(), &rm); err != nil { | ||
| t.Fatalf("collect: %v", err) | ||
| } | ||
| for _, sm := range rm.ScopeMetrics { | ||
| for _, m := range sm.Metrics { | ||
| if m.Name == name { | ||
| return m, true | ||
| } | ||
| } | ||
| } | ||
| return metricdata.Metrics{}, false | ||
| } | ||
|
|
||
| func mustMetric(t *testing.T, reader *sdkmetric.ManualReader, name string) metricdata.Metrics { | ||
| t.Helper() | ||
| m, ok := collectMetric(t, reader, name) | ||
| if !ok { | ||
| t.Fatalf("metric %q not collected", name) | ||
| } | ||
| return m | ||
| } | ||
|
|
||
| func worker(pool, class string, assigned bool) *ateapipb.Worker { | ||
| w := &ateapipb.Worker{WorkerPool: pool, SandboxClass: class} | ||
| if assigned { | ||
| w.Assignment = &ateapipb.Assignment{} | ||
| } | ||
| return w | ||
| } | ||
|
|
||
| func TestWorkerCountTally(t *testing.T) { | ||
| workers := func() ([]*ateapipb.Worker, error) { | ||
| return []*ateapipb.Worker{ | ||
| worker("pool-a", "gvisor", false), | ||
| worker("pool-a", "gvisor", false), | ||
| worker("pool-a", "gvisor", true), | ||
| worker("pool-b", "microvm", false), | ||
| }, nil | ||
| } | ||
| reader := newWorkerCountReader(t, workers, noPools) | ||
|
|
||
| m := mustMetric(t, reader, workerpoolWorkersMetric) | ||
| if m.Unit != "{worker}" { | ||
| t.Errorf("unit = %q, want {worker}", m.Unit) | ||
| } | ||
| sum, ok := m.Data.(metricdata.Sum[int64]) | ||
| if !ok { | ||
| t.Fatalf("data type = %T, want Sum[int64]", m.Data) | ||
| } | ||
| if sum.IsMonotonic { | ||
| t.Errorf("IsMonotonic = true, want false (updowncounter, not counter)") | ||
| } | ||
|
|
||
| type key struct{ pool, state, class string } | ||
| got := make(map[key]int64) | ||
| for _, dp := range sum.DataPoints { | ||
| pool, _ := dp.Attributes.Value(ateattr.WorkerPoolNameKey) | ||
| state, _ := dp.Attributes.Value(ateattr.WorkerStateKey) | ||
| class, _ := dp.Attributes.Value(ateattr.SandboxClassKey) | ||
| got[key{pool.AsString(), state.AsString(), class.AsString()}] = dp.Value | ||
| } | ||
| want := map[key]int64{ | ||
| {"pool-a", ateattr.WorkerStateIdle, "gvisor"}: 2, | ||
| {"pool-a", ateattr.WorkerStateAssigned, "gvisor"}: 1, | ||
| {"pool-b", ateattr.WorkerStateIdle, "microvm"}: 1, | ||
| } | ||
| if len(got) != len(want) { | ||
| t.Fatalf("got %d series, want %d: %v", len(got), len(want), got) | ||
| } | ||
| for k, v := range want { | ||
| if got[k] != v { | ||
| t.Errorf("series %v = %d, want %d", k, got[k], v) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // TestWorkerCountSkipsWhenCacheNotReady asserts the callback emits nothing while | ||
| // the cache is warming up, so we never publish misleading zero-valued points. | ||
| func TestWorkerCountSkipsWhenCacheNotReady(t *testing.T) { | ||
| notReady := func() ([]*ateapipb.Worker, error) { | ||
| return nil, context.DeadlineExceeded | ||
| } | ||
| reader := newWorkerCountReader(t, notReady, noPools) | ||
|
|
||
| if _, ok := collectMetric(t, reader, workerpoolWorkersMetric); ok { | ||
| t.Errorf("%s was collected, want no datapoints while cache not ready", workerpoolWorkersMetric) | ||
| } | ||
| } | ||
|
|
||
| func workerPool(name string, class atev1alpha1.SandboxClass) *atev1alpha1.WorkerPool { | ||
| return &atev1alpha1.WorkerPool{ | ||
| ObjectMeta: metav1.ObjectMeta{Name: name}, | ||
| Spec: atev1alpha1.WorkerPoolSpec{SandboxClass: class}, | ||
| } | ||
| } | ||
|
|
||
| // TestWorkerCountSeedsZeroForKnownPools covers the saturation cases: a pool whose | ||
| // only state has no workers, and a pool with no workers at all, both report 0 | ||
| // rather than an absent series. Empty pool class defaults to gvisor. | ||
| func TestWorkerCountSeedsZeroForKnownPools(t *testing.T) { | ||
| pools := func(labels.Selector) ([]*atev1alpha1.WorkerPool, error) { | ||
| return []*atev1alpha1.WorkerPool{ | ||
| workerPool("pool-a", ""), | ||
| workerPool("pool-c", atev1alpha1.SandboxClassMicroVM), | ||
| }, nil | ||
| } | ||
| workers := func() ([]*ateapipb.Worker, error) { | ||
| return []*ateapipb.Worker{worker("pool-a", "gvisor", true)}, nil | ||
| } | ||
| reader := newWorkerCountReader(t, workers, pools) | ||
|
|
||
| sum := mustMetric(t, reader, workerpoolWorkersMetric).Data.(metricdata.Sum[int64]) | ||
| type key struct{ pool, state, class string } | ||
| got := make(map[key]int64) | ||
| for _, dp := range sum.DataPoints { | ||
| pool, _ := dp.Attributes.Value(ateattr.WorkerPoolNameKey) | ||
| state, _ := dp.Attributes.Value(ateattr.WorkerStateKey) | ||
| class, _ := dp.Attributes.Value(ateattr.SandboxClassKey) | ||
| got[key{pool.AsString(), state.AsString(), class.AsString()}] = dp.Value | ||
| } | ||
| want := map[key]int64{ | ||
| {"pool-a", ateattr.WorkerStateIdle, "gvisor"}: 0, | ||
| {"pool-a", ateattr.WorkerStateAssigned, "gvisor"}: 1, | ||
| {"pool-c", ateattr.WorkerStateIdle, "microvm"}: 0, | ||
| {"pool-c", ateattr.WorkerStateAssigned, "microvm"}: 0, | ||
| } | ||
| if len(got) != len(want) { | ||
| t.Fatalf("got %d series, want %d: %v", len(got), len(want), got) | ||
| } | ||
| for k, v := range want { | ||
| if gv, ok := got[k]; !ok || gv != v { | ||
| t.Errorf("series %v = %d (present=%v), want %d", k, gv, ok, v) | ||
| } | ||
| } | ||
| } |
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
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.