Skip to content

Commit be9c49c

Browse files
authored
spring boot samples - sdk metrics (#480)
* spring boot samples - sdk metrics Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * spelling Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * fix numbering Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> * update readme Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io> --------- Signed-off-by: Tihomir Surdilovic <tihomir@temporal.io>
1 parent 658894f commit be9c49c

File tree

7 files changed

+119
-1
lines changed

7 files changed

+119
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,4 +140,5 @@ See the README.md file in each main sample directory for cut/paste Gradle comman
140140

141141
More info on each sample:
142142
- [**Hello**](/springboot/src/main/java/io/temporal/samples/springboot/hello): Invoke simple "Hello" workflow from a GET endpoint
143+
- [**SDK Metrics**](/springboot/src/main/java/io/temporal/samples/springboot/metrics): Learn how to set up SDK Metrics
143144

springboot/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ dependencies {
55
implementation "org.springframework.boot:spring-boot-starter-thymeleaf"
66
implementation "org.springframework.boot:spring-boot-starter-actuator"
77
implementation "io.temporal:temporal-spring-boot-starter-alpha:$javaSDKVersion"
8+
runtimeOnly "io.micrometer:micrometer-registry-prometheus"
89
testImplementation "org.springframework.boot:spring-boot-starter-test"
910
dependencies {
1011
errorproneJavac('com.google.errorprone:javac:9+181-r4173-1')

springboot/src/main/java/io/temporal/samples/springboot/SamplesController.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,10 @@ ResponseEntity helloSample(@RequestBody Person person) {
5858
// bypass thymeleaf, don't return template name just result
5959
return new ResponseEntity("\"" + workflow.sayHello(person) + "\"", HttpStatus.OK);
6060
}
61+
62+
@GetMapping("/metrics")
63+
public String metrics(Model model) {
64+
model.addAttribute("sample", "SDK Metrics");
65+
return "metrics";
66+
}
6167
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# SpringBoot Metrics Sample
2+
3+
1. Start SpringBoot from main samples repo directory:
4+
5+
./gradlew bootRun
6+
7+
2. In your browser navigate to:
8+
9+
http://localhost:3030/metrics
10+
11+
This sample involves just SpringBoot and Actuator configurations which are
12+
included in the samples already. The page shows information on how to set up
13+
SDK metrics in your SpringBoot applications.

springboot/src/main/resources/application.yaml

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ server:
33
spring:
44
application:
55
name: temporal-samples
6+
# temporal specific configs
67
temporal:
78
connection:
89
target: 127.0.0.1:7233
@@ -21,6 +22,13 @@ spring:
2122
# max-threads: 10
2223
workersAutoDiscovery:
2324
packages: io.temporal.samples.springboot
25+
# actuator (sdk metrics)
26+
management:
27+
endpoints:
28+
web:
29+
exposure:
30+
include: prometheus
31+
# specific for samples
2432
samples:
2533
data:
26-
language: english
34+
language: english

springboot/src/main/resources/templates/index.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ <h5 class="card-title">Temporal Java SDK Samples</h5>
2020
<div class="list-group">
2121
<a href="/hello" class="list-group-item list-group-item-action">Say Hello</a>
2222
</div>
23+
<div class="list-group">
24+
<a href="/metrics" class="list-group-item list-group-item-action">SDK Metrics</a>
25+
</div>
2326
</div>
2427
</div>
2528

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<!DOCTYPE html>
2+
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/html">
3+
<head>
4+
<link rel="stylesheet"
5+
href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"/>
6+
<link rel="stylesheet"
7+
href="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/themes/prism.min.css"/>
8+
<link href="https://fonts.googleapis.com/css2?family=Lato:ital,wght@1,400;1,700&display=swap" rel="stylesheet">
9+
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
10+
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.11.0/umd/popper.min.js"></script>
11+
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"></script>
12+
<script src="https://cdnjs.cloudflare.com/ajax/libs/prism/9000.0.1/prism.min.js"></script>
13+
</head>
14+
<body>
15+
<div class="container">
16+
<div class="card">
17+
<div class="card-body">
18+
<h4 class="card-title" th:text="'Temporal Java SDK Samples: ' + ${sample}">Temporal Java SDK Samples</h4>
19+
<h6>In this sample we show how to set up and consume SDK metrics.</h6>
20+
<br/><br/><br/>
21+
<div>
22+
<h6>Configuring SDK metrics is super easy using <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/actuator.html" target="_blank">Spring Actuator</a></h6><br/>
23+
<div class="list-group">
24+
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
25+
<div class="d-flex w-100 justify-content-between">
26+
<h5 class="mb-1">1. Add spring-boot-starter-actuator to project dependencies</h5>
27+
</div>
28+
<p class="mb-1">
29+
<pre><code>Gradle:
30+
<br/> implementation "org.springframework.boot:spring-boot-starter-actuator"
31+
</code></pre>
32+
<pre><code>Maven:
33+
<br/> &lt;dependency&gt;
34+
&lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
35+
&lt;artifactId&gt;spring-boot-starter-actuator&lt;/artifactId&gt;
36+
&lt;/dependency&gt;
37+
</code></pre>
38+
</p>
39+
</a>
40+
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
41+
<div class="d-flex w-100 justify-content-between">
42+
<h5 class="mb-1">2. Add micrometer-registry-prometheus as runtime dependency</h5>
43+
</div>
44+
<p class="mb-1">
45+
<pre><code>Gradle:
46+
<br/> runtimeOnly "io.micrometer:micrometer-registry-prometheus"
47+
</code></pre>
48+
<pre><code>Maven:
49+
<br/> &lt;dependency&gt;
50+
&lt;groupId>io.micrometer&lt;/groupId&gt;
51+
&lt;artifactId>micrometer-registry-prometheus&lt;/artifactId&gt;
52+
&lt;scope>runtime&lt;/scope&gt;
53+
&lt;/dependency>
54+
</code></pre>
55+
</p>
56+
</a>
57+
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
58+
<div class="d-flex w-100 justify-content-between">
59+
<h5 class="mb-1">3. Configure Actuator in application properties/yaml</h5>
60+
</div>
61+
<p class="mb-1">
62+
<pre><code>management:
63+
endpoints:
64+
web:
65+
exposure:
66+
include: prometheus
67+
</code></pre>
68+
<small class="text-muted">Alternatively, you can define a custom io.micrometer.core.instrument.MeterRegistry bean in the application context.</small>
69+
</a>
70+
<a href="#" class="list-group-item list-group-item-action flex-column align-items-start">
71+
<div class="d-flex w-100 justify-content-between">
72+
<h5 class="mb-1">4. View metrics (Prometheus format)</h5>
73+
</div>
74+
<p>Actuator will expose SDK metrics automatically.<br/>
75+
You can access raw metrics at "localhost:3030/actuator/prometheus".<br/>
76+
This is already set up for the samples project.
77+
</p>
78+
<small class="text-muted">You can set up Prometheus config to scrape SDK metrics from this endpoint.</small>
79+
</a>
80+
</div>
81+
</div>
82+
</div>
83+
</div>
84+
</div>
85+
</body>
86+
</html>

0 commit comments

Comments
 (0)