diff --git a/example_tracecontext_test.go b/example_tracecontext_test.go index 3554b4e3e..965935b67 100644 --- a/example_tracecontext_test.go +++ b/example_tracecontext_test.go @@ -23,9 +23,17 @@ import ( "os" "go.elastic.co/apm/v2" + "go.elastic.co/apm/v2/apmtest" ) func ExampleTransaction_EnsureParent() { + // When environment variables aren't set, the default tracer is inactive. + // For the sake of this example, we want a discarded tracer instead, so we do + // have trace and span IDs generated. + // + // This setup is now something you should be using in your own code. + apm.SetDefaultTracer(apmtest.NewDiscardTracer()) + tx := apm.DefaultTracer().StartTransactionOptions("name", "type", apm.TransactionOptions{ TraceContext: apm.TraceContext{ Trace: apm.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}, diff --git a/tracer.go b/tracer.go index 1b6271559..126d5bfb5 100644 --- a/tracer.go +++ b/tracer.go @@ -53,10 +53,8 @@ var ( // DefaultTracer returns the default global Tracer, set the first time the // function is called, or after calling SetDefaultTracer(nil). // -// The default tracer is configured via environment variables, and will always -// be non-nil. If any of the environment variables are invalid, the -// corresponding errors will be logged to stderr and the default values will be -// used instead. +// The default tracer is configured via environment variables, and if the those are invalid +// the tracer will be disabled. func DefaultTracer() *Tracer { tracerMu.RLock() if defaultTracer != nil { @@ -314,7 +312,7 @@ func (opts *TracerOptions) initDefaults(continueOnError bool) error { if opts.Transport == nil { initialTransport, err := initialTransport(opts.ServiceName, opts.ServiceVersion) if failed(err) { - opts.Transport = transport.NewDiscardTransport(err) + active = false } else { opts.Transport = initialTransport } diff --git a/tracer_test.go b/tracer_test.go index 4537f6348..0a32716d6 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -597,17 +597,10 @@ func TestTracerDefaultTransport(t *testing.T) { require.Error(t, err) assert.EqualError(t, err, "failed to parse ELASTIC_APM_SERVER_TIMEOUT: invalid duration never") - // Implicitly created Tracers will have a discard tracer. + // Implicitly created Tracers will have Inactive tracer in case of invalid configuration apm.SetDefaultTracer(nil) tracer = apm.DefaultTracer() - - tracer.StartTransaction("name", "type").End() - tracer.Flush(nil) - assert.Equal(t, apm.TracerStats{ - Errors: apm.TracerStatsErrors{ - SendStream: 1, - }, - }, tracer.Stats()) + assert.Equal(t, false, tracer.Active()) }) }