Skip to content

Commit

Permalink
spring boot custom actuator worker endpoint sample
Browse files Browse the repository at this point in the history
Signed-off-by: Tihomir Surdilovic <[email protected]>
  • Loading branch information
tsurdilo committed Jun 30, 2024
1 parent d994600 commit a7bec84
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ More info on each sample:
- [**Kafka Request / Reply**](/springboot/src/main/java/io/temporal/samples/springboot/kafka): Sample showing possible integration with event streaming platforms such as Kafka
- [**Customize Options**](/springboot/src/main/java/io/temporal/samples/springboot/customize): Sample showing how to customize options such as WorkerOptions, WorkerFactoryOptions, etc (see options config [here](springboot/src/main/java/io/temporal/samples/springboot/customize/TemporalOptionsConfig.java))
- [**Apache Camel Route**](/springboot/src/main/java/io/temporal/samples/springboot/camel): Sample showing how to start Workflow execution from a Camel Route
- [**Custom Actuator Endpoint**](/springboot/src/main/java/io/temporal/samples/springboot/actuator): Sample showing how to create a custom Actuator endpoint that shows registered Workflow and Activity impls per task queue.

#### Temporal Cloud
To run any of the SpringBoot samples in your Temporal Cloud namespace:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,4 +182,10 @@ public String camel(Model model) {
model.addAttribute("sample", "Camel Route");
return "camel";
}

@GetMapping("/customendpoint")
public String customEndpoint(Model model) {
model.addAttribute("sample", "Custom Actuator Worker Info Endpoint");
return "actuator";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# SpringBoot Actuator Worker Info Endpoint - Sample

1. Start SpringBoot from main samples repo directory:

./gradlew bootRun

2. In your browser navigate to:

http://localhost:3030/actuator/temporalworkerinfo

This sample shows how to create a custom Actuator Endpoint that
displays registered workflow and activity implementations per task queue.
This information comes from actually registered workers done by autoconfig module.
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright (c) 2020 Temporal Technologies, Inc. All Rights Reserved
*
* Copyright 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not
* use this file except in compliance with the License. A copy of the License is
* located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package io.temporal.samples.springboot.actuator;

import io.temporal.common.metadata.*;
import io.temporal.spring.boot.autoconfigure.template.WorkersTemplate;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "temporalworkerinfo")
public class WorkerActuatorEndpoint {
@Autowired
@Qualifier("temporalWorkersTemplate")
private WorkersTemplate workersTemplate;

@ReadOperation
public String workerInfo() {
StringBuilder sb = new StringBuilder();
Map<String, WorkersTemplate.RegisteredInfo> registeredInfo =
workersTemplate.getRegisteredInfo();
sb.append("Worker Info:");
registeredInfo.forEach(
(taskQueue, info) -> {
sb.append("\n\n\tTask Queue: ").append(taskQueue);
info.getRegisteredWorkflowInfo()
.forEach(
(workflowInfo) -> {
sb.append("\n\t\t Workflow Interface: ").append(workflowInfo.getClassName());
POJOWorkflowImplMetadata metadata = workflowInfo.getMetadata();
sb.append("\n\t\t\t Workflow Methods: ");
sb.append(
metadata.getWorkflowMethods().stream()
.map(POJOWorkflowMethodMetadata::getWorkflowMethod)
.map(Method::getName)
.collect(Collectors.joining(", ")));
sb.append("\n\t\t\t Query Methods: ");
sb.append(
metadata.getQueryMethods().stream()
.map(POJOWorkflowMethodMetadata::getWorkflowMethod)
.map(Method::getName)
.collect(Collectors.joining(", ")));
sb.append("\n\t\t\t Signal Methods: ");
sb.append(
metadata.getSignalMethods().stream()
.map(POJOWorkflowMethodMetadata::getWorkflowMethod)
.map(Method::getName)
.collect(Collectors.joining(", ")));
sb.append("\n\t\t\t Update Methods: ");
sb.append(
metadata.getUpdateMethods().stream()
.map(POJOWorkflowMethodMetadata::getWorkflowMethod)
.map(Method::getName)
.collect(Collectors.joining(",")));
sb.append("\n\t\t\t Update Validator Methods: ");
sb.append(
metadata.getUpdateValidatorMethods().stream()
.map(POJOWorkflowMethodMetadata::getWorkflowMethod)
.map(Method::getName)
.collect(Collectors.joining(", ")));
});
info.getRegisteredActivityInfo()
.forEach(
(activityInfo) -> {
sb.append("\n\t\t Activity Impl: ").append(activityInfo.getClassName());
POJOActivityImplMetadata metadata = activityInfo.getMetadata();
sb.append("\n\t\t\t Activity Interfaces: ");
sb.append(
metadata.getActivityInterfaces().stream()
.map(POJOActivityInterfaceMetadata::getInterfaceClass)
.map(Class::getName)
.collect(Collectors.joining(",")));
sb.append("\n\t\t\t Activity Methods: ");
sb.append(
metadata.getActivityMethods().stream()
.map(POJOActivityMethodMetadata::getMethod)
.map(Method::getName)
.collect(Collectors.joining(", ")));
});
});

return sb.toString();
}
}
2 changes: 1 addition & 1 deletion springboot/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ management:
endpoints:
web:
exposure:
include: prometheus
include: prometheus,temporalworkerinfo
# specific for samples
samples:
data:
Expand Down
20 changes: 20 additions & 0 deletions springboot/src/main/resources/templates/actuator.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
<head th:replace="fragments :: samples-header"></head>
<body>
<div class="container">
<div class="card">
<div class="card-body">
<h4 class="card-title" th:text="'Temporal Java SDK Samples: ' + ${sample}">Temporal Java SDK Samples</h4>
<h6>In this sample we show how to create a custom Actuator Endpoint showing Worker Info.</h6>
<br/><br/>
<div>
<h6><a href="https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html" target="_blank">Spring Actuator</a> allows
us to create custom endpoints. This sample shows how to create a Worker Info custom endpoint that shows registered Workflow and Activity impls per task queue.</h6><br/>
View the custom endpoint at <a href="http://localhost:3030/actuator/temporalworkerinfo" target="_blank">localhost:3030/actuator/temporalworkerinfo</a>
</div>
</div>
</div>
<footer th:replace="fragments :: samples-footer"></footer>
</body>
</html>
3 changes: 3 additions & 0 deletions springboot/src/main/resources/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ <h5 class="card-title">Temporal Java SDK Samples</h5>
<div class="list-group">
<a href="/camel" class="list-group-item list-group-item-action">Apache Camel Route</a>
</div>
<div class="list-group">
<a href="/customendpoint" class="list-group-item list-group-item-action">Custom Actuator Endpoint - Worker Info</a>
</div>
</div>
</div>
</div>
Expand Down

0 comments on commit a7bec84

Please sign in to comment.