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

4.x: Archetype - Implement MP JSON and metrics combination #8629

Merged
merged 2 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
209 changes: 2 additions & 207 deletions archetypes/archetypes/src/main/archetype/common/observability.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,215 +35,10 @@
if="${flavor} == 'mp'">
<option value="microprofile"
name="MicroProfile"
description="Expose metrics using the MicroProfile API">
<output>
<model>
<list key="readme-sections">
<value><![CDATA[
## Try metrics

```
# Prometheus Format
curl -s -X GET http://localhost:8080/metrics
# TYPE base:gc_g1_young_generation_count gauge
. . .

# JSON Format
curl -H 'Accept: application/json' -X GET http://localhost:8080/metrics
{"base":...
. . .
```

]]></value>
</list>
<list key="dependencies">
<map>
<value key="groupId">org.eclipse.microprofile.metrics</value>
<value key="artifactId">microprofile-metrics-api</value>
</map>
<map>
<value key="groupId">io.helidon.microprofile.metrics</value>
<value key="artifactId">helidon-microprofile-metrics</value>
</map>
</list>
<list key="SimpleGreetService-imports">
<value>org.eclipse.microprofile.metrics.MetricUnits</value>
<value>org.eclipse.microprofile.metrics.annotation.Counted</value>
<value>org.eclipse.microprofile.metrics.annotation.Timed</value>
<value>jakarta.ws.rs.PathParam</value>
</list>
<list key="SimpleGreetResource-static-fields">
<value><![CDATA[
private static final String PERSONALIZED_GETS_COUNTER_NAME = "personalizedGets";
private static final String PERSONALIZED_GETS_COUNTER_DESCRIPTION = "Counts personalized GET operations";
private static final String GETS_TIMER_NAME = "allGets";
private static final String GETS_TIMER_DESCRIPTION = "Tracks all GET operations";]]></value>
</list>
<list key="SimpleGreetService-methods">
<value><![CDATA[
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
tvallin marked this conversation as resolved.
Show resolved Hide resolved
@Counted(name = PERSONALIZED_GETS_COUNTER_NAME,
absolute = true,
description = PERSONALIZED_GETS_COUNTER_DESCRIPTION)
@Timed(name = GETS_TIMER_NAME,
description = GETS_TIMER_DESCRIPTION,
unit = MetricUnits.SECONDS,
absolute = true)
public String getMessage(@PathParam("name") String name) {
return String.format("Hello %s", name);
}]]></value>
</list>
<list key="MainTest-java-imports">
<value>org.eclipse.microprofile.metrics.Counter</value>
<value>org.eclipse.microprofile.metrics.MetricRegistry</value>
</list>
<list key="MainTest-helidon-imports">
<value>io.helidon.metrics.api.MetricsFactory</value>
</list>
<list key="MainTest-other-imports">
<value>org.junit.jupiter.api.AfterAll</value>
</list>
<list key="MainTest-static-imports">
<value>static org.junit.jupiter.api.Assertions.assertEquals</value>
</list>
<list key="MainTest-methods" order="999">
<value><![CDATA[
@AfterAll
static void clear() {
MetricsFactory.closeAll();
}
]]></value>
</list>
<list key="MainTest-methods">
<value><![CDATA[
@Test
void testMicroprofileMetrics() {
String message = target.path("simple-greet/Joe")
.request()
.get(String.class);

assertThat(message, is("Hello Joe"));
Counter counter = registry.counter("personalizedGets");
double before = counter.getCount();

message = target.path("simple-greet/Eric")
.request()
.get(String.class);

assertThat(message, is("Hello Eric"));
double after = counter.getCount();
assertEquals(1d, after - before, "Difference in personalized greeting counter between successive calls");
}]]></value>
</list>
<list key="MainTest-static-fields">
<value><![CDATA[
@Inject
private MetricRegistry registry;]]></value>
</list>
<list key="module-requires">
<value>io.helidon.microprofile.metrics</value>
</list>
</model>
</output>
</option>
description="Expose metrics using the MicroProfile API"/>
<option value="micrometer"
name="Micrometer"
description="Expose metrics using the Micrometer API">
<output>
<model>
<list key="dependencies">
<map>
<value key="groupId">io.helidon.webserver.observe</value>
<value key="artifactId">helidon-webserver-observe-metrics</value>
</map>
<map>
<value key="groupId">io.helidon.metrics</value>
<value key="artifactId">helidon-metrics-system-meters</value>
<value key="scope">runtime</value>
</map>
<map>
<value key="groupId">org.eclipse.microprofile.metrics</value>
<value key="artifactId">microprofile-metrics-api</value>
</map>
</list>
<list key="SimpleGreetService-imports">
<value>org.eclipse.microprofile.metrics.annotation.Counted</value>
<value>org.eclipse.microprofile.metrics.annotation.Timed</value>
<value>jakarta.ws.rs.PathParam</value>
</list>
<list key="SimpleGreetResource-static-fields">
<value><![CDATA[
private static final String PERSONALIZED_GETS_COUNTER_NAME = "personalizedGets";
private static final String PERSONALIZED_GETS_COUNTER_DESCRIPTION = "Counts personalized GET operations";
private static final String GETS_TIMER_NAME = "allGets";
private static final String GETS_TIMER_DESCRIPTION = "Tracks all GET operations";]]></value>
</list>
<list key="SimpleGreetService-methods">
<value><![CDATA[
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Counted(name = PERSONALIZED_GETS_COUNTER_NAME, description = PERSONALIZED_GETS_COUNTER_DESCRIPTION, absolute = true)
@Timed(name = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, absolute = true)
public String getMessage(@PathParam("name") String name) {
return String.format("Hello %s", name);
}]]></value>
</list>
<list key="MainTest-java-imports">
<value>org.eclipse.microprofile.metrics.Counter</value>
<value>org.eclipse.microprofile.metrics.MetricRegistry</value>
</list>
<list key="MainTest-helidon-imports">
<value>io.helidon.metrics.api.MetricsFactory</value>
</list>
<list key="MainTest-other-imports">
<value>org.junit.jupiter.api.AfterAll</value>
</list>
<list key="MainTest-static-fields">
<value><![CDATA[
@Inject
private MetricRegistry registry;]]></value>
</list>
<list key="MainTest-methods" order="999">
<value><![CDATA[
@AfterAll
static void clear() {
MetricsFactory.closeAll();
}
]]></value>
</list>
<list key="MainTest-methods">
<value><![CDATA[
@Test
void testMicrometerMetrics() {
String message = target.path("simple-greet/Joe")
.request()
.get(String.class);

assertThat(message, is("Hello Joe"));
Counter counter = registry.counter("personalizedGets");
double before = counter.getCount();

message = target.path("simple-greet/Eric")
.request()
.get(String.class);

assertThat(message, is("Hello Eric"));
double after = counter.getCount();
assertEquals(1d, after - before, "Difference in personalized greeting counter between successive calls");
}]]></value>
</list>
<list key="MainTest-static-imports">
<value>static org.junit.jupiter.api.Assertions.assertEquals</value>
</list>
<list key="module-requires">
<value>io.helidon.microprofile.metrics</value>
</list>
</model>
</output>
</option>
description="Expose metrics using the Micrometer API"/>
</enum>
<boolean id="builtin"
name="Built-in Metrics"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class SimpleGreetResource {
/**
* Return a worldly greeting message.
*
* @return {@link JsonObject}
* @return {@link String}
*/
@GET
@Produces(MediaType.TEXT_PLAIN)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

Copyright (c) 2024 Oracle and/or its affiliates.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License 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.

-->
<archetype-script xmlns="https://helidon.io/archetype/2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://helidon.io/archetype/2.0 https://helidon.io/xsd/archetype-2.0.xsd">

<methods>
<method name="metric-micrometer-json">
<output if="${metrics.provider} == 'micrometer'">
<model if="${media} contains 'json'">
<list key="SimpleGreetService-methods">
<value><![CDATA[
@Path("/{name}")
@GET
@Produces(MediaType.APPLICATION_JSON)
@Counted(name = PERSONALIZED_GETS_COUNTER_NAME, description = PERSONALIZED_GETS_COUNTER_DESCRIPTION, absolute = true)
@Timed(name = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, absolute = true)
public Message getMessage(@PathParam("name") String name) {
String message = String.format("Hello %s", name);
return new Message(message);
}]]>
</value>
</list>
<list key="MainTest-methods">
<value><![CDATA[
@Test
void testMicroprofileMetrics() {
Message message = target.path("simple-greet/Joe")
.request()
.get(Message.class);

assertThat(message.getMessage(), is("Hello Joe"));
Counter counter = registry.counter("personalizedGets");
double before = counter.getCount();

message = target.path("simple-greet/Eric")
.request()
.get(Message.class);

assertThat(message.getMessage(), is("Hello Eric"));
double after = counter.getCount();
assertEquals(1d, after - before, "Difference in personalized greeting counter between successive calls");
}]]>
</value>
</list>
</model>
</output>
</method>
<method name="metric-micrometer">
<output if="${metrics.provider} == 'micrometer'">
<model if="!(${media} contains 'json')">
<list key="SimpleGreetService-methods">
<value><![CDATA[
@Path("/{name}")
@GET
@Counted(name = PERSONALIZED_GETS_COUNTER_NAME, description = PERSONALIZED_GETS_COUNTER_DESCRIPTION, absolute = true)
@Timed(name = GETS_TIMER_NAME, description = GETS_TIMER_DESCRIPTION, absolute = true)
public String getMessage(@PathParam("name") String name) {
return String.format("Hello %s", name);
}]]>
</value>
</list>
<list key="MainTest-methods">
<value><![CDATA[
@Test
void testMicrometerMetrics() {
String message = target.path("simple-greet/Joe")
.request()
.get(String.class);

assertThat(message, is("Hello Joe"));
Counter counter = registry.counter("personalizedGets");
double before = counter.getCount();

message = target.path("simple-greet/Eric")
.request()
.get(String.class);

assertThat(message, is("Hello Eric"));
double after = counter.getCount();
assertEquals(1d, after - before, "Difference in personalized greeting counter between successive calls");
}]]>
</value>
</list>
</model>
</output>
</method>
</methods>
<call method="metric-micrometer-json"/>
<call method="metric-micrometer"/>
<output if="${metrics.provider} == 'micrometer'">
<model>
<list key="dependencies">
<map>
<value key="groupId">io.helidon.microprofile.metrics</value>
<value key="artifactId">helidon-microprofile-metrics</value>
</map>
<map>
<value key="groupId">io.helidon.webserver.observe</value>
<value key="artifactId">helidon-webserver-observe-metrics</value>
</map>
<map>
<value key="groupId">io.helidon.metrics</value>
<value key="artifactId">helidon-metrics-system-meters</value>
<value key="scope">runtime</value>
</map>
<map>
<value key="groupId">org.eclipse.microprofile.metrics</value>
<value key="artifactId">microprofile-metrics-api</value>
</map>
</list>
<list key="SimpleGreetService-imports">
<value>org.eclipse.microprofile.metrics.annotation.Counted</value>
<value>org.eclipse.microprofile.metrics.annotation.Timed</value>
<value>jakarta.ws.rs.PathParam</value>
</list>
<list key="SimpleGreetResource-static-fields">
<value><![CDATA[
private static final String PERSONALIZED_GETS_COUNTER_NAME = "personalizedGets";
private static final String PERSONALIZED_GETS_COUNTER_DESCRIPTION = "Counts personalized GET operations";
private static final String GETS_TIMER_NAME = "allGets";
private static final String GETS_TIMER_DESCRIPTION = "Tracks all GET operations";]]></value>
</list>

<list key="MainTest-java-imports">
<value>org.eclipse.microprofile.metrics.Counter</value>
<value>org.eclipse.microprofile.metrics.MetricRegistry</value>
</list>
<list key="MainTest-helidon-imports">
<value>io.helidon.metrics.api.MetricsFactory</value>
</list>
<list key="MainTest-other-imports">
<value>org.junit.jupiter.api.AfterAll</value>
</list>
<list key="MainTest-static-fields">
<value><![CDATA[
@Inject
private MetricRegistry registry;]]></value>
</list>
<list key="MainTest-methods" order="999">
<value><![CDATA[
@AfterAll
static void clear() {
MetricsFactory.closeAll();
}
]]></value>
</list>
<list key="MainTest-static-imports">
<value>static org.junit.jupiter.api.Assertions.assertEquals</value>
</list>
<list key="module-requires">
<value>io.helidon.microprofile.metrics</value>
</list>
</model>
</output>
</archetype-script>
Loading