Determine agent URL version on first upload call

This should remove http request from critical path during app load
This commit is contained in:
Nikolay Martynov 2020-02-20 07:20:08 -05:00
parent d49c08691e
commit 8209a8830c
2 changed files with 33 additions and 25 deletions

View File

@ -60,30 +60,18 @@ public class DDAgentApi {
Types.newParameterizedType(Map.class, String.class, Double.class)));
private static final MediaType MSGPACK = MediaType.get("application/msgpack");
private final String host;
private final int port;
private final String unixDomainSocketPath;
private final OkHttpClient httpClient;
private final HttpUrl tracesUrl;
private HttpUrl tracesUrl;
public DDAgentApi(final String host, final int port, final String unixDomainSocketPath) {
this(
host,
port,
endpointAvailable(getUrl(host, port, TRACES_ENDPOINT_V4), unixDomainSocketPath, true),
unixDomainSocketPath);
}
this.host = host;
this.port = port;
this.unixDomainSocketPath = unixDomainSocketPath;
DDAgentApi(
final String host,
final int port,
final boolean v4EndpointsAvailable,
final String unixDomainSocketPath) {
httpClient = buildHttpClient(unixDomainSocketPath);
if (v4EndpointsAvailable) {
tracesUrl = getUrl(host, port, TRACES_ENDPOINT_V4);
} else {
log.debug("API v0.4 endpoints not available. Downgrading to v0.3");
tracesUrl = getUrl(host, port, TRACES_ENDPOINT_V3);
}
}
public void addResponseListener(final DDAgentResponseListener listener) {
@ -128,6 +116,10 @@ public class DDAgentApi {
Response sendSerializedTraces(
final int representativeCount, final Integer sizeInBytes, final List<byte[]> traces) {
if (tracesUrl == null) {
detectEndpoint();
}
try {
final RequestBody body =
new RequestBody() {
@ -298,6 +290,16 @@ public class DDAgentApi {
}
}
private synchronized void detectEndpoint() {
final HttpUrl v4Url = getUrl(host, port, TRACES_ENDPOINT_V4);
if (endpointAvailable(v4Url, unixDomainSocketPath, true)) {
tracesUrl = v4Url;
} else {
log.debug("API v0.4 endpoints not available. Downgrading to v0.3");
tracesUrl = getUrl(host, port, TRACES_ENDPOINT_V3);
}
}
@Override
public String toString() {
return "DDApi { tracesUrl=" + tracesUrl + " }";

View File

@ -34,10 +34,10 @@ class DDAgentApiTest extends DDSpecification {
def client = new DDAgentApi("localhost", agent.address.port, null)
expect:
client.tracesUrl.toString() == "http://localhost:${agent.address.port}/v0.4/traces"
def response = client.sendTraces([])
response.success()
response.status() == 200
agent.getLastRequest().path == "/v0.4/traces"
cleanup:
agent.close()
@ -50,16 +50,19 @@ class DDAgentApiTest extends DDSpecification {
put("v0.4/traces") {
response.status(404).send()
}
put("v0.3/traces") {
response.status(404).send()
}
}
}
def client = new DDAgentApi("localhost", agent.address.port, null)
expect:
client.tracesUrl.toString() == "http://localhost:${agent.address.port}/v0.3/traces"
def response = client.sendTraces([])
!response.success()
response.status() == 404
agent.getLastRequest().path == "/v0.3/traces"
cleanup:
agent.close()
@ -77,7 +80,6 @@ class DDAgentApiTest extends DDSpecification {
def client = new DDAgentApi("localhost", agent.address.port, null)
expect:
client.tracesUrl.toString() == "http://localhost:${agent.address.port}/v0.4/traces"
client.sendTraces(traces).success()
agent.lastRequest.contentType == "application/msgpack"
agent.lastRequest.headers.get("Datadog-Meta-Lang") == "java"
@ -166,8 +168,8 @@ class DDAgentApiTest extends DDSpecification {
def client = new DDAgentApi("localhost", v3Agent.address.port, null)
expect:
client.tracesUrl.toString() == "http://localhost:${v3Agent.address.port}/v0.3/traces"
client.sendTraces([]).success()
v3Agent.getLastRequest().path == "/v0.3/traces"
cleanup:
v3Agent.close()
@ -191,9 +193,13 @@ class DDAgentApiTest extends DDSpecification {
}
def port = badPort ? 999 : agent.address.port
def client = new DDAgentApi("localhost", port, null)
def result = client.sendTraces([])
expect:
client.tracesUrl.toString() == "http://localhost:${port}/$endpointVersion/traces"
result.success() == !badPort // Expect success of port is ok
if (!badPort) {
assert agent.getLastRequest().path == "/$endpointVersion/traces"
}
cleanup:
agent.close()