Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom actuator endpoint sample #576

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ More info on each sample:
- [**Synchronous Update**](/springboot/src/main/java/io/temporal/samples/springboot/update): Learn how to use Synchronous Update feature with this purchase 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))
- [**Customize Options**](/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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public String metrics(Model model) {
return "metrics";
}

@GetMapping("/customendpoint")
public String customEndpoint(Model model) {
model.addAttribute("sample", "Custom Actuator Worker Info Endpoint");
return "actuator";
}

@GetMapping("/update")
public String update(Model model) {
model.addAttribute("sample", "Synchronous Update");
Expand Down
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,61 @@
/*
* 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.spring.boot.autoconfigure.template.WorkersTemplate;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
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;

private AtomicInteger counter = new AtomicInteger(0);

@ReadOperation
public String workerInfo() {
StringBuilder sb = new StringBuilder();
Map<String, WorkersTemplate.RegisteredInfo> registeredInfo =
workersTemplate.getRegisteredInfo();
sb.append("Worker Info:\n\n");
registeredInfo.forEach(
(taskQueue, info) ->
sb.append(counter.incrementAndGet())
.append(".")
.append("\tTask Queue: ")
.append(taskQueue)
.append("\n\tRegistered Workflow Impls: ")
.append(info.getRegisteredWorkflowInfo().stream().collect(Collectors.joining(",")))
.append("\n\tRegistered Activity Impls: ")
.append(info.getRegisteredActivityInfo().stream().collect(Collectors.joining(",")))
.append("\n\n"));

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 @@ -22,6 +22,9 @@ <h5 class="card-title">Temporal Java SDK Samples</h5>
<div class="list-group">
<a href="/customize" class="list-group-item list-group-item-action">Customize Options</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
Loading