-
Notifications
You must be signed in to change notification settings - Fork 459
feat(metrics): distinguish joined from regular requests (ENG-4072) #2699
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
jakubno
merged 12 commits into
main
from
distinguish-between-joined-and-regular-requests-eng-4072
May 18, 2026
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
da96204
feat(metrics): distinguish joined from regular requests (ENG-4072)
jakubno c80ba1c
refactor(metrics): rename omit* to joined* and pin attribute to serve…
jakubno 353173a
chore: auto-commit generated changes
github-actions[bot] 04e368b
refactor(joined): extract to shared otel package, install in tracing too
jakubno a431c30
chore: auto-commit generated changes
github-actions[bot] aa03049
feat(joined): set request.joined on every server span via tracing mid…
jakubno 43dd9b3
chore(joined): trim verbose comments and drop covered test
jakubno a4bc153
fix(joined): tag WaitForStateChange entry in both storage backends
jakubno af2bb4b
fix(joined): only mark when actually blocking on another transition
jakubno 117b140
fix(joined): only mark the CreateSandbox reservation joiner
jakubno bf1d13d
feat(joined): mark concurrent same-state transitions as joiners
jakubno 59c7630
fix(joined): mark memory backend joiners before waiting
jakubno 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package joined | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync/atomic" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| // AttributeKey is the dotted-lowercase key used for both the histogram | ||
| // attribute and the span attribute. | ||
| const AttributeKey = "request.joined" | ||
|
|
||
| type holder struct { | ||
| joined atomic.Bool | ||
|
|
||
| serverSpan trace.Span | ||
| } | ||
|
|
||
| type holderKey struct{} | ||
|
|
||
| // WithHolder installs a fresh holder on ctx if one is not already present. | ||
| // | ||
| // Call WithHolder *after* the server span has been started so the holder | ||
| // captures the correct span for Mark to write attributes onto. | ||
| func WithHolder(ctx context.Context) context.Context { | ||
| if _, ok := ctx.Value(holderKey{}).(*holder); ok { | ||
| return ctx | ||
| } | ||
|
|
||
| return context.WithValue(ctx, holderKey{}, &holder{ | ||
| serverSpan: trace.SpanFromContext(ctx), | ||
| }) | ||
| } | ||
|
|
||
| // Mark marks the current request as a joiner. First-write-wins | ||
| func Mark(ctx context.Context) { | ||
| h, ok := ctx.Value(holderKey{}).(*holder) | ||
| if !ok { | ||
| return | ||
| } | ||
|
|
||
| if h.joined.CompareAndSwap(false, true) { | ||
| h.serverSpan.SetAttributes( | ||
| attribute.Bool(AttributeKey, true), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| func Attribute(ctx context.Context) attribute.KeyValue { | ||
| h, ok := ctx.Value(holderKey{}).(*holder) | ||
| if !ok { | ||
| return attribute.Bool(AttributeKey, false) | ||
| } | ||
|
|
||
| return attribute.Bool(AttributeKey, h.joined.Load()) | ||
| } | ||
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,79 @@ | ||
| package joined_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/shared/pkg/middleware/otel/joined" | ||
| ) | ||
|
|
||
| // Mark must be safe even when the context carries no holder. | ||
| func TestMark_NoHolder_Noop(t *testing.T) { | ||
| t.Parallel() | ||
| joined.Mark(context.Background()) | ||
| } | ||
|
|
||
| // Attribute must return request.joined=false when no holder is on ctx. | ||
| func TestAttribute_NoHolder_ReturnsFalse(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| a := joined.Attribute(context.Background()) | ||
| assert.Equal(t, joined.AttributeKey, string(a.Key)) | ||
| assert.False(t, a.Value.AsBool()) | ||
| } | ||
|
|
||
| // Attribute must return request.joined=false on a freshly installed holder | ||
| // before Mark has been called. | ||
| func TestAttribute_FreshHolder_ReturnsFalse(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := joined.WithHolder(context.Background()) | ||
|
|
||
| a := joined.Attribute(ctx) | ||
| assert.False(t, a.Value.AsBool()) | ||
| } | ||
|
|
||
| // Mark must flip Attribute to true on the same ctx. | ||
| func TestMark_FlipsAttributeToTrue(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := joined.WithHolder(context.Background()) | ||
| joined.Mark(ctx) | ||
|
|
||
| a := joined.Attribute(ctx) | ||
| assert.True(t, a.Value.AsBool()) | ||
| } | ||
|
|
||
| // WithHolder must be idempotent: calling it twice returns a ctx that shares | ||
| // the same underlying holder (Mark on the first ctx is visible from the | ||
| // second). | ||
| func TestWithHolder_Idempotent(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx1 := joined.WithHolder(context.Background()) | ||
| ctx2 := joined.WithHolder(ctx1) | ||
|
|
||
| joined.Mark(ctx1) | ||
|
|
||
| a := joined.Attribute(ctx2) | ||
| assert.True(t, a.Value.AsBool(), "second WithHolder must reuse the first holder") | ||
| } | ||
|
|
||
| // Mark must be safe when called from a goroutine descended from the | ||
| // request context. | ||
| func TestMark_DescendantGoroutine(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx := joined.WithHolder(context.Background()) | ||
| done := make(chan struct{}) | ||
| go func() { | ||
| joined.Mark(ctx) | ||
| close(done) | ||
| }() | ||
| <-done | ||
|
|
||
| a := joined.Attribute(ctx) | ||
| assert.True(t, a.Value.AsBool()) | ||
| } |
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.