You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
fix(serverless-init): attach per-instance tag to MicroVM enhanced usage metric
Codex flagged on PR #53093 (review comment
#53093 (comment))
that MicroVM's enhanced usage metric (aws.lambda.microvm.instance) is
emitted with the same static tag set for every MicroVM booted from a given
image. MicroVM.GetEnhancedMetricTags returns Usage tags at startup, before
the MicroVM's instance ID is known (it only becomes available once the /run
lifecycle hook fires), and nothing ever adds it afterward. Under normal
autoscaling, with multiple concurrent MicroVMs from the same image, their
usage samples become indistinguishable from each other.
Rather than mutating shared, periodically-read state on
ServerlessMetricAgent (which would need a lock/atomic and broke go vet's
copylocks check, since CloudService.Shutdown takes ServerlessMetricAgent by
value across every cloud service), this attaches the tag at the point of
emission instead:
- lifecycle.Server already tracks the instance ID race-free
(instanceID *atomic.String, captured in handleRun). Added InstanceID(),
a nil-safe accessor, so callers outside the lifecycle package can read it.
- MicroVM.CurrentUsageMetricTags() turns that into an "instance:<id>" tag,
or nil before /run has fired.
- enhanced-metrics.Collector gained an optional usageMetricTagsFunc,
invoked on every collection tick and passed through the existing
AddEnhancedUsageMetric(..., extraTags ...string) parameter — no new
shared mutable state required.
- main.go duck-types cloudService against a local usageMetricTagProvider
interface to wire this hook; every other cloud service doesn't implement
it, so usageMetricTagsFunc stays nil and their usage metrics are
unaffected.
This keeps the change scoped to MicroVM and the (nil-safe, additive)
collector plumbing — no other cloud service, ServerlessMetricAgent, or the
Shutdown interface needed to change.
Unit tests:
dda inv test --targets=./cmd/serverless-init/...
Amended to address Copilot review feedback on this PR:
- `cloudservice/microvm_test.go`: the new
TestMicroVM_CurrentUsageMetricTags_AfterRun_ReturnsInstanceTag dialed
l.Addr().String() directly, which resolves to the unspecified address
(e.g. [::]:port) since the lifecycle server binds with no host — not a
guaranteed-valid dial target on every platform. Now dials
127.0.0.1:<port> explicitly. Also reworded the leftover launchPath/
"/launch" naming to runPath/"/run" to match the route under test.
- `cloudservice/microvm.go`: CurrentUsageMetricTags' doc comment cited
exact file:line locations in appservice.go/cloudrun.go, which drift as
those files change. Now references the method names
(AppService.GetEnhancedMetricTags, CloudRun.GetEnhancedMetricTags)
instead.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
0 commit comments