-
Notifications
You must be signed in to change notification settings - Fork 9
feat: reconcile-to-request trace correlation via span links #207
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
Open
JuanmaBM
wants to merge
2
commits into
openshift-online:main
Choose a base branch
from
JuanmaBM:feat/reconcile-trace-correlation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 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
44 changes: 36 additions & 8 deletions
44
components/api-server/pkg/api/grpc/hypershell/v1/common.pb.go
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| // TraceMeta embeds alongside api.Meta to persist the originating W3C Trace | ||
| // Context on every resource. The json:"-" tag keeps these fields out of REST | ||
| // API responses (RTC-05). The columns are nullable so pre-existing rows and | ||
| // resources created with telemetry disabled have NULL trace context. | ||
| type TraceMeta struct { | ||
| Traceparent *string `json:"-" gorm:"column:traceparent"` | ||
| Tracestate *string `json:"-" gorm:"column:tracestate"` | ||
| } | ||
|
|
||
| // CaptureTraceContext extracts the active span's W3C traceparent and | ||
| // tracestate from ctx and stores them. When no valid span is active (OTel | ||
| // disabled or no sampled span), the fields are left nil. | ||
| func (t *TraceMeta) CaptureTraceContext(ctx context.Context) { | ||
| sc := trace.SpanFromContext(ctx).SpanContext() | ||
| if !sc.IsValid() { | ||
| return | ||
| } | ||
| tp := fmt.Sprintf("00-%s-%s-%s", sc.TraceID(), sc.SpanID(), sc.TraceFlags()) | ||
| t.Traceparent = &tp | ||
| if ts := sc.TraceState().String(); ts != "" { | ||
| t.Tracestate = &ts | ||
| } | ||
| } | ||
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,138 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| func TestCaptureTraceContext(t *testing.T) { | ||
| traceID, _ := trace.TraceIDFromHex("0af7651916cd43dd8448eb211c80319c") | ||
| spanID, _ := trace.SpanIDFromHex("b7ad6b7169203331") | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| ctx context.Context | ||
| wantTraceparent *string | ||
| wantTracestate *string | ||
| }{ | ||
| { | ||
| name: "no span in context leaves fields nil", | ||
| ctx: context.Background(), | ||
| wantTraceparent: nil, | ||
| wantTracestate: nil, | ||
| }, | ||
| { | ||
| name: "invalid span context leaves fields nil", | ||
| ctx: trace.ContextWithSpanContext(context.Background(), trace.SpanContext{}), | ||
| wantTraceparent: nil, | ||
| wantTracestate: nil, | ||
| }, | ||
| { | ||
| name: "valid span context sets traceparent", | ||
| ctx: trace.ContextWithSpanContext(context.Background(), | ||
| trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| Remote: true, | ||
| }), | ||
| ), | ||
| wantTraceparent: strPtr("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"), | ||
| wantTracestate: nil, | ||
| }, | ||
| { | ||
| name: "valid span context with tracestate sets both fields", | ||
| ctx: trace.ContextWithSpanContext(context.Background(), | ||
| trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| TraceState: mustTraceState(t, "vendor=opaque"), | ||
| Remote: true, | ||
| }), | ||
| ), | ||
| wantTraceparent: strPtr("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-01"), | ||
| wantTracestate: strPtr("vendor=opaque"), | ||
| }, | ||
| { | ||
| name: "unsampled span still captures traceparent with flags 00", | ||
| ctx: trace.ContextWithSpanContext(context.Background(), | ||
| trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: 0, | ||
| Remote: true, | ||
| }), | ||
| ), | ||
| wantTraceparent: strPtr("00-0af7651916cd43dd8448eb211c80319c-b7ad6b7169203331-00"), | ||
| wantTracestate: nil, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range tests { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var tm TraceMeta | ||
| tm.CaptureTraceContext(tc.ctx) | ||
|
|
||
| if !strPtrEq(tm.Traceparent, tc.wantTraceparent) { | ||
| t.Errorf("Traceparent = %s, want %s", strPtrFmt(tm.Traceparent), strPtrFmt(tc.wantTraceparent)) | ||
| } | ||
| if !strPtrEq(tm.Tracestate, tc.wantTracestate) { | ||
| t.Errorf("Tracestate = %s, want %s", strPtrFmt(tm.Tracestate), strPtrFmt(tc.wantTracestate)) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCaptureTraceContextIsIdempotent(t *testing.T) { | ||
| traceID, _ := trace.TraceIDFromHex("0af7651916cd43dd8448eb211c80319c") | ||
| spanID, _ := trace.SpanIDFromHex("b7ad6b7169203331") | ||
|
|
||
| ctx := trace.ContextWithSpanContext(context.Background(), | ||
| trace.NewSpanContext(trace.SpanContextConfig{ | ||
| TraceID: traceID, | ||
| SpanID: spanID, | ||
| TraceFlags: trace.FlagsSampled, | ||
| Remote: true, | ||
| }), | ||
| ) | ||
|
|
||
| var tm TraceMeta | ||
| tm.CaptureTraceContext(ctx) | ||
| first := *tm.Traceparent | ||
|
|
||
| tm.CaptureTraceContext(ctx) | ||
| if *tm.Traceparent != first { | ||
| t.Errorf("second call changed Traceparent: got %s, want %s", *tm.Traceparent, first) | ||
| } | ||
| } | ||
|
|
||
| func mustTraceState(t *testing.T, s string) trace.TraceState { | ||
| t.Helper() | ||
| ts, err := trace.ParseTraceState(s) | ||
| if err != nil { | ||
| t.Fatalf("ParseTraceState(%q): %v", s, err) | ||
| } | ||
| return ts | ||
| } | ||
|
|
||
| func strPtr(s string) *string { return &s } | ||
|
|
||
| func strPtrEq(a, b *string) bool { | ||
| if a == nil && b == nil { | ||
| return true | ||
| } | ||
| if a == nil || b == nil { | ||
| return false | ||
| } | ||
| return *a == *b | ||
| } | ||
|
|
||
| func strPtrFmt(s *string) string { | ||
| if s == nil { | ||
| return "<nil>" | ||
| } | ||
| return *s | ||
| } |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On
Replace, when no valid span is active (telemetry disabled), these pointers stay nil. Depending on whether the DAO'sReplaceuses gormSave(writes nil -> NULL) vsUpdateswith a struct (skips zero values), an update performed with telemetry off could null out a previously-storedtraceparent. RTC-01's overwrite-on-update arguably permits this, but please confirm the intended behavior since it is a silent change to existing data.