diff --git a/communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java b/communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java index 16be2e84b98..0fc47235265 100644 --- a/communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java +++ b/communication/src/main/java/datadog/communication/ddagent/DDAgentFeaturesDiscovery.java @@ -199,6 +199,10 @@ private void doDiscovery(State newState) { newState.evpProxyEndpoint, newState.telemetryProxyEndpoint); } + // even if /info failed or sent no container tags hash (e.g. the Lambda extension) + if (BaseHash.getBaseHashStr() == null) { + BaseHash.recalcBaseHash(null); + } } private String probeTracesEndpoint(State newState, List endpoints) { diff --git a/communication/src/test/java/datadog/communication/ddagent/DDAgentFeaturesDiscoveryBaseHashForkedTest.java b/communication/src/test/java/datadog/communication/ddagent/DDAgentFeaturesDiscoveryBaseHashForkedTest.java new file mode 100644 index 00000000000..c13d6e4a22b --- /dev/null +++ b/communication/src/test/java/datadog/communication/ddagent/DDAgentFeaturesDiscoveryBaseHashForkedTest.java @@ -0,0 +1,63 @@ +package datadog.communication.ddagent; + +import static datadog.communication.http.OkHttpUtils.DATADOG_CONTAINER_TAGS_HASH; +import static datadog.trace.api.ProtocolVersion.V0_5; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +import datadog.metrics.api.Monitoring; +import datadog.trace.api.BaseHash; +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.mockwebserver.Dispatcher; +import okhttp3.mockwebserver.MockResponse; +import okhttp3.mockwebserver.MockWebServer; +import okhttp3.mockwebserver.RecordedRequest; +import org.junit.jupiter.api.Test; + +// forked: the base hash is global, and this needs it unset +class DDAgentFeaturesDiscoveryBaseHashForkedTest { + + @Test + void setsBaseHashWithoutContainerTagsHashUntilOneArrives() throws Exception { + HttpUrl agentNotUp; + try (MockWebServer agent = new MockWebServer()) { + agentNotUp = agent.url("/"); + } + assertNull(BaseHash.getBaseHashStr()); + discover(agentNotUp); + assertEquals(BaseHash.calc(null), BaseHash.getBaseHash()); + + try (MockWebServer agent = agent(null)) { + discover(agent.url("/")); + } + assertEquals(BaseHash.calc(null), BaseHash.getBaseHash()); + + try (MockWebServer agent = agent("test-hash")) { + discover(agent.url("/")); + } + assertEquals(BaseHash.calc("test-hash"), BaseHash.getBaseHash()); + } + + private static MockWebServer agent(String containerTagsHash) { + MockWebServer agent = new MockWebServer(); + agent.setDispatcher( + new Dispatcher() { + @Override + public MockResponse dispatch(RecordedRequest request) { + MockResponse response = new MockResponse().setBody("{}"); + return containerTagsHash == null + ? response + : response.setHeader(DATADOG_CONTAINER_TAGS_HASH, containerTagsHash); + } + }); + return agent; + } + + // a new instance each time: discover() skips a run in the same millisecond as the last one + private static void discover(HttpUrl agentUrl) { + new DDAgentFeaturesDiscovery( + new OkHttpClient(), Monitoring.DISABLED, agentUrl, V0_5, true, false) + .discover(); + } +}