File: initialization.go

package info (click to toggle)
golang-gitlab-gitlab-org-labkit 1.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,092 kB
  • sloc: sh: 210; javascript: 49; makefile: 4
file content (54 lines) | stat: -rw-r--r-- 1,264 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 (
	"io"

	opentracing "github.com/opentracing/opentracing-go"
	log "github.com/sirupsen/logrus"
	"gitlab.com/gitlab-org/labkit/tracing/connstr"
	"gitlab.com/gitlab-org/labkit/tracing/impl"
)

type nopCloser struct {
}

func (nopCloser) Close() error { return nil }

// Initialize will initialize distributed tracing.
func Initialize(opts ...InitializationOption) io.Closer {
	config := applyInitializationOptions(opts)

	if config.connectionString == "" {
		// No opentracing connection has been set
		return &nopCloser{}
	}

	driverName, options, err := connstr.Parse(config.connectionString)
	if err != nil {
		log.WithError(err).Infoln("unable to parse connection")
		return &nopCloser{}
	}

	// URL-provided service_name overrides the InitializationOption
	if _, ok := options["service_name"]; !ok {
		options["service_name"] = config.serviceName
	}

	tracer, closer, err := impl.New(driverName, options)
	if err != nil {
		log.WithError(err).Warn("skipping tracing configuration step")
		return &nopCloser{}
	}

	if tracer == nil {
		log.Warn("no tracer provided, tracing will be disabled")
	} else {
		log.Info("Tracing enabled")
		opentracing.SetGlobalTracer(tracer)
	}

	if closer == nil {
		return &nopCloser{}
	}
	return closer
}