Skip to content

Commit

Permalink
Put DB Operation behind flag
Browse files Browse the repository at this point in the history
  • Loading branch information
damemi committed Nov 20, 2024
1 parent 00ddd11 commit 60ddc87
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/e2e/k8s/sample-job.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ spec:
value: "tracecontext,baggage"
- name: OTEL_GO_AUTO_INCLUDE_DB_STATEMENT
value: "true"
- name: OTEL_GO_AUTO_INCLUDE_DB_OPERATION
value: "true"
- name: OTEL_BSP_SCHEDULE_DELAY
value: "60000"
resources: {}
Expand Down
43 changes: 26 additions & 17 deletions internal/pkg/instrumentation/bpf/database/sql/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ 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_INCLUDE_DB_OPERATION"
)

// New returns a new [probe.Probe].
Expand Down Expand Up @@ -97,24 +100,30 @@ func processFn(e *event) ptrace.SpanSlice {
query := unix.ByteSliceToString(e.Query[:])
if query != "" {
span.Attributes().PutStr(string(semconv.DBQueryTextKey), query)
}

q, err := sql.Parse(query)
if err == nil {
operation := ""
switch q.(type) {
case *sql.Select:
operation = "SELECT"
case *sql.Update:
operation = "UPDATE"
case *sql.Insert:
operation = "INSERT"
case *sql.Delete:
operation = "DELETE"
}

if operation != "" {
span.Attributes().PutStr(string(semconv.DBOperationNameKey), operation)
span.SetName(operation)
includeOperationVal := os.Getenv(IncludeDBOperationEnvVar)
if includeOperationVal != "" {
include, err := strconv.ParseBool(includeOperationVal)
if err == nil && include {
q, err := sql.Parse(query)
if err == nil {
operation := ""
switch q.(type) {
case *sql.Select:
operation = "SELECT"
case *sql.Update:
operation = "UPDATE"
case *sql.Insert:
operation = "INSERT"
case *sql.Delete:
operation = "DELETE"
}

if operation != "" {
span.Attributes().PutStr(string(semconv.DBOperationNameKey), operation)
span.SetName(operation)
}
}
}
}
Expand Down

0 comments on commit 60ddc87

Please sign in to comment.