Skip to content

Commit

Permalink
Publish a separate module with jdk11 classes (micrometer-metrics#4728)
Browse files Browse the repository at this point in the history
Introduces a new module named micrometer-java11 with a copy of the classes for the JDK http client instrumentation currently in micrometer-core as multi-release jar classes for Java 11. This also deprecates the corresponding classes in micrometer-core and directs users to the new module.

Fixes micrometer-metricsgh-3577

---------

Co-authored-by: Tommy Ludwig <8924140+shakuzen@users.noreply.github.com>
  • Loading branch information
monosoul and shakuzen committed Feb 13, 2024
1 parent 639c93a commit c69180d
Show file tree
Hide file tree
Showing 19 changed files with 758 additions and 41 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ subprojects {

check.dependsOn("testModules")

if (!(project.name in ['micrometer-jakarta9'])) { // add projects here that do not exist in the previous minor so should be excluded from japicmp
if (!(project.name in ['micrometer-jakarta9', 'micrometer-java11'])) { // add projects here that do not exist in the previous minor so should be excluded from japicmp
apply plugin: 'me.champeau.gradle.japicmp'
apply plugin: 'de.undercouch.download'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@
*
* @author Marcin Grzejszczak
* @since 1.10.0
* @deprecated since 1.13.0 use the same class in the micrometer-java11 module instead
*/
@Deprecated
public class DefaultHttpClientObservationConvention implements HttpClientObservationConvention {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
*
* @author Marcin Grzejszczak
* @since 1.10.0
* @deprecated since 1.13.0 use the same class in the micrometer-java11 module instead
*/
@Deprecated
public class HttpClientContext extends RequestReplySenderContext<HttpRequest.Builder, HttpResponse<?>> {

private final Function<HttpRequest, String> uriMapper;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*
* @author Marcin Grzejszczak
* @since 1.10.0
* @deprecated since 1.13.0 use the same class in the micrometer-java11 module instead
*/
@Deprecated
public interface HttpClientObservationConvention extends ObservationConvention<HttpClientContext> {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;

@SuppressWarnings("deprecation")
enum HttpClientObservationDocumentation implements ObservationDocumentation {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@
*
* @author Marcin Grzejszczak
* @since 1.10.0
* @deprecated since 1.13.0 use the same class in the micrometer-java11 module instead
*/
@Deprecated
public class MicrometerHttpClient extends HttpClient {

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
*/

/**
* Instrumentation of JDK classes.
* Instrumentation of JDK classes. Deprecated since 1.13.0 use the micrometer-java11
* module instead.
*/
// Note we can't use the @deprecated JavaDoc tag due to compiler bug JDK-8160601
@NonNullApi
@NonNullFields
package io.micrometer.core.instrument.binder.jdk;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.BDDAssertions.then;

@SuppressWarnings("deprecation")
@WireMockTest
class MicrometerHttpClientTests {

Expand Down
24 changes: 24 additions & 0 deletions micrometer-java11/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
description 'Micrometer core classes that require Java 11'

dependencies {
api project(":micrometer-core")

testImplementation 'org.junit.jupiter:junit-jupiter'
testImplementation 'org.assertj:assertj-core'
}

java {
targetCompatibility = 11
}

tasks.withType(JavaCompile).configureEach {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
options.release = 11
}

dependencies {
testImplementation 'ru.lanwen.wiremock:wiremock-junit5'
testImplementation 'com.github.tomakehurst:wiremock-jre8-standalone'
testImplementation project(":micrometer-observation-test")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2024 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.java11.instrument.binder.jdk;

import io.micrometer.common.KeyValues;
import io.micrometer.common.lang.NonNull;
import io.micrometer.common.lang.Nullable;
import io.micrometer.core.instrument.binder.http.Outcome;

import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.function.Function;

/**
* Default implementation of {@link HttpClientObservationConvention}.
*
* @author Marcin Grzejszczak
* @since 1.13.0
*/
public class DefaultHttpClientObservationConvention implements HttpClientObservationConvention {

/**
* Instance of this {@link DefaultHttpClientObservationConvention}.
*/
public static DefaultHttpClientObservationConvention INSTANCE = new DefaultHttpClientObservationConvention();

@Override
public KeyValues getLowCardinalityKeyValues(HttpClientContext context) {
if (context.getCarrier() == null) {
return KeyValues.empty();
}
HttpRequest httpRequest = context.getCarrier().build();
KeyValues keyValues = KeyValues.of(
HttpClientObservationDocumentation.LowCardinalityKeys.METHOD.withValue(httpRequest.method()),
HttpClientObservationDocumentation.LowCardinalityKeys.URI
.withValue(getUriTag(httpRequest, context.getResponse(), context.getUriMapper())));
if (context.getResponse() != null) {
keyValues = keyValues
.and(HttpClientObservationDocumentation.LowCardinalityKeys.STATUS
.withValue(String.valueOf(context.getResponse().statusCode())))
.and(HttpClientObservationDocumentation.LowCardinalityKeys.OUTCOME
.withValue(Outcome.forStatus(context.getResponse().statusCode()).name()));
}
return keyValues;
}

String getUriTag(@Nullable HttpRequest request, @Nullable HttpResponse<?> httpResponse,
Function<HttpRequest, String> uriMapper) {
if (request == null) {
return null;
}
return httpResponse != null && (httpResponse.statusCode() == 404 || httpResponse.statusCode() == 301)
? "NOT_FOUND" : uriMapper.apply(request);
}

@Override
@NonNull
public String getName() {
return "http.client.requests";
}

@Nullable
@Override
public String getContextualName(HttpClientContext context) {
if (context.getCarrier() == null) {
return null;
}
return "HTTP " + context.getCarrier().build().method();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2024 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.java11.instrument.binder.jdk;

import io.micrometer.observation.transport.RequestReplySenderContext;

import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Objects;
import java.util.function.Function;

/**
* A {@link RequestReplySenderContext} for an {@link HttpClient}.
*
* @author Marcin Grzejszczak
* @since 1.13.0
*/
public class HttpClientContext extends RequestReplySenderContext<HttpRequest.Builder, HttpResponse<?>> {

private final Function<HttpRequest, String> uriMapper;

public HttpClientContext(Function<HttpRequest, String> uriMapper) {
super((carrier, key, value) -> Objects.requireNonNull(carrier).header(key, value));
this.uriMapper = uriMapper;
}

public Function<HttpRequest, String> getUriMapper() {
return uriMapper;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2024 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.java11.instrument.binder.jdk;

import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;

import java.net.http.HttpClient;

/**
* An {@link ObservationConvention} for an {@link HttpClient}.
*
* @author Marcin Grzejszczak
* @since 1.13.0
*/
public interface HttpClientObservationConvention extends ObservationConvention<HttpClientContext> {

@Override
default boolean supportsContext(Observation.Context context) {
return context instanceof HttpClientContext;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2024 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.java11.instrument.binder.jdk;

import io.micrometer.common.docs.KeyName;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;

enum HttpClientObservationDocumentation implements ObservationDocumentation {

/**
* Observation when an HTTP call is being made.
*/
HTTP_CALL {
@Override
public Class<? extends ObservationConvention<? extends Observation.Context>> getDefaultConvention() {
return DefaultHttpClientObservationConvention.class;
}

@Override
public KeyName[] getLowCardinalityKeyNames() {
return LowCardinalityKeys.values();
}

};

enum LowCardinalityKeys implements KeyName {

/**
* HTTP Method.
*/
METHOD {
@Override
public String asString() {
return "method";
}
},

/**
* HTTP Status.
*/
STATUS {
@Override
public String asString() {
return "status";
}
},

/**
* Key name for outcome.
* @since 1.11.0
*/
OUTCOME {
@Override
public String asString() {
return "outcome";
}
},

/**
* HTTP URI.
*/
URI {
@Override
public String asString() {
return "uri";
}
}

}

}

0 comments on commit c69180d

Please sign in to comment.