-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexporter.go
102 lines (87 loc) · 2.1 KB
/
exporter.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
package hanaexporter
import (
"context"
"encoding/json"
"errors"
"fmt"
"database/sql"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/pdata/ptrace"
// Register hdb driver.
hana "github.com/SAP/go-hdb/driver"
)
type tracesExporter struct {
db *sql.DB
started bool
}
const (
driverName = "hdb"
)
func newTracesExporter(config *Config, settings exporter.CreateSettings) (*tracesExporter, error) {
var db, err = sql.Open(driverName, config.Dsn)
if err != nil {
return nil, err
}
if err := db.Ping(); err != nil {
return nil, err
}
_, err = db.Exec(`
CREATE TABLE SQL_TRACES (
STATEMENT VARCHAR(5000) NOT NULL,
START_TIME TIMESTAMP NOT NULL,
END_TIME TIMESTAMP NOT NULL,
ATTRIBUTES VARCHAR(5000) NOT NULL,
PRIMARY KEY(START_TIME)
)`)
if err != nil {
var dbError hana.Error
if errors.As(err, &dbError) {
if dbError.Code() != 288 {
return nil, err
}
} else {
return nil, err
}
}
return &tracesExporter{
db: db,
started: false,
}, nil
}
func (e *tracesExporter) pushTraces(ctx context.Context, td ptrace.Traces) error {
for i := 0; i < td.ResourceSpans().Len(); i++ {
var span = td.ResourceSpans().At(i).ScopeSpans()
for k := 0; k < span.Len(); k++ {
var s = span.At(k)
for j := 0; j < s.Spans().Len(); j++ {
var x = s.Spans().At(j)
var attributes = x.Attributes().AsRaw()
if _, ok := attributes["sql"]; ok {
fmt.Printf("Span %v\n", attributes)
jsonStr, err := json.Marshal(attributes)
if err != nil {
return err
}
_, err = e.db.Exec(" INSERT INTO SQL_TRACES (STATEMENT,START_TIME,END_TIME,ATTRIBUTES) VALUES(?,?,?,?)",
attributes["sql"], x.StartTimestamp().AsTime(), x.EndTimestamp().AsTime(), jsonStr)
if err != nil {
return err
}
}
}
}
}
return nil
}
func (e *tracesExporter) Start(ctx context.Context, host component.Host) error {
e.started = true
return nil
}
func (e *tracesExporter) Shutdown(ctx context.Context) error {
if !e.started {
return nil
}
e.started = false
return nil
}