feat: Upgrade to OpenTelemetry v1.38.0 with independent OTEL configuration parameters

Co-authored-by: houseme <4829346+houseme@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2025-09-11 08:36:18 +00:00
parent aa44a07d9e
commit 5ea3c4ace6
10 changed files with 408 additions and 67 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/otel"
"github.com/gogf/gf/v2/os/gcache"
"github.com/gogf/gf/v2/os/glog"
"github.com/gogf/gf/v2/text/gregex"
@ -66,7 +67,12 @@ type ConfigNode struct {
// Optional field
Debug bool `json:"debug"`
// Otel specifies the OpenTelemetry tracing configuration
// Optional field
Otel otel.Config `json:"otel"`
// OtelTraceSQLEnabled enables OpenTelemetry tracing for SQL operations
// Deprecated: Use Otel.TraceSQLEnabled instead. This field is kept for backward compatibility.
// Optional field
OtelTraceSQLEnabled bool `json:"otelTraceSQLEnabled"`
@ -440,3 +446,15 @@ func parseConfigNodeLink(node *ConfigNode) (*ConfigNode, error) {
}
return node, nil
}
// IsOtelTraceSQLEnabled returns whether SQL tracing is enabled for this configuration.
// It checks both the new Otel.TraceSQLEnabled field and the deprecated OtelTraceSQLEnabled field
// for backward compatibility.
func (node *ConfigNode) IsOtelTraceSQLEnabled() bool {
// Check new configuration first
if node.Otel.TraceSQLEnabled {
return true
}
// Fall back to deprecated field for backward compatibility
return node.OtelTraceSQLEnabled
}

View File

@ -83,7 +83,7 @@ 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 {
if c.db.GetConfig().IsOtelTraceSQLEnabled() {
events = append(events, attribute.String(traceEventDbExecutionSQL, sql.Format))
}

View File

@ -10,6 +10,7 @@ import (
"testing"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/internal/otel"
"github.com/gogf/gf/v2/test/gtest"
)
@ -21,7 +22,9 @@ func Test_OTEL_SQLTracing_Default(t *testing.T) {
}
// By default, SQL tracing should be disabled
t.Assert(config.IsOtelTraceSQLEnabled(), false)
t.Assert(config.OtelTraceSQLEnabled, false)
t.Assert(config.Otel.TraceSQLEnabled, false)
})
}
@ -33,8 +36,27 @@ func Test_OTEL_SQLTracing_Configuration(t *testing.T) {
OtelTraceSQLEnabled: true,
}
// SQL tracing should be configurable
// SQL tracing should be configurable using legacy field
t.Assert(config.IsOtelTraceSQLEnabled(), true)
t.Assert(config.OtelTraceSQLEnabled, true)
t.Assert(config.Otel.TraceSQLEnabled, false)
})
}
func Test_OTEL_SQLTracing_NewConfiguration(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "sqlite",
Name: ":memory:",
Otel: otel.Config{
TraceSQLEnabled: true,
},
}
// SQL tracing should be configurable using new configuration
t.Assert(config.IsOtelTraceSQLEnabled(), true)
t.Assert(config.OtelTraceSQLEnabled, false)
t.Assert(config.Otel.TraceSQLEnabled, true)
})
}
@ -46,28 +68,23 @@ func Test_OTEL_SQLTracing_Enabled(t *testing.T) {
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)
// Test that the configuration field can be set and retrieved using legacy field
t.Assert(config.IsOtelTraceSQLEnabled(), true)
})
}
func Test_OTEL_SQLTracing_Disabled(t *testing.T) {
func Test_OTEL_SQLTracing_BothFieldsEnabled(t *testing.T) {
gtest.C(t, func(t *gtest.T) {
config := gdb.ConfigNode{
Type: "mysql",
Type: "mysql",
Name: "test_db",
OtelTraceSQLEnabled: false,
Otel: otel.Config{
TraceSQLEnabled: true,
},
}
// 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)
// New field should take precedence over legacy field
t.Assert(config.IsOtelTraceSQLEnabled(), true)
})
}