This repository has been archived by the owner on Jul 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 67
/
main.go
90 lines (77 loc) · 1.81 KB
/
main.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
package main
import (
"context"
"crypto/tls"
"fmt"
"gopkg.in/segmentio/analytics-go.v3"
"net/http"
"os"
"os/signal"
"syscall"
"time"
cfdevos "code.cloudfoundry.org/cfdev/os"
"code.cloudfoundry.org/cfdev/pkg/analyticsd/daemon"
"github.com/denisbrodbeck/machineid"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
var (
analyticsKey string
testAnalyticsKey string
version string
pollingInterval = 10 * time.Minute
)
func main() {
cfg := &clientcredentials.Config{
ClientID: "analytics",
ClientSecret: "analytics",
TokenURL: "https://uaa.dev.cfdev.sh/oauth/token",
}
httpClient := &http.Client{
Timeout: 10 * time.Second,
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
},
}
ctx := context.Background()
ctx = context.WithValue(ctx, oauth2.HTTPClient, httpClient)
userID, err := machineid.ProtectedID("cfdev")
if err != nil {
userID = "UNKNOWN_ID"
}
o := cfdevos.OS{}
osVersion, err := o.Version()
if err != nil {
osVersion = "unknown-os-version"
}
if os.Getenv("CFDEV_MODE") == "debug" {
pollingInterval = 10 * time.Second
}
var analytixKey string
if os.Getenv("CFDEV_MODE") == "debug" || analyticsKey == "" {
analytixKey = testAnalyticsKey
} else {
analytixKey = analyticsKey
}
analyticsDaemon := daemon.New(
"https://api.dev.cfdev.sh",
userID,
version,
osVersion,
os.Stdout,
cfg.Client(ctx),
analytics.New(analytixKey),
pollingInterval,
)
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
analyticsDaemon.Stop()
}()
fmt.Printf("[ANALYTICSD] apiKeyLoaded: %t, pollingInterval: %v, version: %q, time: %v, userID: %q\n",
analyticsKey != "", pollingInterval, version, time.Now(), userID)
analyticsDaemon.Start()
}