From f9f54d495e5c6752b58fae6b66a5cb1107e17b29 Mon Sep 17 00:00:00 2001 From: Joshua MacDonald Date: Tue, 16 Jan 2018 23:00:15 -0800 Subject: [PATCH] Add GetLightStepReporterID helper (#141) * Add GetLightStepReporterID helper * Test three cases --- tracer_0_14._test.go | 9 +++++++++ tracer_helpers.go | 12 ++++++++++++ tracer_test.go | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/tracer_0_14._test.go b/tracer_0_14._test.go index da4421c5a..6907ede5e 100644 --- a/tracer_0_14._test.go +++ b/tracer_0_14._test.go @@ -9,6 +9,7 @@ import ( "github.com/lightstep/lightstep-tracer-go/lightstepfakes" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + opentracing "github.com/opentracing/opentracing-go" ) var _ = Describe("Tracerv0_14", func() { @@ -71,4 +72,12 @@ var _ = Describe("Tracerv0_14", func() { Expect(err).ToNot(HaveOccurred()) }) }) + + Describe("provides its ReporterID", func() { + It("is non-zero", func() { + rid, err := lightstep.GetLightStepReporterID(opentracing.Tracer(tracer)) + Expect(err).To(BeNil()) + Expect(rid).To(Not(BeZero())) + }) + }) }) diff --git a/tracer_helpers.go b/tracer_helpers.go index a7e03c029..b4f312742 100644 --- a/tracer_helpers.go +++ b/tracer_helpers.go @@ -67,3 +67,15 @@ func CloseTracer(tracer opentracing.Tracer) error { return newEventUnsupportedTracer(tracer) } } + +// GetLightStepReporterID returns the currently configured Reporter ID. +func GetLightStepReporterID(tracer opentracing.Tracer) (uint64, error) { + switch lsTracer := tracer.(type) { + case *tracerImpl: + return lsTracer.reporterID, nil + case *tracerv0_14: + return GetLightStepReporterID(lsTracer.Tracer) + default: + return 0, newEventUnsupportedTracer(tracer) + } +} diff --git a/tracer_test.go b/tracer_test.go index c15a553f4..4c968572d 100644 --- a/tracer_test.go +++ b/tracer_test.go @@ -16,6 +16,7 @@ import ( "github.com/lightstep/lightstep-tracer-go/lightstepfakes" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" + opentracing "github.com/opentracing/opentracing-go" ) var _ = Describe("Tracer", func() { @@ -284,4 +285,36 @@ var _ = Describe("Tracer", func() { Expect(span.Finish).ToNot(Panic()) }) }) + + Describe("provides its ReporterID", func() { + BeforeEach(func() { + opts = Options{ + AccessToken: accessToken, + ConnFactory: fakeConn, + Recorder: nil, + } + }) + + It("is non-zero", func() { + rid, err := GetLightStepReporterID(tracer) + Expect(err).To(BeNil()) + Expect(rid).To(Not(BeZero())) + }) + }) +}) + +var _ = Describe("UnsupportedTracer", func() { + type unsupportedTracer struct { + opentracing.Tracer + } + + tracer := unsupportedTracer{} + + Describe("has no ReporterID", func() { + It("is an error", func() { + rid, err := GetLightStepReporterID(tracer) + Expect(err).To(Not(BeNil())) + Expect(rid).To(BeZero()) + }) + }) })