forked from pachyderm/pachyderm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment.go
73 lines (65 loc) · 1.93 KB
/
segment.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
package metrics
import (
"fmt"
"time"
log "github.com/Sirupsen/logrus"
"github.com/segmentio/analytics-go"
)
const reportingInterval time.Duration = 15 * time.Second
func newPersistentClient() *analytics.Client {
c := newSegmentClient()
c.Interval = reportingInterval
c.Size = 100
return c
}
func newSegmentClient() *analytics.Client {
return analytics.New("hhxbyr7x50w3jtgcwcZUyOFrTf4VNMrD")
}
func reportClusterMetricsToSegment(client *analytics.Client, metrics *Metrics) {
err := client.Track(&analytics.Track{
Event: "cluster.metrics",
AnonymousId: metrics.ClusterID,
Properties: map[string]interface{}{
"PodID": metrics.PodID,
"nodes": metrics.Nodes,
"version": metrics.Version,
"repos": metrics.Repos,
"commits": metrics.Commits,
"files": metrics.Files,
"bytes": metrics.Bytes,
"jobs": metrics.Jobs,
"pipelines": metrics.Pipelines,
},
})
if err != nil {
log.Errorf("error reporting cluster metrics to Segment: %s", err.Error())
}
}
/*
Segment needs us to identify a user before we report any events for that user.
We have no way of knowing if a user has previously been identified, so we call this
before every `Track()` call containing user data.
*/
func identifyUser(client *analytics.Client, userID string) {
err := client.Identify(&analytics.Identify{
UserId: userID,
})
if err != nil {
log.Errorf("error reporting user identity to Segment: %s", err.Error())
}
}
func reportUserMetricsToSegment(client *analytics.Client, userID string, prefix string, action string, value interface{}, clusterID string) {
identifyUser(client, userID)
properties := map[string]interface{}{
"ClusterID": clusterID,
}
properties[action] = value
err := client.Track(&analytics.Track{
Event: fmt.Sprintf("%v.usage", prefix),
UserId: userID,
Properties: properties,
})
if err != nil {
log.Errorf("error reporting user action to Segment: %s", err.Error())
}
}