mirror of
https://gitee.com/johng/gf
synced 2026-07-07 14:25:17 +08:00
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:
@ -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"`
|
||||
|
||||
@ -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...))
|
||||
}
|
||||
|
||||
73
database/gdb/gdb_z_unit_feature_otel_tracing_test.go
Normal file
73
database/gdb/gdb_z_unit_feature_otel_tracing_test.go
Normal 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)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user