This repository has been archived by the owner on Sep 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
telemetry.go
109 lines (96 loc) · 2.76 KB
/
telemetry.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
package main
import (
"fmt"
"os"
"os/user"
"time"
)
// events
const (
telemetryUsageOpen = iota
telemetryUsageOnline = iota
telemetryUsageOffline = iota
telemetryStreamName = "REPLAY_ZERO_TELEMETRY_STREAM"
telemetryStreamRole = "REPLAY_ZERO_TELEMETRY_ROLE"
)
type telemetryAgent interface {
logUsage(event int)
}
// No-op agent in case the proper variables are not set
type nopTelemetryAgent struct{}
// Agent to send telemetry to a Kinesis stream
type kinesisTelemetryAgent struct {
stream string
client *kinesisWrapper
}
// logInfo contains data to send to the HTTP endpoint.
type logInfo struct {
Username string
Mode string
Message string
Timestamp int64
}
// Factory-type builder for telemetry agent, in case there's want / need
// to add different telemetry sinks in the future
func getTelemetryAgent() telemetryAgent {
streamName := os.Getenv(telemetryStreamName)
streamRole := os.Getenv(telemetryStreamRole)
if streamName == "" {
logDebug("Missing telemetry stream name, returning no-op agent (will not send telemetry)")
return &nopTelemetryAgent{}
}
logDebug("Building Kinesis agent for sending telemetry")
return &kinesisTelemetryAgent{
stream: streamName,
client: buildClient(streamName, streamRole, logDebug),
}
}
func (agent *nopTelemetryAgent) logUsage(event int) {
logDebug("Telemetry: NO-OP")
}
// logUsage sends usage information - when enabled - to
// an HTTP endpoint. Whether or not this func sends data
// is dependent on the `telemetryEndpoint` global.
func (agent *kinesisTelemetryAgent) logUsage(event int) {
// convert the telemetry events from ints to strings
eventMessage := ""
mode := ""
switch event {
case telemetryUsageOpen:
mode = "open"
eventMessage = "started the app"
case telemetryUsageOnline:
mode = "online"
eventMessage = "recorded data in online mode"
case telemetryUsageOffline:
mode = "offline"
eventMessage = "recorded data in offline mode"
}
// construct and send the payload
info := &logInfo{
Username: getCurrentUser(),
Mode: mode,
Message: eventMessage,
Timestamp: time.Now().Unix(),
}
err := agent.streamTelemetry(info)
if err != nil {
logDebug(fmt.Sprintf("Could not send telemetry: %v\n", err))
} else {
logDebug("Telemetry log success")
}
}
// getCurrentUser returns the user's login username.
func getCurrentUser() string {
user, err := user.Current()
if err != nil {
logDebug(fmt.Sprintf("Could not get the current user's name: %v\n", err))
return "(unknown)"
}
return user.Username
}
// Send a telemetry message to the stream specified by `REPLAY_ZERO_TELEMETRY_STREAM`
// and authorized by the IAM role `REPLAY_ZERO_TELEMETRY_ROLE`
func (agent *kinesisTelemetryAgent) streamTelemetry(info *logInfo) error {
return agent.client.sendToStream(info, agent.stream)
}