/
telemetry.go
78 lines (61 loc) · 1.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
package telemetry
import (
"context"
"fmt"
"os"
"runtime"
"github.com/google/uuid"
"github.com/werf/logboek"
"github.com/werf/werf/pkg/util"
"github.com/werf/werf/pkg/werf"
)
const (
MetricsURL = "http://localhost:4318/v1/metrics"
TracesURL = "http://localhost:4318/v1/traces"
)
var (
executionID string
projectID string
command string
metrics *Metrics
)
func ChangeMetrics(changeFunc func(metrics *Metrics)) {
if !IsEnabled() {
return
}
changeFunc(metrics)
}
func Init(ctx context.Context) error {
if !IsEnabled() {
return nil
}
executionID = uuid.New().String()
projectID = "26a38bba-51ed-4f93-8104-e6f51a5454ef"
command = fmt.Sprintf("%v", os.Args)
metrics = &Metrics{
Version: werf.Version,
Command: "TODO",
OS: runtime.GOOS,
Arch: runtime.GOARCH,
}
if err := SetupTraceExporter(ctx, TracesURL); err != nil {
return fmt.Errorf("unable to setup telemetry traces exporter for url %q: %w", TracesURL, err)
}
return nil
}
func Shutdown(ctx context.Context) error {
if !IsEnabled() {
return nil
}
if err := traceExporter.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace exporter: %w", err)
}
if err := tracerProvider.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace provider: %w", err)
}
logboek.Context(ctx).Debug().LogF("Telemetry collection done\n")
return nil
}
func IsEnabled() bool {
return util.GetBoolEnvironmentDefaultFalse("WERF_TELEMETRY")
}