Skip to content

Commit 9628ee9

Browse files
Added RestEasy server and client examples
modified the jakarta imports to have both jakarta and javax imports on the classpath. Added filters for client and server side that use jakarta imports and added RestEasy tests for client and server
1 parent 6b770e7 commit 9628ee9

13 files changed

+490
-55
lines changed

dependencies.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ def VERSIONS = [
7373
'org.hsqldb:hsqldb:2.5.+', // 2.6.0 requires JDK 11.
7474
'org.jooq:jooq:3.14.+',
7575
'org.jsr107.ri:cache-ri-impl:1.0.0',
76+
'org.jboss.resteasy:resteasy-client:6.2.4.Final',
77+
'org.jboss.resteasy:resteasy-netty4:6.2.4.Final',
78+
'javax.validation:validation-api:1.0.0.GA',
7679
'org.latencyutils:LatencyUtils:latest.release',
7780
'org.mockito:mockito-core:5.+', // 5.x requires JDK 11.
7881
'org.mockito:mockito-inline:5.+',

micrometer-core/build.gradle

+5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ jar {
6767
dependencies {
6868
api project(":micrometer-commons")
6969
api project(":micrometer-observation")
70+
optionalApi 'javax.ws.rs:javax.ws.rs-api:2.+' // javax
71+
optionalApi 'jakarta.ws.rs:jakarta.ws.rs-api:3.+' // jakarta
7072

7173
java11Api project(":micrometer-commons")
7274
java11Api project(":micrometer-observation")
@@ -200,6 +202,9 @@ dependencies {
200202
// gRPC
201203
testImplementation("io.grpc:grpc-core")
202204
testImplementation("io.grpc:grpc-testing-proto")
205+
testImplementation("javax.validation:validation-api")
206+
testImplementation("org.jboss.resteasy:resteasy-netty4")
207+
testImplementation("org.jboss.resteasy:resteasy-netty4")
203208
}
204209

205210
task shenandoahTest(type: Test) {

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/AbstractDefaultHttpClientRequestObservationConvention.java

+5-22
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
*/
1616
package io.micrometer.core.instrument.binder.http;
1717

18+
import java.net.URI;
19+
import java.util.regex.Pattern;
20+
1821
import io.micrometer.common.KeyValue;
1922
import io.micrometer.common.KeyValues;
2023
import io.micrometer.common.lang.Nullable;
@@ -23,9 +26,6 @@
2326
import io.micrometer.core.instrument.binder.http.HttpObservationDocumentation.CommonHighCardinalityKeys;
2427
import io.micrometer.core.instrument.binder.http.HttpObservationDocumentation.CommonLowCardinalityKeys;
2528

26-
import java.net.URI;
27-
import java.util.regex.Pattern;
28-
2929
class AbstractDefaultHttpClientRequestObservationConvention {
3030

3131
private static final Pattern PATTERN_BEFORE_PATH = Pattern.compile("^https?://[^/]+/");
@@ -36,10 +36,6 @@ class AbstractDefaultHttpClientRequestObservationConvention {
3636

3737
private static final KeyValue STATUS_CLIENT_ERROR = KeyValue.of(CommonLowCardinalityKeys.STATUS, "CLIENT_ERROR");
3838

39-
private static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(CommonLowCardinalityKeys.OUTCOME, "SUCCESS");
40-
41-
private static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(CommonLowCardinalityKeys.OUTCOME, "UNKNOWN");
42-
4339
private static final KeyValue CLIENT_NAME_NONE = KeyValue.of(ClientLowCardinalityKeys.CLIENT_NAME,
4440
KeyValue.NONE_VALUE);
4541

@@ -106,9 +102,9 @@ private KeyValue exception(@Nullable Throwable error) {
106102

107103
private KeyValue outcome(@Nullable Integer statusCode) {
108104
if (statusCode != null) {
109-
return HttpOutcome.forStatus(statusCode);
105+
return HttpOutcome.forStatus(false, statusCode);
110106
}
111-
return HTTP_OUTCOME_UNKNOWN;
107+
return HttpOutcome.forStatus(false, statusCode);
112108
}
113109

114110
protected KeyValues getHighCardinalityKeyValues(@Nullable URI uri, @Nullable String userAgent) {
@@ -131,17 +127,4 @@ private KeyValue userAgent(@Nullable String userAgent) {
131127
return USER_AGENT_NONE;
132128
}
133129

134-
static class HttpOutcome {
135-
136-
static KeyValue forStatus(int statusCode) {
137-
if (statusCode >= 200 && statusCode < 300) {
138-
return HTTP_OUTCOME_SUCCESS;
139-
}
140-
else {
141-
return HTTP_OUTCOME_UNKNOWN;
142-
}
143-
}
144-
145-
}
146-
147130
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/AbstractDefaultHttpServerRequestObservationConvention.java

+1-14
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private KeyValue exception(@Nullable Throwable error) {
129129

130130
private KeyValue outcome(@Nullable Integer status) {
131131
if (status != null) {
132-
return HttpOutcome.forStatus(status);
132+
return HttpOutcome.forStatus(true, status);
133133
}
134134
return HTTP_OUTCOME_UNKNOWN;
135135
}
@@ -141,17 +141,4 @@ private KeyValue httpUrl(@Nullable String requestUri) {
141141
return HTTP_URL_UNKNOWN;
142142
}
143143

144-
static class HttpOutcome {
145-
146-
static KeyValue forStatus(int statusCode) {
147-
if (statusCode >= 200 && statusCode < 300) {
148-
return HTTP_OUTCOME_SUCCESS;
149-
}
150-
else {
151-
return HTTP_OUTCOME_UNKNOWN;
152-
}
153-
}
154-
155-
}
156-
157144
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/DefaultHttpJakartaClientRequestObservationConvention.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
public class DefaultHttpJakartaClientRequestObservationConvention extends
3030
AbstractDefaultHttpClientRequestObservationConvention implements HttpJakartaClientRequestObservationConvention {
3131

32+
public static DefaultHttpJakartaClientRequestObservationConvention INSTANCE = new DefaultHttpJakartaClientRequestObservationConvention();
33+
3234
private static final String DEFAULT_NAME = "http.client.requests";
3335

3436
private final String name;
@@ -62,7 +64,7 @@ public String getContextualName(HttpJakartaClientRequestObservationContext conte
6264

6365
@Override
6466
public KeyValues getLowCardinalityKeyValues(HttpJakartaClientRequestObservationContext context) {
65-
URI uri = context.getCarrier() != null ? context.getCarrier().getUriInfo().getRequestUri() : null;
67+
URI uri = context.getCarrier() != null ? context.getCarrier().getUri() : null;
6668
Throwable throwable = context.getError();
6769
String methodName = context.getCarrier() != null ? context.getCarrier().getMethod() : null;
6870
Integer statusCode = context.getResponse() != null ? context.getResponse().getStatus() : null;
@@ -72,7 +74,7 @@ public KeyValues getLowCardinalityKeyValues(HttpJakartaClientRequestObservationC
7274

7375
@Override
7476
public KeyValues getHighCardinalityKeyValues(HttpJakartaClientRequestObservationContext context) {
75-
URI uri = context.getCarrier() != null ? context.getCarrier().getUriInfo().getRequestUri() : null;
77+
URI uri = context.getCarrier() != null ? context.getCarrier().getUri() : null;
7678
String userAgent = context.getCarrier() != null ? context.getCarrier().getHeaderString("User-Agent") : null;
7779
return getHighCardinalityKeyValues(uri, userAgent);
7880
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/DefaultHttpJakartaServerRequestObservationConvention.java

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
public class DefaultHttpJakartaServerRequestObservationConvention extends
2828
AbstractDefaultHttpServerRequestObservationConvention implements HttpJakartaServerRequestObservationConvention {
2929

30+
public static DefaultHttpJakartaServerRequestObservationConvention INSTANCE = new DefaultHttpJakartaServerRequestObservationConvention();
31+
3032
private static final String DEFAULT_NAME = "http.server.requests";
3133

3234
private final String name;
@@ -61,13 +63,15 @@ public KeyValues getLowCardinalityKeyValues(HttpJakartaServerRequestObservationC
6163
String method = context.getCarrier() != null ? context.getCarrier().getMethod() : null;
6264
Integer status = context.getResponse() != null ? context.getResponse().getStatus() : null;
6365
String pathPattern = context.getPathPattern();
64-
String requestUri = context.getCarrier() != null ? context.getCarrier().getRequestURI() : null;
66+
String requestUri = context.getCarrier() != null ? context.getCarrier().getUriInfo().getRequestUri().toString()
67+
: null;
6568
return getLowCardinalityKeyValues(context.getError(), method, status, pathPattern, requestUri);
6669
}
6770

6871
@Override
6972
public KeyValues getHighCardinalityKeyValues(HttpJakartaServerRequestObservationContext context) {
70-
String requestUri = context.getCarrier() != null ? context.getCarrier().getRequestURI() : null;
73+
String requestUri = context.getCarrier() != null ? context.getCarrier().getUriInfo().getRequestUri().toString()
74+
: null;
7175
return getHighCardinalityKeyValues(requestUri);
7276
}
7377

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/HttpJakartaClientRequestObservationContext.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2020 VMware, Inc.
2+
* Copyright 2023 VMware, Inc.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,10 +17,8 @@
1717

1818
import io.micrometer.common.lang.Nullable;
1919
import io.micrometer.observation.transport.RequestReplySenderContext;
20-
21-
import javax.ws.rs.container.ContainerRequestContext;
22-
import javax.ws.rs.container.ContainerRequestFilter;
23-
import javax.ws.rs.container.ContainerResponseContext;
20+
import jakarta.ws.rs.client.ClientRequestContext;
21+
import jakarta.ws.rs.client.ClientResponseContext;
2422

2523
/**
2624
* Context that holds information for metadata collection during the client HTTP exchanges
@@ -34,21 +32,21 @@
3432
* @since 1.12.0
3533
*/
3634
public class HttpJakartaClientRequestObservationContext
37-
extends RequestReplySenderContext<ContainerRequestContext, ContainerResponseContext> {
35+
extends RequestReplySenderContext<ClientRequestContext, ClientResponseContext> {
3836

3937
@Nullable
4038
private String uriTemplate;
4139

4240
/**
4341
* Create an observation context for HTTP client observations.
44-
* @param containerRequestContext the context for a {@link ContainerRequestFilter}
42+
* @param containerRequestContext the context for a {@link ClientRequestContext}
4543
*/
46-
public HttpJakartaClientRequestObservationContext(ContainerRequestContext containerRequestContext) {
44+
public HttpJakartaClientRequestObservationContext(ClientRequestContext containerRequestContext) {
4745
super(HttpJakartaClientRequestObservationContext::setRequestHeader);
4846
this.setCarrier(containerRequestContext);
4947
}
5048

51-
private static void setRequestHeader(@Nullable ContainerRequestContext context, String name, String value) {
49+
private static void setRequestHeader(@Nullable ClientRequestContext context, String name, String value) {
5250
if (context != null) {
5351
context.getHeaders().add(name, value);
5452
}

micrometer-core/src/main/java/io/micrometer/core/instrument/binder/http/HttpJakartaServerRequestObservationContext.java

+5-6
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
import io.micrometer.common.lang.Nullable;
1919
import io.micrometer.observation.transport.RequestReplyReceiverContext;
20-
import jakarta.servlet.http.HttpServletRequest;
21-
import jakarta.servlet.http.HttpServletResponse;
20+
import jakarta.ws.rs.container.ContainerRequestContext;
21+
import jakarta.ws.rs.container.ContainerResponseContext;
2222

2323
/**
2424
* Context that holds information for metadata collection regarding Servlet HTTP requests
@@ -32,15 +32,14 @@
3232
* @since 1.12.0
3333
*/
3434
public class HttpJakartaServerRequestObservationContext
35-
extends RequestReplyReceiverContext<HttpServletRequest, HttpServletResponse> {
35+
extends RequestReplyReceiverContext<ContainerRequestContext, ContainerResponseContext> {
3636

3737
@Nullable
3838
private String pathPattern;
3939

40-
public HttpJakartaServerRequestObservationContext(HttpServletRequest request, HttpServletResponse response) {
41-
super(HttpServletRequest::getHeader);
40+
public HttpJakartaServerRequestObservationContext(ContainerRequestContext request) {
41+
super(ContainerRequestContext::getHeaderString);
4242
setCarrier(request);
43-
setResponse(response);
4443
}
4544

4645
@Nullable
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* Copyright 2023 VMware, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.micrometer.core.instrument.binder.http;
17+
18+
import io.micrometer.common.KeyValue;
19+
import io.micrometer.common.lang.Nullable;
20+
import io.micrometer.core.instrument.binder.http.HttpObservationDocumentation.CommonLowCardinalityKeys;
21+
22+
class HttpOutcome {
23+
24+
static final KeyValue HTTP_OUTCOME_SUCCESS = KeyValue.of(CommonLowCardinalityKeys.OUTCOME, "SUCCESS");
25+
26+
static final KeyValue HTTP_OUTCOME_UNKNOWN = KeyValue.of(CommonLowCardinalityKeys.OUTCOME, "UNKNOWN");
27+
28+
static KeyValue forStatus(boolean server, @Nullable Integer statusCode) {
29+
if (statusCode == null) {
30+
return HTTP_OUTCOME_UNKNOWN;
31+
}
32+
if (statusCode >= 200 && statusCode < 300) {
33+
return HTTP_OUTCOME_SUCCESS;
34+
}
35+
else if (!server) {
36+
Series resolved = Series.resolve(statusCode);
37+
if (resolved == null) {
38+
return HTTP_OUTCOME_UNKNOWN;
39+
}
40+
return KeyValue.of(CommonLowCardinalityKeys.OUTCOME, resolved.name());
41+
}
42+
else {
43+
return HTTP_OUTCOME_UNKNOWN;
44+
}
45+
}
46+
47+
// Taken from Spring Http
48+
enum Series {
49+
50+
INFORMATIONAL(1), SUCCESSFUL(2), REDIRECTION(3), CLIENT_ERROR(4), SERVER_ERROR(5);
51+
52+
private final int value;
53+
54+
Series(int value) {
55+
this.value = value;
56+
}
57+
58+
@Nullable
59+
static Series resolve(int statusCode) {
60+
int seriesCode = statusCode / 100;
61+
for (Series series : values()) {
62+
if (series.value == seriesCode) {
63+
return series;
64+
}
65+
}
66+
return null;
67+
}
68+
69+
}
70+
71+
}

0 commit comments

Comments
 (0)