1
1
package datadog.trace.common.writer
2
2
3
+ import static datadog.trace.api.config.TracerConfig.PRIORITIZATION_TYPE
4
+
3
5
import datadog.communication.ddagent.DDAgentFeaturesDiscovery
4
6
import datadog.communication.ddagent.SharedCommunicationObjects
5
7
import datadog.trace.api.Config
@@ -10,10 +12,16 @@ import datadog.trace.common.writer.ddintake.DDEvpProxyApi
10
12
import datadog.trace.common.writer.ddintake.DDIntakeApi
11
13
import datadog.trace.core.monitor.HealthMetrics
12
14
import datadog.trace.test.util.DDSpecification
13
-
15
+ import groovy.json.JsonBuilder
14
16
import java.util.stream.Collectors
15
-
16
- import static datadog.trace.api.config.TracerConfig.PRIORITIZATION_TYPE
17
+ import okhttp3.Call
18
+ import okhttp3.HttpUrl
19
+ import okhttp3.MediaType
20
+ import okhttp3.OkHttpClient
21
+ import okhttp3.Protocol
22
+ import okhttp3.Request
23
+ import okhttp3.Response
24
+ import okhttp3.ResponseBody
17
25
18
26
class WriterFactoryTest extends DDSpecification {
19
27
@@ -27,19 +35,30 @@ class WriterFactoryTest extends DDSpecification {
27
35
config. isCiVisibilityEnabled() >> true
28
36
config. isCiVisibilityCodeCoverageEnabled() >> false
29
37
30
- def agentFeaturesDiscovery = Mock (DDAgentFeaturesDiscovery )
31
- agentFeaturesDiscovery. getEvpProxyEndpoint() >> DDAgentFeaturesDiscovery . V2_EVP_PROXY_ENDPOINT
32
- agentFeaturesDiscovery. supportsContentEncodingHeadersWithEvpProxy() >> evpProxySupportsCompression
38
+ // Mock agent info response
39
+ def response = buildHttpResponse(hasEvpProxy, evpProxySupportsCompression, HttpUrl . parse(config. agentUrl + " /info" ))
40
+
41
+ // Mock HTTP client that simulates delayed response for async feature discovery
42
+ def mockCall = Mock (Call )
43
+ def mockHttpClient = Mock (OkHttpClient )
44
+ mockCall. execute() >> {
45
+ // Add a delay
46
+ sleep(400 )
47
+ return response
48
+ }
49
+ mockHttpClient. newCall(_ as Request ) >> mockCall
33
50
51
+ // Create SharedCommunicationObjects with mocked HTTP client
34
52
def sharedComm = new SharedCommunicationObjects ()
35
- sharedComm. setFeaturesDiscovery(agentFeaturesDiscovery)
53
+ sharedComm. okHttpClient = mockHttpClient
54
+ sharedComm. agentUrl = HttpUrl . parse(config. agentUrl)
36
55
sharedComm. createRemaining(config)
37
56
38
57
def sampler = Mock (Sampler )
39
58
40
59
when :
41
- agentFeaturesDiscovery. supportsEvpProxy() >> hasEvpProxy
42
60
config. ciVisibilityAgentlessEnabled >> isCiVisibilityAgentlessEnabled
61
+
43
62
def writer = WriterFactory . createWriter(config, sharedComm, sampler, null , HealthMetrics . NO_OP , configuredType)
44
63
45
64
def apis
@@ -77,4 +96,28 @@ class WriterFactoryTest extends DDSpecification {
77
96
" not-found" | false | false | true | DDIntakeWriter | [DDIntakeApi ] | true
78
97
" not-found" | false | false | false | DDAgentWriter | [DDAgentApi ] | false
79
98
}
99
+
100
+ Response buildHttpResponse (boolean hasEvpProxy , boolean evpProxySupportsCompression , HttpUrl agentUrl ) {
101
+ def endpoints = []
102
+ if (hasEvpProxy && evpProxySupportsCompression) {
103
+ endpoints = [DDAgentFeaturesDiscovery . V4_EVP_PROXY_ENDPOINT ]
104
+ } else if (hasEvpProxy) {
105
+ endpoints = [DDAgentFeaturesDiscovery . V2_EVP_PROXY_ENDPOINT ]
106
+ } else {
107
+ endpoints = [DDAgentFeaturesDiscovery . V4_ENDPOINT ]
108
+ }
109
+
110
+ def response = [
111
+ " version" : " 7.40.0" ,
112
+ " endpoints" : endpoints,
113
+ ]
114
+
115
+ def builder = new Response.Builder ()
116
+ .code(200 )
117
+ .message(" OK" )
118
+ .protocol(Protocol . HTTP_1_1 )
119
+ .request(new Request.Builder (). url(agentUrl. resolve(" /info" )). build())
120
+ .body(ResponseBody . create(MediaType . parse(" application/json" ), new JsonBuilder (response). toString()))
121
+ return builder. build()
122
+ }
80
123
}
0 commit comments