-
Notifications
You must be signed in to change notification settings - Fork 4
/
gcpservice.go
303 lines (254 loc) · 7.7 KB
/
gcpservice.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
// Copyright © 2019 Circonus, Inc. <support@circonus.com>
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package gcpservice
import (
"context"
"encoding/json"
"fmt"
"io"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/circonus-labs/circonus-cloud-agent/internal/circonus"
"github.com/circonus-labs/circonus-cloud-agent/internal/config"
"github.com/circonus-labs/circonus-cloud-agent/internal/config/defaults"
"github.com/circonus-labs/circonus-cloud-agent/internal/release"
"github.com/circonus-labs/circonus-cloud-agent/internal/services/gcpservice/collectors"
"github.com/pelletier/go-toml"
"github.com/pkg/errors"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
"gopkg.in/yaml.v2"
)
const (
// KeyEnabled toggles whether the GCP module is active or not.
KeyEnabled = "gcp.enabled"
// DefaultEnabled defines the default setting.
DefaultEnabled = false
// KeyConfDir defines the gcp configuration directory.
KeyConfDir = "gcp.conf_dir"
// KeyConfExample shows an example configuration.
KeyConfExample = "gcp.config_example"
)
var (
// DefaultConfDir is the default location of gcp configuration files.
DefaultConfDir = path.Join(defaults.EtcPath, "gcp.d")
)
// GCPService defines the GCP cloud service client.
type GCPService struct {
logger zerolog.Logger
groupCtx context.Context
group *errgroup.Group
instances []*Instance
enabled bool
}
// New returns a GCP metric collector service.
func New(ctx context.Context) (*GCPService, error) {
if fmt := viper.GetString(KeyConfExample); fmt != "" {
if err := showExampleConfig(fmt, os.Stdout); err != nil {
log.Error().Err(err).Msg("generating example configuration")
}
os.Exit(0)
}
g, gctx := errgroup.WithContext(ctx)
svc := GCPService{
enabled: viper.GetBool(KeyEnabled),
group: g,
groupCtx: gctx,
instances: make([]*Instance, 0),
logger: log.With().Str("pkg", "gcp").Logger(),
}
if !svc.enabled {
return &svc, nil
}
svc.logger.Debug().Msg("enabled, initializing client")
confDir := viper.GetString(KeyConfDir)
if confDir == "" {
confDir = DefaultConfDir
}
if err := svc.initInstances(confDir); err != nil {
return nil, errors.Wrap(err, "initializing telemetry collector(s)")
}
if len(svc.instances) == 0 {
svc.logger.Info().Msg("disabling, no telemetry collector(s) initialized")
svc.enabled = false
}
return &svc, nil
}
// Enabled indicates whether the GCP service is enabled.
func (svc *GCPService) Enabled() bool {
return svc.enabled
}
// Scan checks the service config directory for configurations and loads them.
func (svc *GCPService) Scan() error {
if !svc.enabled {
svc.logger.Info().Msg("client disabled, not checking for configurations")
return nil
}
svc.logger.Info().Msg("client checking for configuration(s)")
return errors.New("not implemented")
}
// Start begins collecting metrics from GCP.
func (svc *GCPService) Start() error {
if !svc.enabled {
svc.logger.Info().Msg("client disabled, not starting")
return nil
}
svc.logger.Info().Msg("client starting")
for _, instance := range svc.instances {
inst := instance
svc.group.Go(inst.Start)
}
go func() {
if err := svc.group.Wait(); err != nil {
svc.logger.Error().Err(err).Msg("waiting for service group")
}
}()
return svc.group.Wait()
}
func (svc *GCPService) initInstances(confDir string) error {
if confDir == "" {
return errors.New("invalid config dir (empty)")
}
entries, err := os.ReadDir(confDir)
if err != nil {
return errors.Wrap(err, "reading GCP config dir")
}
for _, entry := range entries {
entry := entry
if entry.IsDir() {
continue
}
if !strings.Contains(".json|.toml|.yaml", filepath.Ext(entry.Name())) { //nolint:gocritic
continue
}
cfgFile := path.Join(confDir, entry.Name())
instance, err := svc.instanceFromConfig(cfgFile)
if err != nil {
svc.logger.Error().Err(err).Str("config_file", cfgFile).Msg("skipping")
continue
}
svc.instances = append(svc.instances, instance)
}
if len(svc.instances) == 0 {
return errors.New("no valid gcp configs found")
}
return nil
}
func (svc *GCPService) instanceFromConfig(cfgFile string) (*Instance, error) {
var cfg Config
if err := config.LoadConfigFile(cfgFile, &cfg); err != nil {
return nil, errors.Wrap(err, "loading config file")
}
if cfg.ID == "" {
return nil, errors.New("invalid config ID (empty)")
}
if strings.Contains(cfg.ID, " ") {
return nil, errors.New("invalid config ID (contains spaces)")
}
instance := &Instance{
cfg: &cfg,
ctx: svc.groupCtx,
logger: svc.logger.With().Str("id", cfg.ID).Logger(),
}
if cfg.GCP.CredentialsFile == "" {
return nil, errors.New("invalid GCP credentials file (empty)")
}
cfgFile, err := config.VerifyFile(instance.cfg.GCP.CredentialsFile)
if err != nil {
return nil, errors.Wrap(err, "invalid GCP credentials file")
}
if !strings.HasPrefix(cfgFile, string(filepath.Separator)) {
cfgFile = filepath.Join(defaults.EtcPath, cfgFile)
}
instance.cfg.GCP.CredentialsFile = cfgFile
data, err := os.ReadFile(instance.cfg.GCP.CredentialsFile)
if err != nil {
return nil, errors.Wrap(err, "loading GCP credentials file")
}
instance.cfg.GCP.credentialData = data
var v struct {
ProjectID string `json:"project_id"`
}
if err = json.Unmarshal(data, &v); err != nil {
return nil, err
}
instance.cfg.GCP.projectID = v.ProjectID
if err = instance.loadProjectMeta(); err != nil {
return nil, err
}
if cfg.GCP.Interval < defaultInterval {
cfg.GCP.Interval = defaultInterval
}
checkConfig := &circonus.Config{
ID: "gcp_" + instance.cfg.ID,
DisplayName: fmt.Sprintf("%s %s %s/gcp", instance.cfg.ID, instance.cfg.GCP.projectName, release.NAME),
CheckBundleID: cfg.Circonus.CID,
APIKey: cfg.Circonus.Key,
APIApp: cfg.Circonus.App,
APIURL: cfg.Circonus.URL,
Debug: cfg.Circonus.Debug,
TraceMetrics: cfg.Circonus.TraceMetrics,
Logger: instance.logger,
Tags: release.NAME + ":gcp",
}
if len(instance.cfg.Tags) > 0 { // if top-level tags are configured, add them to check
tags := make([]string, len(instance.cfg.Tags))
for idx, tag := range instance.cfg.Tags {
tags[idx] = tag.Category + ":" + tag.Value
}
checkConfig.Tags += "," + strings.Join(tags, ",")
}
if len(instance.baseTags) > 0 { // if project-level tags were defined, add them to check
tags := make([]string, len(instance.baseTags))
for idx, tag := range instance.baseTags {
tags[idx] = tag.Category + ":" + tag.Value
}
checkConfig.Tags += "," + strings.Join(tags, ",")
}
chk, err := circonus.NewCheck("gcp", checkConfig)
if err != nil {
return nil, errors.Wrap(err, "creating/retrieving circonus check")
}
instance.check = chk
// initialize collectors
pollingInterval := time.Duration(cfg.GCP.Interval) * time.Minute
ms, err := collectors.New(instance.ctx, instance.check, cfg.GCP.Collectors, pollingInterval, instance.logger)
if err != nil {
return nil, err
}
instance.collectors = ms
return instance, nil
}
func showExampleConfig(format string, w io.Writer) error {
var err error
var data []byte
cfg := &Config{}
cfg.GCP.Interval = 5
cc, err := collectors.ConfigExample()
if err != nil {
return errors.Wrap(err, "generating example service collectors config")
}
cfg.GCP.Collectors = cc
switch format {
case "json":
data, err = json.MarshalIndent(cfg, " ", " ")
case "yaml":
data, err = yaml.Marshal(cfg)
case "toml":
data, err = toml.Marshal(*cfg)
default:
return errors.Errorf("unknown config format '%s'", format)
}
if err != nil {
return errors.Wrapf(err, "formatting config (%s)", format)
}
_, err = fmt.Fprintf(w, "\n%s\n", data)
return err
}