summaryrefslogtreecommitdiff
path: root/go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go
diff options
context:
space:
mode:
Diffstat (limited to 'go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go')
-rw-r--r--go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go63
1 files changed, 63 insertions, 0 deletions
diff --git a/go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go b/go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go
new file mode 100644
index 0000000..2b92a8a
--- /dev/null
+++ b/go/vendor/gitlab.com/gitlab-org/labkit/tracing/env_extractor.go
@@ -0,0 +1,63 @@
+package tracing
+
+import (
+ "context"
+ "os"
+ "strings"
+
+ opentracing "github.com/opentracing/opentracing-go"
+ "github.com/opentracing/opentracing-go/ext"
+ "gitlab.com/gitlab-org/labkit/correlation"
+)
+
+// ExtractFromEnv will extract a span from the environment after it has been passed in
+// from the parent process. Returns a new context, and a defer'able function, which
+// should be called on process termination
+func ExtractFromEnv(ctx context.Context, opts ...ExtractFromEnvOption) (context.Context, func()) {
+ /* config not yet used */ applyExtractFromEnvOptions(opts)
+ tracer := opentracing.GlobalTracer()
+
+ // Extract the Correlation-ID
+ envMap := environAsMap(os.Environ())
+ correlationID := envMap[envCorrelationIDKey]
+ if correlationID != "" {
+ ctx = correlation.ContextWithCorrelation(ctx, correlationID)
+ }
+
+ // Attempt to deserialize tracing identifiers
+ wireContext, err := tracer.Extract(
+ opentracing.TextMap,
+ opentracing.TextMapCarrier(envMap))
+
+ if err != nil {
+ /* Clients could send bad data, in which case we simply ignore it */
+ return ctx, func() {}
+ }
+
+ // Create the span referring to the RPC client if available.
+ // If wireContext == nil, a root span will be created.
+ additionalStartSpanOpts := []opentracing.StartSpanOption{
+ ext.RPCServerOption(wireContext),
+ }
+
+ if correlationID != "" {
+ additionalStartSpanOpts = append(additionalStartSpanOpts, opentracing.Tag{Key: "correlation_id", Value: correlationID})
+ }
+
+ serverSpan := opentracing.StartSpan(
+ "execute",
+ additionalStartSpanOpts...,
+ )
+ ctx = opentracing.ContextWithSpan(ctx, serverSpan)
+
+ return ctx, func() { serverSpan.Finish() }
+}
+
+func environAsMap(env []string) map[string]string {
+ envMap := make(map[string]string, len(env))
+ for _, v := range env {
+ s := strings.SplitN(v, "=", 2)
+ envMap[s[0]] = s[1]
+ }
+ return envMap
+}