-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathexporter_traces.go
299 lines (276 loc) · 9.66 KB
/
exporter_traces.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package clickhouseexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter"
import (
"context"
"database/sql"
"fmt"
"strings"
"time"
_ "github.com/ClickHouse/clickhouse-go/v2" // For register database driver.
"github.com/ClickHouse/clickhouse-go/v2/lib/column"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/clickhouseexporter/internal"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
)
type tracesExporter struct {
client *sql.DB
insertSQL string
logger *zap.Logger
cfg *Config
}
func newTracesExporter(logger *zap.Logger, cfg *Config) (*tracesExporter, error) {
client, err := newClickhouseClient(cfg)
if err != nil {
return nil, err
}
return &tracesExporter{
client: client,
insertSQL: renderInsertTracesSQL(cfg),
logger: logger,
cfg: cfg,
}, nil
}
func (e *tracesExporter) start(ctx context.Context, _ component.Host) error {
if !e.cfg.shouldCreateSchema() {
return nil
}
if err := createDatabase(ctx, e.cfg); err != nil {
return err
}
return createTracesTable(ctx, e.cfg, e.client)
}
// shutdown will shut down the exporter.
func (e *tracesExporter) shutdown(_ context.Context) error {
if e.client != nil {
return e.client.Close()
}
return nil
}
func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) error {
start := time.Now()
err := doWithTx(ctx, e.client, func(tx *sql.Tx) error {
statement, err := tx.PrepareContext(ctx, e.insertSQL)
if err != nil {
return fmt.Errorf("PrepareContext:%w", err)
}
defer func() {
_ = statement.Close()
}()
for i := 0; i < td.ResourceSpans().Len(); i++ {
spans := td.ResourceSpans().At(i)
res := spans.Resource()
resAttr := internal.AttributesToMap(res.Attributes())
serviceName := internal.GetServiceName(res.Attributes())
for j := 0; j < spans.ScopeSpans().Len(); j++ {
rs := spans.ScopeSpans().At(j).Spans()
scopeName := spans.ScopeSpans().At(j).Scope().Name()
scopeVersion := spans.ScopeSpans().At(j).Scope().Version()
for k := 0; k < rs.Len(); k++ {
r := rs.At(k)
spanAttr := internal.AttributesToMap(r.Attributes())
status := r.Status()
eventTimes, eventNames, eventAttrs := convertEvents(r.Events())
linksTraceIDs, linksSpanIDs, linksTraceStates, linksAttrs := convertLinks(r.Links())
_, err = statement.ExecContext(ctx,
r.StartTimestamp().AsTime(),
traceutil.TraceIDToHexOrEmptyString(r.TraceID()),
traceutil.SpanIDToHexOrEmptyString(r.SpanID()),
traceutil.SpanIDToHexOrEmptyString(r.ParentSpanID()),
r.TraceState().AsRaw(),
r.Name(),
r.Kind().String(),
serviceName,
resAttr,
scopeName,
scopeVersion,
spanAttr,
r.EndTimestamp().AsTime().Sub(r.StartTimestamp().AsTime()).Nanoseconds(),
status.Code().String(),
status.Message(),
eventTimes,
eventNames,
eventAttrs,
linksTraceIDs,
linksSpanIDs,
linksTraceStates,
linksAttrs,
)
if err != nil {
return fmt.Errorf("ExecContext:%w", err)
}
}
}
}
return nil
})
duration := time.Since(start)
e.logger.Debug("insert traces", zap.Int("records", td.SpanCount()),
zap.String("cost", duration.String()))
return err
}
func convertEvents(events ptrace.SpanEventSlice) (times []time.Time, names []string, attrs []column.IterableOrderedMap) {
for i := 0; i < events.Len(); i++ {
event := events.At(i)
times = append(times, event.Timestamp().AsTime())
names = append(names, event.Name())
attrs = append(attrs, internal.AttributesToMap(event.Attributes()))
}
return
}
func convertLinks(links ptrace.SpanLinkSlice) (traceIDs []string, spanIDs []string, states []string, attrs []column.IterableOrderedMap) {
for i := 0; i < links.Len(); i++ {
link := links.At(i)
traceIDs = append(traceIDs, traceutil.TraceIDToHexOrEmptyString(link.TraceID()))
spanIDs = append(spanIDs, traceutil.SpanIDToHexOrEmptyString(link.SpanID()))
states = append(states, link.TraceState().AsRaw())
attrs = append(attrs, internal.AttributesToMap(link.Attributes()))
}
return
}
const (
// language=ClickHouse SQL
createTracesTableSQL = `
CREATE TABLE IF NOT EXISTS %s %s (
Timestamp DateTime64(9) CODEC(Delta, ZSTD(1)),
TraceId String CODEC(ZSTD(1)),
SpanId String CODEC(ZSTD(1)),
ParentSpanId String CODEC(ZSTD(1)),
TraceState String CODEC(ZSTD(1)),
SpanName LowCardinality(String) CODEC(ZSTD(1)),
SpanKind LowCardinality(String) CODEC(ZSTD(1)),
ServiceName LowCardinality(String) CODEC(ZSTD(1)),
ResourceAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
ScopeName String CODEC(ZSTD(1)),
ScopeVersion String CODEC(ZSTD(1)),
SpanAttributes Map(LowCardinality(String), String) CODEC(ZSTD(1)),
Duration UInt64 CODEC(ZSTD(1)),
StatusCode LowCardinality(String) CODEC(ZSTD(1)),
StatusMessage String CODEC(ZSTD(1)),
Events Nested (
Timestamp DateTime64(9),
Name LowCardinality(String),
Attributes Map(LowCardinality(String), String)
) CODEC(ZSTD(1)),
Links Nested (
TraceId String,
SpanId String,
TraceState String,
Attributes Map(LowCardinality(String), String)
) CODEC(ZSTD(1)),
INDEX idx_trace_id TraceId TYPE bloom_filter(0.001) GRANULARITY 1,
INDEX idx_res_attr_key mapKeys(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_res_attr_value mapValues(ResourceAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_span_attr_key mapKeys(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_span_attr_value mapValues(SpanAttributes) TYPE bloom_filter(0.01) GRANULARITY 1,
INDEX idx_duration Duration TYPE minmax GRANULARITY 1
) ENGINE = %s
PARTITION BY toDate(Timestamp)
ORDER BY (ServiceName, SpanName, toDateTime(Timestamp))
%s
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
`
// language=ClickHouse SQL
insertTracesSQLTemplate = `INSERT INTO %s (
Timestamp,
TraceId,
SpanId,
ParentSpanId,
TraceState,
SpanName,
SpanKind,
ServiceName,
ResourceAttributes,
ScopeName,
ScopeVersion,
SpanAttributes,
Duration,
StatusCode,
StatusMessage,
Events.Timestamp,
Events.Name,
Events.Attributes,
Links.TraceId,
Links.SpanId,
Links.TraceState,
Links.Attributes
) VALUES (
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?,
?
)`
)
const (
createTraceIDTsTableSQL = `
CREATE TABLE IF NOT EXISTS %s_trace_id_ts %s (
TraceId String CODEC(ZSTD(1)),
Start DateTime CODEC(Delta, ZSTD(1)),
End DateTime CODEC(Delta, ZSTD(1)),
INDEX idx_trace_id TraceId TYPE bloom_filter(0.01) GRANULARITY 1
) ENGINE = %s
PARTITION BY toDate(Start)
ORDER BY (TraceId, Start)
%s
SETTINGS index_granularity=8192, ttl_only_drop_parts = 1;
`
createTraceIDTsMaterializedViewSQL = `
CREATE MATERIALIZED VIEW IF NOT EXISTS %s_trace_id_ts_mv %s
TO %s.%s_trace_id_ts
AS SELECT
TraceId,
min(Timestamp) as Start,
max(Timestamp) as End
FROM
%s.%s
WHERE TraceId != ''
GROUP BY TraceId;
`
)
func createTracesTable(ctx context.Context, cfg *Config, db *sql.DB) error {
if _, err := db.ExecContext(ctx, renderCreateTracesTableSQL(cfg)); err != nil {
return fmt.Errorf("exec create traces table sql: %w", err)
}
if _, err := db.ExecContext(ctx, renderCreateTraceIDTsTableSQL(cfg)); err != nil {
return fmt.Errorf("exec create traceID timestamp table sql: %w", err)
}
if _, err := db.ExecContext(ctx, renderTraceIDTsMaterializedViewSQL(cfg)); err != nil {
return fmt.Errorf("exec create traceID timestamp view sql: %w", err)
}
return nil
}
func renderInsertTracesSQL(cfg *Config) string {
return fmt.Sprintf(strings.ReplaceAll(insertTracesSQLTemplate, "'", "`"), cfg.TracesTableName)
}
func renderCreateTracesTableSQL(cfg *Config) string {
ttlExpr := generateTTLExpr(cfg.TTL, "toDate(Timestamp)")
return fmt.Sprintf(createTracesTableSQL, cfg.TracesTableName, cfg.clusterString(), cfg.tableEngineString(), ttlExpr)
}
func renderCreateTraceIDTsTableSQL(cfg *Config) string {
ttlExpr := generateTTLExpr(cfg.TTL, "toDate(Start)")
return fmt.Sprintf(createTraceIDTsTableSQL, cfg.TracesTableName, cfg.clusterString(), cfg.tableEngineString(), ttlExpr)
}
func renderTraceIDTsMaterializedViewSQL(cfg *Config) string {
return fmt.Sprintf(createTraceIDTsMaterializedViewSQL, cfg.TracesTableName,
cfg.clusterString(), cfg.Database, cfg.TracesTableName, cfg.Database, cfg.TracesTableName)
}