File: tracing.go

package info (click to toggle)
gitlab-shell 14.35.0%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,652 kB
  • sloc: ruby: 1,129; makefile: 583; sql: 391; sh: 384
file content (54 lines) | stat: -rw-r--r-- 2,126 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package tracing

import (
	"context"

	"github.com/opentracing/opentracing-go"
	"gitlab.com/gitlab-org/labkit/tracing"
)

// Tags is a key-value map. It is used to set tags for a span
type Tags map[string]any

// StartSpan creates a new span with name and options (mostly tags). This function is a wrapper for
// underlying tracing libraries. This method should only be used at the entrypoint of the program.
func StartSpan(ctx context.Context, spanName string, tags Tags, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
	return opentracing.StartSpanFromContext(ctx, spanName, tagsToOpentracingTags(opts, tags)...)
}

// StartSpanIfHasParent creates a new span if the context already has an existing span. This function
// adds a simple validation to prevent orphan spans outside interested code paths. It returns a dummy
// span, which acts as normal span, but does absolutely nothing and is not recorded later.
func StartSpanIfHasParent(ctx context.Context, spanName string, tags Tags, opts ...opentracing.StartSpanOption) (opentracing.Span, context.Context) {
	parent := opentracing.SpanFromContext(ctx)
	if parent == nil {
		return &NoopSpan{}, ctx
	}
	return opentracing.StartSpanFromContext(ctx, spanName, tagsToOpentracingTags(opts, tags)...)
}

// DiscardSpanInContext discards the current active span from the context. This function is helpful
// when the current code path enters an area shared by other code paths. Git catfile cache is a
// good example of this type of span.
func DiscardSpanInContext(ctx context.Context) context.Context {
	if opentracing.SpanFromContext(ctx) == nil {
		return ctx
	}
	return opentracing.ContextWithSpan(ctx, nil)
}

// IsSampled tells whether a span belongs to a sampled trace
func IsSampled(ctx context.Context) bool {
	span := opentracing.SpanFromContext(ctx)
	if span != nil {
		return tracing.IsSampled(span)
	}
	return false
}

func tagsToOpentracingTags(opts []opentracing.StartSpanOption, tags Tags) []opentracing.StartSpanOption {
	for key, value := range tags {
		opts = append(opts, opentracing.Tag{Key: key, Value: value})
	}
	return opts
}