Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement tracing package for toolkit #193

Merged
merged 17 commits into from
Jun 22, 2020
84 changes: 81 additions & 3 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@
name = "google.golang.org/grpc"
version = ">=1.0.0"

[[constraint]]
name = "go.opencensus.io"
version = ">=0.22.3"

[prune]
go-tests = true
unused-packages = true
78 changes: 78 additions & 0 deletions tracing/annotator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package tracing

import (
"context"
"encoding/hex"
"net/http"

"go.opencensus.io/trace"
"go.opencensus.io/trace/propagation"
"google.golang.org/grpc/metadata"
)

const (
traceContextKey = "grpc-trace-bin"

//ParentSpanIDKey is a header name for ParrentSpanId B3 header
ParentSpanIDKey = "X-B3-ParentSpanId"

//SpanIDKey is a header name for SpanId B3 header
SpanIDKey = "X-B3-SpanId"

//TraceIDKey is a header name for TraceId B3 header
TraceIDKey = "X-B3-TraceId"

//TraceSampledKey is a header name for Sampled B3 header
TraceSampledKey = "X-B3-Sampled"

//TODO: handle ParentSpanIDKey and TraceSampledKey as well
//TODO: check headers names
)

//SpanContextAnnotator retrieve information about current span from context or HTTP headers
//and propogate in binary format to gRPC service
func SpanContextAnnotator(ctx context.Context, req *http.Request) metadata.MD {
md := make(metadata.MD)

span := trace.FromContext(ctx)
if span != nil {
traceContextBinary := propagation.Binary(span.SpanContext())
md[traceContextKey] = []string{string(traceContextBinary)}

return md
}

//Fallback to assembling trace.SpanContext from headers
var sc trace.SpanContext

spanID := req.Header.Get(SpanIDKey)
if spanID == "" {
return md
}

buf, err := hex.DecodeString(spanID)
if err != nil {
return md
}

copy(sc.SpanID[:], buf)

traceID := req.Header.Get(TraceIDKey)
if traceID == "" {
return md
}

buf, err = hex.DecodeString(traceID)
if err != nil {
return md
}

copy(sc.TraceID[:], buf)

//TODO: check how to use TraceSampledKey and logTraceParentSpanID

traceContextBinary := propagation.Binary(sc)
md[traceContextKey] = []string{string(traceContextBinary)}

return md
}
31 changes: 31 additions & 0 deletions tracing/exporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tracing

import (
"time"

"contrib.go.opencensus.io/exporter/ocagent"
"go.opencensus.io/trace"
)

//Exporter creates a new OC Agent exporter and configure for tracing
func Exporter(address string, serviceName string, sampler trace.Sampler) error {
// TRACE: Setup OC agent for tracing
exporter, err := ocagent.NewExporter(
ocagent.WithInsecure(),
ocagent.WithReconnectionPeriod(5*time.Second),
ocagent.WithAddress(address),
ocagent.WithServiceName(serviceName))
if err != nil {
return err
}

trace.RegisterExporter(exporter)
trace.ApplyConfig(trace.Config{DefaultSampler: sampler})

return nil
}

//SamplerForFraction init sampler for specified fraction
func SamplerForFraction(fraction float64) trace.Sampler {
return trace.ProbabilitySampler(fraction)
}
Loading