File: profiler.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 (69 lines) | stat: -rw-r--r-- 1,608 bytes parent folder | download | duplicates (3)
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// +build continuous_profiler_stackdriver

package monitoring

import (
	"net/url"
	"os"

	"cloud.google.com/go/profiler"
	"gitlab.com/gitlab-org/labkit/log"
	"google.golang.org/api/option"
)

const profilerEnvKey = "GITLAB_CONTINUOUS_PROFILING"

// Exposing to tests.
var profStart = profiler.Start

func initProfiler(opts profilerOpts) {
	driverParams, exists := os.LookupEnv(profilerEnvKey)
	if !exists {
		return
	}

	u, err := url.Parse(driverParams)
	if err != nil {
		log.WithError(err).Warn("unable to parse env var content")
		return
	}

	query := u.Query()
	driverName := u.Path

	if driverName != "stackdriver" {
		log.WithField("driver", driverName).Warn("unknown driver")
		return
	}

	config := profiler.Config{
		Service:        query.Get("service"),
		ServiceVersion: query.Get("service_version"),
		ProjectID:      query.Get("project_id"),
		DebugLogging:   query.Get("debug_logging") == "true",

		MutexProfiling: true,
	}

	// If the service version is given through opts, it takes precedence.
	if opts.ServiceVersion != "" {
		config.ServiceVersion = opts.ServiceVersion
	}

	var clientOpts []option.ClientOption
	if opts.CredentialsFile != "" {
		clientOpts = append(clientOpts, option.WithCredentialsFile(opts.CredentialsFile))
	}

	if err := profStart(config, clientOpts...); err != nil {
		log.WithError(err).Warnf("unable to initialize %v profiler", driverName)
		return
	}

	log.WithFields(log.Fields{
		"driver":         driverName,
		"service":        config.Service,
		"serviceVersion": config.ServiceVersion,
		"projectId":      config.ProjectID,
	}).Info("profiler enabled")
}