-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
337 lines (312 loc) · 8.69 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
// Binary chotel exports clichkouse traces to otel collector.
package main
import (
"context"
"fmt"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/ClickHouse/ch-go"
"github.com/ClickHouse/ch-go/proto"
"github.com/cenkalti/backoff/v4"
"github.com/go-faster/errors"
"github.com/go-faster/sdk/app"
"github.com/go-faster/sdk/zctx"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/sdk/resource"
tracesdk "go.opentelemetry.io/otel/sdk/trace"
"go.opentelemetry.io/otel/sdk/trace/tracetest"
semconv "go.opentelemetry.io/otel/semconv/v1.10.0"
"go.opentelemetry.io/otel/trace"
"go.uber.org/zap"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"github.com/go-faster/oteldb/internal/chtrace"
)
func main() {
app.Run(func(ctx context.Context, lg *zap.Logger, m *app.Metrics) (err error) {
a, err := NewApp(lg, m)
if err != nil {
return errors.Wrap(err, "init")
}
return a.Run(ctx)
})
}
// App is the trace exporter application.
type App struct {
log *zap.Logger
metrics *app.Metrics
clickHouseAddr string
clickHousePassword string
clickHouseUser string
clickHouseDB string
otlpAddr string
latest time.Time
rate time.Duration
spansSaved metric.Int64Counter
traceExporter *otlptrace.Exporter
}
const DDL = `CREATE TABLE IF NOT EXISTS opentelemetry_span_export
(
trace_id UUID,
span_id UInt64,
exported_at DATETIME
)
ENGINE = MergeTree
ORDER BY (toStartOfMinute(exported_at), trace_id, span_id)
TTL toStartOfMinute(exported_at) + INTERVAL 10 MINUTE
`
// NewApp initializes the trace exporter application.
func NewApp(lg *zap.Logger, metrics *app.Metrics) (*App, error) {
a := &App{
log: lg,
metrics: metrics,
clickHouseAddr: "clickhouse:9000",
clickHouseUser: "default",
clickHousePassword: "",
clickHouseDB: "default",
otlpAddr: "otelcol:4317",
rate: time.Millisecond * 500,
}
if v := os.Getenv("CHOTEL_SEND_RATE"); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return nil, errors.Wrap(err, "parse CHOTEL_SEND_RATE")
}
a.rate = d
}
if v := os.Getenv("OTEL_EXPORTER_OTLP_ENDPOINT"); v != "" {
a.otlpAddr = strings.TrimPrefix(v, "http://")
}
if v := os.Getenv("CH_DSN"); v != "" {
u, err := url.Parse(v)
if err != nil {
return nil, errors.Wrap(err, "parse DSN")
}
a.clickHouseAddr = u.Host
if auth := u.User; auth != nil {
if user := auth.Username(); user != "" {
a.clickHouseUser = user
}
if pass, ok := auth.Password(); ok {
a.clickHousePassword = pass
}
}
if db := strings.TrimPrefix(u.Path, "/"); db != "" {
a.clickHouseDB = db
}
}
{
meter := metrics.MeterProvider().Meter("chotel")
var err error
if a.spansSaved, err = meter.Int64Counter("chotel.spans.saved"); err != nil {
return nil, err
}
}
lg.Info("Initialized")
return a, nil
}
// Run starts and runs the application.
func (a *App) Run(ctx context.Context) error {
ctx = zctx.Base(ctx, a.log)
if err := a.setup(ctx); err != nil {
return errors.Wrap(err, "setup")
}
g, ctx := errgroup.WithContext(ctx)
g.Go(func() error { return a.runSender(ctx) })
return g.Wait()
}
func (a *App) setup(ctx context.Context) error {
a.log.Info("Setup")
ctx, cancel := context.WithTimeout(ctx, time.Second*10)
defer cancel()
a.log.Info("Connecting to clickhouse")
db, err := ch.Dial(ctx, ch.Options{
Address: a.clickHouseAddr,
User: a.clickHouseUser,
Password: a.clickHousePassword,
Database: a.clickHouseDB,
OpenTelemetryInstrumentation: false,
})
if err != nil {
return errors.Wrap(err, "clickhouse")
}
defer func() {
_ = db.Close()
}()
if err := db.Ping(ctx); err != nil {
return errors.Wrap(err, "clickhouse ping")
}
a.log.Info("Connected to clickhouse")
if err := db.Do(ctx, ch.Query{Body: DDL}); err != nil {
return errors.Wrap(err, "ensure db")
}
conn, err := grpc.NewClient(a.otlpAddr,
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithStatsHandler(otelgrpc.NewClientHandler(
otelgrpc.WithMeterProvider(a.metrics.MeterProvider()),
)),
grpc.WithBlock(),
)
if err != nil {
return errors.Wrap(err, "dial otlp")
}
traceExporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithGRPCConn(conn))
if err != nil {
return errors.Wrap(err, "setup trace exporter")
}
a.traceExporter = traceExporter
a.log.Info("Initialized")
return nil
}
func noPropagation(ctx context.Context) context.Context {
return trace.ContextWithSpanContext(ctx, trace.SpanContext{})
}
func (a *App) send(ctx context.Context, now time.Time) error {
db, err := ch.Dial(noPropagation(ctx), ch.Options{
Address: a.clickHouseAddr,
Compression: ch.CompressionZSTD,
User: a.clickHouseUser,
Password: a.clickHousePassword,
Database: a.clickHouseDB,
OpenTelemetryInstrumentation: false,
})
if err != nil {
return errors.Wrap(err, "clickhouse")
}
defer func() { _ = db.Close() }()
t := chtrace.NewTable()
q := fmt.Sprintf("SELECT %s FROM system.opentelemetry_span_log log ", strings.Join(t.Columns(), ", "))
q += " ANTI JOIN opentelemetry_span_export ose ON log.trace_id = ose.trace_id AND log.span_id = ose.span_id"
if !a.latest.IsZero() {
q += fmt.Sprintf(" PREWHERE start_time_us > %d", a.latest.Add(time.Minute).UnixMilli())
}
q += " ORDER BY log.start_time_us DESC LIMIT 10000"
zctx.From(ctx).Debug("Selecting spans",
zap.String("query", q),
zap.Time("time", a.latest),
)
var (
batch []tracesdk.ReadOnlySpan
exported struct {
TraceID proto.ColUUID
SpanID proto.ColUInt64
ExportedAt proto.ColDateTime
}
)
clickhouseResource, err := resource.New(ctx,
resource.WithAttributes(
semconv.ServiceNameKey.String("clickhouse"),
),
)
if err != nil {
return errors.Wrap(err, "clickhouse resource")
}
var latest time.Time
if err := db.Do(noPropagation(ctx), ch.Query{
Body: q,
Result: t.Result(),
OnResult: func(ctx context.Context, block proto.Block) error {
exported.TraceID = append(exported.TraceID, t.TraceID...)
exported.SpanID = append(exported.SpanID, t.SpanID...)
for _, r := range t.Rows() {
exported.ExportedAt.Append(now)
stub := tracetest.SpanStub{
SpanKind: r.Kind,
Resource: clickhouseResource,
Name: r.OperationName,
StartTime: r.StartTime,
EndTime: r.FinishTime,
SpanContext: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: r.TraceID,
SpanID: r.SpanID,
}),
Parent: trace.NewSpanContext(trace.SpanContextConfig{
TraceID: r.TraceID,
SpanID: r.ParentSpanID,
}),
}
Attribute:
for k, v := range r.Attributes {
for _, marker := range []string{
"count",
"thread_id",
"_bytes",
"_rows",
"memory_usage",
"thread_num",
"exception_code",
} {
if !strings.Contains(k, marker) {
continue
}
n, err := strconv.ParseInt(v, 10, 64)
if err != nil {
break
}
stub.Attributes = append(stub.Attributes, attribute.Int64(k, n))
continue Attribute
}
stub.Attributes = append(stub.Attributes, attribute.String(k, v))
}
if latest.Before(stub.EndTime) {
latest = stub.EndTime
}
batch = append(batch, stub.Snapshot())
}
return nil
},
}); err != nil {
return errors.Wrap(err, "query")
}
eb := backoff.NewExponentialBackOff()
if err := backoff.Retry(func() error {
if err := a.traceExporter.ExportSpans(ctx, batch); err != nil {
return errors.Wrap(err, "export")
}
return nil
}, eb); err != nil {
return errors.Wrap(err, "export")
}
if err := db.Do(noPropagation(ctx), ch.Query{
Body: "INSERT INTO opentelemetry_span_export (trace_id, span_id, exported_at) VALUES",
Input: proto.Input{
{Name: "trace_id", Data: exported.TraceID},
{Name: "span_id", Data: exported.SpanID},
{Name: "exported_at", Data: exported.ExportedAt},
},
}); err != nil {
return errors.Wrap(err, "insert")
}
a.latest = latest
zctx.From(ctx).Info("Exported",
zap.Int("count", len(exported.TraceID)),
zap.String("latest_time", a.latest.String()),
)
return nil
}
func (a *App) runSender(ctx context.Context) error {
ticker := time.NewTicker(a.rate)
defer ticker.Stop()
// First immediate tick.
if err := a.send(ctx, time.Now()); err != nil {
return errors.Wrap(err, "send")
}
for {
select {
case <-ctx.Done():
return ctx.Err()
case now := <-ticker.C:
if err := a.send(ctx, now); err != nil {
return errors.Wrap(err, "send")
}
}
}
}