/
telemetrywerfio.go
202 lines (166 loc) · 5.77 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
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/util"
"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)
SetCommandOptions(ctx context.Context, options []CommandOption)
CommandStarted(ctx context.Context)
CommandExited(ctx context.Context, exitCode int)
}
type TelemetryWerfIO struct {
handleErrorFunc func(err error)
tracerProvider *sdktrace.TracerProvider
traceExporter *otlptrace.Exporter
startedAt time.Time
executionID string
projectID string
command string
commandOptions []CommandOption
}
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(1*time.Millisecond), // send all available events immediately
sdktrace.WithExportTimeout(800*time.Millisecond),
),
),
traceExporter: e,
executionID: uuid.New().String(),
startedAt: time.Now(),
}, nil
}
func (t *TelemetryWerfIO) Start(ctx context.Context) error {
LogF("start trace exporter")
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 {
LogF("start shutdown")
LogF("flush trace provider")
if err := t.tracerProvider.ForceFlush(ctx); err != nil {
return fmt.Errorf("unable to force flush tracer provider: %w", err)
}
LogF("shutdown trace exporter")
if err := t.traceExporter.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace exporter: %w", err)
}
LogF("shutdown trace provider")
if err := t.tracerProvider.Shutdown(ctx); err != nil {
return fmt.Errorf("unable to shutdown trace provider: %w", err)
}
LogF("shutdown complete")
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) SetCommandOptions(ctx context.Context, options []CommandOption) {
t.commandOptions = options
}
func (t *TelemetryWerfIO) SetProjectID(ctx context.Context, projectID string) {
t.projectID = projectID
}
func (t *TelemetryWerfIO) CommandStarted(ctx context.Context) {
t.sendEvent(ctx, NewCommandStarted(t.commandOptions))
}
func (t *TelemetryWerfIO) CommandExited(ctx context.Context, exitCode int) {
duration := time.Now().Sub(t.startedAt)
t.sendEvent(ctx, NewCommandExited(exitCode, int64(duration/time.Millisecond)))
}
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
}
{
if isCI := util.GetBoolEnvironmentDefaultFalse("CI"); isCI {
attributes["ci"] = true
}
if isGitlabCI := util.GetBoolEnvironmentDefaultFalse("GITLAB_CI"); isGitlabCI {
attributes["ciName"] = "gitlab"
}
if val := os.Getenv("JENKINS_URL"); val != "" {
attributes["ciName"] = "jenkins"
}
if isTravis := util.GetBoolEnvironmentDefaultFalse("TRAVIS"); isTravis {
attributes["ciName"] = "travis"
}
if isGithubActions := util.GetBoolEnvironmentDefaultFalse("GITHUB_ACTIONS"); isGithubActions {
attributes["ciName"] = "github-actions"
}
if isCircleCI := util.GetBoolEnvironmentDefaultFalse("CIRCLECI"); isCircleCI {
attributes["ciName"] = "circleci"
}
if val := os.Getenv("TEAMCITY_VERSION"); val != "" {
attributes["ciName"] = "teamcity"
}
if isBuddy := util.GetBoolEnvironmentDefaultFalse("BUDDY"); isBuddy {
attributes["ciName"] = "buddy"
}
if val := os.Getenv("GO_SERVER_URL"); val != "" {
attributes["ciName"] = "gocd"
}
}
return attributes
}
func (t *TelemetryWerfIO) sendEvent(ctx context.Context, event Event) error {
trc := t.getTracer()
_, span := trc.Start(ctx, spanName)
LogF("start sending event")
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(event.GetType())))
rawEventData, err := json.Marshal(event)
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()
LogF("sent event: ts=%d executionID=%q projectID=%q command=%q attributes=%q eventType=%q eventData=%q schemaVersion=%d", ts, t.executionID, t.projectID, t.command, string(rawAttributes), string(event.GetType()), string(rawEventData), schemaVersion)
return nil
}