-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement tracing package for toolkit (#193)
* 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
authored
Jun 22, 2020
1 parent
4b42da4
commit bdfb656
Showing
8 changed files
with
786 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,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 | ||
} |
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,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) | ||
} |
Oops, something went wrong.