Skip to content

Commit 9426755

Browse files
fix(edge/poll): update logging format
1 parent 46c81c1 commit 9426755

File tree

3 files changed

+19
-12
lines changed

3 files changed

+19
-12
lines changed

edge/client/error_response.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,19 @@ import (
99
"github.com/rs/zerolog/log"
1010
)
1111

12-
func logPollingError(resp *http.Response, errMsg string) error {
12+
func logPollingError(resp *http.Response, ctxMsg, errMsg string) error {
1313
var respErr httperror.HandlerError
1414
if err := json.NewDecoder(resp.Body).Decode(&respErr); err != nil {
15-
log.Error().Err(err).Int("response_code", resp.StatusCode).Msg("failed to parse response error")
15+
log.
16+
Error().
17+
Err(err).
18+
Str("context", ctxMsg).
19+
Int("response_code", resp.StatusCode).
20+
Msg("PollClient failed to decode server response")
1621
}
17-
log.Error().Err(respErr.Err).
22+
log.
23+
Error().Err(respErr.Err).
24+
Str("context", ctxMsg).
1825
Str("response message", respErr.Message).
1926
Int("status code", respErr.StatusCode).
2027
Msg(errMsg)

edge/client/portainer_edge_async_client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ func (client *PortainerAsyncClient) executeAsyncRequest(payload AsyncRequest, po
415415
defer resp.Body.Close()
416416

417417
if resp.StatusCode != http.StatusOK {
418-
return nil, logPollingError(resp, "short async poll request failed")
418+
return nil, logPollingError(resp, "AsyncEdgeAgentExecuteAsyncRequest", fmt.Sprintf("AsyncEdgeAgent [%d] failed to execute async request", client.getEndpointIDFn()))
419419
}
420420

421421
var asyncResponse AsyncResponse

edge/client/portainer_edge_client.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (client *PortainerEdgeClient) GetEnvironmentID() (portainer.EndpointID, err
117117
defer resp.Body.Close()
118118

119119
if resp.StatusCode != http.StatusOK {
120-
return 0, logPollingError(resp, "global key request failed")
120+
return 0, logPollingError(resp, "EdgeAgentGetEnvironmentID", fmt.Sprintf("EdgeAgent [%d] failed to request global key", client.getEndpointIDFn()))
121121
}
122122

123123
var responseData globalKeyResponse
@@ -160,7 +160,7 @@ func (client *PortainerEdgeClient) GetEnvironmentStatus(flags ...string) (*PollS
160160
}
161161

162162
if resp.StatusCode != http.StatusOK {
163-
return nil, logPollingError(resp, "short poll request failed")
163+
return nil, logPollingError(resp, "EdgeAgentGetEnvironmentStatus", fmt.Sprintf("EdgeAgent [%d] failed to request edge environment status", client.getEndpointIDFn()))
164164
}
165165

166166
var responseData PollStatusResponse
@@ -195,7 +195,7 @@ func (client *PortainerEdgeClient) GetEdgeStackConfig(edgeStackID int, version *
195195
defer resp.Body.Close()
196196

197197
if resp.StatusCode != http.StatusOK {
198-
return nil, logPollingError(resp, "GetEdgeStackConfig operation failed")
198+
return nil, logPollingError(resp, "EdgeAgentGetEdgeStackConfig", fmt.Sprintf("EdgeAgent [%d] failed to request edge stack config", client.getEndpointIDFn()))
199199
}
200200

201201
var data edge.StackPayload
@@ -250,7 +250,7 @@ func (client *PortainerEdgeClient) SetEdgeStackStatus(
250250
resp.Body.Close()
251251

252252
if resp.StatusCode != http.StatusOK {
253-
return logPollingError(resp, "SetEdgeStackStatus operation failed")
253+
return logPollingError(resp, "EdgeAgentSetEdgeStackStatus", fmt.Sprintf("EdgeAgent [%d] failed to set edge stack status", client.getEndpointIDFn()))
254254
}
255255

256256
return nil
@@ -285,7 +285,7 @@ func (client *PortainerEdgeClient) SetEdgeJobStatus(edgeJobStatus agent.EdgeJobS
285285
resp.Body.Close()
286286

287287
if resp.StatusCode != http.StatusOK {
288-
return logPollingError(resp, "SetEdgeJobStatus operation failed")
288+
return logPollingError(resp, "EdgeAgentSetEdgeJobStatus", fmt.Sprintf("EdgeAgent [%d] failed to set edge job status", client.getEndpointIDFn()))
289289
}
290290

291291
return nil
@@ -308,10 +308,10 @@ func (client *PortainerEdgeClient) GetEdgeConfig(id EdgeConfigID) (*EdgeConfig,
308308

309309
if resp.StatusCode != http.StatusOK {
310310
if resp.StatusCode == http.StatusForbidden {
311-
return nil, logPollingError(resp, "GetEdgeConfig operation forbidden")
311+
return nil, logPollingError(resp, "EdgeAgentGetEdgeConfig", fmt.Sprintf("EdgeAgent [%d] is forbidden to get the info of edge config [%d]", client.getEndpointIDFn(), id))
312312
}
313313

314-
return nil, logPollingError(resp, "GetEdgeConfig operation failed")
314+
return nil, logPollingError(resp, "EdgeAgentGetEdgeConfig", fmt.Sprintf("EdgeAgent [%d] failed to get the info of edge config [%d]", client.getEndpointIDFn(), id))
315315
}
316316

317317
var data EdgeConfig
@@ -341,7 +341,7 @@ func (client *PortainerEdgeClient) SetEdgeConfigState(id EdgeConfigID, state Edg
341341
resp.Body.Close()
342342

343343
if resp.StatusCode != http.StatusOK {
344-
return logPollingError(resp, fmt.Sprintf("edge_config_id: %d, state: %s, error: %s", id, state, "SetEdgeConfigState operation failed"))
344+
return logPollingError(resp, "EdgeAgentSetEdgeConfigState", fmt.Sprintf("EdgeAgent [%d] failed to set the state [%s] to edge config [%d]", client.getEndpointIDFn(), state, id))
345345
}
346346

347347
return nil

0 commit comments

Comments
 (0)