-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUsageLoggers.go
138 lines (119 loc) · 3.49 KB
/
UsageLoggers.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
// © 2016-2024 Graylog, Inc.
package logger
import (
"log"
"os"
"strconv"
"sync"
"time"
"github.com/joho/godotenv"
)
// sync.Once for UsageLoggers
var onceUsageLoggers sync.Once
type UsageLoggers struct {
bricked bool
disabled bool
}
type TLSConfig struct {
timeout time.Duration
customCert string
insecure bool
}
var usageLoggers *UsageLoggers
func getEnvVar(key string) string {
// Override existing (or not) env vars with values from .env (e.g. test/dev envs)
_ = godotenv.Overload()
// If there's no .env file, the value will be loaded from the existing (or not) env vars for the current process
value := os.Getenv(key)
return value
}
func GetUsageLoggers() (*UsageLoggers, error) {
//var envError error
var parseError error
onceUsageLoggers.Do(func() {
//lookup returns a false bool if it fails, along with a nil value.
//We can ignore this because parsebool will throw an error anyway if this fails
envLookup, _ := os.LookupEnv("USAGE_LOGGERS_DISABLE")
if envLookup == "" { // added to avoid failing test, I'm still not sure the best way to test the env variable lookups in internal testing.
envLookup = "false"
}
_bricked, err := strconv.ParseBool(envLookup)
parseError = err
usageLoggers = &UsageLoggers{
bricked: _bricked,
disabled: _bricked,
}
})
return usageLoggers, parseError
}
/**
* Enable all usage loggers, except those explicitly disabled.
*/
func (uLogger *UsageLoggers) Enable() {
if !uLogger.bricked {
uLogger.disabled = false
}
}
/**
* Disable all usage loggers.
*/
func (uLogger *UsageLoggers) Disable() {
uLogger.disabled = true
}
/**
* Returns true if usage loggers are generally enabled.
*/
func (uLogger *UsageLoggers) IsEnabled() bool {
return !uLogger.disabled
}
/**
* Returns url to use by default.
*/
func (uLogger *UsageLoggers) UrlByDefault() string {
url := getEnvVar("USAGE_LOGGERS_URL")
if url == "" {
log.Println("USAGE_LOGGERS_URL env var not set or does not exist; logger disabled")
}
return url
}
/**
* Returns additional configuration for the base logger
*/
func (uLogger *UsageLoggers) ConfigByDefault() map[string]int {
config := map[string]int{
"BODY_LIMIT": 1024 * 1024, // HTTP payload bytes
"BUNDLE_SIZE": 5 * 1024 * 1024, // NDJSON bundle size, in bytes
"MESSAGE_QUEUE_SIZE": 1000, // Number of buffered messages
"BUNDLE_QUEUE_SIZE": 128, // Number of buffered NDJSON bundles
}
for key := range config {
value := getEnvVar("USAGE_LOGGERS_" + key)
if parsedValue, err := strconv.Atoi(value); err == nil {
config[key] = parsedValue
}
}
return config
}
/**
* Returns additional TLS configuration for the internal http client
*/
func (uLogger *UsageLoggers) TLSConfigByDefault() TLSConfig {
config := TLSConfig{
timeout: 0 * time.Second, // Time to wait for TLS Handshake. Zero means no timeout
customCert: "", // TLS certificate filepath to assign custom CA (e.g. self-signed cert)
insecure: false, // Skip certificate verification. It is not recommended to modify this value.
}
value := getEnvVar("USAGE_LOGGERS_TLS_TIMEOUT")
if parsedValue, err := strconv.Atoi(value); err == nil {
config.timeout = time.Duration(parsedValue) * time.Second
}
value = getEnvVar("USAGE_LOGGERS_TLS_CUSTOM_CERT")
if _, err := os.Stat(value); err == nil {
config.customCert = value
}
value = getEnvVar("USAGE_LOGGERS_TLS_INSECURE")
if parsedValue, err := strconv.ParseBool(value); err == nil {
config.insecure = parsedValue
}
return config
}