-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
158 lines (137 loc) · 3.87 KB
/
config.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
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package tracegen
import (
"errors"
"fmt"
"math"
"os"
"go.elastic.co/apm/v2"
)
type ConfigOption func(*Config)
type Config struct {
apmServerURL string
apiKey string
sampleRate float64
traceID apm.TraceID
insecure bool
apmServiceName string
otlpServiceName string
otlpProtocol string
}
func NewConfig(opts ...ConfigOption) Config {
cfg := Config{
sampleRate: 1.0,
traceID: NewRandomTraceID(),
insecure: false,
otlpProtocol: "grpc",
}
for _, opt := range opts {
opt(&cfg)
}
cfg.configureEnv()
return cfg
}
// WithSampleRate specifies the sample rate for the APM GO Agent
func WithSampleRate(r float64) ConfigOption {
return func(c *Config) {
c.sampleRate = math.Round(r*10000) / 10000
}
}
// WithAPMServerURL set APM Server URL (env value ELASTIC_APM_SERVER_URL)
func WithAPMServerURL(a string) ConfigOption {
return func(c *Config) {
c.apmServerURL = a
}
}
// WithAPIKey sets auth apiKey to communicate with APM Server
func WithAPIKey(k string) ConfigOption {
return func(c *Config) {
c.apiKey = k
}
}
// WithTraceID specifies the user defined traceID
func WithTraceID(t apm.TraceID) ConfigOption {
return func(c *Config) {
c.traceID = t
}
}
// WithInsecureConn skip the server's TLS certificate verification
func WithInsecureConn(b bool) ConfigOption {
return func(c *Config) {
c.insecure = b
}
}
// WithElasticAPMServiceName specifies the service name that
// the Elastic APM agent will use.
//
// This config will be ignored when using SendOTLPTrace.
func WithElasticAPMServiceName(s string) ConfigOption {
return func(c *Config) {
c.apmServiceName = s
}
}
// WithOTLPServiceName specifies the service name that the
// OpenTelemetry SDK will use.
//
// This config will be ignored when using SendIntakeV2Trace.
func WithOTLPServiceName(s string) ConfigOption {
return func(c *Config) {
c.otlpServiceName = s
}
}
// WithOTLPProtocol specifies OTLP transport protocol to one of:
// grpc (default), http/protobuf.
//
// This config will be ignored when using SendIntakeV2Trace
func WithOTLPProtocol(p string) ConfigOption {
return func(c *Config) {
c.otlpProtocol = p
}
}
func (cfg Config) validate() error {
var errs []error
if cfg.sampleRate < 0.0001 || cfg.sampleRate > 1.0 {
errs = append(errs,
fmt.Errorf("invalid sample rate %f provided. allowed value: 0.0001 <= sample-rate <= 1.0", cfg.sampleRate),
)
}
if cfg.apmServerURL == "" {
errs = append(errs, errors.New("APM Server URL must be configured"))
}
if cfg.apiKey == "" {
errs = append(errs, errors.New("API Key must be configured"))
}
return errors.Join(errs...)
}
// configureEnv parses or sets env configs to work with both Elastic GO Agent and OTLP library
func (cfg *Config) configureEnv() error {
if cfg.apiKey == "" {
cfg.apiKey = os.Getenv("ELASTIC_APM_API_KEY")
} else {
os.Setenv("ELASTIC_APM_API_KEY", cfg.apiKey)
}
if cfg.apmServerURL == "" {
cfg.apmServerURL = os.Getenv("ELASTIC_APM_SERVER_URL")
} else {
os.Setenv("ELASTIC_APM_SERVER_URL", cfg.apmServerURL)
}
if cfg.insecure {
os.Setenv("ELASTIC_APM_VERIFY_SERVER_CERT", "false")
}
return nil
}