Skip to content

Commit 6d54278

Browse files
IWF-415: Improve activity logging (#521)
1 parent 1bed2dc commit 6d54278

File tree

3 files changed

+34
-32
lines changed

3 files changed

+34
-32
lines changed

service/api/handler.go

+15-28
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"encoding/json"
54
"github.com/indeedeng/iwf/config"
65
"net/http"
76

@@ -48,7 +47,7 @@ func (h *handler) apiV1WorkflowStart(c *gin.Context) {
4847
invalidRequestSchema(c)
4948
return
5049
}
51-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
50+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
5251

5352
resp, errResp := h.svc.ApiV1WorkflowStartPost(c.Request.Context(), req)
5453
if errResp != nil {
@@ -65,7 +64,7 @@ func (h *handler) apiV1WorkflowWaitForStateCompletion(c *gin.Context) {
6564
invalidRequestSchema(c)
6665
return
6766
}
68-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
67+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
6968

7069
resp, errResp := h.svc.ApiV1WorkflowWaitForStateCompletion(c.Request.Context(), req)
7170
if errResp != nil {
@@ -82,7 +81,7 @@ func (h *handler) apiV1WorkflowSignal(c *gin.Context) {
8281
invalidRequestSchema(c)
8382
return
8483
}
85-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
84+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
8685

8786
errResp := h.svc.ApiV1WorkflowSignalPost(c.Request.Context(), req)
8887
if errResp != nil {
@@ -99,7 +98,7 @@ func (h *handler) apiV1WorkflowStop(c *gin.Context) {
9998
invalidRequestSchema(c)
10099
return
101100
}
102-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
101+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
103102

104103
errResp := h.svc.ApiV1WorkflowStopPost(c.Request.Context(), req)
105104
if errResp != nil {
@@ -116,7 +115,7 @@ func (h *handler) apiV1WorkflowInternalDump(c *gin.Context) {
116115
invalidRequestSchema(c)
117116
return
118117
}
119-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
118+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
120119

121120
resp, errResp := h.svc.ApiV1WorkflowDumpPost(c.Request.Context(), req)
122121
if errResp != nil {
@@ -133,7 +132,7 @@ func (h *handler) apiV1WorkflowConfigUpdate(c *gin.Context) {
133132
invalidRequestSchema(c)
134133
return
135134
}
136-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
135+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
137136

138137
errResp := h.svc.ApiV1WorkflowConfigUpdate(c.Request.Context(), req)
139138
if errResp != nil {
@@ -150,7 +149,7 @@ func (h *handler) apiV1WorkflowTriggerContinueAsNew(c *gin.Context) {
150149
invalidRequestSchema(c)
151150
return
152151
}
153-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
152+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
154153

155154
errResp := h.svc.ApiV1WorkflowTriggerContinueAsNew(c.Request.Context(), req)
156155
if errResp != nil {
@@ -167,7 +166,7 @@ func (h *handler) apiV1WorkflowSearch(c *gin.Context) {
167166
invalidRequestSchema(c)
168167
return
169168
}
170-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
169+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
171170

172171
resp, errResp := h.svc.ApiV1WorkflowSearchPost(c.Request.Context(), req)
173172
if errResp != nil {
@@ -184,7 +183,7 @@ func (h *handler) apiV1WorkflowRpc(c *gin.Context) {
184183
invalidRequestSchema(c)
185184
return
186185
}
187-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
186+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
188187

189188
resp, errResp := h.svc.ApiV1WorkflowRpcPost(c.Request.Context(), req)
190189
if errResp != nil {
@@ -210,7 +209,7 @@ func (h *handler) apiV1WorkflowGetDataObjects(c *gin.Context) {
210209
invalidRequestSchema(c)
211210
return
212211
}
213-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
212+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
214213

215214
resp, errResp := h.svc.ApiV1WorkflowGetQueryAttributesPost(c.Request.Context(), req)
216215
if errResp != nil {
@@ -227,7 +226,7 @@ func (h *handler) apiV1WorkflowSetDataObjects(c *gin.Context) {
227226
invalidRequestSchema(c)
228227
return
229228
}
230-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
229+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
231230

232231
errResp := h.svc.ApiV1WorkflowSetQueryAttributesPost(c.Request.Context(), req)
233232
if errResp != nil {
@@ -244,7 +243,7 @@ func (h *handler) apiV1WorkflowGetSearchAttributes(c *gin.Context) {
244243
invalidRequestSchema(c)
245244
return
246245
}
247-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
246+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
248247

249248
resp, errResp := h.svc.ApiV1WorkflowGetSearchAttributesPost(c.Request.Context(), req)
250249
if errResp != nil {
@@ -261,7 +260,7 @@ func (h *handler) apiV1WorkflowSetSearchAttributes(c *gin.Context) {
261260
invalidRequestSchema(c)
262261
return
263262
}
264-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
263+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
265264

266265
errResp := h.svc.ApiV1WorkflowSetSearchAttributesPost(c.Request.Context(), req)
267266
if errResp != nil {
@@ -286,7 +285,7 @@ func (h *handler) doApiV1WorkflowGetPost(c *gin.Context, waitIfStillRunning bool
286285
invalidRequestSchema(c)
287286
return
288287
}
289-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
288+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
290289

291290
var resp *iwfidl.WorkflowGetResponse
292291
var errResp *errors.ErrorAndStatus
@@ -310,7 +309,7 @@ func (h *handler) apiV1WorkflowReset(c *gin.Context) {
310309
invalidRequestSchema(c)
311310
return
312311
}
313-
h.logger.Debug("received API request", tag.Value(h.toJsonLogging(req)))
312+
h.logger.Debug("received API request", tag.Value(log.ToJsonAndTruncateForLogging(req)))
314313

315314
resp, errResp := h.svc.ApiV1WorkflowResetPost(c.Request.Context(), req)
316315
if errResp != nil {
@@ -345,15 +344,3 @@ func invalidRequestSchema(c *gin.Context) {
345344
func (h *handler) processError(c *gin.Context, resp *errors.ErrorAndStatus) {
346345
c.JSON(resp.StatusCode, resp.Error)
347346
}
348-
349-
func (h *handler) toJsonLogging(req any) string {
350-
str, err := json.Marshal(req)
351-
if len(str) > 1000 {
352-
str = str[0:1000]
353-
}
354-
if err != nil {
355-
h.logger.Error("error when serializing request", tag.Error(err), tag.DefaultValue(req))
356-
return ""
357-
}
358-
return string(str)
359-
}

service/common/log/truncate.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package log
2+
3+
import "encoding/json"
4+
5+
func ToJsonAndTruncateForLogging(req any) string {
6+
str, err := json.Marshal(req)
7+
if len(str) > 1000 {
8+
str = str[0:1000]
9+
}
10+
if err != nil {
11+
return "Error when serializing request: " + err.Error()
12+
}
13+
return string(str)
14+
}

service/interpreter/activityImpl.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/indeedeng/iwf/service"
88
"github.com/indeedeng/iwf/service/common/compatibility"
99
"github.com/indeedeng/iwf/service/common/event"
10+
"github.com/indeedeng/iwf/service/common/log"
1011
"github.com/indeedeng/iwf/service/common/ptr"
1112
"github.com/indeedeng/iwf/service/common/rpc"
1213
"github.com/indeedeng/iwf/service/common/urlautofix"
@@ -31,7 +32,7 @@ func StateApiWaitUntil(
3132
stateApiWaitUntilStartTime := time.Now().UnixMilli()
3233
provider := getActivityProviderByType(backendType)
3334
logger := provider.GetLogger(ctx)
34-
logger.Info("StateStartActivity", "input", input)
35+
logger.Info("StateWaitUntilActivity", "input", log.ToJsonAndTruncateForLogging(input))
3536
iwfWorkerBaseUrl := urlautofix.FixWorkerUrl(input.IwfWorkerUrl)
3637

3738
svcCfg := env.GetSharedConfig()
@@ -116,7 +117,7 @@ func StateApiExecute(
116117
stateApiExecuteStartTime := time.Now().UnixMilli()
117118
provider := getActivityProviderByType(backendType)
118119
logger := provider.GetLogger(ctx)
119-
logger.Info("StateDecideActivity", "input", input)
120+
logger.Info("StateExecuteActivity", "input", log.ToJsonAndTruncateForLogging(input))
120121

121122
iwfWorkerBaseUrl := urlautofix.FixWorkerUrl(input.IwfWorkerUrl)
122123
svcCfg := env.GetSharedConfig()
@@ -330,7 +331,7 @@ func DumpWorkflowInternal(
330331
) (*iwfidl.WorkflowDumpResponse, error) {
331332
provider := getActivityProviderByType(backendType)
332333
logger := provider.GetLogger(ctx)
333-
logger.Info("DumpWorkflowInternal", "input", req)
334+
logger.Info("DumpWorkflowInternalActivity", "input", log.ToJsonAndTruncateForLogging(req))
334335

335336
svcCfg := env.GetSharedConfig()
336337
apiAddress := svcCfg.GetApiServiceAddressWithDefault()
@@ -359,7 +360,7 @@ func InvokeWorkerRpc(
359360
) (*InvokeRpcActivityOutput, error) {
360361
provider := getActivityProviderByType(backendType)
361362
logger := provider.GetLogger(ctx)
362-
logger.Info("invoke worker RPC by activity", "input", req)
363+
logger.Info("InvokeWorkerRpcActivity", "input", log.ToJsonAndTruncateForLogging(req))
363364

364365
apiMaxSeconds := env.GetSharedConfig().Api.MaxWaitSeconds
365366

0 commit comments

Comments
 (0)