Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix failure_reason label for nexus_task_execution_failed metric on task timeout #1684

Merged
merged 4 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions internal/internal_nexus_task_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ import (
"go.temporal.io/sdk/log"
)

// errNexusTaskTimeout is returned when the Nexus task handler times out.
// It is used instead of context.DeadlineExceeded to allow the poller to differentiate between Nexus task handler
// timeout and other errors.
var errNexusTaskTimeout = errors.New("nexus task timeout")

func nexusHandlerError(t nexus.HandlerErrorType, message string) *nexuspb.HandlerError {
return &nexuspb.HandlerError{
ErrorType: string(t),
Expand Down Expand Up @@ -211,7 +216,7 @@ func (h *nexusTaskHandler) handleStartOperation(
opres, err = h.nexusHandler.StartOperation(ctx, req.GetService(), req.GetOperation(), input, startOptions)
}()
if ctx.Err() != nil {
return nil, nil, ctx.Err()
return nil, nil, errNexusTaskTimeout
}
if err != nil {
var unsuccessfulOperationErr *nexus.UnsuccessfulOperationError
Expand Down Expand Up @@ -302,7 +307,7 @@ func (h *nexusTaskHandler) handleCancelOperation(ctx context.Context, nctx *Nexu
err = h.nexusHandler.CancelOperation(ctx, req.GetService(), req.GetOperation(), req.GetOperationId(), cancelOptions)
}()
if ctx.Err() != nil {
return nil, nil, ctx.Err()
return nil, nil, errNexusTaskTimeout
}
if err != nil {
err = convertKnownErrors(err)
Expand Down
8 changes: 7 additions & 1 deletion internal/internal_nexus_task_poller.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,14 @@ func (ntp *nexusTaskPoller) ProcessTask(task interface{}) error {
// Failure from user handler.
// Special case for the start response with operation error.
if err != nil {
var failureTag string
if err == errNexusTaskTimeout {
failureTag = "timeout"
} else {
failureTag = "internal_sdk_error"
}
metricsHandler.
WithTags(metrics.NexusTaskFailureTags("internal_sdk_error")).
WithTags(metrics.NexusTaskFailureTags(failureTag)).
Counter(metrics.NexusTaskExecutionFailedCounter).
Inc(1)
} else if failure != nil {
Expand Down
20 changes: 20 additions & 0 deletions test/nexus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ var syncOp = temporalnexus.NewSyncOperation("sync-op", func(ctx context.Context,
return "", temporal.NewApplicationErrorWithOptions("fake app error for test", "FakeTestError", temporal.ApplicationErrorOptions{
NonRetryable: true,
})
case "timeout":
<-ctx.Done()
return "", ctx.Err()
case "panic":
panic("panic requested")
}
Expand Down Expand Up @@ -351,6 +354,23 @@ func TestNexusSyncOperation(t *testing.T) {
tc.requireFailureCounter(t, service.Name, syncOp.Name(), "handler_error_INTERNAL")
}, time.Second*3, time.Millisecond*100)
})

t.Run("timeout", func(t *testing.T) {
_, err := nexus.ExecuteOperation(ctx, nc, syncOp, "timeout", nexus.ExecuteOperationOptions{
// Force shorter timeout to speed up the test and get a response back.
Header: nexus.Header{nexus.HeaderRequestTimeout: "300ms"},
})
var unexpectedResponseErr *nexus.UnexpectedResponseError
require.ErrorAs(t, err, &unexpectedResponseErr)
require.Equal(t, nexus.StatusDownstreamTimeout, unexpectedResponseErr.Response.StatusCode)

require.EventuallyWithT(t, func(t *assert.CollectT) {
// NOTE metrics.NexusTaskEndToEndLatency isn't recorded on timeouts.
tc.requireTimer(t, metrics.NexusTaskScheduleToStartLatency, service.Name, syncOp.Name())
tc.requireTimer(t, metrics.NexusTaskExecutionLatency, service.Name, syncOp.Name())
tc.requireFailureCounter(t, service.Name, syncOp.Name(), "timeout")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only assert the failure counter? shouldn't NexusTaskScheduleToStartLatency be published?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I figured it's covered in the other tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm I think this case is different because we don't report E2E latency on timeout

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, I meant the other two metrics. In any case, I've added the tests.

}, time.Second*3, time.Millisecond*100)
})
}

func TestNexusWorkflowRunOperation(t *testing.T) {
Expand Down
Loading