Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/supervisor/cmd/send-analytics.go
2498 views
1
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"encoding/json"
10
"os"
11
"time"
12
13
"github.com/gitpod-io/gitpod/common-go/analytics"
14
"github.com/gitpod-io/gitpod/supervisor/api"
15
log "github.com/sirupsen/logrus"
16
"github.com/spf13/cobra"
17
)
18
19
var sendAnalyticsCmdOpts struct {
20
data string
21
event string
22
}
23
24
// sendAnalyticsCmd represents the send-analytics command
25
var sendAnalyticsCmd = &cobra.Command{
26
Use: "send-analytics",
27
Long: "Sending anonymous statistics",
28
Hidden: true,
29
Args: cobra.ExactArgs(0),
30
Run: func(cmd *cobra.Command, args []string) {
31
file, err := os.OpenFile(os.TempDir()+"/supervisor-errors.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
32
if err == nil {
33
log.SetOutput(file)
34
defer file.Close()
35
} else {
36
log.SetLevel(log.FatalLevel)
37
}
38
39
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
40
defer cancel()
41
42
data := make(map[string]interface{})
43
err = json.Unmarshal([]byte(sendAnalyticsCmdOpts.data), &data)
44
if err != nil {
45
log.Fatal(err)
46
}
47
48
conn := dialSupervisor()
49
defer conn.Close()
50
wsInfo, err := api.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &api.WorkspaceInfoRequest{})
51
if err != nil {
52
log.Fatal(err)
53
}
54
55
data["instanceId"] = wsInfo.InstanceId
56
data["workspaceId"] = wsInfo.WorkspaceId
57
58
w := analytics.NewFromEnvironment()
59
w.Track(analytics.TrackMessage{
60
Identity: analytics.Identity{UserID: wsInfo.OwnerId},
61
Event: sendAnalyticsCmdOpts.event,
62
Properties: data,
63
})
64
w.Close()
65
},
66
}
67
68
func init() {
69
rootCmd.AddCommand(sendAnalyticsCmd)
70
71
sendAnalyticsCmd.Flags().StringVarP(&sendAnalyticsCmdOpts.event, "event", "", "", "event name")
72
sendAnalyticsCmd.Flags().StringVarP(&sendAnalyticsCmdOpts.data, "data", "", "", "json data")
73
74
_ = sendAnalyticsCmd.MarkFlagRequired("event")
75
_ = sendAnalyticsCmd.MarkFlagRequired("data")
76
}
77
78