go.undefinedlabs.com/scopeagent@v0.4.2/events/event_nettrace.go (about)

     1  package events
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  
     7  	"golang.org/x/net/trace"
     8  
     9  	basictracer "github.com/opentracing/basictracer-go"
    10  )
    11  
    12  // NetTraceIntegrator can be passed into a basictracer as NewSpanEventListener
    13  // and causes all traces to be registered with the net/trace endpoint.
    14  var NetTraceIntegrator = func() func(basictracer.SpanEvent) {
    15  	var tr trace.Trace
    16  	return func(e basictracer.SpanEvent) {
    17  		switch t := e.(type) {
    18  		case basictracer.EventCreate:
    19  			tr = trace.New("tracing", t.OperationName)
    20  			tr.SetMaxEvents(1000)
    21  		case basictracer.EventFinish:
    22  			tr.Finish()
    23  		case basictracer.EventTag:
    24  			tr.LazyPrintf("%s:%v", t.Key, t.Value)
    25  		case basictracer.EventLogFields:
    26  			var buf bytes.Buffer
    27  			for i, f := range t.Fields {
    28  				if i > 0 {
    29  					buf.WriteByte(' ')
    30  				}
    31  				fmt.Fprintf(&buf, "%s:%v", f.Key(), f.Value())
    32  			}
    33  
    34  			tr.LazyPrintf("%s", buf.String())
    35  		case basictracer.EventLog:
    36  			if t.Payload != nil {
    37  				tr.LazyPrintf("%s (payload %v)", t.Event, t.Payload)
    38  			} else {
    39  				tr.LazyPrintf("%s", t.Event)
    40  			}
    41  		}
    42  	}
    43  }