-
Notifications
You must be signed in to change notification settings - Fork 617
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenTracing zipKin Support. Reference - Issue #429
Co-authored-by: Kristina Fischer <[email protected]> Co-authored-by: Michael Murphy <[email protected]> Co-authored-by: Nathan West <[email protected]> Co-authored-by: Austin Hartzheim <[email protected]> Co-authored-by: Jacob Hansen <[email protected]>
- Loading branch information
1 parent
f6be07e
commit 6845629
Showing
8 changed files
with
261 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package trace | ||
|
||
import ( | ||
"log" | ||
"net/http" | ||
"os" | ||
"strings" | ||
|
||
opentracing "github.com/opentracing/opentracing-go" | ||
zipkin "github.com/openzipkin/zipkin-go-opentracing" | ||
"github.com/opentracing/opentracing-go/ext" | ||
) | ||
|
||
func InjectHeaders(span opentracing.Span, req *http.Request) { | ||
// Inject span data into the request headers | ||
opentracing.GlobalTracer().Inject( | ||
span.Context(), | ||
opentracing.HTTPHeaders, | ||
opentracing.HTTPHeadersCarrier(req.Header), | ||
) | ||
} | ||
|
||
func CreateCollector(collectorType string, connectString string, topic string) zipkin.Collector { | ||
var collector zipkin.Collector | ||
var err error | ||
|
||
if collectorType == "http" { | ||
collector, err = zipkin.NewHTTPCollector(connectString) | ||
} else if collectorType == "kafka" { | ||
// TODO set logger? | ||
kafkaHosts := strings.Split(connectString, ",") | ||
collector, err = zipkin.NewKafkaCollector( | ||
kafkaHosts, | ||
zipkin.KafkaTopic(topic), | ||
) | ||
} | ||
|
||
if err != nil { | ||
log.Printf("Unable to create Zipkin %s collector: %+v", collectorType, err) | ||
os.Exit(-1) | ||
} | ||
|
||
return collector | ||
} | ||
|
||
func CreateTracer(recorder zipkin.SpanRecorder, samplerRate float64) opentracing.Tracer { | ||
tracer, err := zipkin.NewTracer( | ||
recorder, | ||
zipkin.WithSampler(zipkin.NewBoundarySampler(samplerRate, 1)), | ||
zipkin.ClientServerSameSpan(false), | ||
zipkin.TraceID128Bit(true), | ||
) | ||
|
||
if err != nil { | ||
log.Printf("Unable to create Zipkin tracer: %+v", err) | ||
os.Exit(-1) | ||
} | ||
|
||
return tracer | ||
} | ||
|
||
func CreateSpan(r *http.Request, serviceName string) opentracing.Span { | ||
globalTracer := opentracing.GlobalTracer() | ||
|
||
// If headers contain trace data, create child span from parent; else, create root span | ||
var span opentracing.Span | ||
if globalTracer != nil { | ||
spanCtx, err := globalTracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(r.Header)) | ||
if err != nil { | ||
span = globalTracer.StartSpan(serviceName) | ||
} else { | ||
span = globalTracer.StartSpan(serviceName, ext.RPCServerOption(spanCtx)) | ||
} | ||
} | ||
|
||
return span // caller must defer span.finish() | ||
} | ||
|
||
func InitializeTracer(collectorType string, connectString string, serviceName string, topic string, samplerRate float64, addressPort string) { | ||
log.Printf("Tracing initializing - type: %s, connection string: %s, service name: %s, topic: %s, samplerRate: %v", collectorType, connectString, serviceName, topic, samplerRate) | ||
|
||
// Create a new Zipkin Collector, Recorder, and Tracer | ||
collector := CreateCollector(collectorType, connectString, topic) | ||
recorder := zipkin.NewRecorder(collector, false, addressPort, serviceName) | ||
tracer := CreateTracer(recorder, samplerRate) | ||
|
||
// Set the Zipkin Tracer created above to the GlobalTracer | ||
opentracing.SetGlobalTracer(tracer) | ||
|
||
log.Printf("\n\nTRACER: %v\n\n", tracer) | ||
log.Printf("\n\nCOLLECTOR: %v\n\n", collector) | ||
log.Printf("\n\nRECORDER: %v\n\n", recorder) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package trace | ||
|
||
import ( | ||
opentracing "github.com/opentracing/opentracing-go" | ||
mocktracer "github.com/opentracing/opentracing-go/mocktracer" | ||
"github.com/opentracing/opentracing-go/ext" | ||
"net/http" | ||
"testing" | ||
) | ||
|
||
func mimicTracerInject(req *http.Request) { | ||
// TODO maybe replace this will a call to opentracing.GlobalTracer().Inject() | ||
req.Header.Add("X-B3-TraceId", "1234562345678") | ||
req.Header.Add("X-B3-SpanId", "123456789") | ||
req.Header.Add("X-B3-ParentSpanId", "123456789") | ||
req.Header.Add("X-B3-Flags", "1") | ||
} | ||
|
||
// go test -v ./trace | ||
func TestInjectHeaders(t *testing.T) { | ||
serviceName := "TESTING" | ||
|
||
req, err := http.NewRequest("GET", "http://example.com", nil) | ||
if err != nil { | ||
t.Error("Error when creating new request.") | ||
t.Fail() | ||
} | ||
mimicTracerInject(req) | ||
|
||
mt := mocktracer.New() | ||
opentracing.SetGlobalTracer(mt) | ||
globalTracer := opentracing.GlobalTracer() | ||
|
||
var span opentracing.Span | ||
if globalTracer != nil { | ||
spanCtx, err := globalTracer.Extract(opentracing.HTTPHeaders, opentracing.HTTPHeadersCarrier(req.Header)) | ||
if err != nil { | ||
span = globalTracer.StartSpan(serviceName) | ||
} else { | ||
span = globalTracer.StartSpan(serviceName, ext.RPCServerOption(spanCtx)) | ||
} | ||
} | ||
|
||
InjectHeaders(span, req) | ||
|
||
if req.Header.Get("X-B3-Traceid") == "" { | ||
t.Error("Zipkin headers not set in request.") | ||
t.Fail() | ||
} | ||
if req.Header.Get("X-B3-Traceid") != "1234562345678" { | ||
t.Error("Zipkin headers do not match the values set.") | ||
t.Fail() | ||
} | ||
} |