Repository navigation
feat(api): retain error spans and sample remaining traces at 50% #1034
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
AchoArnold
wants to merge
8
commits into
main
Choose a base branch
from
feat/backend-error-aware-trace-sampling
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
8 commits
Select commit
Hold shift + click to select a range
6c1881c
feat(api): retain error spans and sample remaining traces at 50%
AchoArnold b0f0f22
fix(api): address trace sampling review feedback
AchoArnold 46a404c
refactor(api): mark spans for sampling without copying logs
AchoArnold 1d2aaf9
fix(api): preserve GORM callers and honor severity variants
AchoArnold f5ce3d2
refactor(api): restore original GORM logging
AchoArnold 736a098
refactor(api): use existing logger for span retention
AchoArnold 6415451
fix(api): align metric exemplars with exported traces
AchoArnold 0467d46
fix(api): hash full trace IDs before sampling
AchoArnold 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
Some comments aren't visible on the classic Files Changed page.
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
47 changes: 47 additions & 0 deletions
47
api/pkg/repositories/gorm_heartbeat_monitor_repository_test.go
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,47 @@ | ||
| package repositories | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/NdoleStudio/httpsms/pkg/telemetry" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/require" | ||
| "gorm.io/driver/postgres" | ||
| "gorm.io/gorm" | ||
| ) | ||
|
|
||
| func TestHeartbeatMonitorUpdatesCarryContext(t *testing.T) { | ||
| updates := map[string]func(HeartbeatMonitorRepository, context.Context) error{ | ||
| "phone online": func(repository HeartbeatMonitorRepository, ctx context.Context) error { | ||
| return repository.UpdatePhoneOnline(ctx, "user-id", uuid.New(), true) | ||
| }, | ||
| "queue ID": func(repository HeartbeatMonitorRepository, ctx context.Context) error { | ||
| return repository.UpdateQueueID(ctx, uuid.New(), "queue-id") | ||
| }, | ||
| } | ||
| for name, update := range updates { | ||
| t.Run(name, func(t *testing.T) { | ||
| db, err := gorm.Open(postgres.New(postgres.Config{Conn: &messageThreadTestConnPool{}}), &gorm.Config{ | ||
| DisableAutomaticPing: true, | ||
| SkipDefaultTransaction: true, | ||
| DryRun: true, | ||
| }) | ||
| require.NoError(t, err) | ||
| type contextKey struct{} | ||
| ctx := context.WithValue(context.Background(), contextKey{}, "request") | ||
| called := false | ||
| require.NoError(t, db.Callback().Update().Before("gorm:update").Register("check_context", func(db *gorm.DB) { | ||
| called = true | ||
| require.Equal(t, "request", db.Statement.Context.Value(contextKey{})) | ||
| _, hasDeadline := db.Statement.Context.Deadline() | ||
| require.True(t, hasDeadline) | ||
| })) | ||
| logger := &messageThreadTestLogger{} | ||
| repository := NewGormHeartbeatMonitorRepository(logger, telemetry.NewOtelLogger("test", logger), db) | ||
|
|
||
| require.NoError(t, update(repository, ctx)) | ||
| require.True(t, called) | ||
| }) | ||
| } | ||
| } |
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
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,104 @@ | ||
| package telemetry | ||
|
|
||
| import ( | ||
| "context" | ||
| "crypto/sha256" | ||
| "time" | ||
|
|
||
| "go.opentelemetry.io/otel/attribute" | ||
| "go.opentelemetry.io/otel/codes" | ||
| sdktrace "go.opentelemetry.io/otel/sdk/trace" | ||
| "go.opentelemetry.io/otel/trace" | ||
| ) | ||
|
|
||
| const ( | ||
| mustSampleAttribute attribute.Key = "httpsms.must_sample" | ||
| slowDatabaseSpanThreshold = 200 * time.Millisecond | ||
| ) | ||
|
|
||
| type samplingSpanProcessor struct { | ||
| sdktrace.SpanProcessor | ||
| } | ||
|
|
||
| // NewSamplingSpanProcessor exports warning/error spans, database spans over 200ms, | ||
| // and 50% of other spans. Truncated spans are retained conservatively. | ||
| // The provider must record all spans so the decision can be made after they end. | ||
| func NewSamplingSpanProcessor(exporter sdktrace.SpanExporter) sdktrace.SpanProcessor { | ||
| return &samplingSpanProcessor{ | ||
| SpanProcessor: sdktrace.NewBatchSpanProcessor(exporter), | ||
| } | ||
| } | ||
|
|
||
| func (processor *samplingSpanProcessor) OnEnd(span sdktrace.ReadOnlySpan) { | ||
| if isSampledTrace(span.SpanContext().TraceID()) || shouldRetainSpan(span) { | ||
| processor.SpanProcessor.OnEnd(span) | ||
| } | ||
| } | ||
|
|
||
| // SampledTraceExemplarFilter links metrics only to the stable trace-ID sample. | ||
| func SampledTraceExemplarFilter(ctx context.Context) bool { | ||
| spanContext := trace.SpanContextFromContext(ctx) | ||
| return spanContext.IsValid() && spanContext.IsSampled() && isSampledTrace(spanContext.TraceID()) | ||
| } | ||
|
|
||
| func isSampledTrace(traceID trace.TraceID) bool { | ||
| // UUID version/variant bits must not bias the trace-ID decision. | ||
| hash := sha256.Sum256(traceID[:]) | ||
| return hash[0] < 128 | ||
| } | ||
|
|
||
| func shouldRetainSpan(span sdktrace.ReadOnlySpan) bool { | ||
| // Drop counters survive buffer limits, unlike severity attributes and events. | ||
| return span.DroppedEvents() > 0 || | ||
| span.DroppedAttributes() > 0 || | ||
| span.Status().Code == codes.Error || | ||
| hasRetentionFlag(span.Attributes()) || | ||
| hasExceptionalEvents(span.Events()) || | ||
| hasTruncatedLinks(span) || | ||
| isSlowDatabaseSpan(span) | ||
| } | ||
|
|
||
| func hasTruncatedLinks(span sdktrace.ReadOnlySpan) bool { | ||
| if span.DroppedLinks() > 0 { | ||
| return true | ||
| } | ||
| for _, link := range span.Links() { | ||
| if link.DroppedAttributeCount > 0 { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func isSlowDatabaseSpan(span sdktrace.ReadOnlySpan) bool { | ||
| if span.EndTime().Sub(span.StartTime()) <= slowDatabaseSpanThreshold { | ||
| return false | ||
| } | ||
| for _, value := range span.Attributes() { | ||
| switch value.Key { | ||
| case "db.system.name", "db.system": | ||
| if value.Value.AsString() != "" { | ||
| return true | ||
| } | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func hasExceptionalEvents(events []sdktrace.Event) bool { | ||
| for _, event := range events { | ||
| if event.Name == "exception" || event.DroppedAttributeCount > 0 { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
|
|
||
| func hasRetentionFlag(attributes []attribute.KeyValue) bool { | ||
| for _, value := range attributes { | ||
| if value.Key == mustSampleAttribute && value.Value.AsBool() { | ||
| return true | ||
| } | ||
| } | ||
| return false | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.