-
Notifications
You must be signed in to change notification settings - Fork 9
/
config.go
185 lines (154 loc) · 5.05 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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package rrtemporal
import (
"crypto/tls"
"os"
"time"
"github.com/roadrunner-server/errors"
"github.com/roadrunner-server/pool/pool"
)
// Config of the temporal client and dependent services.
type Config struct {
Metrics *Metrics `mapstructure:"metrics"`
Activities *pool.Config `mapstructure:"activities"`
TLS *TLS `mapstructure:"tls, omitempty"`
Address string `mapstructure:"address"`
Namespace string `mapstructure:"namespace"`
CacheSize int `mapstructure:"cache_size"`
}
const (
MetricsTypeSummary string = "summary"
// metrics types
driverPrometheus string = "prometheus"
driverStatsd string = "statsd"
)
type ClientAuthType string
const (
NoClientCert ClientAuthType = "no_client_cert"
RequestClientCert ClientAuthType = "request_client_cert"
RequireAnyClientCert ClientAuthType = "require_any_client_cert"
VerifyClientCertIfGiven ClientAuthType = "verify_client_cert_if_given"
RequireAndVerifyClientCert ClientAuthType = "require_and_verify_client_cert"
)
// ref:https://github.dev/temporalio/temporal/common/metrics/config.go:79
type Statsd struct {
// The host and port of the statsd server
HostPort string `mapstructure:"host_port" validate:"nonzero"`
// The prefix to use in reporting to statsd
Prefix string `mapstructure:"prefix" validate:"nonzero"`
// FlushInterval is the maximum interval for sending packets.
// If it is not specified, it defaults to 1 second.
FlushInterval time.Duration `mapstructure:"flush_interval"`
// FlushBytes specifies the maximum udp packet size you wish to send.
// If FlushBytes is unspecified, it defaults to 1432 bytes, which is
// considered safe for local traffic.
FlushBytes int `mapstructure:"flush_bytes"`
// Tags to pass to the Tally scope options
Tags map[string]string `mapstructure:"tags"`
// TagPrefix ...
TagPrefix string `mapstructure:"tag_prefix"`
// TagSeparator allows tags to be appended with a separator. If not specified tag keys and values
// are embedded to the stat name directly.
TagSeparator string `mapstructure:"tag_separator"`
}
type StatsdReporterConfig struct {
// TagSeparator allows tags to be appended with a separator. If not specified tag keys and values
// are embedded to the stat name directly.
TagSeparator string `yaml:"tag_separator"`
}
type Prometheus struct {
Address string `mapstructure:"address"`
Type string `mapstructure:"type"`
Prefix string `mapstructure:"prefix"`
}
type Metrics struct {
Driver string `mapstructure:"driver"`
Prometheus *Prometheus `mapstructure:"prometheus"`
Statsd *Statsd `mapstructure:"statsd"`
}
type TLS struct {
Key string `mapstructure:"key"`
Cert string `mapstructure:"cert"`
RootCA string `mapstructure:"root_ca"`
AuthType ClientAuthType `mapstructure:"client_auth_type"`
ServerName string `mapstructure:"server_name"`
// auth type
auth tls.ClientAuthType
}
func (c *Config) InitDefault() error {
const op = errors.Op("init_defaults_temporal")
if c.Activities == nil {
c.Activities = &pool.Config{}
}
c.Activities.InitDefaults()
if c.CacheSize == 0 {
c.CacheSize = 10000
}
if c.Namespace == "" {
c.Namespace = "default"
}
if c.Metrics != nil {
if c.Metrics.Driver == "" {
c.Metrics.Driver = driverPrometheus
}
switch c.Metrics.Driver {
case driverPrometheus:
if c.Metrics.Prometheus == nil {
c.Metrics.Prometheus = &Prometheus{}
}
if c.Metrics.Prometheus.Type == "" {
c.Metrics.Prometheus.Type = MetricsTypeSummary
}
if c.Metrics.Prometheus.Address == "" {
c.Metrics.Prometheus.Address = "127.0.0.1:9091"
}
case driverStatsd:
if c.Metrics.Statsd != nil {
if c.Metrics.Statsd.HostPort == "" {
c.Metrics.Statsd.HostPort = "127.0.0.1:8125"
}
if c.Metrics.Statsd.FlushBytes == 0 {
c.Metrics.Statsd.FlushBytes = 1432
}
}
}
}
if c.TLS != nil {
if _, err := os.Stat(c.TLS.Key); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("private key file '%s' does not exist", c.TLS.Key))
}
return errors.E(op, err)
}
if _, err := os.Stat(c.TLS.Cert); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("public certificate file '%s' does not exist", c.TLS.Cert))
}
return errors.E(op, err)
}
// RootCA is optional, but if provided - check it
if c.TLS.RootCA != "" {
if _, err := os.Stat(c.TLS.RootCA); err != nil {
if os.IsNotExist(err) {
return errors.E(op, errors.Errorf("root CA file '%s' does not exist", c.TLS.RootCA))
}
return errors.E(op, err)
}
// auth type used only for the CA
switch c.TLS.AuthType {
case NoClientCert:
c.TLS.auth = tls.NoClientCert
case RequestClientCert:
c.TLS.auth = tls.RequestClientCert
case RequireAnyClientCert:
c.TLS.auth = tls.RequireAnyClientCert
case VerifyClientCertIfGiven:
c.TLS.auth = tls.VerifyClientCertIfGiven
case RequireAndVerifyClientCert:
c.TLS.auth = tls.RequireAndVerifyClientCert
default:
c.TLS.auth = tls.NoClientCert
}
}
}
return nil
}