Skip to content

Commit

Permalink
Typos fix
Browse files Browse the repository at this point in the history
Signed-off-by: Maxim Nesen <[email protected]>
  • Loading branch information
senivam committed Oct 18, 2023
1 parent 5af43c4 commit 2e13720
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 56 deletions.
12 changes: 6 additions & 6 deletions examples/micrometer/README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ The mapping of the URI path space is presented in the following table:

URI path | Resource class | HTTP methods
------------------------------------------ | ------------------------- | --------------
**_/micro/measure/timed_** | MeasuredTimedResource | GET
**_/micro/init_** | MetricsResource | GET
**_/micro/timed_** | MeasuredTimedResource | GET
**_/micro/metrics_** | MetricsResource | GET
**_/micro/summary_** | SummaryResource | GET

Sample Response
---------------

```html
--- (micro/measure/timed)
--- (micro/timed)
Requests to this method are measured. Use /init to see more
---- (micro/init)
---- (micro/metrics)
Static meters are initialized, try summary. If you want more measurements just refresh this page several times.
---- (micro/summary)
Listing available meters
Expand All @@ -47,9 +47,9 @@ Run the example using [Grizzly](https://javaee.github.io/grizzly/) container as

> mvn clean compile exec:java
- <http://localhost:8080/micro/init>
- <http://localhost:8080/micro/metrics>
- after few request to the main page go to the url
- <http://localhost:8080/micro/measure/timed>
- <http://localhost:8080/micro/timed>
- and see the responses from available resource pages
- then go to the
- <http://localhost:8080/micro/summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
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/";

public static void main(String[] args) {
Expand All @@ -42,7 +41,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, "init"));
BASE_URI, "timed", BASE_URI, "metrics"));
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 @@ -10,23 +10,19 @@

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;

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

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

@GET
@Produces("text/html")
public String getMeters() {
return "<html><body>Static meters are initialized, try <a href=\""
return "<html><body>Gaining measurements for the summary page, try <a href=\""
+ WEB_PATH + "summary\">summary</a>. If you want more measurements just refresh this page several times."
+ "</body></html>";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,27 @@

package org.glassfish.jersey.examples.micrometer;

import org.glassfish.jersey.internal.inject.AbstractBinder;
import org.glassfish.jersey.server.ResourceConfig;

import javax.ws.rs.ApplicationPath;

@ApplicationPath("/*")
@ApplicationPath("/")
public class MetricsResourceConfig extends ResourceConfig {

private final MetricsStore store = new MetricsStore();

public MetricsResourceConfig() {
register(new AbstractBinder() {
@Override
protected void configure() {
bind(store).to(MetricsStore.class);
}
});
register(store.getMetricsApplicationEventListener());
register(MeasuredTimedResource.class);
register(TimedResource.class);
register(MetricsResource.class);
register(new SummaryResource(store));
register(SummaryResource.class);
}

public MetricsStore getStore() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
package org.glassfish.jersey.examples.micrometer;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.binder.jersey.server.DefaultJerseyTagsProvider;
import io.micrometer.core.instrument.binder.jersey.server.MetricsApplicationEventListener;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProvider;
import org.glassfish.jersey.micrometer.server.MetricsApplicationEventListener;

public class MetricsStore {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,58 +16,63 @@
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import java.util.concurrent.TimeUnit;

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

@Path("summary")
public class SummaryResource {

private final MetricsStore store;

public SummaryResource(MetricsStore store) {
this.store = store;
}
@Context
private MetricsStore 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"
+ "Listing available meters<br/><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/> ");
+ " but this is actually a challenge to handle all available metrics :<br/><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);
result.append("Try clicking links below to gain more metrics.<br/>");
}
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("<br/>\n\r ");
result.append("<br/>\n\r ");
try {
final Timer timer = store.getRegistry().get(REGISTRY_NAME)
.tags("method", "GET", "status", "200", "exception", "None",
"outcome", "SUCCESS", "uri", "/micro/metrics")
.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)));
result.append(
String.format("Counts to the page with standard measurements: %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");
final Timer annotatedTimer = store.getRegistry().timer(TimedResource.TIMER_NAME,
"method", "GET", "status", "200", "exception", "None",
"outcome", "SUCCESS", "uri", "/micro/timed");

result.append(String.format("Requests to 'measure/timed' counts: %d, total time (millis): %f <br/>\n\r",
annotatedTimer.count(), annotatedTimer.totalTime(TimeUnit.MILLISECONDS)));
result.append(
String.format("Counts to the page with annotated measurements: %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);
}
} catch (Exception ex) {
result.append(String.format("Counts to the init page: %d, total time (millis): %d <br/>\n\r",
0, 0));
result.append("Try clicking links below to gain more metrics.<br/>");
}
result.append("<br/><br/>Available pages for measurements: <a href=\""
+ WEB_PATH + "metrics\">measure requests in the standard way</a> &nbsp;, <a href=\""
+ WEB_PATH + "timed\">measure requests in the annotated way</a>");
return result.append("</body></html>").toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@

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

@Path("measure")
public class MeasuredTimedResource {
@Path("timed")
public class TimedResource {

public static final String MESSAGE = "<html><body>Requests to this method are measured. "
+ "Use <a href=\"" + WEB_PATH + "init\">/init</a> to see more</body></html";
public static final String MESSAGE = "<html><body>Gaining measures in the annotated way. "
+ "<br/>Take a look at <a href=\"" + WEB_PATH + "metrics\">the standard way of measurements</a><br/> "
+ "Or just go to <a href=\"" + WEB_PATH + "summary\">summary</a> to check what you've got</body></html>";
public static final String TIMER_NAME = "http.timers";
public static final String TIMER_DESCRIPTION = "resource measurement timer";

@GET
@Produces("text/html")
@Timed(value = TIMER_NAME, description = TIMER_DESCRIPTION, histogram = true)
@Path("timed")
public String getTimedMessage() {
return MESSAGE;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import javax.ws.rs.core.Application;
import java.util.concurrent.TimeUnit;

import static org.glassfish.jersey.examples.micrometer.MeasuredTimedResource.MESSAGE;
import static org.glassfish.jersey.examples.micrometer.TimedResource.MESSAGE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

Expand All @@ -36,16 +36,16 @@ protected Application configure() {

@Test
void meterResourceTest() throws InterruptedException {
final String response = target("/measure/timed").request().get(String.class);
final String response = target("/timed").request().get(String.class);
assertEquals(response, MESSAGE);
for (int i = 0; i < REQUESTS_COUNT; i++) {
target("/init").request().get(String.class);
target("/metrics").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", "/init", "status", "200", "exception", "None", "outcome", "SUCCESS")
.tags("method", "GET", "uri", "/metrics", "status", "200", "exception", "None", "outcome", "SUCCESS")
.timer();
assertEquals(REQUESTS_COUNT, timer.count());
assertNotNull(timer.totalTime(TimeUnit.NANOSECONDS));
Expand Down

0 comments on commit 2e13720

Please sign in to comment.