Skip to content

Commit

Permalink
Use UserAgent.of to append informational agents (#1802)
Browse files Browse the repository at this point in the history
Use UserAgent.of to append informational agents
  • Loading branch information
schlosna authored Oct 24, 2022
1 parent 6eb8735 commit 33ad266
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.conjure.java.api.config.service.UserAgent;
import com.palantir.conjure.java.api.config.service.UserAgent.Agent;
import com.palantir.conjure.java.api.config.service.UserAgents;
import com.palantir.dialogue.Channel;
import com.palantir.dialogue.Endpoint;
Expand All @@ -27,6 +28,8 @@
import com.palantir.logsafe.SafeArg;
import com.palantir.logsafe.logger.SafeLogger;
import com.palantir.logsafe.logger.SafeLoggerFactory;
import java.util.List;
import javax.annotation.Nullable;

/**
* Adds a {@code user-agent} header that is the combination of the given base user agent, the version of the
Expand All @@ -35,7 +38,7 @@
*/
final class UserAgentEndpointChannel implements EndpointChannel {
private static final SafeLogger log = SafeLoggerFactory.get(UserAgentEndpointChannel.class);
static final UserAgent.Agent DIALOGUE_AGENT = extractDialogueAgent();
static final Agent DIALOGUE_AGENT = extractDialogueAgent();

private final EndpointChannel delegate;
private final String userAgent;
Expand All @@ -60,14 +63,23 @@ public ListenableFuture<Response> execute(Request request) {
}

private static UserAgent augmentUserAgent(UserAgent baseAgent, Endpoint endpoint) {
return tryAddEndpointAgent(baseAgent, endpoint).addAgent(DIALOGUE_AGENT);
Agent endpointAgent = getEndpointAgent(endpoint);
try {
List<Agent> informationalAgents =
(endpointAgent == null) ? List.of(DIALOGUE_AGENT) : List.of(endpointAgent, DIALOGUE_AGENT);
return UserAgent.of(baseAgent, informationalAgents);
} catch (RuntimeException e) {
log.error("Could not construct user agent", e);
return baseAgent;
}
}

private static UserAgent tryAddEndpointAgent(UserAgent baseAgent, Endpoint endpoint) {
@Nullable
private static Agent getEndpointAgent(Endpoint endpoint) {
String endpointService = endpoint.serviceName();
String endpointVersion = getEndpointVersion(endpoint);
try {
return baseAgent.addAgent(UserAgent.Agent.of(endpoint.serviceName(), endpointVersion));
return Agent.of(endpoint.serviceName(), endpointVersion);
} catch (IllegalArgumentException e) {
if (log.isDebugEnabled()) {
log.debug(
Expand All @@ -77,15 +89,15 @@ private static UserAgent tryAddEndpointAgent(UserAgent baseAgent, Endpoint endpo
SafeArg.of("version", endpointVersion),
e);
}
return baseAgent;
return null;
}
}

private static String getEndpointVersion(Endpoint endpoint) {
String endpointVersion = endpoint.version();
// Until conjure-java 5.14.2, we mistakenly embedded 0.0.0 in everything. This fallback logic attempts
// to work-around this and produce a more helpful user agent
if ("0.0.0".equals(endpointVersion)) {
if (Agent.DEFAULT_VERSION.equals(endpointVersion)) {
String jarVersion = endpoint.getClass().getPackage().getImplementationVersion();
if (jarVersion != null) {
return jarVersion;
Expand All @@ -94,14 +106,14 @@ private static String getEndpointVersion(Endpoint endpoint) {
return endpointVersion;
}

private static UserAgent.Agent extractDialogueAgent() {
private static Agent extractDialogueAgent() {
String version = dialogueVersion();
return UserAgent.Agent.of("dialogue", version);
return Agent.of("dialogue", version);
}

static String dialogueVersion() {
String maybeDialogueVersion = Channel.class.getPackage().getImplementationVersion();
return maybeDialogueVersion != null ? maybeDialogueVersion : "0.0.0";
return maybeDialogueVersion != null ? maybeDialogueVersion : Agent.DEFAULT_VERSION;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.mockito.Mockito.verify;

import com.palantir.conjure.java.api.config.service.UserAgent;
import com.palantir.conjure.java.api.config.service.UserAgent.Agent;
import com.palantir.dialogue.Channel;
import com.palantir.dialogue.Endpoint;
import com.palantir.dialogue.EndpointChannel;
Expand All @@ -40,15 +41,15 @@
@SuppressWarnings("FutureReturnValueIgnored")
public final class UserAgentEndpointChannelTest {

private static final UserAgent baseAgent = UserAgent.of(UserAgent.Agent.of("test-class", "1.2.3"));
private static final UserAgent baseAgent = UserAgent.of(Agent.of("test-class", "1.2.3"));

@Mock
private EndpointChannel delegate;

@Captor
private ArgumentCaptor<Request> requestCaptor;

private Request request = Request.builder()
private final Request request = Request.builder()
.putHeaderParams("header", "value")
.putQueryParams("query", "value")
.putPathParams("path", "value")
Expand All @@ -59,7 +60,7 @@ public void injectsDialogueVersionAndEndpointVersion() {
EndpointChannel channel = UserAgentEndpointChannel.create(delegate, TestEndpoint.POST, baseAgent);
// Special case: In IDEs, tests are run against classes (not JARs) and thus don't carry versions.
String dialogueVersion = Optional.ofNullable(Channel.class.getPackage().getImplementationVersion())
.orElse("0.0.0");
.orElse(Agent.DEFAULT_VERSION);

channel.execute(request);
verify(delegate).execute(requestCaptor.capture());
Expand Down Expand Up @@ -98,7 +99,7 @@ public String version() {
baseAgent);
// Special case: In IDEs, tests are run against classes (not JARs) and thus don't carry versions.
String dialogueVersion = Optional.ofNullable(Channel.class.getPackage().getImplementationVersion())
.orElse("0.0.0");
.orElse(Agent.DEFAULT_VERSION);
channel.execute(request);
verify(delegate).execute(requestCaptor.capture());
assertThat(requestCaptor.getValue().headerParams().get("user-agent"))
Expand Down
25 changes: 13 additions & 12 deletions versions.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Run ./gradlew --write-locks to regenerate this file
com.fasterxml.jackson.core:jackson-annotations:2.13.4 (6 constraints: f661ba00)
com.fasterxml.jackson.core:jackson-annotations:2.13.4 (6 constraints: f761bb00)
com.fasterxml.jackson.core:jackson-core:2.13.4 (10 constraints: 47d0767f)
com.fasterxml.jackson.core:jackson-databind:2.13.4.2 (16 constraints: 3949b45c)
com.fasterxml.jackson.core:jackson-databind:2.13.4.2 (16 constraints: 3a49b55c)
com.fasterxml.jackson.dataformat:jackson-dataformat-cbor:2.13.4 (1 constraints: 7d1c9aa4)
com.fasterxml.jackson.dataformat:jackson-dataformat-smile:2.13.4 (1 constraints: 7d1c9aa4)
com.fasterxml.jackson.datatype:jackson-datatype-guava:2.13.4 (1 constraints: 7d1c9aa4)
Expand All @@ -19,23 +19,23 @@ com.google.guava:listenablefuture:9999.0-empty-to-avoid-conflict-with-guava (1 c
com.google.j2objc:j2objc-annotations:1.3 (1 constraints: b809eda0)
com.palantir.common:streams:2.0.0 (1 constraints: 0405f535)
com.palantir.conjure.java:conjure-lib:6.65.0 (1 constraints: 43056a3b)
com.palantir.conjure.java.api:errors:2.27.0 (2 constraints: 5f15feda)
com.palantir.conjure.java.api:service-config:2.27.0 (2 constraints: 56198adc)
com.palantir.conjure.java.api:ssl-config:2.27.0 (2 constraints: a5252239)
com.palantir.conjure.java.api:errors:2.29.0 (2 constraints: 611562db)
com.palantir.conjure.java.api:service-config:2.29.0 (2 constraints: 581902dd)
com.palantir.conjure.java.api:ssl-config:2.29.0 (2 constraints: a7259239)
com.palantir.conjure.java.runtime:client-config:7.33.0 (1 constraints: 3f055e3b)
com.palantir.conjure.java.runtime:conjure-java-jackson-optimizations:7.33.0 (1 constraints: 831cbfa4)
com.palantir.conjure.java.runtime:conjure-java-jackson-serialization:7.33.0 (2 constraints: 5e16f60e)
com.palantir.conjure.java.runtime:keystores:7.33.0 (2 constraints: 5d1931dd)
com.palantir.goethe:goethe:0.8.0 (1 constraints: 0a050336)
com.palantir.refreshable:refreshable:2.2.0 (2 constraints: eb18d5b1)
com.palantir.ri:resource-identifier:2.5.0 (2 constraints: f714fcb6)
com.palantir.safe-logging:logger:1.28.0 (9 constraints: 0189ebe2)
com.palantir.safe-logging:logger-slf4j:1.28.0 (1 constraints: 360e9750)
com.palantir.safe-logging:logger-spi:1.28.0 (2 constraints: 7b1e44b1)
com.palantir.safe-logging:preconditions:1.28.0 (14 constraints: 96e7bee4)
com.palantir.safe-logging:safe-logging:1.28.0 (13 constraints: b1cb9628)
com.palantir.safe-logging:logger:3.1.0 (9 constraints: 9788b310)
com.palantir.safe-logging:logger-slf4j:3.1.0 (1 constraints: ff0d5642)
com.palantir.safe-logging:logger-spi:3.1.0 (2 constraints: 0d1e647a)
com.palantir.safe-logging:preconditions:3.1.0 (14 constraints: 8be6c06f)
com.palantir.safe-logging:safe-logging:3.1.0 (13 constraints: 6bcaf57a)
com.palantir.safethreadlocalrandom:safe-thread-local-random:0.1.0 (1 constraints: 0305ee35)
com.palantir.tokens:auth-tokens:3.14.0 (3 constraints: 1a28f68b)
com.palantir.tokens:auth-tokens:3.16.0 (3 constraints: 1c28fc8b)
com.palantir.tracing:tracing:6.12.0 (2 constraints: 5616100e)
com.palantir.tracing:tracing-api:6.12.0 (2 constraints: 0b121518)
com.palantir.tritium:tritium-api:0.49.0 (2 constraints: 3b1fd6bd)
Expand All @@ -58,6 +58,7 @@ org.glassfish.hk2.external:jakarta.inject:2.6.1 (1 constraints: f410fcc2)
org.glassfish.jersey.core:jersey-common:2.31 (1 constraints: da04fa30)
org.hdrhistogram:HdrHistogram:2.1.12 (1 constraints: 3e103aa2)
org.immutables:value:2.8.8 (1 constraints: 14051536)
org.jetbrains:annotations:23.0.0 (1 constraints: 31115ed1)
org.mpierce.metrics.reservoir:hdrhistogram-metrics-reservoir:1.1.3 (1 constraints: 0d10f991)
org.slf4j:slf4j-api:1.7.36 (8 constraints: b080201b)

Expand All @@ -68,7 +69,7 @@ com.google.auto.value:auto-value-annotations:1.7.4 (1 constraints: 640a29b9)
com.google.code.findbugs:annotations:3.0.1 (1 constraints: 9e0aafc3)
com.google.testing.compile:compile-testing:0.19 (1 constraints: de04f630)
com.google.truth:truth:1.1 (1 constraints: b71111d7)
com.palantir.safe-logging:preconditions-assertj:1.28.0 (1 constraints: 3d05453b)
com.palantir.safe-logging:preconditions-assertj:3.1.0 (1 constraints: 3d05453b)
com.palantir.tracing:tracing-test-utils:6.12.0 (1 constraints: 3b054d3b)
com.spotify.dataenum:dataenum:1.4.1 (1 constraints: e9105ac1)
com.squareup.okhttp3:mockwebserver:3.13.1 (1 constraints: 3a053f3b)
Expand Down
2 changes: 1 addition & 1 deletion versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ com.google.testing.compile:compile-testing = 0.19
com.palantir.common:streams = 2.0.0
com.palantir.conjure:conjure = 4.35.0
com.palantir.conjure.java:* = 6.65.0
com.palantir.conjure.java.api:* = 2.27.0
com.palantir.conjure.java.api:* = 2.29.0
com.palantir.conjure.java.runtime:* = 7.33.0
com.palantir.refreshable:* = 2.2.0
com.palantir.ri:resource-identifier = 2.5.0
Expand Down

0 comments on commit 33ad266

Please sign in to comment.