forked from alibaba/sentinel-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentity.go
261 lines (225 loc) · 8.3 KB
/
entity.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
// Copyright 1999-2020 Alibaba Group Holding Ltd.
//
// Licensed 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 config
import (
"encoding/json"
"fmt"
"github.com/alibaba/sentinel-golang/core/base"
"github.com/alibaba/sentinel-golang/logging"
"github.com/pkg/errors"
)
type Entity struct {
// Version represents the format version of the entity.
Version string
Sentinel SentinelConfig
}
// SentinelConfig represent the general configuration of Sentinel.
type SentinelConfig struct {
App struct {
// Name represents the name of current running service.
Name string
// Type indicates the classification of the service (e.g. web service, API gateway).
Type int32
}
// Exporter represents configuration items related to exporter, like metric exporter.
Exporter ExporterConfig
// Log represents configuration items related to logging.
Log LogConfig
// Stat represents configuration items related to statistics.
Stat StatConfig
// UseCacheTime indicates whether to cache time(ms)
UseCacheTime bool `yaml:"useCacheTime"`
}
// ExporterConfig represents configuration items related to exporter, like metric exporter.
type ExporterConfig struct {
Metric MetricExporterConfig
}
// MetricExporterConfig represents configuration of metric exporter.
type MetricExporterConfig struct {
// HttpAddr is the http server listen address, like ":8080".
HttpAddr string `yaml:"http_addr"`
// HttpPath is the http request path of access metrics, like "/metrics".
HttpPath string `yaml:"http_path"`
}
// LogConfig represent the configuration of logging in Sentinel.
type LogConfig struct {
// Logger indicates that using logger to replace default logging.
Logger logging.Logger
// Dir represents the log directory path.
Dir string
// UsePid indicates whether the filename ends with the process ID (PID).
UsePid bool `yaml:"usePid"`
// Metric represents the configuration items of the metric log.
Metric MetricLogConfig
}
// MetricLogConfig represents the configuration items of the metric log.
type MetricLogConfig struct {
SingleFileMaxSize uint64 `yaml:"singleFileMaxSize"`
MaxFileCount uint32 `yaml:"maxFileCount"`
FlushIntervalSec uint32 `yaml:"flushIntervalSec"`
}
// StatConfig represents the configuration items of statistics.
type StatConfig struct {
// GlobalStatisticSampleCountTotal and GlobalStatisticIntervalMsTotal is the per resource's global default statistic sliding window config
GlobalStatisticSampleCountTotal uint32 `yaml:"globalStatisticSampleCountTotal"`
GlobalStatisticIntervalMsTotal uint32 `yaml:"globalStatisticIntervalMsTotal"`
// MetricStatisticSampleCount and MetricStatisticIntervalMs is the per resource's default readonly metric statistic
// This default readonly metric statistic must be reusable based on global statistic.
MetricStatisticSampleCount uint32 `yaml:"metricStatisticSampleCount"`
MetricStatisticIntervalMs uint32 `yaml:"metricStatisticIntervalMs"`
System SystemStatConfig `yaml:"system"`
}
// SystemStatConfig represents the configuration items of system statistics.
type SystemStatConfig struct {
// CollectIntervalMs represents the collecting interval of the system metrics collector.
CollectIntervalMs uint32 `yaml:"collectIntervalMs"`
// CollectLoadIntervalMs represents the collecting interval of the system load collector.
CollectLoadIntervalMs uint32 `yaml:"collectLoadIntervalMs"`
// CollectCpuIntervalMs represents the collecting interval of the system cpu usage collector.
CollectCpuIntervalMs uint32 `yaml:"collectCpuIntervalMs"`
// CollectMemoryIntervalMs represents the collecting interval of the system memory usage collector.
CollectMemoryIntervalMs uint32 `yaml:"collectMemoryIntervalMs"`
}
// NewDefaultConfig creates a new default config entity.
func NewDefaultConfig() *Entity {
return &Entity{
Version: "v1",
Sentinel: SentinelConfig{
App: struct {
Name string
Type int32
}{
Name: UnknownProjectName,
Type: DefaultAppType,
},
Log: LogConfig{
Logger: nil,
Dir: GetDefaultLogDir(),
UsePid: false,
Metric: MetricLogConfig{
SingleFileMaxSize: DefaultMetricLogSingleFileMaxSize,
MaxFileCount: DefaultMetricLogMaxFileAmount,
FlushIntervalSec: DefaultMetricLogFlushIntervalSec,
},
},
Stat: StatConfig{
GlobalStatisticSampleCountTotal: base.DefaultSampleCountTotal,
GlobalStatisticIntervalMsTotal: base.DefaultIntervalMsTotal,
MetricStatisticSampleCount: base.DefaultSampleCount,
MetricStatisticIntervalMs: base.DefaultIntervalMs,
System: SystemStatConfig{
CollectIntervalMs: DefaultSystemStatCollectIntervalMs,
CollectLoadIntervalMs: DefaultLoadStatCollectIntervalMs,
CollectCpuIntervalMs: DefaultCpuStatCollectIntervalMs,
CollectMemoryIntervalMs: DefaultMemoryStatCollectIntervalMs,
},
},
UseCacheTime: true,
},
}
}
func CheckValid(entity *Entity) error {
if entity == nil {
return errors.New("Nil entity")
}
if len(entity.Version) == 0 {
return errors.New("Empty version")
}
return checkConfValid(&entity.Sentinel)
}
func checkConfValid(conf *SentinelConfig) error {
if conf == nil {
return errors.New("Nil globalCfg")
}
if conf.App.Name == "" {
return errors.New("App.Name is empty")
}
mc := conf.Log.Metric
if mc.MaxFileCount <= 0 {
return errors.New("Illegal metric log globalCfg: maxFileCount <= 0")
}
if mc.SingleFileMaxSize <= 0 {
return errors.New("Illegal metric log globalCfg: singleFileMaxSize <= 0")
}
if err := base.CheckValidityForReuseStatistic(conf.Stat.MetricStatisticSampleCount, conf.Stat.MetricStatisticIntervalMs,
conf.Stat.GlobalStatisticSampleCountTotal, conf.Stat.GlobalStatisticIntervalMsTotal); err != nil {
return err
}
return nil
}
func (entity *Entity) String() string {
e, err := json.Marshal(entity)
if err != nil {
return fmt.Sprintf("%+v", *entity)
}
return string(e)
}
func (entity *Entity) AppName() string {
return entity.Sentinel.App.Name
}
func (entity *Entity) AppType() int32 {
return entity.Sentinel.App.Type
}
func (entity *Entity) LogBaseDir() string {
return entity.Sentinel.Log.Dir
}
func (entity *Entity) Logger() logging.Logger {
return entity.Sentinel.Log.Logger
}
// LogUsePid returns whether the log file name contains the PID suffix.
func (entity *Entity) LogUsePid() bool {
return entity.Sentinel.Log.UsePid
}
func (entity *Entity) MetricExportHTTPAddr() string {
return entity.Sentinel.Exporter.Metric.HttpAddr
}
func (entity *Entity) MetricExportHTTPPath() string {
return entity.Sentinel.Exporter.Metric.HttpPath
}
func (entity *Entity) MetricLogFlushIntervalSec() uint32 {
return entity.Sentinel.Log.Metric.FlushIntervalSec
}
func (entity *Entity) MetricLogSingleFileMaxSize() uint64 {
return entity.Sentinel.Log.Metric.SingleFileMaxSize
}
func (entity *Entity) MetricLogMaxFileAmount() uint32 {
return entity.Sentinel.Log.Metric.MaxFileCount
}
func (entity *Entity) SystemStatCollectIntervalMs() uint32 {
return entity.Sentinel.Stat.System.CollectIntervalMs
}
func (entity *Entity) LoadStatCollectIntervalMs() uint32 {
return entity.Sentinel.Stat.System.CollectLoadIntervalMs
}
func (entity *Entity) CpuStatCollectIntervalMs() uint32 {
return entity.Sentinel.Stat.System.CollectCpuIntervalMs
}
func (entity *Entity) MemoryStatCollectIntervalMs() uint32 {
return entity.Sentinel.Stat.System.CollectMemoryIntervalMs
}
func (entity *Entity) UseCacheTime() bool {
return entity.Sentinel.UseCacheTime
}
func (entity *Entity) GlobalStatisticIntervalMsTotal() uint32 {
return entity.Sentinel.Stat.GlobalStatisticIntervalMsTotal
}
func (entity *Entity) GlobalStatisticSampleCountTotal() uint32 {
return entity.Sentinel.Stat.GlobalStatisticSampleCountTotal
}
func (entity *Entity) MetricStatisticIntervalMs() uint32 {
return entity.Sentinel.Stat.MetricStatisticIntervalMs
}
func (entity *Entity) MetricStatisticSampleCount() uint32 {
return entity.Sentinel.Stat.MetricStatisticSampleCount
}