Files
Sean KaneandGitHub 99f097036d Add request metrics for activity operator commands (#9661)
## What changed?
Add instrumentation for the activity operator commands which previously
had no metrics. `activity_targeting_method` is `id` when targetting a
specific ID, else `type`. UpdateOptions also has a structured log line
for changed fields.

## Why?
Add visibility into the usage of the activity operator commands.

## How did you test it?
- [ ] built
- [ ] run locally and tested manually
- [ ] covered by existing tests
- [ ] added new unit test(s)
- [ ] added new functional test(s)

## Potential risks
Minimal, cardinality is low for these
2026-03-25 15:31:15 -06:00

131 lines
3.8 KiB
Go

package unpauseactivity
import (
"context"
"go.temporal.io/api/workflowservice/v1"
"go.temporal.io/server/api/historyservice/v1"
"go.temporal.io/server/common/definition"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/service/history/api"
"go.temporal.io/server/service/history/consts"
historyi "go.temporal.io/server/service/history/interfaces"
"go.temporal.io/server/service/history/workflow"
)
func Invoke(
ctx context.Context,
request *historyservice.UnpauseActivityRequest,
shardContext historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
) (resp *historyservice.UnpauseActivityResponse, retError error) {
var response *historyservice.UnpauseActivityResponse
err := api.GetAndUpdateWorkflowWithNew(
ctx,
nil,
definition.NewWorkflowKey(
request.NamespaceId,
request.GetFrontendRequest().GetExecution().GetWorkflowId(),
request.GetFrontendRequest().GetExecution().GetRunId(),
),
func(workflowLease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
mutableState := workflowLease.GetMutableState()
var err error
response, err = processUnpauseActivityRequest(shardContext, mutableState, request)
if err != nil {
return nil, err
}
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: false,
}, nil
},
nil,
shardContext,
workflowConsistencyChecker,
)
if err != nil {
return nil, err
}
frontendReq := request.GetFrontendRequest()
targetingMethod := "type"
if _, ok := frontendReq.GetActivity().(*workflowservice.UnpauseActivityRequest_Id); ok {
targetingMethod = "id"
}
if ns, err := shardContext.GetNamespaceRegistry().GetNamespaceByID(namespace.ID(request.NamespaceId)); err == nil {
metrics.ActivityUnpauseRequests.With(shardContext.GetMetricsHandler().WithTags(
metrics.NamespaceTag(ns.Name().String()),
metrics.ActivityTargetingMethodTag(targetingMethod),
)).Record(1)
}
shardContext.GetLogger().Info("unpauseactivity: activity unpaused",
tag.WorkflowNamespaceID(request.GetNamespaceId()),
tag.WorkflowID(frontendReq.GetExecution().GetWorkflowId()),
tag.WorkflowRunID(frontendReq.GetExecution().GetRunId()),
tag.NewBoolTag("reset_attempts", frontendReq.GetResetAttempts()),
tag.NewBoolTag("reset_heartbeat", frontendReq.GetResetHeartbeat()),
tag.NewDurationTag("jitter", frontendReq.GetJitter().AsDuration()),
)
return response, err
}
func processUnpauseActivityRequest(
shardContext historyi.ShardContext,
mutableState historyi.MutableState,
request *historyservice.UnpauseActivityRequest,
) (*historyservice.UnpauseActivityResponse, error) {
if !mutableState.IsWorkflowExecutionRunning() {
return nil, consts.ErrWorkflowCompleted
}
frontendRequest := request.GetFrontendRequest()
var activityIDs []string
switch a := frontendRequest.GetActivity().(type) {
case *workflowservice.UnpauseActivityRequest_Id:
activityIDs = append(activityIDs, a.Id)
case *workflowservice.UnpauseActivityRequest_Type:
activityType := a.Type
for _, ai := range mutableState.GetPendingActivityInfos() {
if ai.ActivityType.Name == activityType {
activityIDs = append(activityIDs, ai.ActivityId)
}
}
}
if len(activityIDs) == 0 {
return nil, consts.ErrActivityNotFound
}
for _, activityId := range activityIDs {
ai, activityFound := mutableState.GetActivityByActivityID(activityId)
if !activityFound {
return nil, consts.ErrActivityNotFound
}
if !ai.Paused {
// do nothing
continue
}
if err := workflow.UnpauseActivity(
shardContext, mutableState, ai,
frontendRequest.GetResetAttempts(),
frontendRequest.GetResetHeartbeat(),
frontendRequest.GetJitter().AsDuration()); err != nil {
return nil, err
}
}
return &historyservice.UnpauseActivityResponse{}, nil
}