-
Notifications
You must be signed in to change notification settings - Fork 8
/
telemetry.go
328 lines (268 loc) · 8.46 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
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package fnapi
import (
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"go.uber.org/atomic"
spb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc/status"
"namespacelabs.dev/foundation/internal/cli/version"
"namespacelabs.dev/foundation/internal/console"
"namespacelabs.dev/foundation/internal/environment"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/fnerrors/format"
"namespacelabs.dev/foundation/internal/workspace/dirs"
"namespacelabs.dev/go-ids"
)
// TODO #93 compute this when we can move the service definition into the foundation repo.
const telemetryServiceName = "telemetry.TelemetryService"
const postTimeout = 1 * time.Second
type Telemetry struct {
useTelemetry bool
errorLogging bool // For testing and debugging.
backendAddress string
recID *atomic.String // Never nil, set after an invocation is recorded.
id ephemeralCliID
created bool // True if this the first invocation with a new ID.
}
func NewTelemetry(ctx context.Context) *Telemetry {
return InternalNewTelemetry(ctx, getOrGenerateEphemeralCliID)
}
func InternalNewTelemetry(ctx context.Context, makeID func(context.Context) (ephemeralCliID, bool)) *Telemetry {
id, created := makeID(ctx)
return &Telemetry{
errorLogging: false,
backendAddress: EndpointAddress,
recID: atomic.NewString(""),
id: id,
created: created,
}
}
type contextKey string
var (
_telemetryKey = contextKey("fn.telemetry")
)
func TelemetryOn(ctx context.Context) *Telemetry {
v := ctx.Value(_telemetryKey)
if v == nil {
return nil
}
return v.(*Telemetry)
}
func WithTelemetry(ctx context.Context) context.Context {
return context.WithValue(ctx, _telemetryKey, NewTelemetry(ctx))
}
// Telemetry needs to be excplicitly enabled by calling this function.
// IsTelemetryEnabled() may still be false if telemetry is disabled through DO_NOT_TRACK, etc.
func (tel *Telemetry) Enable() {
tel.useTelemetry = true
}
func (tel *Telemetry) IsTelemetryEnabled() bool {
doNotTrack := os.Getenv("DO_NOT_TRACK")
enableTelemetry := viper.GetBool("telemetry")
return !environment.IsRunningInCI() && tel.useTelemetry && doNotTrack == "" && enableTelemetry
}
func (tel *Telemetry) logError(ctx context.Context, err error) {
if tel.errorLogging {
format.Format(console.Stderr(ctx), err)
}
}
func combinedHash(ins ...string) string {
h := sha256.New()
for _, in := range ins {
h.Write([]byte(in))
}
return hex.EncodeToString(h.Sum(nil))
}
// TODO #93 remove structs when we can move the service definition into the foundation repo.
type flag struct {
Name string `json:"name,omitempty"`
Hash string `json:"hash,omitempty"`
Plaintext string `json:"plaintext,omitempty"`
}
type arg struct {
Hash string `json:"hash,omitempty"`
Plaintext string `json:"plaintext,omitempty"`
}
type binaryVersion struct {
Version string `json:"version,omitempty"`
BuildTime string `json:"build_time,omitempty"`
Modified bool `json:"modified,omitempty"`
}
type recordInvocationRequest struct {
ID string `json:"id,omitempty"`
Command string `json:"command,omitempty"`
Arg []arg `json:"arg"`
Flag []flag `json:"flag"`
UserId string `json:"user_id,omitempty"`
Os string `json:"os,omitempty"`
Arch string `json:"arch,omitempty"`
NumCpu int `json:"num_cpu"`
Version *binaryVersion `json:"version"`
}
type recordErrorRequest struct {
ID string `json:"id,omitempty"`
Message string `json:"message,omitempty"`
Status *spb.Status `json:"status,omitempty"`
}
type ephemeralCliID struct {
ID string `json:"id"`
Salt string `json:"salt"`
}
func newRandID() string {
return ids.NewRandomBase62ID(16)
}
func getOrGenerateEphemeralCliID(ctx context.Context) (ephemeralCliID, bool) {
configDir, err := dirs.Config()
if err != nil {
panic(err) // XXX Config() should not return an error.
}
idfile := filepath.Join(configDir, "clientid.json")
idcontents, err := os.ReadFile(idfile)
if err == nil {
var clientID ephemeralCliID
if err := json.Unmarshal(idcontents, &clientID); err == nil {
if clientID.ID != "" && clientID.Salt != "" {
return clientID, false
}
}
}
newClientID := ephemeralCliID{newRandID(), newRandID()}
if err := writeJSON(idfile, newClientID); err != nil {
fmt.Fprintln(console.Warnings(ctx), "failed to persist user-id", err)
}
return newClientID, os.IsNotExist(err)
}
func writeJSON(path string, msg interface{}) error {
data, err := json.Marshal(msg)
if err != nil {
return err
}
return os.WriteFile(path, data, 0644)
}
func fullCommand(cmd *cobra.Command) string {
commands := []string{cmd.Use}
for cmd.HasParent() {
cmd = cmd.Parent()
commands = append([]string{cmd.Use}, commands...)
}
return strings.Join(commands, " ")
}
// Extracts command name and set flags from cmd. Reports args and flag values in hashed form.
func buildRecordInvocationRequest(ctx context.Context, cmd *cobra.Command, c ephemeralCliID, reqID string, args []string) *recordInvocationRequest {
req := recordInvocationRequest{
ID: reqID,
Command: fullCommand(cmd),
UserId: c.ID,
Os: runtime.GOOS,
Arch: runtime.GOARCH,
NumCpu: runtime.NumCPU(),
}
if v, err := version.Current(); err == nil {
if v.Modified {
// don't upload version information on modified binaries
req.Version = &binaryVersion{
Modified: true,
}
} else {
req.Version = &binaryVersion{
Version: v.GitCommit,
BuildTime: v.BuildTimeStr,
Modified: false,
}
}
}
cmd.Flags().Visit(func(pflag *pflag.Flag) {
req.Flag = append(req.Flag, flag{
Name: pflag.Name,
Hash: combinedHash(pflag.Value.String(), pflag.Name, c.Salt),
})
})
for _, a := range args {
req.Arg = append(req.Arg, arg{Hash: combinedHash(a, c.Salt)})
}
return &req
}
func (tel *Telemetry) postRecordInvocationRequest(ctx context.Context, req *recordInvocationRequest) error {
ctx, cancel := context.WithTimeout(ctx, postTimeout)
defer cancel()
record := Call[recordInvocationRequest]{
Endpoint: tel.backendAddress,
Method: fmt.Sprintf("%s/RecordInvocation", telemetryServiceName),
OptionalAuth: true,
}
return record.Do(ctx, *req, nil)
}
func (tel *Telemetry) recordInvocation(ctx context.Context, cmd *cobra.Command, reqID string, args []string) {
if !tel.IsTelemetryEnabled() {
return
}
req := buildRecordInvocationRequest(ctx, cmd, tel.id, reqID, args)
if err := tel.postRecordInvocationRequest(ctx, req); err != nil {
tel.logError(ctx, err)
return
}
// Only store request id if recoding the invocation succeeded.
tel.recID.Store(req.ID)
}
func (tel *Telemetry) RecordInvocation(ctx context.Context, cmd *cobra.Command, args []string) string {
reqID := ids.NewRandomBase62ID(16)
// Telemetry should be best effort and not block the user.
go tel.recordInvocation(ctx, cmd, reqID, args)
return reqID
}
func (tel *Telemetry) postRecordErrorRequest(ctx context.Context, req recordErrorRequest) error {
ctx, cancel := context.WithTimeout(ctx, postTimeout)
defer cancel()
return AnonymousCall(ctx, tel.backendAddress, fmt.Sprintf("%s/RecordError", telemetryServiceName), req, nil)
}
func (tel *Telemetry) RecordError(ctx context.Context, err error) {
if !tel.IsTelemetryEnabled() || err == nil {
return
}
tel.recordError(ctx, tel.recID.Load(), err)
}
func (tel *Telemetry) recordError(ctx context.Context, recID string, err error) {
if err == nil {
return
}
if _, isExpected := fnerrors.IsExpected(err); isExpected {
// We are only interested in unexpected errors.
return
}
// If we never saw a recorded ID, bail out.
if recID == "" {
tel.logError(ctx, fmt.Errorf("didn't receive telemetry record id"))
return
}
req := recordErrorRequest{ID: recID}
st, _ := status.FromError(err)
req.Message = err.Error()
req.Status = st.Proto()
if err := tel.postRecordErrorRequest(ctx, req); err != nil {
tel.logError(ctx, err)
}
}
func (tel *Telemetry) IsFirstRun() bool { return tel.created }
func (tel *Telemetry) GetClientID() string {
if tel == nil {
return ""
}
if !tel.IsTelemetryEnabled() {
return ""
}
return tel.id.ID
}