Skip to content

Commit

Permalink
suppress Panic in Emitter
Browse files Browse the repository at this point in the history
  • Loading branch information
wangzlei committed Sep 27, 2023
1 parent 4cdaf99 commit 16febea
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
6 changes: 6 additions & 0 deletions xray/default_emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package xray
import (
"encoding/json"
"net"
"runtime/debug"
"sync"

"github.com/aws/aws-xray-sdk-go/internal/logger"
Expand Down Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions xray/default_emitter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 16febea

Please sign in to comment.