-
Notifications
You must be signed in to change notification settings - Fork 992
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve
JettyConnectionMetrics
connection type detection (#4325)
The `HttpConnection` class was relocated to a different package in Jetty 12 which was causing a `NoClassDefFoundError` when doing an instanceof check on it. The goal of it is to determine whether the connection instrumented is a server connection or a client connection. However, it specifically checks against `HttpConnection` and assumes that all server connections will be an instanceof `HttpConnection` and any not an instanceof it are client connections. This is brittle because Jetty supports more implementations of `Connection` on the server side than `HttpConnection` and there could in theory be an arbitrary implementation where we do not know whether it is a server or client connection (it could also be neither). This instead checks whether the package name contains `server` or `client` or neither. This is admittedly also brittle, but given the known implementations of `Connection` provided by the Jetty project, this pattern generally seems to hold, and it is at least more honest using `UNKNOWN` when our heuristic fails. It also avoids the `NoClassDefFoundError` when using `JettyConnectionMetrics` with Jetty 12. Adds a jetty12 module to the samples to run the `JettyConnectionMetricsTest` against Jetty 12. Resolves gh-4324
- Loading branch information
Showing
9 changed files
with
186 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
// skip this module when building with jdk <17 | ||
if (!javaLanguageVersion.canCompileOrRun(17)) { | ||
project.tasks.configureEach { task -> task.enabled = false } | ||
} | ||
|
||
dependencies { | ||
implementation project(":micrometer-core") | ||
|
||
testImplementation libs.jetty12Server | ||
testImplementation libs.jetty12Client | ||
testImplementation libs.httpcomponents.client | ||
testImplementation libs.junitJupiter | ||
testImplementation libs.assertj | ||
} | ||
|
||
compileJava { | ||
sourceCompatibility = JavaVersion.VERSION_17 | ||
targetCompatibility = JavaVersion.VERSION_17 | ||
options.release = 17 | ||
} |
140 changes: 140 additions & 0 deletions
140
...mples-jetty12/src/test/java/io.micrometer.samples.jetty12/JettyConnectionMetricsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
/* | ||
* Copyright 2023 VMware, Inc. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package io.micrometer.samples.jetty12; | ||
|
||
import io.micrometer.core.instrument.MockClock; | ||
import io.micrometer.core.instrument.binder.jetty.JettyConnectionMetrics; | ||
import io.micrometer.core.instrument.simple.SimpleConfig; | ||
import io.micrometer.core.instrument.simple.SimpleMeterRegistry; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.StringEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.eclipse.jetty.client.HttpClient; | ||
import org.eclipse.jetty.client.Request; | ||
import org.eclipse.jetty.client.StringRequestContent; | ||
import org.eclipse.jetty.server.Connector; | ||
import org.eclipse.jetty.server.Server; | ||
import org.eclipse.jetty.server.ServerConnector; | ||
import org.eclipse.jetty.util.component.LifeCycle; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.concurrent.CountDownLatch; | ||
|
||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
class JettyConnectionMetricsTest { | ||
|
||
private SimpleMeterRegistry registry = new SimpleMeterRegistry(SimpleConfig.DEFAULT, new MockClock()); | ||
|
||
private Server server = new Server(0); | ||
|
||
private ServerConnector connector = new ServerConnector(server); | ||
|
||
private CloseableHttpClient client = HttpClients.createDefault(); | ||
|
||
void setup() throws Exception { | ||
connector.addBean(new JettyConnectionMetrics(registry)); | ||
server.setConnectors(new Connector[] { connector }); | ||
server.start(); | ||
} | ||
|
||
@AfterEach | ||
void teardown() throws Exception { | ||
if (server.isRunning()) { | ||
server.stop(); | ||
} | ||
} | ||
|
||
@Test | ||
void contributesServerConnectorMetrics() throws Exception { | ||
setup(); | ||
HttpPost post = new HttpPost("http://localhost:" + connector.getLocalPort()); | ||
post.setEntity(new StringEntity("123456")); | ||
|
||
try (CloseableHttpResponse ignored = client.execute(post)) { | ||
try (CloseableHttpResponse ignored2 = client.execute(post)) { | ||
assertThat(registry.get("jetty.connections.current").gauge().value()).isEqualTo(2.0); | ||
assertThat(registry.get("jetty.connections.max").gauge().value()).isEqualTo(2.0); | ||
} | ||
} | ||
|
||
CountDownLatch latch = new CountDownLatch(1); | ||
connector.addEventListener(new LifeCycle.Listener() { | ||
@Override | ||
public void lifeCycleStopped(LifeCycle event) { | ||
latch.countDown(); | ||
} | ||
}); | ||
// Convenient way to get Jetty to flush its connections, which is required to | ||
// update the sent/received bytes metrics | ||
server.stop(); | ||
|
||
assertThat(latch.await(10, SECONDS)).isTrue(); | ||
assertThat(registry.get("jetty.connections.max").gauge().value()).isEqualTo(2.0); | ||
assertThat(registry.get("jetty.connections.request").tag("type", "server").timer().count()).isEqualTo(2); | ||
assertThat(registry.get("jetty.connections.bytes.in").summary().totalAmount()).isGreaterThan(1); | ||
} | ||
|
||
@Test | ||
void contributesClientConnectorMetrics() throws Exception { | ||
setup(); | ||
HttpClient httpClient = new HttpClient(); | ||
httpClient.setFollowRedirects(false); | ||
httpClient.addBean(new JettyConnectionMetrics(registry)); | ||
|
||
CountDownLatch latch = new CountDownLatch(1); | ||
httpClient.addEventListener(new LifeCycle.Listener() { | ||
@Override | ||
public void lifeCycleStopped(LifeCycle event) { | ||
latch.countDown(); | ||
} | ||
}); | ||
|
||
httpClient.start(); | ||
|
||
Request post = httpClient.POST("http://localhost:" + connector.getLocalPort()); | ||
post.body(new StringRequestContent("123456")); | ||
post.send(); | ||
httpClient.stop(); | ||
|
||
assertThat(latch.await(10, SECONDS)).isTrue(); | ||
assertThat(registry.get("jetty.connections.max").gauge().value()).isEqualTo(1.0); | ||
assertThat(registry.get("jetty.connections.request").tag("type", "client").timer().count()).isEqualTo(1); | ||
assertThat(registry.get("jetty.connections.bytes.out").summary().totalAmount()).isGreaterThan(1); | ||
} | ||
|
||
@Test | ||
void passingConnectorAddsConnectorNameTag() { | ||
new JettyConnectionMetrics(registry, connector); | ||
|
||
assertThat(registry.get("jetty.connections.messages.in").counter().getId().getTag("connector.name")) | ||
.isEqualTo("unnamed"); | ||
} | ||
|
||
@Test | ||
void namedConnectorsGetTaggedWithName() { | ||
connector.setName("super-fast-connector"); | ||
new JettyConnectionMetrics(registry, connector); | ||
|
||
assertThat(registry.get("jetty.connections.messages.in").counter().getId().getTag("connector.name")) | ||
.isEqualTo("super-fast-connector"); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters