forked from DataDog/dd-trace-go
/
options.go
176 lines (152 loc) · 4.41 KB
/
options.go
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-2020 Datadog, Inc.
package profiler
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/DataDog/datadog-go/statsd"
)
const (
// DefaultMutexFraction specifies the mutex profile fraction to be used with the mutex profiler.
// For more information or for changing this value, check runtime.SetMutexProfileFraction.
DefaultMutexFraction = 10
// DefaultBlockRate specifies the default block profiling rate used by the block profiler.
// For more information or for changing this value, check runtime.SetBlockProfileRate.
DefaultBlockRate = 100
// DefaultPeriod specifies the default period at which profiles will be collected.
DefaultPeriod = time.Minute
// DefaultDuration specifies the default length of the CPU profile snapshot.
DefaultDuration = time.Second * 15
)
const (
defaultAPIURL = "https://intake.profile.datadoghq.com/v1/input"
defaultEnv = "none"
)
var defaultProfileTypes = []ProfileType{CPUProfile, HeapProfile}
type config struct {
apiKey string
apiURL string
service, env string
hostname string
statsd StatsdClient
tags []string
types map[ProfileType]struct{}
period time.Duration
cpuDuration time.Duration
mutexFraction int
blockRate int
}
func (c *config) addProfileType(t ProfileType) {
if c.types == nil {
c.types = make(map[ProfileType]struct{})
}
c.types[t] = struct{}{}
}
func defaultConfig() *config {
c := config{
apiURL: defaultAPIURL,
env: defaultEnv,
service: filepath.Base(os.Args[0]),
statsd: &statsd.NoOpClient{},
period: DefaultPeriod,
cpuDuration: DefaultDuration,
blockRate: DefaultBlockRate,
mutexFraction: DefaultMutexFraction,
tags: []string{fmt.Sprintf("pid:%d", os.Getpid())},
}
for _, t := range defaultProfileTypes {
c.addProfileType(t)
}
if v := os.Getenv("DD_ENV"); v != "" {
WithEnv(v)(&c)
}
if v := os.Getenv("DD_SERVICE"); v != "" {
WithService(v)(&c)
}
if v := os.Getenv("DD_VERSION"); v != "" {
WithVersion(v)(&c)
}
if v := os.Getenv("DD_TAGS"); v != "" {
for _, tag := range strings.Split(v, ",") {
tag = strings.TrimSpace(tag)
if tag == "" {
continue
}
WithTags(tag)(&c)
}
}
return &c
}
// An Option is used to configure the profiler's behaviour.
type Option func(*config)
// WithAPIKey specifies the API key to use when connecting to the Datadog API.
func WithAPIKey(key string) Option {
return func(cfg *config) {
cfg.apiKey = key
}
}
// WithURL specifies the HTTP URL for the Datadog Profiling API.
func WithURL(url string) Option {
return func(cfg *config) {
cfg.apiURL = url
}
}
// WithPeriod specifies the interval at which to collect profiles.
func WithPeriod(d time.Duration) Option {
return func(cfg *config) {
cfg.period = d
}
}
// CPUDuration specifies the length at which to collect CPU profiles.
func CPUDuration(d time.Duration) Option {
return func(cfg *config) {
cfg.cpuDuration = d
}
}
// WithProfileTypes specifies the profile types to be collected by the profiler.
func WithProfileTypes(types ...ProfileType) Option {
return func(cfg *config) {
// reset the types and only use what the user has specified
for k := range cfg.types {
delete(cfg.types, k)
}
for _, t := range types {
cfg.addProfileType(t)
}
}
}
// WithService specifies the service name to attach to a profile.
func WithService(name string) Option {
return func(cfg *config) {
cfg.service = name
}
}
// WithEnv specifies the environment to which these profiles should be registered.
func WithEnv(env string) Option {
return func(cfg *config) {
cfg.env = env
}
}
// WithVersion specifies the service version tag to attach to profiles
func WithVersion(version string) Option {
return WithTags("version:" + version)
}
// WithTags specifies a set of tags to be attached to the profiler. These may help
// filter the profiling view based on various information.
func WithTags(tags ...string) Option {
return func(cfg *config) {
cfg.tags = append(cfg.tags, tags...)
}
}
// WithStatsd specifies an optional statsd client to use for metrics. By default,
// no metrics are sent.
func WithStatsd(client StatsdClient) Option {
return func(cfg *config) {
cfg.statsd = client
}
}