forked from datastax/pulsar-heartbeat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
309 lines (271 loc) · 10.1 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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//
// Copyright (c) 2020-2021 Datastax, Inc.
//
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF 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 cfg
import (
"bytes"
"context"
"encoding/json"
"io/ioutil"
"os"
"strings"
"time"
"unicode"
"golang.org/x/oauth2/clientcredentials"
"github.com/datastax/pulsar-heartbeat/src/util"
"github.com/apex/log"
"github.com/ghodss/yaml"
)
// PrometheusCfg configures Premetheus set up
type PrometheusCfg struct {
Port string `json:"port"`
ExposeMetrics bool `json:"exposeMetrics"`
PrometheusProxyURL string `json:"prometheusProxyURL"`
PrometheusProxyAPIKey string `json:"prometheusProxyAPIKey"`
}
// SlackCfg is slack configuration
type SlackCfg struct {
AlertURL string `json:"alertUrl"`
Verbose bool `json:"verbose"`
}
// OpsGenieCfg is opsGenie configuration
type OpsGenieCfg struct {
HeartBeatURL string `json:"heartbeatUrl"`
HeartbeatKey string `json:"heartbeatKey"`
AlertKey string `json:"alertKey"`
IntervalSeconds int `json:"intervalSeconds"`
}
// PagerDutyCfg is opsGenie configuration
type PagerDutyCfg struct {
IntegrationKey string `json:"integrationKey"`
}
// AnalyticsCfg is analytics usage and statistucs tracking configuration
type AnalyticsCfg struct {
APIKey string `json:"apiKey"`
IngestionURL string `json:"ingestionUrl"`
InsightsWriteKey string `json:"insightsWriteKey"`
InsightsAccountID string `json:"insightsAccountId"`
}
// SiteCfg configures general website
type SiteCfg struct {
Headers map[string]string `json:"headers"`
URL string `json:"url"`
Name string `json:"name"`
IntervalSeconds int `json:"intervalSeconds"`
ResponseSeconds int `json:"responseSeconds"`
StatusCode int `json:"statusCode"`
StatusCodeExpr string `json:"statusCodeExpr"`
Retries int `json:"retries"`
AlertPolicy AlertPolicyCfg `json:"alertPolicy"`
}
// SitesCfg configures a list of website`
type SitesCfg struct {
Sites []SiteCfg `json:"sites"`
}
// OpsClusterCfg is each cluster's configuration
type OpsClusterCfg struct {
Name string `json:"name"`
URL string `json:"url"`
AlertPolicy AlertPolicyCfg `json:"alertPolicy"`
}
// PulsarAdminRESTCfg is for monitor a list of Pulsar cluster
type PulsarAdminRESTCfg struct {
Token string `json:"Token"`
Clusters []OpsClusterCfg `json:"clusters"`
IntervalSeconds int `json:"intervalSeconds"`
}
// TopicCfg is topic configuration
type TopicCfg struct {
Name string `json:"name"`
ClusterName string `json:"clusterName"` // used for broker monitoring if specified
Token string `json:"token"`
TrustStore string `json:"trustStore"`
NumberOfPartitions int `json:"numberOfPartitions"`
LatencyBudgetMs int `json:"latencyBudgetMs"`
PulsarURL string `json:"pulsarUrl"`
AdminURL string `json:"adminUrl"`
TopicName string `json:"topicName"`
OutputTopic string `json:"outputTopic"`
IntervalSeconds int `json:"intervalSeconds"`
ExpectedMsg string `json:"expectedMsg"`
PayloadSizes []string `json:"payloadSizes"`
NumberOfMessages int `json:"numberOfMessages"`
AlertPolicy AlertPolicyCfg `json:"AlertPolicy"`
DowntimeTrackerDisabled bool `json:"downtimeTrackerDisabled"`
RemoteClusters []string `json:"RemoteClusters"`
}
// WsConfig is configuration to monitor WebSocket pub sub latency
type WsConfig struct {
Name string `json:"name"`
Token string `json:"token"`
Cluster string `json:"cluster"` // can be used for alert de-dupe
LatencyBudgetMs int `json:"latencyBudgetMs"`
ProducerURL string `json:"producerUrl"`
ConsumerURL string `json:"consumerUrl"`
TopicName string `json:"topicName"`
IntervalSeconds int `json:"intervalSeconds"`
Scheme string `json:"scheme"`
Port string `json:"port"`
Subscription string `json:"subscription"`
URLQueryParams string `json:"urlQueryParams"`
AlertPolicy AlertPolicyCfg `json:"AlertPolicy"`
}
// K8sClusterCfg is configuration to monitor kubernete cluster
// only to be enabled in-cluster monitoring
type K8sClusterCfg struct {
Enabled bool `json:"enabled"`
PulsarNamespace string `json:"pulsarNamespace"`
KubeConfigDir string `json:"kubeConfigDir"`
AlertPolicy AlertPolicyCfg `json:"AlertPolicy"`
}
// BrokersCfg monitors all brokers in the cluster
type BrokersCfg struct {
BrokerTestRequired bool `json:"brokerTestRequired"`
InClusterRESTURL string `json:"inclusterRestURL"`
IntervalSeconds int `json:"intervalSeconds"`
AlertPolicy AlertPolicyCfg `json:"AlertPolicy"`
}
// TenantUsageCfg tenant usage reporting and monitoring
type TenantUsageCfg struct {
OutBytesLimit uint64 `json:"outBytesLimit"`
AlertIntervalMinutes int `json:"alertIntervalMinutes"`
}
// Configuration - this server's configuration
type Configuration struct {
// Name is the Pulsar cluster name, it is mandatory
Name string `json:"name"`
// ClusterName is the Pulsar cluster name if the Name cannot be used as the Pulsar cluster name, optional
ClusterName string `json:"clusterName"`
TokenOAuthConfig *clientcredentials.Config `json:"tokenOAuthConfig"`
// TokenFilePath is the file path to Pulsar JWT. It takes precedence of the token attribute.
TokenFilePath string `json:"tokenFilePath"`
// Token is a Pulsar JWT can be used for both client client or http admin client
Token string `json:"token"`
BrokersConfig BrokersCfg `json:"brokersConfig"`
TrustStore string `json:"trustStore"`
K8sConfig K8sClusterCfg `json:"k8sConfig"`
AnalyticsConfig AnalyticsCfg `json:"analyticsConfig"`
PrometheusConfig PrometheusCfg `json:"prometheusConfig"`
SlackConfig SlackCfg `json:"slackConfig"`
OpsGenieConfig OpsGenieCfg `json:"opsGenieConfig"`
PagerDutyConfig PagerDutyCfg `json:"pagerDutyConfig"`
PulsarAdminConfig PulsarAdminRESTCfg `json:"pulsarAdminRestConfig"`
PulsarTopicConfig []TopicCfg `json:"pulsarTopicConfig"`
SitesConfig SitesCfg `json:"sitesConfig"`
WebSocketConfig []WsConfig `json:"webSocketConfig"`
TenantUsageConfig TenantUsageCfg `json:"tenantUsageConfig"`
tokenFunc func() (string, error)
}
func (c *Configuration) Init() {
if len(c.Name) < 1 {
panic("a valid `name` in Configuration must be specified")
}
// reconcile the JWT
if len(c.TokenFilePath) > 1 {
tokenBytes, err := ioutil.ReadFile(c.TokenFilePath)
if err != nil {
log.Errorf("failed to read Pulsar JWT from a file %s", c.TokenFilePath)
} else {
log.Infof("read Pulsar token from the file %s", c.TokenFilePath)
c.Token = string(tokenBytes)
}
}
c.Token = strings.TrimSuffix(util.AssignString(c.Token, os.Getenv("PulsarToken")), "\n")
if c.TokenOAuthConfig != nil {
tokenSrc := c.TokenOAuthConfig.TokenSource(context.Background())
c.tokenFunc = func() (string, error) {
ot, err := tokenSrc.Token()
if err != nil {
return "", err
}
return ot.AccessToken, nil
}
} else if c.Token != "" {
c.tokenFunc = func() (string, error) {
return c.Token, nil
}
}
for k, v := range c.PulsarTopicConfig {
c.PulsarTopicConfig[k].RemoteClusters = append(v.RemoteClusters, c.Name) //TODO best option ?
}
}
func (c *Configuration) TokenSupplier() func() (string, error) {
return c.tokenFunc
}
// AlertPolicyCfg is a set of criteria to evaluation triggers for incident alert
type AlertPolicyCfg struct {
// first evaluation to count continuous failure
Ceiling int `json:"ceiling"`
// Second evaluation for moving window
MovingWindowSeconds int `json:"movingWindowSeconds"`
CeilingInMovingWindow int `json:"ceilingInMovingWindow"`
}
// Config - this server's configuration instance
var Config Configuration
// ReadConfigFile reads configuration file.
func ReadConfigFile(configFile string) {
fileBytes, err := ioutil.ReadFile(configFile)
if err != nil {
log.Errorf("failed to load configuration file %s", configFile)
panic(err)
}
if hasJSONPrefix(fileBytes) {
err = json.Unmarshal(fileBytes, &Config)
if err != nil {
panic(err)
}
} else {
err = yaml.Unmarshal(fileBytes, &Config)
if err != nil {
panic(err)
}
}
Config.Init()
log.Infof("config %v", Config)
}
var jsonPrefix = []byte("{")
func hasJSONPrefix(buf []byte) bool {
return hasPrefix(buf, jsonPrefix)
}
// Return true if the first non-whitespace bytes in buf is prefix.
func hasPrefix(buf []byte, prefix []byte) bool {
trim := bytes.TrimLeftFunc(buf, unicode.IsSpace)
return bytes.HasPrefix(trim, prefix)
}
//GetConfig returns a reference to the Configuration
func GetConfig() *Configuration {
return &Config
}
//
type monitorFunc func()
// RunInterval runs interval
func RunInterval(fn monitorFunc, interval time.Duration) {
go func() {
ticker := time.NewTicker(interval)
defer ticker.Stop()
fn()
for {
select {
case <-ticker.C:
fn()
}
}
}()
}