Skip to content

Commit

Permalink
Implement tracing package for toolkit (#193)
Browse files Browse the repository at this point in the history
* add SpanContextAnnotator
SpanContextAnnotator retrieve current trace.Span from context.Context
or, in case there is no open trace.Span assemble trace.SpanContext from HTTP
headers

* Implement wrapper to extend ochttp.Handler

* Add options to configure what should be added to span

* Implement propogation of request/response headers, and body to span

* Implement wrapper to extend ocgrpc.ServerHandler

* Add options to configure what should be added to span

* Implement propogation of inbound/outbound metadata, and payload to span

* add util functions

* add grpc error into span

* implement simple exporter constructor

* implement obfuscation logic

* move obfuscation factor to constant

* Get lattest span value after calling wrapped handler

* Use library function to assemble span context
  • Loading branch information
Aliaksei Burau committed Jun 22, 2020
1 parent 4b42da4 commit bdfb656
Show file tree
Hide file tree
Showing 8 changed files with 786 additions and 3 deletions.
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
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
39 changes: 39 additions & 0 deletions tracing/annotator.go
@@ -0,0 +1,39 @@
package tracing

import (
"context"
"net/http"

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

const (
traceContextKey = "grpc-trace-bin"
)

var defaultFormat propagation.HTTPFormat = &b3.HTTPFormat{}

//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
}

sc, ok := defaultFormat.SpanContextFromRequest(req)
if ok {
traceContextBinary := propagation.Binary(sc)
md[traceContextKey] = []string{string(traceContextBinary)}
}

return md
}
31 changes: 31 additions & 0 deletions tracing/exporter.go
@@ -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)
}

0 comments on commit bdfb656

Please sign in to comment.