-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathprobe.go
184 lines (162 loc) · 4.96 KB
/
probe.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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package sql
import (
"fmt"
"log/slog"
"os"
"strconv"
"github.com/xwb1989/sqlparser"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sys/unix"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/context"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/probe"
"go.opentelemetry.io/auto/internal/pkg/instrumentation/utils"
)
//go:generate go run github.com/cilium/ebpf/cmd/bpf2go -target amd64,arm64 bpf ./bpf/probe.bpf.c
const (
// pkg is the package being instrumented.
pkg = "database/sql"
// IncludeDBStatementEnvVar is the environment variable to opt-in for sql query inclusion in the trace.
IncludeDBStatementEnvVar = "OTEL_GO_AUTO_INCLUDE_DB_STATEMENT"
// ParseDBStatementEnvVar is the environment variable to opt-in for sql query operation in the trace.
ParseDBStatementEnvVar = "OTEL_GO_AUTO_PARSE_DB_STATEMENT"
)
// New returns a new [probe.Probe].
func New(logger *slog.Logger, version string) probe.Probe {
id := probe.ID{
SpanKind: trace.SpanKindClient,
InstrumentedPkg: pkg,
}
return &probe.SpanProducer[bpfObjects, event]{
Base: probe.Base[bpfObjects, event]{
ID: id,
Logger: logger,
Consts: []probe.Const{
probe.RegistersABIConst{},
probe.AllocationConst{},
probe.KeyValConst{
Key: "should_include_db_statement",
Val: shouldIncludeDBStatement(),
},
},
Uprobes: []*probe.Uprobe{
{
Sym: "database/sql.(*DB).queryDC",
EntryProbe: "uprobe_queryDC",
ReturnProbe: "uprobe_queryDC_Returns",
FailureMode: probe.FailureModeIgnore,
},
{
Sym: "database/sql.(*DB).execDC",
EntryProbe: "uprobe_execDC",
ReturnProbe: "uprobe_execDC_Returns",
FailureMode: probe.FailureModeIgnore,
},
},
SpecFn: loadBpf,
},
Version: version,
SchemaURL: semconv.SchemaURL,
ProcessFn: processFn,
}
}
// event represents an event in an SQL database
// request-response.
type event struct {
context.BaseSpanProperties
Query [256]byte
}
func processFn(e *event) ptrace.SpanSlice {
spans := ptrace.NewSpanSlice()
span := spans.AppendEmpty()
span.SetName("DB")
span.SetKind(ptrace.SpanKindClient)
span.SetStartTimestamp(utils.BootOffsetToTimestamp(e.StartTime))
span.SetEndTimestamp(utils.BootOffsetToTimestamp(e.EndTime))
span.SetTraceID(pcommon.TraceID(e.SpanContext.TraceID))
span.SetSpanID(pcommon.SpanID(e.SpanContext.SpanID))
span.SetFlags(uint32(trace.FlagsSampled))
if e.ParentSpanContext.SpanID.IsValid() {
span.SetParentSpanID(pcommon.SpanID(e.ParentSpanContext.SpanID))
}
query := unix.ByteSliceToString(e.Query[:])
if query != "" {
span.Attributes().PutStr(string(semconv.DBQueryTextKey), query)
}
includeOperationVal := os.Getenv(ParseDBStatementEnvVar)
if includeOperationVal != "" {
include, err := strconv.ParseBool(includeOperationVal)
if err == nil && include {
operation, target, err := Parse(query)
if err == nil {
name := ""
if operation != "" {
span.Attributes().PutStr(string(semconv.DBOperationNameKey), operation)
name = operation
}
if target != "" {
span.Attributes().PutStr(string(semconv.DBCollectionNameKey), target)
if name != "" {
// if operation is in the name and target is available, set name to {operation} {target}
name += " " + target
}
}
if name != "" {
span.SetName(name)
}
}
}
}
return spans
}
// shouldIncludeDBStatement returns if the user has configured SQL queries to be included.
func shouldIncludeDBStatement() bool {
val := os.Getenv(IncludeDBStatementEnvVar)
if val != "" {
boolVal, err := strconv.ParseBool(val)
if err == nil {
return boolVal
}
}
return false
}
// Parse takes a SQL query string and returns the parsed query statement type
// and table name, or an error if parsing failed.
func Parse(query string) (string, string, error) {
stmt, err := sqlparser.Parse(query)
if err != nil {
return "", "", fmt.Errorf("failed to parse query: %w", err)
}
switch stmt := stmt.(type) {
case *sqlparser.Select:
return "SELECT", getTableName(stmt.From), nil
case *sqlparser.Update:
return "UPDATE", getTableName(stmt.TableExprs), nil
case *sqlparser.Insert:
return "INSERT", stmt.Table.Name.String(), nil
case *sqlparser.Delete:
return "DELETE", getTableName(stmt.TableExprs), nil
default:
return "", "", fmt.Errorf("unsupported operation")
}
}
// getTableName extracts the table name from a SQL node.
func getTableName(node sqlparser.SQLNode) string {
switch tableExpr := node.(type) {
case sqlparser.TableName:
return tableExpr.Name.String()
case sqlparser.TableExprs:
for _, expr := range tableExpr {
if tableName, ok := expr.(*sqlparser.AliasedTableExpr); ok {
if name, ok := tableName.Expr.(sqlparser.TableName); ok {
return name.Name.String()
}
}
}
}
return ""
}