Skip to content

Commit

Permalink
Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Dec 9, 2024
1 parent 751b0d8 commit 84d094f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
17 changes: 12 additions & 5 deletions internal/pkg/instrumentation/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"log/slog"
"os"
"strconv"
"strings"

"github.com/xwb1989/sqlparser"

Expand All @@ -32,8 +31,8 @@ const (
// IncludeDBStatementEnvVar is the environment variable to opt-in for sql query inclusion in the trace.
IncludeDBStatementEnvVar = "OTEL_GO_AUTO_INCLUDE_DB_STATEMENT"

// IncludeDBOperationEnvVar is the environment variable to opt-in for sql query operation in the trace.
IncludeDBOperationEnvVar = "OTEL_GO_AUTO_PARSE_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].
Expand Down Expand Up @@ -104,18 +103,26 @@ func processFn(e *event) ptrace.SpanSlice {
span.Attributes().PutStr(string(semconv.DBQueryTextKey), query)
}

includeOperationVal := os.Getenv(IncludeDBOperationEnvVar)
includeOperationVal := os.Getenv(ParseDBStatementEnvVar)
if includeOperationVal != "" {
include, err := strconv.ParseBool(includeOperationVal)
if err == nil && include {
operation, target, err := Parse(query)
if err == nil {
span.SetName(strings.Join([]string{operation, target}, " "))
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)
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion internal/pkg/instrumentation/bpf/database/sql/probe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func BenchmarkProcessFn(b *testing.B) {
b.Run(t.name, func(b *testing.B) {
var byteQuery [256]byte
copy(byteQuery[:], []byte(t.query))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = processFn(&event{
Expand All @@ -70,7 +71,7 @@ func BenchmarkProcessFn(b *testing.B) {
}

func TestProbeConvertEvent(t *testing.T) {
t.Setenv(IncludeDBOperationEnvVar, "true")
t.Setenv(ParseDBStatementEnvVar, "true")
start := time.Unix(0, time.Now().UnixNano()) // No wall clock.
end := start.Add(1 * time.Second)

Expand Down

0 comments on commit 84d094f

Please sign in to comment.