Skip to content

Commit

Permalink
More specific naming and packaging and polish
Browse files Browse the repository at this point in the history
Renames classes for the specific API/implementation they target for clarity. Re-organizes the classes into more specific packages to more easily identify related classes.
  • Loading branch information
shakuzen committed Oct 4, 2023
1 parent d0b2960 commit b2ca433
Show file tree
Hide file tree
Showing 23 changed files with 236 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
public class DefaultHttpServerRequestObservationConvention extends AbstractDefaultHttpServerRequestObservationConvention
implements HttpServerRequestObservationConvention {

private static final String DEFAULT_NAME = "http.server.requests";

private final String name;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

import io.micrometer.common.docs.KeyName;
import io.micrometer.core.instrument.binder.http.HttpObservationDocumentation.*;
import io.micrometer.jakarta.instrument.binder.http.jaxrs.container.DefaultJaxRsContainerObservationConvention;
import io.micrometer.jakarta.instrument.binder.http.jaxrs.client.DefaultJaxRsHttpClientObservationConvention;
import io.micrometer.jakarta.instrument.binder.http.servlet.DefaultHttpServletObservationConvention;
import io.micrometer.observation.Observation.Context;
import io.micrometer.observation.ObservationConvention;
import io.micrometer.observation.docs.ObservationDocumentation;
Expand All @@ -31,12 +34,12 @@
public enum JakartaHttpObservationDocumentation implements ObservationDocumentation {

/**
* Observation created when a request is sent out via Jakarta API.
* Observation created when a request is sent out via a JAX-RS client.
*/
JAKARTA_CLIENT_OBSERVATION {
JAX_RS_CLIENT_OBSERVATION {
@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultHttpJakartaClientRequestObservationConvention.class;
return DefaultJaxRsHttpClientObservationConvention.class;
}

@Override
Expand All @@ -51,12 +54,12 @@ public KeyName[] getHighCardinalityKeyNames() {
},

/**
* Observation created when a request is received with Jakarta Http.
* Observation created when a request is received via JAX-RS API.
*/
JAKARTA_SERVER_OBSERVATION {
JAX_RS_CONTAINER_OBSERVATION {
@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultHttpJakartaServerRequestObservationConvention.class;
return DefaultJaxRsContainerObservationConvention.class;
}

@Override
Expand All @@ -75,10 +78,10 @@ public KeyName[] getHighCardinalityKeyNames() {
/**
* Observation created when a request is received with jakarta.servlet Http.
*/
JAKARTA_SERVLET_SERVER_OBSERVATION {
SERVLET_OBSERVATION {
@Override
public Class<? extends ObservationConvention<? extends Context>> getDefaultConvention() {
return DefaultHttpJakartaServerServletRequestObservationConvention.class;
return DefaultHttpServletObservationConvention.class;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs;

import io.micrometer.jakarta.instrument.binder.http.jaxrs.client.ObservationJaxRsHttpClientFilter;
import io.micrometer.jakarta.instrument.binder.http.jaxrs.client.ObservationJerseyClientInterceptor;
import jakarta.ws.rs.ConstrainedTo;
import jakarta.ws.rs.RuntimeType;
import jakarta.ws.rs.core.FeatureContext;
Expand All @@ -30,9 +32,9 @@ public class ObservationAutoDiscoverable implements AutoDiscoverable {

@Override
public void configure(FeatureContext context) {
if (!context.getConfiguration().isRegistered(ObservationHttpJakartaClientFilter.class)) {
context.register(ObservationHttpJakartaClientFilter.class, CLIENT_OBSERVABILITY_PRIORITY);
context.register(ObservationHttpJakartaInterceptor.class, CLIENT_OBSERVABILITY_PRIORITY);
if (!context.getConfiguration().isRegistered(ObservationJaxRsHttpClientFilter.class)) {
context.register(ObservationJaxRsHttpClientFilter.class, CLIENT_OBSERVABILITY_PRIORITY);
context.register(ObservationJerseyClientInterceptor.class, CLIENT_OBSERVABILITY_PRIORITY);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,25 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

import io.micrometer.common.KeyValues;
import io.micrometer.core.instrument.binder.http.AbstractDefaultHttpClientRequestObservationConvention;
import io.micrometer.jakarta.instrument.binder.http.jaxrs.container.JaxRsContainerObservationConvention;

import java.net.URI;

/**
* Default {@link HttpJakartaServerRequestObservationConvention}.
* Default {@link JaxRsContainerObservationConvention}.
*
* @author Brian Clozel
* @author Marcin Grzejszczak
* @since 1.12.0
*/
public class DefaultHttpJakartaClientRequestObservationConvention extends
AbstractDefaultHttpClientRequestObservationConvention implements HttpJakartaClientRequestObservationConvention {
public class DefaultJaxRsHttpClientObservationConvention extends AbstractDefaultHttpClientRequestObservationConvention
implements JaxRsHttpClientObservationConvention {

public static DefaultHttpJakartaClientRequestObservationConvention INSTANCE = new DefaultHttpJakartaClientRequestObservationConvention();
public static DefaultJaxRsHttpClientObservationConvention INSTANCE = new DefaultJaxRsHttpClientObservationConvention();

private static final String DEFAULT_NAME = "http.client.requests";

Expand All @@ -39,15 +40,15 @@ public class DefaultHttpJakartaClientRequestObservationConvention extends
/**
* Create a convention with the default name {@code "http.client.requests"}.
*/
public DefaultHttpJakartaClientRequestObservationConvention() {
public DefaultJaxRsHttpClientObservationConvention() {
this(DEFAULT_NAME);
}

/**
* Create a convention with a custom name.
* @param name the observation name
*/
public DefaultHttpJakartaClientRequestObservationConvention(String name) {
public DefaultJaxRsHttpClientObservationConvention(String name) {
this.name = name;
}

Expand All @@ -57,14 +58,14 @@ public String getName() {
}

@Override
public String getContextualName(HttpJakartaClientRequestObservationContext context) {
public String getContextualName(JaxRsHttpClientObservationContext context) {
String method = context.getCarrier() != null
? (context.getCarrier().getMethod() != null ? context.getCarrier().getMethod() : null) : null;
return getContextualName(method);
}

@Override
public KeyValues getLowCardinalityKeyValues(HttpJakartaClientRequestObservationContext context) {
public KeyValues getLowCardinalityKeyValues(JaxRsHttpClientObservationContext context) {
URI uri = context.getCarrier() != null ? context.getCarrier().getUri() : null;
Throwable throwable = context.getError();
String methodName = context.getCarrier() != null ? context.getCarrier().getMethod() : null;
Expand All @@ -74,7 +75,7 @@ public KeyValues getLowCardinalityKeyValues(HttpJakartaClientRequestObservationC
}

@Override
public KeyValues getHighCardinalityKeyValues(HttpJakartaClientRequestObservationContext context) {
public KeyValues getHighCardinalityKeyValues(JaxRsHttpClientObservationContext context) {
URI uri = context.getCarrier() != null ? context.getCarrier().getUri() : null;
String userAgent = context.getCarrier() != null ? context.getCarrier().getHeaderString("User-Agent") : null;
return getHighCardinalityKeyValues(uri, userAgent);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

import io.micrometer.common.lang.Nullable;
import io.micrometer.observation.transport.RequestReplySenderContext;
Expand All @@ -31,7 +31,7 @@
* @author Marcin Grzejszczak
* @since 1.12.0
*/
public class HttpJakartaClientRequestObservationContext
public class JaxRsHttpClientObservationContext
extends RequestReplySenderContext<ClientRequestContext, ClientResponseContext> {

@Nullable
Expand All @@ -41,8 +41,8 @@ public class HttpJakartaClientRequestObservationContext
* Create an observation context for HTTP client observations.
* @param containerRequestContext the context for a {@link ClientRequestContext}
*/
public HttpJakartaClientRequestObservationContext(ClientRequestContext containerRequestContext) {
super(HttpJakartaClientRequestObservationContext::setRequestHeader);
public JaxRsHttpClientObservationContext(ClientRequestContext containerRequestContext) {
super(JaxRsHttpClientObservationContext::setRequestHeader);
this.setCarrier(containerRequestContext);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,23 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

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

/**
* Interface for an {@link ObservationConvention} for Servlet HTTP requests.
* Interface for an {@link ObservationConvention} for a JAX-RS client HTTP requests.
*
* @author Brian Clozel
* @author Marcin Grzejszczak
* @since 1.12.0
*/
public interface HttpJakartaClientRequestObservationConvention
extends ObservationConvention<HttpJakartaClientRequestObservationContext> {
public interface JaxRsHttpClientObservationConvention extends ObservationConvention<JaxRsHttpClientObservationContext> {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

import io.micrometer.common.lang.Nullable;
import io.micrometer.jakarta.instrument.binder.http.JakartaHttpObservationDocumentation;
import io.micrometer.observation.Observation;
import io.micrometer.observation.ObservationRegistry;
import jakarta.ws.rs.client.ClientRequestContext;
Expand All @@ -26,17 +27,19 @@
import java.io.IOException;

/**
* A {@link ClientResponseFilter} and {@link ClientResponseFilter} that is basically and
* around wrapper over sending an HTTP call.
* A {@link ClientResponseFilter} and {@link ClientResponseFilter} that is an around
* wrapper over sending an HTTP call. For handling exceptions, configure
* {@link ObservationJerseyClientInterceptor}.
*
* @author Marcin Grzejszczak
* @since 1.12.0
* @see ObservationJerseyClientInterceptor
*/
public class ObservationHttpJakartaClientFilter implements ClientRequestFilter, ClientResponseFilter {
public class ObservationJaxRsHttpClientFilter implements ClientRequestFilter, ClientResponseFilter {

static final String OBSERVATION_PROPERTY = ObservationHttpJakartaClientFilter.class.getName() + ".observation";
static final String OBSERVATION_PROPERTY = ObservationJaxRsHttpClientFilter.class.getName() + ".observation";

static final String OBSERVATION_SCOPE_PROPERTY = ObservationHttpJakartaClientFilter.class.getName()
static final String OBSERVATION_SCOPE_PROPERTY = ObservationJaxRsHttpClientFilter.class.getName()
+ ".observationScope";

@Nullable
Expand All @@ -45,16 +48,16 @@ public class ObservationHttpJakartaClientFilter implements ClientRequestFilter,
private final ObservationRegistry observationRegistry;

@Nullable
private final HttpJakartaClientRequestObservationConvention convention;
private final JaxRsHttpClientObservationConvention convention;

/**
* Creates a new instance of the filter.
* @param name optional observation name
* @param observationRegistry observation registry
* @param convention optional convention
*/
public ObservationHttpJakartaClientFilter(@Nullable String name, ObservationRegistry observationRegistry,
@Nullable HttpJakartaClientRequestObservationConvention convention) {
public ObservationJaxRsHttpClientFilter(@Nullable String name, ObservationRegistry observationRegistry,
@Nullable JaxRsHttpClientObservationConvention convention) {
this.name = name;
this.observationRegistry = observationRegistry;
this.convention = convention;
Expand All @@ -65,24 +68,23 @@ public ObservationHttpJakartaClientFilter(@Nullable String name, ObservationRegi
* @param observationRegistry observation registry
* @param convention optional convention
*/
public ObservationHttpJakartaClientFilter(ObservationRegistry observationRegistry,
@Nullable HttpJakartaClientRequestObservationConvention convention) {
public ObservationJaxRsHttpClientFilter(ObservationRegistry observationRegistry,
@Nullable JaxRsHttpClientObservationConvention convention) {
this(null, observationRegistry, convention);
}

@Override
public void filter(ClientRequestContext requestContext) throws IOException {
HttpJakartaClientRequestObservationContext context = new HttpJakartaClientRequestObservationContext(
requestContext);
Observation observation = JakartaHttpObservationDocumentation.JAKARTA_CLIENT_OBSERVATION.start(convention,
JaxRsHttpClientObservationContext context = new JaxRsHttpClientObservationContext(requestContext);
Observation observation = JakartaHttpObservationDocumentation.JAX_RS_CLIENT_OBSERVATION.start(convention,
defaultConvention(), () -> context, observationRegistry);
requestContext.setProperty(OBSERVATION_PROPERTY, observation);
requestContext.setProperty(OBSERVATION_SCOPE_PROPERTY, observation.openScope());
}

private DefaultHttpJakartaClientRequestObservationConvention defaultConvention() {
return this.name != null ? new DefaultHttpJakartaClientRequestObservationConvention(this.name)
: DefaultHttpJakartaClientRequestObservationConvention.INSTANCE;
private DefaultJaxRsHttpClientObservationConvention defaultConvention() {
return this.name != null ? new DefaultJaxRsHttpClientObservationConvention(this.name)
: DefaultJaxRsHttpClientObservationConvention.INSTANCE;
}

@Override
Expand All @@ -109,8 +111,7 @@ public void filter(ClientRequestContext requestContext, ClientResponseContext re
if (scope != null) {
scope.close();
}
HttpJakartaClientRequestObservationContext context = (HttpJakartaClientRequestObservationContext) observation
.getContext();
JaxRsHttpClientObservationContext context = (JaxRsHttpClientObservationContext) observation.getContext();
context.setResponse(responseContext);
observation.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,26 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.jakarta.instrument.binder.http;
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

import io.micrometer.observation.Observation;
import io.micrometer.observation.Observation.Scope;
import jakarta.ws.rs.client.ClientRequestContext;
import jakarta.ws.rs.client.ClientResponseContext;
import org.glassfish.jersey.client.spi.PostInvocationInterceptor;

import static io.micrometer.jakarta.instrument.binder.http.ObservationHttpJakartaClientFilter.OBSERVATION_PROPERTY;
import static io.micrometer.jakarta.instrument.binder.http.ObservationHttpJakartaClientFilter.OBSERVATION_SCOPE_PROPERTY;
import static io.micrometer.jakarta.instrument.binder.http.jaxrs.client.ObservationJaxRsHttpClientFilter.OBSERVATION_PROPERTY;
import static io.micrometer.jakarta.instrument.binder.http.jaxrs.client.ObservationJaxRsHttpClientFilter.OBSERVATION_SCOPE_PROPERTY;

/**
* A post-invocation client interceptor. Will stop an observation started in
* {@link ObservationHttpJakartaClientFilter} in case of exceptions.
* A post-invocation client interceptor that will stop an observation started in
* {@link ObservationJaxRsHttpClientFilter} in case of exceptions.
*
* @author Marcin Grzejszczak
* @since 1.12.0
* @see ObservationJaxRsHttpClientFilter
*/
public class ObservationHttpJakartaInterceptor implements PostInvocationInterceptor {
public class ObservationJerseyClientInterceptor implements PostInvocationInterceptor {

@Override
public void afterRequest(ClientRequestContext requestContext, ClientResponseContext responseContext) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* 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.
*/

/**
* Observation instrumentation for Jakarta JAX-RS clients.
*/
@NonNullFields
@NonNullApi
package io.micrometer.jakarta.instrument.binder.http.jaxrs.client;

import io.micrometer.common.lang.NonNullApi;
import io.micrometer.common.lang.NonNullFields;
Loading

0 comments on commit b2ca433

Please sign in to comment.