This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
forked from cloudfoundry-attic/datadog-firehose-nozzle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
nozzle_config.go
103 lines (91 loc) · 2.84 KB
/
nozzle_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
package nozzleconfig
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"strconv"
"time"
)
type NozzleConfig struct {
UAAURL string
Username string
Password string
TrafficControllerURL string
FirehoseSubscriptionID string
OpenTSDBURL string
FlushDurationSeconds uint32
InsecureSSLSkipVerify bool
MetricPrefix string
Deployment string
DisableAccessControl bool
UseTelnetAPI bool
Job string
Index string
IdleTimeoutSeconds uint32
FirehoseReconnectDelay time.Duration
}
func Parse(configPath string) (*NozzleConfig, error) {
configBytes, err := ioutil.ReadFile(configPath)
var config NozzleConfig
if err != nil {
return nil, fmt.Errorf("Can not read config file [%s]: %s", configPath, err)
}
err = json.Unmarshal(configBytes, &config)
if err != nil {
return nil, fmt.Errorf("Can not parse config file %s: %s", configPath, err)
}
overrideWithEnvVar("NOZZLE_UAAURL", &config.UAAURL)
overrideWithEnvVar("NOZZLE_USERNAME", &config.Username)
overrideWithEnvVar("NOZZLE_PASSWORD", &config.Password)
overrideWithEnvVar("NOZZLE_TRAFFICCONTROLLERURL", &config.TrafficControllerURL)
overrideWithEnvVar("NOZZLE_FIREHOSESUBSCRIPTIONID", &config.FirehoseSubscriptionID)
overrideWithEnvVar("NOZZLE_OPENTSDBURL", &config.OpenTSDBURL)
overrideWithEnvUint32("NOZZLE_FLUSHDURATIONSECONDS", &config.FlushDurationSeconds)
overrideWithEnvBool("NOZZLE_INSECURESSLSKIPVERIFY", &config.InsecureSSLSkipVerify)
overrideWithEnvVar("NOZZLE_METRICPREFIX", &config.MetricPrefix)
overrideWithEnvVar("NOZZLE_DEPLOYMENT", &config.Deployment)
overrideWithEnvBool("NOZZLE_DISABLEACCESSCONTROL", &config.DisableAccessControl)
overrideWithEnvBool("NOZZLE_USETELNETAPI", &config.UseTelnetAPI)
overrideWithEnvVar("NOZZLE_JOB", &config.Job)
overrideWithEnvVar("NOZZLE_INDEX", &config.Index)
overrideWithEnvUint32("NOZZLE_IDLETIMEOUTSECONDS", &config.IdleTimeoutSeconds)
overrideWithEnvDuration("NOZZLE_FIREHOSERECONNECTDELAY", &config.FirehoseReconnectDelay)
return &config, nil
}
func overrideWithEnvVar(name string, value *string) {
envValue := os.Getenv(name)
if envValue != "" {
*value = envValue
}
}
func overrideWithEnvUint32(name string, value *uint32) {
envValue := os.Getenv(name)
if envValue != "" {
tmpValue, err := strconv.Atoi(envValue)
if err != nil {
panic(err)
}
*value = uint32(tmpValue)
}
}
func overrideWithEnvDuration(name string, value *time.Duration) {
envValue := os.Getenv(name)
if envValue != "" {
tmpValue, err := time.ParseDuration(envValue)
if err != nil {
panic(err)
}
*value = tmpValue
}
}
func overrideWithEnvBool(name string, value *bool) {
envValue := os.Getenv(name)
if envValue != "" {
var err error
*value, err = strconv.ParseBool(envValue)
if err != nil {
panic(err)
}
}
}