From 627262f2555378bec1ee559df1e8e873af0af108 Mon Sep 17 00:00:00 2001 From: Hamid Malek Mohammadi Date: Tue, 23 Dec 2025 11:14:28 +0330 Subject: [PATCH] feat: enhance context handling for distributed tracing in API calls - Introduced context.Context in CallParamData for improved distributed tracing and request cancellation. - Updated CallApiJSON and CallApiForm handlers to set context for tracing if not already provided. - Ensured context is passed in ConsumeHandler for consistent tracing across API calls. --- .cursor/{rules => rules/basic.mdc} | 0 handlers/callApi.go | 6 ++++++ handlers/consumeHandler.go | 2 ++ libCallApi/call.go | 2 ++ 4 files changed, 10 insertions(+) rename .cursor/{rules => rules/basic.mdc} (100%) diff --git a/.cursor/rules b/.cursor/rules/basic.mdc similarity index 100% rename from .cursor/rules rename to .cursor/rules/basic.mdc diff --git a/handlers/callApi.go b/handlers/callApi.go index 7957ae5..58192ee 100644 --- a/handlers/callApi.go +++ b/handlers/callApi.go @@ -95,6 +95,9 @@ func CallApiJSON[Req any, Resp any]( webFramework.AddLog(w, CallApiLogEntry, slog.Any(method, param)) param.BodyType = libCallApi.JSON + if param.Context == nil { + param.Context = w.Ctx // Set context for distributed tracing if not already set + } resp, err := libCallApi.RemoteCall(param) if err != nil { webFramework.AddLog(w, CallApiLogEntry, slog.Any(fmt.Sprintf("%s-error", method), err)) @@ -113,6 +116,9 @@ func CallApiForm[Req any, Resp any]( webFramework.AddLog(w, CallApiLogEntry, slog.Any(method, param)) param.BodyType = libCallApi.Form + if param.Context == nil { + param.Context = w.Ctx // Set context for distributed tracing if not already set + } resp, err := libCallApi.RemoteCall(param) if err != nil { webFramework.AddLog(w, CallApiLogEntry, slog.Any(fmt.Sprintf("%s-error", method), err)) diff --git a/handlers/consumeHandler.go b/handlers/consumeHandler.go index 552681a..45dded2 100644 --- a/handlers/consumeHandler.go +++ b/handlers/consumeHandler.go @@ -117,6 +117,7 @@ func (c CallArgs[Req, Resp]) Handler(req HandlerRequest[Req, Resp]) (Resp, error Api: *req.Core.Params().GetRemoteApi(c.Api), Method: c.Method, Path: finalPath, + Context: req.W.Ctx, // Pass context for distributed tracing }, ) if err != nil { @@ -224,6 +225,7 @@ func (h *ConsumeHandlerType[Req, Resp]) Handler(req HandlerRequest[Req, Resp]) ( EnableLog: false, Headers: headersMap, Builder: req.Builder, + Context: req.W.Ctx, // Pass context for distributed tracing }) if errCall != nil { return req.Response, errCall diff --git a/libCallApi/call.go b/libCallApi/call.go index 24b2884..23a2a1c 100644 --- a/libCallApi/call.go +++ b/libCallApi/call.go @@ -24,6 +24,7 @@ type CallParamData struct { ValidateTls bool EnableLog bool JsonBody any + Context context.Context `json:"-"` // Context for distributed tracing and request cancellation } func (r CallParamData) LogValue() slog.Value { @@ -99,6 +100,7 @@ func Call[RespType any](param CallParam) CallResult[RespType] { EnableLog: param.EnableLog, Timeout: param.Timeout, Req: param.JsonBody, + Context: param.Context, // Pass context for distributed tracing httpClient: param.HttpClient, } resp, wsResp, callResp, err := ConsumeRest(callData)