Skip to content

Commit

Permalink
Micrometer example improvement
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <maxim.nesen@oracle.com>
  • Loading branch information
senivam committed Oct 18, 2023
1 parent bc912c0 commit 0233464
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class App {

private static final URI BASE_URI = URI.create("http://localhost:8080/micro/");
public static final String ROOT_PATH = "measure";
public static final String WEB_PATH = "/micro/metrics/";
public static final String WEB_PATH = "/micro/";

public static void main(String[] args) {
try {
Expand All @@ -42,7 +42,7 @@ public void run() {
System.out.println(String.format("Application started.\nTry out %s%s\n"
+ "And after that go to the %s%s\n"
+ "Stop the application using CTRL+C",
BASE_URI, ROOT_PATH + "/timed", BASE_URI, "metrics"));
BASE_URI, ROOT_PATH + "/timed", BASE_URI, "init"));
Thread.currentThread().join();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class MeasuredTimedResource {

public static final String MESSAGE = "<html><body>Requests to this method are measured. "
+ "Use <a href=\"" + WEB_PATH + "\">/metrics</a> to see more</body></html";
+ "Use <a href=\"" + WEB_PATH + "init\">/init</a> to see more</body></html";
public static final String TIMER_NAME = "http.timers";
public static final String TIMER_DESCRIPTION = "resource measurement timer";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,62 +20,14 @@

import static org.glassfish.jersey.examples.micrometer.App.WEB_PATH;

@Path("metrics")
@Path("init")
public class MetricsResource {

private MetricsStore store;


public MetricsResource(MetricsStore store) {
this.store = store;
}

@GET
@Produces("text/html")
public String getMeters() {
return "<html><body>Static meters are initialized, try <a href=\""
+ WEB_PATH + "extendedMeters\">extendedMeters</a></body></html>";
}


@GET
@Produces("text/plain")
@Path("extendedMeters")
public String getExtendedMeters() {
final StringBuffer result = new StringBuffer();
try {
result.append("Listing available meters: ");
for (final Meter meter : store.getRegistry().getMeters()) {
result.append(meter.getId().getName());
result.append(";\n\r ");
}
} catch (Exception ex) {
result.append("Looks like there are no proper metrics.");
result.append("\n\r");
result.append("Please visit /measure/timed and /measure/counted first ");
result.append(ex);
}
if (store.getRegistry().getMeters().size() > 0) {
try {
final Timer timer = store.getRegistry().get("http.shared.metrics")
.tags("method", "GET", "status", "200", "exception", "None", "outcome", "SUCCESS")
.timer();

result.append(String.format("Overall requests counts: %d, total time (millis): %f \n\r",
timer.count(), timer.totalTime(TimeUnit.MILLISECONDS)));

final Timer annotatedTimer = store.getRegistry().timer(MeasuredTimedResource.TIMER_NAME,
"method", "GET", "status", "200", "exception", "None",
"outcome", "SUCCESS", "uri", "/micro/measure/timed");

result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f \n\r",
annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS)));

} catch (Exception ex) {
result.append("Exception occurred, see log for details...");
result.append(ex);
}
}
return result.toString();
+ WEB_PATH + "summary\">summary</a>. If you wan more measurements just refresh this page several times."
+ "</body></html>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ public class MetricsResourceConfig extends ResourceConfig {
public MetricsResourceConfig() {
register(store.getMetricsApplicationEventListener());
register(MeasuredTimedResource.class);
register(new MetricsResource(store));
register(MetricsResource.class);
register(new SummaryResource(store));
}

public MetricsStore getStore() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) 2023 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/

package org.glassfish.jersey.examples.micrometer;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Timer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.concurrent.TimeUnit;

@Path("summary")
public class SummaryResource {

private final MetricsStore store;

public SummaryResource(MetricsStore store) {
this.store = store;
}

@GET
@Produces("text/html")
public String getExtendedMeters() {
final StringBuffer result = new StringBuffer();
try {
result.append("<html><body>"
+ "Listing available meters<br/>Many occurrences of the same name means that there are more meters"
+ " which could be used with different tags,"
+ " but this is actually a challenge to handle all available metrics :<br/> ");
for (final Meter meter : store.getRegistry().getMeters()) {
result.append(meter.getId().getName());
result.append(";<br/>\n\r ");
}
} catch (Exception ex) {
result.append("Looks like there are no proper metrics.<br/>");
result.append("\n\r");
result.append("Please visit /measure/timed first <br/>");
result.append(ex);
}
if (store.getRegistry().getMeters().size() > 0) {
try {
final Timer timer = store.getRegistry().get("http.shared.metrics")
.tags("method", "GET", "status", "200", "exception", "None", "outcome", "SUCCESS", "uri", "/micro/init")
.timer();

result.append(String.format("Counts to the init page: %d, time spent on requests to the init "
+ "page (millis): %f <br/>\n\r",
timer.count(), timer.totalTime(TimeUnit.MILLISECONDS)));

final Timer annotatedTimer = store.getRegistry().timer(MeasuredTimedResource.TIMER_NAME,
"method", "GET", "status", "200", "exception", "None",
"outcome", "SUCCESS", "uri", "/micro/measure/timed");

result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f <br/>\n\r",
annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS)));

} catch (Exception ex) {
result.append("Exception occurred, more info is in console...<br/>");
result.append(ex);
}
}
return result.append("</body></html>").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ void meterResourceTest() throws InterruptedException {
final String response = target("/measure/timed").request().get(String.class);
assertEquals(response, MESSAGE);
for (int i = 0; i < REQUESTS_COUNT; i++) {
target("/metrics").request().get(String.class);
target("/init").request().get(String.class);
}
// Jersey metrics are recorded asynchronously to the request completing
Thread.sleep(10);
Timer timer = resourceConfig.getStore().getRegistry()
.get(MetricsStore.REGISTRY_NAME)
.tags("method", "GET", "uri", "/metrics", "status", "200", "exception", "None", "outcome", "SUCCESS")
.tags("method", "GET", "uri", "/init", "status", "200", "exception", "None", "outcome", "SUCCESS")
.timer();
assertEquals(REQUESTS_COUNT, timer.count());
assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS));
Expand Down

0 comments on commit 0233464

Please sign in to comment.