@Override
    protected CompletableFuture<TaskManagerDetailsInfo> handleRequest(
            @Nonnull HandlerRequest<EmptyRequestBody> request,
            @Nonnull ResourceManagerGateway gateway)
            throws RestHandlerException {
        final ResourceID taskManagerResourceId =
                request.getPathParameter(TaskManagerIdPathParameter.class);

        CompletableFuture<TaskManagerInfoWithSlots> taskManagerInfoWithSlotsFuture =
                gateway.requestTaskManagerDetailsInfo(taskManagerResourceId, timeout);

        metricFetcher.update();

        return taskManagerInfoWithSlotsFuture
                .thenApply(
                        (taskManagerInfoWithSlots) -> {
                            final MetricStore.TaskManagerMetricStore tmMetrics =
                                    metricStore.getTaskManagerMetricStore(
                                            taskManagerResourceId.getResourceIdString());

                            final TaskManagerMetricsInfo taskManagerMetricsInfo;

                            if (tmMetrics != null) {
                                log.debug(
                                        "Create metrics info for TaskManager {}.",
                                        taskManagerResourceId.getStringWithMetadata());
                                taskManagerMetricsInfo = createTaskManagerMetricsInfo(tmMetrics);
                            } else {
                                log.debug(
                                        "No metrics for TaskManager {}.",
                                        taskManagerResourceId.getStringWithMetadata());
                                taskManagerMetricsInfo = TaskManagerMetricsInfo.empty();
                            }

                            return new TaskManagerDetailsInfo(
                                    taskManagerInfoWithSlots, taskManagerMetricsInfo);
                        })
                .exceptionally(
                        (Throwable throwable) -> {
                            final Throwable strippedThrowable =
                                    ExceptionUtils.stripExecutionException(throwable);

                            if (strippedThrowable instanceof UnknownTaskExecutorException) {
                                throw new CompletionException(
                                        new RestHandlerException(
                                                "Could not find TaskExecutor "
                                                        + taskManagerResourceId
                                                        + '.',
                                                HttpResponseStatus.NOT_FOUND,
                                                strippedThrowable));
                            } else {
                                throw new CompletionException(strippedThrowable);
                            }
                        });
    }
@Test
    void testTaskManagerMetricsInfoExtraction()
            throws RestHandlerException, ExecutionException, InterruptedException,
                    JsonProcessingException, HandlerRequestException {
        initializeMetricStore(metricFetcher.getMetricStore());
        resourceManagerGateway.setRequestTaskManagerDetailsInfoFunction(
                taskManagerId ->
                        CompletableFuture.completedFuture(
                                new TaskManagerInfoWithSlots(
                                        createEmptyTaskManagerInfo(), Collections.emptyList())));

        HandlerRequest<EmptyRequestBody> request = createRequest();
        TaskManagerDetailsInfo taskManagerDetailsInfo =
                testInstance.handleRequest(request, resourceManagerGateway).get();

        TaskManagerMetricsInfo actual = taskManagerDetailsInfo.getTaskManagerMetricsInfo();
        TaskManagerMetricsInfo expected =
                new TaskManagerMetricsInfo(
                        1L,
                        2L,
                        3L,
                        4L,
                        5L,
                        6L,
                        7L,
                        8L,
                        9L,
                        10L,
                        11L,
                        12L,
                        15L,
                        16L,
                        17L,
                        18L,
                        19L,
                        20L,
                        Collections.emptyList());

        ObjectMapper objectMapper = JacksonMapperFactory.createObjectMapper();
        String actualJson = objectMapper.writeValueAsString(actual);
        String expectedJson = objectMapper.writeValueAsString(expected);

        assertThat(actualJson).isEqualTo(expectedJson);
    }