diff --git a/xray/default_emitter.go b/xray/default_emitter.go index 76757730..27ebecbf 100644 --- a/xray/default_emitter.go +++ b/xray/default_emitter.go @@ -11,6 +11,7 @@ package xray import ( "encoding/json" "net" + "runtime/debug" "sync" "github.com/aws/aws-xray-sdk-go/internal/logger" @@ -57,6 +58,11 @@ func (de *DefaultEmitter) refresh(raddr *net.UDPAddr) (err error) { // Emit segment or subsegment if root segment is sampled. // seg has a write lock acquired by the caller. func (de *DefaultEmitter) Emit(seg *Segment) { + defer func() { + if r := recover(); r != nil { + logger.Errorf("Panic emitting segment: %s\n%s", r, string(debug.Stack())) + } + }() HeaderBytes := []byte(Header) if seg == nil || !seg.ParentSegment.Sampled { diff --git a/xray/default_emitter_test.go b/xray/default_emitter_test.go index 50a95087..13568b43 100644 --- a/xray/default_emitter_test.go +++ b/xray/default_emitter_test.go @@ -180,3 +180,17 @@ func BenchmarkDefaultEmitter(b *testing.B) { } }) } + +func TestDefaultEmitterWithPanic(t *testing.T) { + seg := &Segment{ + ParentSegment: nil, // cause Panic + } + emitter, err := NewDefaultEmitter(&net.UDPAddr{ + IP: net.IPv4(127, 0, 0, 1), + Port: 3000, + }) + if err != nil { + t.Fatal(err) + } + emitter.Emit(seg) +}