Can't get name of an activity that failed

I’m trying to make a tool that will eventually dump data about failed workflows to an excel sheet.

I’m running into an issue where no matter what I do, I can’t find the name of the activity that failed. I can grab the error from the failure event just fine, but the activity name/type is always null.

How I’m fetching the activity name:

public HashMap<String, String> getFailuresFromHistory(History history) {
        HashMap<String, String> extractedFailures = new HashMap<>();
        history.getEventsList().forEach(event -> {
            if (event.getEventType() == EventType.EVENT_TYPE_ACTIVITY_TASK_FAILED) {
                ActivityTaskFailedEventAttributes attributes = event.getActivityTaskFailedEventAttributes();
                Failure failure = attributes.getFailure();
                String failureMessage = failure.getMessage();
                ActivityFailureInfo failureInfo = failure.getActivityFailureInfo();
                String activityName = (failureInfo.getActivityType().getName() != null) ? "Unknown activity" : failureInfo.getActivityType().getName();
                extractedFailures.put(activityName, failureMessage);
            }
            
        });
        return extractedFailures;
    }

How I’m fetching the “History”:

public History getExecutionHistory(WorkflowExecution execution, WorkflowServiceStubs service) {
        GetWorkflowExecutionHistoryRequest request = 
            GetWorkflowExecutionHistoryRequest
                .newBuilder()
                    .setExecution(execution)
                    .setNamespace("default")
                    .build();
        
        GetWorkflowExecutionHistoryResponse response = service.blockingStub().getWorkflowExecutionHistory(request);
        return response.getHistory();
    }

Edit:

I just looked at the “JSON” tab in the dashboard and realized the name is only kept in the “Scheduled” step. How is it possible for me to tie the failure event to the scheduled event? The only thing I can think of is “grouping” the events in groups of 3 (since it’s always Scheduled, Started, Completed/Failed) but this static approach seems error prone.

Edit 2:

Apparently, I was looking in the wrong place. Theoretically, the last event, of type WorkflowExecutionFailed should contain both.

"activityFailureInfo": {
          "scheduledEventId": "11",
          "startedEventId": "12",
          "identity": "",
          "activityType": {
            "name": "GetGroup"
          },
          "activityId": "6d4aff0c-c249-3eec-9934-ca5a3697788d",
          "retryState": "MaximumAttemptsReached"
        }
      },

How is it possible for me to tie the failure event to the scheduled event?

ActivityTaskFailed event has scheduledEventId property which is the event id of the associated ActivityTaskScheduled event that contains activity info.

The only thing I can think of is “grouping” the events in groups of 3 (since it’s always Scheduled, Started, Completed/Failed) but this static approach seems error prone.

Yes, I think it would work for sync activity invocation only, not async. Would need to associate via scheduledEventId on ActivityTaskStarted and associated Completed/Failed/Timedout events.

Just to add, SDKs emit the activity_execution_failed counter metric that you could use and alert on and might be easier to look at with Grafana for example as coul set time ranges etc.