-
-
Notifications
You must be signed in to change notification settings - Fork 496
/
telemetry.go
158 lines (132 loc) · 3.51 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
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
package telemetry
import (
"bytes"
"context"
"net/http"
"time"
"go.keploy.io/server/pkg/models"
// "go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
)
type Telemetry struct {
db DB
Enabled bool
OffMode bool
logger *zap.Logger
InstallationID string
}
func NewTelemetry(col DB, enabled, offMode bool, logger *zap.Logger) *Telemetry {
tele := Telemetry{
Enabled: enabled,
OffMode: offMode,
logger: logger,
db: col,
}
return &tele
}
func (ac *Telemetry) Ping(isTestMode bool) {
check := false
if !ac.Enabled {
return
}
if isTestMode {
check = true
}
go func() {
for {
var count int64
var err error
if ac.Enabled && !isTestMode {
count, err = ac.db.Count()
}
if err != nil {
ac.logger.Debug("failed to countDocuments in analytics collection", zap.Error(err))
}
event := models.TeleEvent{
EventType: "Ping",
CreatedAt: time.Now().Unix(),
TeleCheck: check,
}
if count == 0 {
bin, err := marshalEvent(event, ac.logger)
if err != nil {
break
}
resp, err := http.Post("https://telemetry.keploy.io/analytics", "application/json", bytes.NewBuffer(bin))
if err != nil {
ac.logger.Debug("failed to send request for analytics", zap.Error(err))
break
}
id, err := unmarshalResp(resp, ac.logger)
if err != nil {
break
}
ac.InstallationID = id
ac.db.Insert(id)
} else {
ac.SendTelemetry("Ping", http.Client{}, context.TODO())
}
time.Sleep(5 * time.Minute)
}
}()
}
func (ac *Telemetry) Normalize(client http.Client, ctx context.Context) {
ac.SendTelemetry("NormaliseTC", client, ctx)
}
func (ac *Telemetry) DeleteTc(client http.Client, ctx context.Context) {
ac.SendTelemetry("DeleteTC", client, ctx)
}
func (ac *Telemetry) EditTc(client http.Client, ctx context.Context) {
ac.SendTelemetry("EditTC", client, ctx)
}
func (ac *Telemetry) Testrun(success int, failure int, client http.Client, ctx context.Context) {
ac.SendTelemetry("TestRun", client, ctx, map[string]interface{}{"Passed-Tests": success, "Failed-Tests": failure})
}
func (ac *Telemetry) GetApps(apps int, client http.Client, ctx context.Context) {
ac.SendTelemetry("GetApps", client, ctx, map[string]interface{}{"Apps": apps})
}
func (ac *Telemetry) SendTelemetry(eventType string, client http.Client, ctx context.Context, output ...map[string]interface{}) {
if ac.Enabled {
event := models.TeleEvent{
EventType: eventType,
CreatedAt: time.Now().Unix(),
}
if len(output) != 0 {
event.Meta = output[0]
}
if ac.InstallationID == "" {
sr := ac.db.Find()
ac.InstallationID = sr
}
event.InstallationID = ac.InstallationID
bin, err := marshalEvent(event, ac.logger)
if err != nil {
ac.logger.Error("failed to marshal event", zap.Error(err))
return
}
req, err := http.NewRequest(http.MethodPost, "https://telemetry.keploy.io/analytics", bytes.NewBuffer(bin))
if err != nil {
ac.logger.Debug("failed to create request for analytics", zap.Error(err))
return
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
if !ac.OffMode {
req = req.WithContext(ctx)
resp, err := client.Do(req)
if err != nil {
ac.logger.Debug("failed to send request for analytics", zap.Error(err))
return
}
unmarshalResp(resp, ac.logger)
return
}
go func() {
resp, err := client.Do(req)
if err != nil {
ac.logger.Debug("failed to send request for analytics", zap.Error(err))
return
}
unmarshalResp(resp, ac.logger)
}()
}
}