-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.go
140 lines (118 loc) · 3.24 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
package telemetry
import (
"bytes"
"crypto/tls"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strconv"
"strings"
"time"
"github.com/rs/zerolog/log"
)
var cloudApiURL = "https://api.kubeshark.co"
const (
ENV_CLOUD_API_URL = "KUBESHARK_CLOUD_API_URL"
)
func init() {
envCloudApiURL := os.Getenv(ENV_CLOUD_API_URL)
if envCloudApiURL != "" {
cloudApiURL = envCloudApiURL
}
}
const (
ENV_TELEMETRY_DISABLED = "TELEMETRY_DISABLED"
ENV_TELEMETRY_INTERVAL_SECONDS = "TELEMETRY_INTERVAL_SECONDS"
DEFAULT_TELEMETRY_INTERVAL_SECONDS = 60
)
func Start(serviceName string, licenseKeyChan <-chan string) {
telemetryDisabled := os.Getenv(ENV_TELEMETRY_DISABLED)
log.Debug().Str(ENV_TELEMETRY_DISABLED, telemetryDisabled).Msg("Environment variable:")
if telemetryDisabled == "true" {
return
}
telemetryIntervalSecondsEnv := os.Getenv(ENV_TELEMETRY_INTERVAL_SECONDS)
log.Debug().Str(ENV_TELEMETRY_INTERVAL_SECONDS, telemetryIntervalSecondsEnv).Msg("Environment variable:")
telemetryIntervalSeconds, err := strconv.Atoi(telemetryIntervalSecondsEnv)
if err != nil {
telemetryIntervalSeconds = DEFAULT_TELEMETRY_INTERVAL_SECONDS
}
startTime := time.Now()
ticker := time.NewTicker(time.Second * time.Duration(telemetryIntervalSeconds))
defer ticker.Stop()
var license string
for {
select {
case <-ticker.C: // next tick
stats, err := Run(startTime, serviceName, license)
if err != nil {
log.Warn().Err(err).Msg("Telemetry")
} else {
log.Debug().Interface("stats", stats).Msg("Telemetry")
}
case newLicense := <-licenseKeyChan: // license updated
license = newLicense
}
}
}
func Run(startTime time.Time, serviceName string, license string) (stats *Stats, err error) {
var hostname string
hostname, err = getHostname()
if err != nil {
return
}
cpuUsage := getCPUUsage()
cpuNum := getCPUNum()
memAlloc, memSys := getMemoryUsage()
memUsage := float64(memAlloc) / float64(memSys) * 100
now := time.Now()
stats = &Stats{
Timestamp: now,
TimeFromStart: now.Sub(startTime),
CPUUsage: cpuUsage,
CPUNum: cpuNum,
MemoryAlloc: memAlloc,
MemoryUsage: memUsage,
Hostname: strings.TrimSpace(hostname),
}
err = emitMetrics(stats, serviceName, license)
return
}
func emitMetrics(data *Stats, serviceName string, license string) (err error) {
endpointURL := fmt.Sprintf("%s/telemetry/%s", cloudApiURL, serviceName)
var payload []byte
payload, err = json.Marshal(data)
if err != nil {
return
}
var req *http.Request
req, err = http.NewRequest("POST", endpointURL, bytes.NewBuffer(payload))
if err != nil {
return
}
req.Header.Set("Content-Type", "application/json")
req.Header.Set("License-Key", license)
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
var resp *http.Response
resp, err = client.Do(req)
if err != nil {
return
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
var bodyBytes []byte
bodyBytes, err = io.ReadAll(resp.Body)
if err != nil {
err = fmt.Errorf("unexpected response status: %d error: %s", resp.StatusCode, err.Error())
} else {
err = fmt.Errorf("unexpected response status: %d message: %s", resp.StatusCode, string(bodyBytes))
}
}
return
}