Skip to content

Commit

Permalink
Declare current observation context as available since 6.0.15
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Jan 24, 2024
1 parent 2f921dd commit 199a675
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 30 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,10 +23,12 @@
import org.springframework.lang.Nullable;

/**
* Context that holds information for metadata collection
* during the {@link ClientHttpObservationDocumentation#HTTP_CLIENT_EXCHANGES client HTTP exchanges} observations.
* Context that holds information for metadata collection during the
* {@link ClientHttpObservationDocumentation#HTTP_CLIENT_EXCHANGES client HTTP exchanges} observations.
*
* <p>This context also extends {@link RequestReplySenderContext} for propagating tracing
* information with the HTTP client exchange.
*
* @author Brian Clozel
* @since 6.0
*/
Expand All @@ -35,6 +37,7 @@ public class ClientRequestObservationContext extends RequestReplySenderContext<C
@Nullable
private String uriTemplate;


/**
* Create an observation context for {@link ClientHttpRequest} observations.
* @param request the HTTP client request
Expand All @@ -50,13 +53,6 @@ private static void setRequestHeader(@Nullable ClientHttpRequest request, String
}
}

/**
* Return the URI template used for the current client exchange, {@code null} if none was used.
*/
@Nullable
public String getUriTemplate() {
return this.uriTemplate;
}

/**
* Set the URI template used for the current client exchange.
Expand All @@ -65,4 +61,12 @@ public void setUriTemplate(@Nullable String uriTemplate) {
this.uriTemplate = uriTemplate;
}

/**
* Return the URI template used for the current client exchange, {@code null} if none was used.
*/
@Nullable
public String getUriTemplate() {
return this.uriTemplate;
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -23,11 +23,12 @@
import org.springframework.lang.Nullable;

/**
* Context that holds information for metadata collection
* during the {@link ClientHttpObservationDocumentation#HTTP_REACTIVE_CLIENT_EXCHANGES HTTP client exchange observations}.
* Context that holds information for metadata collection during the
* {@link ClientHttpObservationDocumentation#HTTP_REACTIVE_CLIENT_EXCHANGES HTTP client exchange observations}.
*
* <p>The {@link #getCarrier() tracing context carrier} is a {@link ClientRequest.Builder request builder},
* since the actual request is immutable. For {@code KeyValue} extraction, the {@link #getRequest() actual request}
* should be used instead.
* since the actual request is immutable. For {@code KeyValue} extraction,
* the {@link #getRequest() actual request} should be used instead.
*
* @author Brian Clozel
* @since 6.0
Expand All @@ -37,10 +38,11 @@ public class ClientRequestObservationContext extends RequestReplySenderContext<C
/**
* Name of the request attribute holding the {@link ClientRequestObservationContext context}
* for the current observation.
* @since 6.1.2
* @since 6.0.15
*/
public static final String CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE = ClientRequestObservationContext.class.getName();


@Nullable
private String uriTemplate;

Expand Down Expand Up @@ -77,6 +79,14 @@ private static void setRequestHeader(@Nullable ClientRequest.Builder request, St
}
}


/**
* Set the URI template used for the current client exchange.
*/
public void setUriTemplate(@Nullable String uriTemplate) {
this.uriTemplate = uriTemplate;
}

/**
* Return the URI template used for the current client exchange, {@code null} if none was used.
*/
Expand All @@ -86,10 +96,11 @@ public String getUriTemplate() {
}

/**
* Set the URI template used for the current client exchange.
* Set whether the client aborted the current HTTP exchange.
* @param aborted whether the exchange has been aborted
*/
public void setUriTemplate(@Nullable String uriTemplate) {
this.uriTemplate = uriTemplate;
void setAborted(boolean aborted) {
this.aborted = aborted;
}

/**
Expand All @@ -101,11 +112,10 @@ public boolean isAborted() {
}

/**
* Set whether the client aborted the current HTTP exchange.
* @param aborted whether the exchange has been aborted
* Set the client request.
*/
void setAborted(boolean aborted) {
this.aborted = aborted;
public void setRequest(ClientRequest request) {
this.request = request;
}

/**
Expand All @@ -116,21 +126,16 @@ public ClientRequest getRequest() {
return this.request;
}

/**
* Set the client request.
*/
public void setRequest(ClientRequest request) {
this.request = request;
}

/**
* Get the current {@link ClientRequestObservationContext observation context}
* from the given request, if available.
* @param request the current client request
* @return the current observation context
* @since 6.1.2
* @since 6.0.15
*/
public static Optional<ClientRequestObservationContext> findCurrent(ClientRequest request) {
return Optional.ofNullable((ClientRequestObservationContext) request.attributes().get(CURRENT_OBSERVATION_CONTEXT_ATTRIBUTE));
}

}

0 comments on commit 199a675

Please sign in to comment.