/
run.go
225 lines (199 loc) · 7.36 KB
/
run.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
/*
Copyright (c) 2021-2022 Dell Inc. or its subsidiaries. All Rights Reserved.
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 entrypoint
import (
"context"
"fmt"
"os"
"runtime"
"time"
pstoreServices "github.com/dell/csm-metrics-powerstore/internal/service"
otlexporters "github.com/dell/csm-metrics-powerstore/opentelemetry/exporters"
tracer "github.com/dell/csm-metrics-powerstore/opentelemetry/tracers"
"github.com/sirupsen/logrus"
"go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc"
"google.golang.org/grpc/credentials"
)
const (
// MaximumVolTickInterval is the maximum allowed interval when querying volume metrics
MaximumVolTickInterval = 10 * time.Minute
// MinimumVolTickInterval is the minimum allowed interval when querying volume metrics
MinimumVolTickInterval = 5 * time.Second
// DefaultEndPoint for leader election path
DefaultEndPoint = "karavi-metrics-powerstore"
// DefaultNameSpace for PowerStore pod running metrics collection
DefaultNameSpace = "karavi"
)
// ConfigValidatorFunc is used to override config validation in testing
var ConfigValidatorFunc = ValidateConfig
// Config holds data that will be used by the service
type Config struct {
VolumeTickInterval time.Duration
SpaceTickInterval time.Duration
ArrayTickInterval time.Duration
FileSystemTickInterval time.Duration
LeaderElector pstoreServices.LeaderElector
VolumeMetricsEnabled bool
CollectorAddress string
CollectorCertPath string
Logger *logrus.Logger
}
// Run is the entry point for starting the service
func Run(ctx context.Context, config *Config, exporter otlexporters.Otlexporter, powerStoreSvc pstoreServices.Service) error {
err := ConfigValidatorFunc(config)
if err != nil {
return err
}
logger := config.Logger
errCh := make(chan error, 1)
go func() {
powerstoreEndpoint := os.Getenv("POWERSTORE_METRICS_ENDPOINT")
if powerstoreEndpoint == "" {
powerstoreEndpoint = DefaultEndPoint
}
powerstoreNamespace := os.Getenv("POWERSTORE_METRICS_NAMESPACE")
if powerstoreNamespace == "" {
powerstoreNamespace = DefaultNameSpace
}
errCh <- config.LeaderElector.InitLeaderElection(powerstoreEndpoint, powerstoreNamespace)
}()
go func() {
options := []otlpmetricgrpc.Option{
otlpmetricgrpc.WithEndpoint(config.CollectorAddress),
}
if config.CollectorCertPath != "" {
transportCreds, err := credentials.NewClientTLSFromFile(config.CollectorCertPath, "")
if err != nil {
errCh <- err
}
options = append(options, otlpmetricgrpc.WithTLSCredentials(transportCreds))
} else {
options = append(options, otlpmetricgrpc.WithInsecure())
}
errCh <- exporter.InitExporter(options...)
}()
defer exporter.StopExporter()
runtime.GOMAXPROCS(runtime.NumCPU())
// set initial tick intervals
VolumeTickInterval := config.VolumeTickInterval
volumeTicker := time.NewTicker(VolumeTickInterval)
SpaceTickInterval := config.SpaceTickInterval
spaceTicker := time.NewTicker(SpaceTickInterval)
ArrayTickInterval := config.ArrayTickInterval
arrayTicker := time.NewTicker(ArrayTickInterval)
FileSystemTickInterval := config.FileSystemTickInterval
filesystemTicker := time.NewTicker(FileSystemTickInterval)
for {
select {
case <-volumeTicker.C:
ctx, span := tracer.GetTracer(ctx, "volume-metrics")
if !config.LeaderElector.IsLeader() {
logger.Info("not leader pod to collect metrics")
span.End()
continue
}
if !config.VolumeMetricsEnabled {
logger.Info("powerstore volume metrics collection is disabled")
span.End()
continue
}
powerStoreSvc.ExportVolumeStatistics(ctx)
span.End()
case <-spaceTicker.C:
ctx, span := tracer.GetTracer(ctx, "volume-space-metrics")
if !config.LeaderElector.IsLeader() {
logger.Info("not leader pod to collect metrics")
span.End()
continue
}
if !config.VolumeMetricsEnabled {
logger.Info("powerstore volume metrics collection is disabled")
span.End()
continue
}
powerStoreSvc.ExportSpaceVolumeMetrics(ctx)
span.End()
case <-arrayTicker.C:
ctx, span := tracer.GetTracer(ctx, "array-space-metrics")
if !config.LeaderElector.IsLeader() {
logger.Info("not leader pod to collect metrics")
span.End()
continue
}
if !config.VolumeMetricsEnabled {
logger.Info("powerstore volume metrics collection is disabled")
span.End()
continue
}
powerStoreSvc.ExportArraySpaceMetrics(ctx)
span.End()
case <-filesystemTicker.C:
ctx, span := tracer.GetTracer(ctx, "filesystem-metrics")
if !config.LeaderElector.IsLeader() {
logger.Info("not leader pod to collect metrics")
span.End()
continue
}
if !config.VolumeMetricsEnabled {
logger.Info("powerstore filesystem metrics collection is disabled")
span.End()
continue
}
powerStoreSvc.ExportFileSystemStatistics(ctx)
span.End()
case err := <-errCh:
if err == nil {
continue
}
return err
case <-ctx.Done():
return nil
}
// check if tick interval config settings have changed
if VolumeTickInterval != config.VolumeTickInterval {
VolumeTickInterval = config.VolumeTickInterval
volumeTicker = time.NewTicker(VolumeTickInterval)
}
if SpaceTickInterval != config.SpaceTickInterval {
SpaceTickInterval = config.SpaceTickInterval
spaceTicker = time.NewTicker(SpaceTickInterval)
}
if ArrayTickInterval != config.ArrayTickInterval {
ArrayTickInterval = config.ArrayTickInterval
arrayTicker = time.NewTicker(ArrayTickInterval)
}
if FileSystemTickInterval != config.FileSystemTickInterval {
FileSystemTickInterval = config.FileSystemTickInterval
filesystemTicker = time.NewTicker(FileSystemTickInterval)
}
}
}
// ValidateConfig will validate the configuration and return any errors
func ValidateConfig(config *Config) error {
if config == nil {
return fmt.Errorf("no config provided")
}
if config.VolumeTickInterval > MaximumVolTickInterval || config.VolumeTickInterval < MinimumVolTickInterval {
return fmt.Errorf("volume polling frequency not within allowed range of %v and %v", MinimumVolTickInterval.String(), MaximumVolTickInterval.String())
}
if config.SpaceTickInterval > MaximumVolTickInterval || config.SpaceTickInterval < MinimumVolTickInterval {
return fmt.Errorf("volume space polling frequency not within allowed range of %v and %v", MinimumVolTickInterval.String(), MaximumVolTickInterval.String())
}
if config.ArrayTickInterval > MaximumVolTickInterval || config.ArrayTickInterval < MinimumVolTickInterval {
return fmt.Errorf("array space polling frequency not within allowed range of %v and %v", MinimumVolTickInterval.String(), MaximumVolTickInterval.String())
}
if config.FileSystemTickInterval > MaximumVolTickInterval || config.FileSystemTickInterval < MinimumVolTickInterval {
return fmt.Errorf("filesystem polling frequency not within allowed range of %v and %v", MinimumVolTickInterval.String(), MaximumVolTickInterval.String())
}
return nil
}