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

91 lines
2.6 KiB
Go

package resetactivity
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/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,
req *historyservice.ResetActivityRequest,
shardContext historyi.ShardContext,
workflowConsistencyChecker api.WorkflowConsistencyChecker,
) (resp *historyservice.ResetActivityResponse, retError error) {
request := req.GetFrontendRequest()
workflowKey := definition.NewWorkflowKey(
req.NamespaceId,
request.GetExecution().GetWorkflowId(),
request.GetExecution().GetRunId(),
)
err := api.GetAndUpdateWorkflowWithNew(
ctx,
nil,
workflowKey,
func(workflowLease api.WorkflowLease) (*api.UpdateWorkflowAction, error) {
mutableState := workflowLease.GetMutableState()
var activityIDs []string
switch a := request.GetActivity().(type) {
case *workflowservice.ResetActivityRequest_Id:
activityIDs = append(activityIDs, a.Id)
case *workflowservice.ResetActivityRequest_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 {
if err := workflow.ResetActivity(
ctx,
shardContext, mutableState, activityId,
request.ResetHeartbeat, request.KeepPaused, request.RestoreOriginalOptions,
request.Jitter.AsDuration(),
); err != nil {
return nil, err
}
}
return &api.UpdateWorkflowAction{
Noop: false,
CreateWorkflowTask: false,
}, nil
},
nil,
shardContext,
workflowConsistencyChecker,
)
if err != nil {
return nil, err
}
targetingMethod := "type"
if _, ok := req.GetFrontendRequest().GetActivity().(*workflowservice.ResetActivityRequest_Id); ok {
targetingMethod = "id"
}
if ns, err := shardContext.GetNamespaceRegistry().GetNamespaceByID(namespace.ID(req.NamespaceId)); err == nil {
metrics.ActivityResetRequests.With(shardContext.GetMetricsHandler().WithTags(
metrics.NamespaceTag(ns.Name().String()),
metrics.ActivityTargetingMethodTag(targetingMethod),
)).Record(1)
}
return &historyservice.ResetActivityResponse{}, nil
}