This repository has been archived by the owner on Nov 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stackdriver.go
66 lines (55 loc) · 1.62 KB
/
stackdriver.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
package monitoring
import (
"context"
"sync"
log "github.com/sirupsen/logrus"
"golang.org/x/oauth2/google"
gmonitoring "google.golang.org/api/monitoring/v3"
)
// Stackdriver is used for creating metrics to write to on Stackdriver
type Stackdriver struct {
ProjectID string
Client *gmonitoring.Service
ctx context.Context
isDebug bool
}
// NewStackdriver is the constructor/factory for the Stackdriver struct
func NewStackdriver(ctx context.Context, isDebug bool, projectID string) (*Stackdriver, error) {
hc, err := google.DefaultClient(ctx, gmonitoring.MonitoringScope)
if err != nil {
log.WithError(err).Error("Failed to create monitoring client")
return nil, err
}
client, err := gmonitoring.New(hc)
if err != nil {
log.WithError(err).Error("Failed to create monitoring service")
return nil, err
}
return &Stackdriver{ProjectID: projectID, Client: client, isDebug: isDebug}, nil
}
// GetProjectID returns the project id
func (s Stackdriver) GetProjectID() string {
return s.ProjectID
}
// CreateMetric creates a metric that can then be written to
func (s Stackdriver) CreateMetric(
metricType string,
metricLabels map[string]string,
resourceType string,
resourceLabels map[string]string,
) *StackdriverAggregation {
sa := &StackdriverAggregation{
projectID: s.ProjectID,
client: s.Client,
ctx: s.ctx,
metricType: metricType,
metricLabels: metricLabels,
resourceType: resourceType,
resourceLabels: resourceLabels,
points: []*gmonitoring.Point{},
mutex: &sync.Mutex{},
isDebug: s.isDebug,
}
sa.startWriteLoop()
return sa
}