prysm-pulse/shared/p2p/adapter/tracer/tracer.go
Divyank Katira 2a51ed3f39 P2P Message Execution Tracing (#517)
* Request execution tracing initial commit

* Resolve linter issues

* Run gazelle

* Make trace sampling configurable, clean up, update doc

* Document trace span creation

* Fix linter issue
2018-09-20 07:46:35 -04:00

50 lines
1.2 KiB
Go

package tracer
import (
"errors"
"github.com/prysmaticlabs/prysm/shared/p2p"
"github.com/sirupsen/logrus"
"go.opencensus.io/exporter/jaeger"
"go.opencensus.io/trace"
)
var log = logrus.WithField("prefix", "tracer")
// New creates and initializes a new tracing adapter.
func New(name, endpoint string, sampleFraction float64, enable bool) (p2p.Adapter, error) {
if !enable {
trace.ApplyConfig(trace.Config{DefaultSampler: trace.NeverSample()})
return adapter, nil
}
if name == "" {
return nil, errors.New("tracing service name cannot be empty")
}
trace.ApplyConfig(trace.Config{DefaultSampler: trace.ProbabilitySampler(sampleFraction)})
log.Infof("Starting Jaeger exporter endpoint at address = %s", endpoint)
exporter, err := jaeger.NewExporter(jaeger.Options{
Endpoint: endpoint,
Process: jaeger.Process{
ServiceName: name,
},
})
if err != nil {
return nil, err
}
trace.RegisterExporter(exporter)
return adapter, nil
}
var adapter p2p.Adapter = func(next p2p.Handler) p2p.Handler {
return func(msg p2p.Message) {
var messageSpan *trace.Span
msg.Ctx, messageSpan = trace.StartSpan(msg.Ctx, "handleP2pMessage")
next(msg)
messageSpan.End()
}
}