func packSegments(seg *Segment, outSegments [][]byte) [][]byte {
	trimSubsegment := func(s *Segment) []byte {
		ss := globalCfg.StreamingStrategy()
		if seg.ParentSegment.Configuration != nil && seg.ParentSegment.Configuration.StreamingStrategy != nil {
			ss = seg.ParentSegment.Configuration.StreamingStrategy
		}
		for ss.RequiresStreaming(s) {
			if len(s.rawSubsegments) == 0 {
				break
			}
			cb := ss.StreamCompletedSubsegments(s)
			outSegments = append(outSegments, cb...)
		}
		b, err := json.Marshal(s)
		if err != nil {
			logger.Errorf("JSON error while marshalling (Sub)Segment: %v", err)
		}
		return b
	}
	for _, s := range seg.rawSubsegments {
		s.Lock()
		outSegments = packSegments(s, outSegments)
		if b := trimSubsegment(s); b != nil {
			seg.Subsegments = append(seg.Subsegments, b)
		}
		s.Unlock()
	}
	if seg.isOrphan() {
		if b := trimSubsegment(seg); b != nil {
			outSegments = append(outSegments, b)
		}
	}
	return outSegments
}
seg has a write lock acquired by the caller.
package xray
import (
	"encoding/json"
	"net"
	"sync"
	"github.com/aws/aws-xray-sdk-go/internal/logger"
)
const Header = `{"format": "json", "version": 1}` + "\n"
type DefaultEmitter struct {
	sync.Mutex
	conn *net.UDPConn
	addr *net.UDPAddr
}
func NewDefaultEmitter(raddr *net.UDPAddr) (*DefaultEmitter, error) {
	initLambda()
	d := &DefaultEmitter{addr: raddr}
	return d, nil
}
func (de *DefaultEmitter) RefreshEmitterWithAddress(raddr *net.UDPAddr) {
	de.Lock()
	de.refresh(raddr)
	de.Unlock()
}
func (de *DefaultEmitter) refresh(raddr *net.UDPAddr) (err error) {
	de.conn, err = net.DialUDP("udp", nil, raddr)
	de.addr = raddr
	if err != nil {
		logger.Errorf("Error dialing emitter address %v: %s", raddr, err)
		return err
	}
	logger.Infof("Emitter using address: %v", raddr)
	return nil
}
func (de *DefaultEmitter) Emit(seg *Segment) {
	HeaderBytes := []byte(Header)
	if seg == nil || !seg.ParentSegment.Sampled {
		return
	}
	for _, p := range packSegments(seg, nil) {
		logger.Debug(string(p))
		de.Lock()
		if de.conn == nil {
			if err := de.refresh(de.addr); err != nil {
				de.Unlock()
				return
			}
		}
		_, err := de.conn.Write(append(HeaderBytes, p...))
		if err != nil {
			logger.Error(err)
		}
		de.Unlock()
	}
}