// testingTracer creates a Tracer that appends the events to the given slice. func testingTracer(ev *events) opentracing.Tracer { opts := basictracer.DefaultOptions() opts.ShouldSample = func(_ uint64) bool { return true } opts.NewSpanEventListener = func() func(basictracer.SpanEvent) { // The op variable is used in the returned function and is associated with // a span; it lives for as long as the span is open. var op string return func(e basictracer.SpanEvent) { switch t := e.(type) { case basictracer.EventCreate: op = t.OperationName *ev = append(*ev, fmt.Sprintf("%s:start", op)) case basictracer.EventFinish: *ev = append(*ev, fmt.Sprintf("%s:finish", op)) case basictracer.EventLogFields: *ev = append(*ev, fmt.Sprintf("%s:%s", op, t.Fields[0].Value())) case basictracer.EventLog: panic("EventLog is deprecated") } } } opts.DebugAssertUseAfterFinish = true // We don't care about the recorder but we need to set it to something. opts.Recorder = &basictracer.InMemorySpanRecorder{} return basictracer.NewWithOptions(opts) }
func defaultOptions(recorder func(basictracer.RawSpan)) basictracer.Options { opts := basictracer.DefaultOptions() opts.TrimUnsampledSpans = true opts.Recorder = CallbackRecorder(recorder) opts.NewSpanEventListener = basictracer.NetTraceIntegrator opts.DebugAssertUseAfterFinish = true // provoke crash on use-after-Finish return opts }
// basicTracerOptions initializes options for basictracer. // The recorder should be nil if we don't need to record spans. func basictracerOptions(recorder func(basictracer.RawSpan)) basictracer.Options { opts := basictracer.DefaultOptions() opts.ShouldSample = func(traceID uint64) bool { return false } opts.TrimUnsampledSpans = true opts.NewSpanEventListener = netTraceIntegrator opts.DebugAssertUseAfterFinish = true // provoke crash on use-after-Finish if recorder == nil { opts.Recorder = CallbackRecorder(func(_ basictracer.RawSpan) {}) // If we are not recording the spans, there is no need to keep them in // memory. Events still get passed to netTraceIntegrator. opts.DropAllLogs = true } else { opts.Recorder = CallbackRecorder(recorder) // Set a comfortable limit of log events per span. opts.MaxLogsPerSpan = maxLogsPerSpan } return opts }
// callback. If the given DelegatingCarrier is nil, a new Span is created. // otherwise, the created Span is a child. func JoinOrNewSnowball(opName string, carrier *Span, callback func(sp basictracer.RawSpan)) (opentracing.Span, error) { tr := basictracer.New(CallbackRecorder(callback)) sp, err := JoinOrNew(tr, carrier, opName) if err == nil { sp.SetBaggageItem(Snowball, "1") // We definitely want to sample a Snowball trace. ext.SamplingPriority.Set(sp, 1) } return sp, err } // newTracer implements NewTracer and allows that function to be mocked out via Disable(). var newTracer = func() opentracing.Tracer { opts := basictracer.DefaultOptions() opts.TrimUnsampledSpans = true opts.Recorder = CallbackRecorder(func(_ basictracer.RawSpan) {}) opts.NewSpanEventListener = basictracer.NetTraceIntegrator opts.DebugAssertUseAfterFinish = true // provoke crash on use-after-Finish return basictracer.NewWithOptions(opts) } // NewTracer creates a Tracer which records to the net/trace // endpoint. func NewTracer() opentracing.Tracer { return newTracer() } // SpanFromContext returns the Span obtained from the context or, if none is // found, a new one started through the tracer. Callers should call (or defer)
// NewTracerWithOptions creates a new opentracing.Tracer that records spans to // the given appdash.Collector. func NewTracerWithOptions(c appdash.Collector, options Options) opentracing.Tracer { opts := basictracer.DefaultOptions() opts.ShouldSample = options.ShouldSample opts.Recorder = NewRecorder(c, options) return basictracer.NewWithOptions(opts) }