forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 2
/
exporter_traces.go
149 lines (124 loc) · 4.04 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package cassandraexporter // import "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/cassandraexporter"
import (
"context"
"fmt"
"time"
"github.com/gocql/gocql"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/traceutil"
)
type tracesExporter struct {
client *gocql.Session
logger *zap.Logger
cfg *Config
}
func newTracesExporter(logger *zap.Logger, cfg *Config) *tracesExporter {
return &tracesExporter{logger: logger, cfg: cfg}
}
func initializeTraceKernel(cfg *Config) error {
ctx := context.Background()
cluster, err := newCluster(cfg)
if err != nil {
return err
}
cluster.Consistency = gocql.Quorum
cluster.Port = cfg.Port
cluster.Timeout = cfg.Timeout
session, err := cluster.CreateSession()
if err != nil {
return err
}
defer session.Close()
createDatabaseError := session.Query(parseCreateDatabaseSQL(cfg)).WithContext(ctx).Exec()
if createDatabaseError != nil {
return createDatabaseError
}
createLinksTypeError := session.Query(parseCreateLinksTypeSQL(cfg)).WithContext(ctx).Exec()
if createLinksTypeError != nil {
return createLinksTypeError
}
createEventsTypeError := session.Query(parseCreateEventsTypeSQL(cfg)).WithContext(ctx).Exec()
if createEventsTypeError != nil {
return createEventsTypeError
}
createSpanTableError := session.Query(parseCreateSpanTableSQL(cfg)).WithContext(ctx).Exec()
if createSpanTableError != nil {
return createSpanTableError
}
return nil
}
func parseCreateSpanTableSQL(cfg *Config) string {
return fmt.Sprintf(createSpanTableSQL, cfg.Keyspace, cfg.TraceTable, cfg.Compression.Algorithm)
}
func parseCreateEventsTypeSQL(cfg *Config) string {
return fmt.Sprintf(createEventTypeSQL, cfg.Keyspace)
}
func parseCreateLinksTypeSQL(cfg *Config) string {
return fmt.Sprintf(createLinksTypeSQL, cfg.Keyspace)
}
func parseCreateDatabaseSQL(cfg *Config) string {
return fmt.Sprintf(createDatabaseSQL, cfg.Keyspace, cfg.Replication.Class, cfg.Replication.ReplicationFactor)
}
func (e *tracesExporter) Start(_ context.Context, _ component.Host) error {
cluster, err := newCluster(e.cfg)
if err != nil {
return err
}
cluster.Keyspace = e.cfg.Keyspace
cluster.Consistency = gocql.Quorum
cluster.Port = e.cfg.Port
cluster.Timeout = e.cfg.Timeout
session, err := cluster.CreateSession()
if err != nil {
return err
}
e.client = session
initializeErr := initializeTraceKernel(e.cfg)
return initializeErr
}
func (e *tracesExporter) Shutdown(_ context.Context) error {
if e.client != nil {
e.client.Close()
}
return nil
}
func (e *tracesExporter) pushTraceData(ctx context.Context, td ptrace.Traces) error {
start := time.Now()
for i := 0; i < td.ResourceSpans().Len(); i++ {
spans := td.ResourceSpans().At(i)
res := spans.Resource()
resAttr := attributesToMap(res.Attributes().AsRaw())
for j := 0; j < spans.ScopeSpans().Len(); j++ {
rs := spans.ScopeSpans().At(j).Spans()
for k := 0; k < rs.Len(); k++ {
r := rs.At(k)
spanAttr := attributesToMap(r.Attributes().AsRaw())
status := r.Status()
insertSpanError := e.client.Query(fmt.Sprintf(insertSpanSQL, e.cfg.Keyspace, e.cfg.TraceTable), r.StartTimestamp().AsTime(),
traceutil.TraceIDToHexOrEmptyString(r.TraceID()),
traceutil.SpanIDToHexOrEmptyString(r.SpanID()),
traceutil.SpanIDToHexOrEmptyString(r.ParentSpanID()),
r.TraceState().AsRaw(),
r.Name(),
traceutil.SpanKindStr(r.Kind()),
resAttr,
spanAttr,
r.EndTimestamp().AsTime().Sub(r.StartTimestamp().AsTime()).Nanoseconds(),
traceutil.StatusCodeStr(status.Code()),
status.Message(),
).WithContext(ctx).Exec()
if insertSpanError != nil {
e.logger.Error("insert span error", zap.Error(insertSpanError))
}
}
}
}
duration := time.Since(start)
e.logger.Debug("insert traces", zap.Int("records", td.SpanCount()),
zap.String("cost", duration.String()))
return nil
}