Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Add support to GlobalTracer to indicate if a tracer has been register…
Browse files Browse the repository at this point in the history
…ed (#201)

* add support to GlobalTracer to indicate if a tracer has been registered

* rename IsRegistered to IsGlobalTracerRegistered

* add unit tests for GlobalTracer
  • Loading branch information
MikeGoldsmith authored and yurishkuro committed Nov 29, 2018
1 parent f6bb0d0 commit 8b5a441
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
18 changes: 14 additions & 4 deletions globaltracer.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package opentracing

type registeredTracer struct {
tracer Tracer
isRegistered bool
}

var (
globalTracer Tracer = NoopTracer{}
globalTracer = registeredTracer{NoopTracer{}, false}
)

// SetGlobalTracer sets the [singleton] opentracing.Tracer returned by
Expand All @@ -11,22 +16,27 @@ var (
// Prior to calling `SetGlobalTracer`, any Spans started via the `StartSpan`
// (etc) globals are noops.
func SetGlobalTracer(tracer Tracer) {
globalTracer = tracer
globalTracer = registeredTracer{tracer, true}
}

// GlobalTracer returns the global singleton `Tracer` implementation.
// Before `SetGlobalTracer()` is called, the `GlobalTracer()` is a noop
// implementation that drops all data handed to it.
func GlobalTracer() Tracer {
return globalTracer
return globalTracer.tracer
}

// StartSpan defers to `Tracer.StartSpan`. See `GlobalTracer()`.
func StartSpan(operationName string, opts ...StartSpanOption) Span {
return globalTracer.StartSpan(operationName, opts...)
return globalTracer.tracer.StartSpan(operationName, opts...)
}

// InitGlobalTracer is deprecated. Please use SetGlobalTracer.
func InitGlobalTracer(tracer Tracer) {
SetGlobalTracer(tracer)
}

// IsGlobalTracerRegistered returns a `bool` to indicate if a tracer has been globally registered
func IsGlobalTracerRegistered() bool {
return globalTracer.isRegistered
}
26 changes: 26 additions & 0 deletions globaltracer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package opentracing

import (
"reflect"
"testing"
)

func TestIsGlobalTracerRegisteredDefaultIsFalse(t *testing.T) {
if IsGlobalTracerRegistered() {
t.Errorf("Should return false when no global tracer is registered.")
}
}

func TestAfterSettingGlobalTracerIsGlobalTracerRegisteredReturnsTrue(t *testing.T) {
SetGlobalTracer(NoopTracer{})

if !IsGlobalTracerRegistered() {
t.Errorf("Should return true after a tracer has been registered.")
}
}

func TestDefaultTracerIsNoopTracer(t *testing.T) {
if reflect.TypeOf(GlobalTracer()) != reflect.TypeOf(NoopTracer{}) {
t.Errorf("Should return false when no global tracer is registered.")
}
}

0 comments on commit 8b5a441

Please sign in to comment.