Implement OpenTelemetry V2.8 improvements: configurable SQL, request, and response tracing

Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-09-11 08:20:22 +00:00
parent caf53edebc
commit 16ed1f8b2e
6 changed files with 292 additions and 5 deletions

View File

@ -66,6 +66,10 @@ type ConfigNode struct {
// Optional field
Debug bool `json:"debug"`
// OtelTraceSQLEnabled enables OpenTelemetry tracing for SQL operations
// Optional field
OtelTraceSQLEnabled bool `json:"otelTraceSQLEnabled"`
// Prefix specifies the table name prefix
// Optional field
Prefix string `json:"prefix"`

View File

@ -33,6 +33,7 @@ const (
traceEventDbExecutionRows = "db.execution.rows"
traceEventDbExecutionTxID = "db.execution.txid"
traceEventDbExecutionType = "db.execution.type"
traceEventDbExecutionSQL = "db.execution.sql"
)
// addSqlToTracing adds sql information to tracer if it's enabled.
@ -80,5 +81,11 @@ func (c *Core) traceSpanEnd(ctx context.Context, span trace.Span, sql *Sql) {
}
}
events = append(events, attribute.String(traceEventDbExecutionType, string(sql.Type)))
// Add SQL statement to tracing if enabled
if c.db.GetConfig().OtelTraceSQLEnabled {
events = append(events, attribute.String(traceEventDbExecutionSQL, sql.Format))
}
span.AddEvent(traceEventDbExecution, trace.WithAttributes(events...))
}

View File

@ -0,0 +1,73 @@
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/gogf/gf.
package gdb_test
import (
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/test/gtest"
)
func Test_OTEL_SQLTracing_Default(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "sqlite",
Name: ":memory:",
}
// By default, SQL tracing should be disabled
t.Assert(config.OtelTraceSQLEnabled, false)
})
}
func Test_OTEL_SQLTracing_Configuration(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "sqlite",
Name: ":memory:",
OtelTraceSQLEnabled: true,
}
// SQL tracing should be configurable
t.Assert(config.OtelTraceSQLEnabled, true)
})
}
func Test_OTEL_SQLTracing_Enabled(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "mysql",
Name: "test_db",
OtelTraceSQLEnabled: true,
}
// Test that the configuration field can be set and retrieved
t.Assert(config.OtelTraceSQLEnabled, true)
// Test that the field is preserved during configuration operations
configGroup := gdb.ConfigGroup{config}
t.Assert(configGroup[0].OtelTraceSQLEnabled, true)
})
}
func Test_OTEL_SQLTracing_Disabled(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "mysql",
Name: "test_db",
OtelTraceSQLEnabled: false,
}
// Test that the configuration field can be set and retrieved
t.Assert(config.OtelTraceSQLEnabled, false)
// Test that the field is preserved during configuration operations
configGroup := gdb.ConfigGroup{config}
t.Assert(configGroup[0].OtelTraceSQLEnabled, false)
})
}