diff --git a/database/gdb/gdb_core_config.go b/database/gdb/gdb_core_config.go index c73680829..da7cfc733 100644 --- a/database/gdb/gdb_core_config.go +++ b/database/gdb/gdb_core_config.go @@ -70,7 +70,7 @@ type ConfigNode struct { // 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 diff --git a/database/gdb/gdb_core_trace.go b/database/gdb/gdb_core_trace.go index cd6429674..737f5633f 100644 --- a/database/gdb/gdb_core_trace.go +++ b/database/gdb/gdb_core_trace.go @@ -81,11 +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().IsOtelTraceSQLEnabled() { events = append(events, attribute.String(traceEventDbExecutionSQL, sql.Format)) } - + span.AddEvent(traceEventDbExecution, trace.WithAttributes(events...)) } diff --git a/database/gdb/gdb_z_unit_feature_otel_tracing_test.go b/database/gdb/gdb_z_unit_feature_otel_tracing_test.go index 13101723c..4b96930bc 100644 --- a/database/gdb/gdb_z_unit_feature_otel_tracing_test.go +++ b/database/gdb/gdb_z_unit_feature_otel_tracing_test.go @@ -20,7 +20,7 @@ func Test_OTEL_SQLTracing_Default(t *testing.T) { Type: "sqlite", Name: ":memory:", } - + // By default, SQL tracing should be disabled t.Assert(config.IsOtelTraceSQLEnabled(), false) t.Assert(config.OtelTraceSQLEnabled, false) @@ -31,11 +31,11 @@ func Test_OTEL_SQLTracing_Default(t *testing.T) { func Test_OTEL_SQLTracing_Configuration(t *testing.T) { gtest.C(t, func(t *gtest.T) { config := gdb.ConfigNode{ - Type: "sqlite", - Name: ":memory:", + Type: "sqlite", + Name: ":memory:", OtelTraceSQLEnabled: true, } - + // SQL tracing should be configurable using legacy field t.Assert(config.IsOtelTraceSQLEnabled(), true) t.Assert(config.OtelTraceSQLEnabled, true) @@ -52,7 +52,7 @@ func Test_OTEL_SQLTracing_NewConfiguration(t *testing.T) { TraceSQLEnabled: true, }, } - + // SQL tracing should be configurable using new configuration t.Assert(config.IsOtelTraceSQLEnabled(), true) t.Assert(config.OtelTraceSQLEnabled, false) @@ -63,11 +63,11 @@ func Test_OTEL_SQLTracing_NewConfiguration(t *testing.T) { func Test_OTEL_SQLTracing_Enabled(t *testing.T) { gtest.C(t, func(t *gtest.T) { config := gdb.ConfigNode{ - Type: "mysql", - Name: "test_db", + Type: "mysql", + Name: "test_db", OtelTraceSQLEnabled: true, } - + // Test that the configuration field can be set and retrieved using legacy field t.Assert(config.IsOtelTraceSQLEnabled(), true) }) @@ -76,15 +76,15 @@ func Test_OTEL_SQLTracing_Enabled(t *testing.T) { func Test_OTEL_SQLTracing_BothFieldsEnabled(t *testing.T) { gtest.C(t, func(t *gtest.T) { config := gdb.ConfigNode{ - Type: "mysql", - Name: "test_db", + Type: "mysql", + Name: "test_db", OtelTraceSQLEnabled: false, Otel: otel.Config{ TraceSQLEnabled: true, }, } - + // New field should take precedence over legacy field t.Assert(config.IsOtelTraceSQLEnabled(), true) }) -} \ No newline at end of file +} diff --git a/net/ghttp/ghttp_middleware_tracing.go b/net/ghttp/ghttp_middleware_tracing.go index c056bd8fc..bfbf2818a 100644 --- a/net/ghttp/ghttp_middleware_tracing.go +++ b/net/ghttp/ghttp_middleware_tracing.go @@ -91,12 +91,12 @@ func internalMiddlewareServerTracing(r *Request) { if r.Server != nil && r.Server.config.IsOtelTraceRequestEnabled() { // Get all request parameters (query + form + body) requestParams := make(map[string]any) - + // Query parameters for k, v := range r.URL.Query() { requestParams[k] = v } - + // Form parameters if r.ContentLength > 0 && gtrace.MaxContentLogSize() > 0 { contentType := r.Header.Get("Content-Type") @@ -108,9 +108,9 @@ func internalMiddlewareServerTracing(r *Request) { } } } - + if len(requestParams) > 0 { - traceAttrs = append(traceAttrs, + traceAttrs = append(traceAttrs, attribute.String(tracingEventHttpRequestParams, gconv.String(requestParams)), ) } diff --git a/net/ghttp/ghttp_server_config.go b/net/ghttp/ghttp_server_config.go index 41c5b08d3..a0dff5a32 100644 --- a/net/ghttp/ghttp_server_config.go +++ b/net/ghttp/ghttp_server_config.go @@ -290,48 +290,48 @@ type ServerConfig struct { // some pointer attributes that may be shared in different servers. func NewConfig() ServerConfig { return ServerConfig{ - Name: DefaultServerName, - Address: ":0", - HTTPSAddr: "", - Listeners: nil, - Handler: nil, - ReadTimeout: 60 * time.Second, - WriteTimeout: 0, // No timeout. - IdleTimeout: 60 * time.Second, - MaxHeaderBytes: 10240, // 10KB - KeepAlive: true, - IndexFiles: []string{"index.html", "index.htm"}, - IndexFolder: false, - ServerAgent: "GoFrame HTTP Server", - ServerRoot: "", - StaticPaths: make([]staticPathItem, 0), - FileServerEnabled: false, - CookieMaxAge: time.Hour * 24 * 365, - CookiePath: "/", - CookieDomain: "", - SessionIdName: "gfsessionid", - SessionPath: gsession.DefaultStorageFilePath, - SessionMaxAge: time.Hour * 24, - SessionCookieOutput: true, - SessionCookieMaxAge: time.Hour * 24, - Logger: glog.New(), - LogLevel: "all", - LogStdout: true, - ErrorStack: true, - ErrorLogEnabled: true, - ErrorLogPattern: "error-{Ymd}.log", - AccessLogEnabled: false, + Name: DefaultServerName, + Address: ":0", + HTTPSAddr: "", + Listeners: nil, + Handler: nil, + ReadTimeout: 60 * time.Second, + WriteTimeout: 0, // No timeout. + IdleTimeout: 60 * time.Second, + MaxHeaderBytes: 10240, // 10KB + KeepAlive: true, + IndexFiles: []string{"index.html", "index.htm"}, + IndexFolder: false, + ServerAgent: "GoFrame HTTP Server", + ServerRoot: "", + StaticPaths: make([]staticPathItem, 0), + FileServerEnabled: false, + CookieMaxAge: time.Hour * 24 * 365, + CookiePath: "/", + CookieDomain: "", + SessionIdName: "gfsessionid", + SessionPath: gsession.DefaultStorageFilePath, + SessionMaxAge: time.Hour * 24, + SessionCookieOutput: true, + SessionCookieMaxAge: time.Hour * 24, + Logger: glog.New(), + LogLevel: "all", + LogStdout: true, + ErrorStack: true, + ErrorLogEnabled: true, + ErrorLogPattern: "error-{Ymd}.log", + AccessLogEnabled: false, AccessLogPattern: "access-{Ymd}.log", - Otel: *otel.NewConfig(), + Otel: *otel.NewConfig(), OtelTraceRequestEnabled: false, OtelTraceResponseEnabled: false, - DumpRouterMap: true, - ClientMaxBodySize: 8 * 1024 * 1024, // 8MB - FormParsingMemory: 1024 * 1024, // 1MB - Rewrites: make(map[string]string), - Graceful: false, - GracefulTimeout: 2, // seconds - GracefulShutdownTimeout: 5, // seconds + DumpRouterMap: true, + ClientMaxBodySize: 8 * 1024 * 1024, // 8MB + FormParsingMemory: 1024 * 1024, // 1MB + Rewrites: make(map[string]string), + Graceful: false, + GracefulTimeout: 2, // seconds + GracefulShutdownTimeout: 5, // seconds } } diff --git a/net/ghttp/ghttp_z_unit_feature_otel_tracing_test.go b/net/ghttp/ghttp_z_unit_feature_otel_tracing_test.go index 6c1ce2aaa..1c1a3ff76 100644 --- a/net/ghttp/ghttp_z_unit_feature_otel_tracing_test.go +++ b/net/ghttp/ghttp_z_unit_feature_otel_tracing_test.go @@ -27,7 +27,7 @@ func Test_OTEL_RequestTracing_Disabled(t *testing.T) { r.Response.WriteJson(g.Map{"result": "ok"}) }) s.SetDumpRouterMap(false) - + // By default, request tracing should be disabled s.Start() defer s.Shutdown() @@ -40,20 +40,20 @@ func Test_OTEL_RequestTracing_Disabled(t *testing.T) { res, err := client.Post(ctx, "/test", g.Map{"param1": "value1"}) t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) }) } func Test_OTEL_RequestTracing_Enabled(t *testing.T) { s := g.Server(guid.S()) - + // Enable request tracing using SetConfigWithMap err := s.SetConfigWithMap(g.Map{ "OtelTraceRequestEnabled": true, }) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{"result": "ok"}) }) @@ -69,7 +69,7 @@ func Test_OTEL_RequestTracing_Enabled(t *testing.T) { res, err := client.Post(ctx, "/test?query1=qvalue1", g.Map{"param1": "value1"}) t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test passes if no errors occurred during tracing }) @@ -77,13 +77,13 @@ func Test_OTEL_RequestTracing_Enabled(t *testing.T) { func Test_OTEL_ResponseTracing_Enabled(t *testing.T) { s := g.Server(guid.S()) - + // Enable response tracing using SetConfigWithMap err := s.SetConfigWithMap(g.Map{ "OtelTraceResponseEnabled": true, }) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{"result": "success", "data": "test data"}) }) @@ -99,7 +99,7 @@ func Test_OTEL_ResponseTracing_Enabled(t *testing.T) { res, err := client.Get(ctx, "/test") t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test passes if no errors occurred during response tracing }) @@ -107,19 +107,19 @@ func Test_OTEL_ResponseTracing_Enabled(t *testing.T) { func Test_OTEL_BothTracingEnabled(t *testing.T) { s := g.Server(guid.S()) - + // Enable both request and response tracing using SetConfigWithMap err := s.SetConfigWithMap(g.Map{ "OtelTraceRequestEnabled": true, "OtelTraceResponseEnabled": true, }) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{ "received_param": r.Get("param1"), "received_query": r.Get("query1"), - "result": "success", + "result": "success", }) }) s.SetDumpRouterMap(false) @@ -134,7 +134,7 @@ func Test_OTEL_BothTracingEnabled(t *testing.T) { res, err := client.Post(ctx, "/test?query1=testquery", g.Map{"param1": "testparam"}) t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test passes if no errors occurred during both request and response tracing }) @@ -142,13 +142,13 @@ func Test_OTEL_BothTracingEnabled(t *testing.T) { func Test_OTEL_NewConfiguration_RequestTracing(t *testing.T) { s := g.Server(guid.S()) - + // Enable request tracing using new independent OTEL configuration config := ghttp.NewConfig() config.Otel.TraceRequestEnabled = true err := s.SetConfig(config) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{"result": "ok"}) }) @@ -164,7 +164,7 @@ func Test_OTEL_NewConfiguration_RequestTracing(t *testing.T) { res, err := client.Post(ctx, "/test?query1=qvalue1", g.Map{"param1": "value1"}) t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test configuration helper methods t.Assert(s.GetConfig().IsOtelTraceRequestEnabled(), true) @@ -174,13 +174,13 @@ func Test_OTEL_NewConfiguration_RequestTracing(t *testing.T) { func Test_OTEL_NewConfiguration_ResponseTracing(t *testing.T) { s := g.Server(guid.S()) - + // Enable response tracing using new independent OTEL configuration config := ghttp.NewConfig() config.Otel.TraceResponseEnabled = true err := s.SetConfig(config) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{"result": "success", "data": "test data"}) }) @@ -196,7 +196,7 @@ func Test_OTEL_NewConfiguration_ResponseTracing(t *testing.T) { res, err := client.Get(ctx, "/test") t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test configuration helper methods t.Assert(s.GetConfig().IsOtelTraceRequestEnabled(), false) @@ -206,14 +206,14 @@ func Test_OTEL_NewConfiguration_ResponseTracing(t *testing.T) { func Test_OTEL_BackwardCompatibility(t *testing.T) { s := g.Server(guid.S()) - + // Test that legacy configuration still works alongside new configuration config := ghttp.NewConfig() - config.OtelTraceRequestEnabled = true // Legacy field + config.OtelTraceRequestEnabled = true // Legacy field config.Otel.TraceResponseEnabled = true // New field err := s.SetConfig(config) gtest.AssertNil(err) - + s.BindHandler("/test", func(r *ghttp.Request) { r.Response.WriteJson(g.Map{"result": "backward_compatible"}) }) @@ -229,7 +229,7 @@ func Test_OTEL_BackwardCompatibility(t *testing.T) { res, err := client.Post(ctx, "/test?query=test", g.Map{"param": "test"}) t.AssertNil(err) defer res.Close() - + t.Assert(res.StatusCode, 200) // Test that both legacy and new configuration work together t.Assert(s.GetConfig().IsOtelTraceRequestEnabled(), true) @@ -244,11 +244,11 @@ func Test_OTEL_Configuration_Helpers(t *testing.T) { t.Assert(otelConfig.IsTracingSQLEnabled(), false) t.Assert(otelConfig.IsTracingRequestEnabled(), false) t.Assert(otelConfig.IsTracingResponseEnabled(), false) - + otelConfig.TraceSQLEnabled = true otelConfig.TraceRequestEnabled = true t.Assert(otelConfig.IsTracingSQLEnabled(), true) t.Assert(otelConfig.IsTracingRequestEnabled(), true) t.Assert(otelConfig.IsTracingResponseEnabled(), false) }) -} \ No newline at end of file +}