/
telemetrywerfio.go
149 lines (119 loc) · 4.04 KB
/
telemetrywerfio.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
package telemetry
import (
"context"
"encoding/json"
"fmt"
"os"
"runtime"
"time"
"github.com/google/uuid"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/trace"
"github.com/werf/werf/pkg/werf"
)
const (
spanName = "telemetry.werf.io"
schemaVersion = 1
)
type TelemetryWerfIOInterface interface {
SetProjectID(ctx context.Context, projectID string)
SetCommand(ctx context.Context, command string)
CommandStarted(ctx context.Context)
}
type TelemetryWerfIO struct {
handleErrorFunc func(err error)
tracerProvider *sdktrace.TracerProvider
traceExporter *otlptrace.Exporter
executionID string
projectID string
command string
}
type TelemetryWerfIOOptions struct {
HandleErrorFunc func(err error)
}
func NewTelemetryWerfIO(url string, opts TelemetryWerfIOOptions) (*TelemetryWerfIO, error) {
e, err := NewTraceExporter(url)
if err != nil {
return nil, fmt.Errorf("unable to create telemetry trace exporter: %w", err)
}
return &TelemetryWerfIO{
handleErrorFunc: opts.HandleErrorFunc,
tracerProvider: sdktrace.NewTracerProvider(
sdktrace.WithBatcher(e,
sdktrace.WithBatchTimeout(0),
sdktrace.WithExportTimeout(3*time.Second),
),
),
traceExporter: e,
executionID: uuid.New().String(),
}, nil
}
func (t *TelemetryWerfIO) Start(ctx context.Context) error {
if err := t.traceExporter.Start(ctx); err != nil {
return fmt.Errorf("error starting telemetry trace exporter: %w", err)
}
return nil
}
func (t *TelemetryWerfIO) Shutdown(ctx context.Context) error {
if err := t.tracerProvider.ForceFlush(ctx); err != nil {
return fmt.Errorf("unable to force flush tracer provider: %w", err)
}
if err := t.traceExporter.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace exporter: %w", err)
}
if err := t.tracerProvider.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace provider: %w", err)
}
return nil
}
func (t *TelemetryWerfIO) getTracer() trace.Tracer {
return t.tracerProvider.Tracer("telemetry.werf.io")
}
func (t *TelemetryWerfIO) SetCommand(ctx context.Context, command string) {
t.command = command
}
func (t *TelemetryWerfIO) SetProjectID(ctx context.Context, projectID string) {
t.projectID = projectID
}
func (t *TelemetryWerfIO) CommandStarted(ctx context.Context) {
t.sendEvent(ctx, CommandStartedEvent, nil)
}
func (t *TelemetryWerfIO) getAttributes() map[string]interface{} {
attributes := map[string]interface{}{
"version": werf.Version,
"os": runtime.GOOS,
"arch": runtime.GOARCH,
}
if val := os.Getenv("TRDL_USE_WERF_GROUP_CHANNEL"); val != "" {
attributes["groupChannel"] = val
}
return attributes
}
func (t *TelemetryWerfIO) sendEvent(ctx context.Context, eventType EventType, eventData interface{}) error {
trc := t.getTracer()
_, span := trc.Start(ctx, spanName)
ts := time.Now().UnixMilli()
span.SetAttributes(attribute.Key("ts").Int64(ts))
span.SetAttributes(attribute.Key("executionID").String(t.executionID))
span.SetAttributes(attribute.Key("projectID").String(t.projectID))
span.SetAttributes(attribute.Key("command").String(t.command))
rawAttributes, err := json.Marshal(t.getAttributes())
if err != nil {
return fmt.Errorf("unable to marshal attributes: %w", err)
}
span.SetAttributes(attribute.Key("attributes").String(string(rawAttributes)))
span.SetAttributes(attribute.Key("eventType").String(string(eventType)))
rawEventData, err := json.Marshal(eventData)
if err != nil {
return fmt.Errorf("unable to marshal event data: %w", err)
}
span.SetAttributes(attribute.Key("eventData").String(string(rawEventData)))
span.SetAttributes(attribute.Key("schemaVersion").Int64(schemaVersion))
span.End()
if IsLogsEnabled() {
fmt.Printf("Telemetry: sent event: ts=%d executionID=%q projectID=%q command=%q attributes=%q eventType=%q eventData=%q schemaVersion=%d\n", ts, t.executionID, t.projectID, t.command, string(rawAttributes), string(eventType), string(rawEventData), schemaVersion)
}
return nil
}