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

Added jersey-micrometer module #5391

Merged
merged 3 commits into from
Sep 22, 2023
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
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
<artifactId>jersey-entity-filtering</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-micrometer</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-metainf-services</artifactId>
Expand Down
57 changes: 57 additions & 0 deletions examples/micrometer/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[//]: # " 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 Public License v. 2.0, which is available at "
[//]: # " http://www.eclipse.org/legal/epl-2.0. "
[//]: # " "
[//]: # " This Source Code may also be made available under the following Secondary "
[//]: # " Licenses when the conditions for such availability set forth in the "
[//]: # " Eclipse Public License v. 2.0 are satisfied: GNU General Public License, "
[//]: # " version 2 with the GNU Classpath Exception, which is available at "
[//]: # " https://www.gnu.org/software/classpath/license.html. "
[//]: # " "
[//]: # " SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 "

jersey-micrometer-webapp
==========================================================

This example demonstrates basics of Micrometer Jersey integration

Contents
--------

The mapping of the URI path space is presented in the following table:

URI path | Resource class | HTTP methods
------------------------------------------ | ------------------------- | --------------
**_/micro/meter_** | JerseyResource | GET
**_/micro/metrics_** | JerseyResource | GET
**_/micro/metrics/metrics_** | JerseyResource | GET

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

```javascript
--- (micro/meter)
Hello World!
---- (micro/metrics)
Listing available meters: http.shared.metrics;
---- (micro/metric/metrics)
Overall requests counts: 9, total time (millis): 35.799483
```


Running the Example
-------------------

Run the example using [Grizzly](https://javaee.github.io/grizzly/) container as follows:

> mvn clean compile exec:java

- <http://localhost:8080/micro/meter>
- after few request to the main page go to the url
- - <http://localhost:8080/micro/metrics>
- and see the list of available meters
- then go to the
- - <http://localhost:8080/micro/metrics/metrics>
- and see statistics for the micro/meter page
98 changes: 98 additions & 0 deletions examples/micrometer/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--

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 Public License v. 2.0, which is available at
http://www.eclipse.org/legal/epl-2.0.

This Source Code may also be made available under the following Secondary
Licenses when the conditions for such availability set forth in the
Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
version 2 with the GNU Classpath Exception, which is available at
https://www.gnu.org/software/classpath/license.html.

SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0

-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.glassfish.jersey.examples</groupId>
<artifactId>project</artifactId>
<version>2.41-SNAPSHOT</version>
</parent>

<artifactId>jersey-micrometer-webapp</artifactId>
<packaging>jar</packaging>
<name>jersey-micrometer-example-webapp</name>

<description>Micrometer/Jersey metrics basic example</description>

<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-grizzly2-http</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.ext</groupId>
<artifactId>jersey-micrometer</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework</groupId>
<artifactId>jersey-test-framework-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-grizzly2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<configuration>
<mainClass>org.glassfish.jersey.examples.micrometer.App</mainClass>
</configuration>
</plugin>
</plugins>
</build>

<profiles>
<profile>
<id>pre-release</id>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>xml-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
</plugin>
</plugins>
</build>
</profile>
</profiles>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* 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 Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.examples.micrometer;

import java.io.IOException;
import java.net.URI;
import java.util.logging.Level;
import java.util.logging.Logger;

import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.micrometer.server.DefaultJerseyTagsProvider;
import org.glassfish.jersey.micrometer.server.MetricsApplicationEventListener;
import org.glassfish.jersey.server.ResourceConfig;

import org.glassfish.grizzly.http.server.HttpServer;

public class App {

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

public static void main(String[] args) {
try {
System.out.println("Micrometer/ Jersey Basic Example App");

final MeterRegistry registry = new SimpleMeterRegistry();

final ResourceConfig resourceConfig = new ResourceConfig(MicrometerResource.class)
.register(new MetricsApplicationEventListener(registry, new DefaultJerseyTagsProvider(),
"http.shared.metrics", true))
.register(new MetricsResource(registry));
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, resourceConfig, false);
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
server.shutdownNow();
}
}));
server.start();

System.out.println(String.format("Application started.\nTry out %s%s\n"
+ "After several requests go to %s%s\nAnd after that go to the %s%s\n"
+ "Stop the application using CTRL+C",
BASE_URI, ROOT_PATH, BASE_URI, "metrics", BASE_URI, "metrics/metrics"));
Thread.currentThread().join();
} catch (IOException | InterruptedException ex) {
Logger.getLogger(App.class.getName()).log(Level.SEVERE, null, ex);
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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 Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.examples.micrometer;

import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.MeterRegistry;
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("metrics")
public class MetricsResource {

private final MeterRegistry registry;

public MetricsResource(MeterRegistry registry) {
this.registry = registry;
}

@GET
@Produces("text/plain")
public String getMeters() {
final StringBuffer result = new StringBuffer();
try {
result.append("Listing available meters: ");
for (final Meter meter : registry.getMeters()) {
result.append(meter.getId().getName());
result.append("; ");
}
} catch (Exception ex) {
System.out.println(ex);
result.append("Exception occured, see log for details...");
result.append(ex.toString());
}
return result.toString();
}
@GET
@Path("metrics")
@Produces("text/plain")
public String getMetrics() {
final StringBuffer result = new StringBuffer();
try {
final Timer timer = registry.get("http.shared.metrics")
.tags("method", "GET", "uri", "/micro/meter", "status", "200", "exception", "None", "outcome", "SUCCESS")
.timer();
result.append(String.format("Overall requests counts: %d, total time (millis): %f",
timer.count(), timer.totalTime(TimeUnit.MILLISECONDS)));
} catch (Exception ex) {
System.out.println(ex);
result.append("Exception occured, see log for details...");
result.append(ex.toString());
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* 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 Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.jersey.examples.micrometer;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

@Path("meter")
public class MicrometerResource {
public static final String CLICHED_MESSAGE = "Hello World!";

@GET
@Produces("text/plain")
public String getHello() {
return CLICHED_MESSAGE;
}

}
Loading