-
Notifications
You must be signed in to change notification settings - Fork 339
Expand file tree
/
Copy pathDDAgentFeaturesDiscovery.java
More file actions
441 lines (377 loc) · 14.5 KB
/
DDAgentFeaturesDiscovery.java
File metadata and controls
441 lines (377 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
package datadog.communication.ddagent;
import static datadog.communication.http.OkHttpUtils.DATADOG_CONTAINER_ID;
import static datadog.communication.http.OkHttpUtils.DATADOG_CONTAINER_TAGS_HASH;
import static datadog.communication.serialization.msgpack.MsgPackWriter.FIXARRAY;
import static java.util.Collections.emptySet;
import static java.util.Collections.singletonList;
import static java.util.Collections.unmodifiableSet;
import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.Moshi;
import com.squareup.moshi.Types;
import datadog.common.container.ContainerInfo;
import datadog.communication.http.OkHttpUtils;
import datadog.communication.monitor.DDAgentStatsDClientManager;
import datadog.communication.monitor.Monitoring;
import datadog.communication.monitor.Recording;
import datadog.config.util.Strings;
import datadog.trace.api.telemetry.LogCollector;
import java.nio.ByteBuffer;
import java.security.NoSuchAlgorithmException;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DDAgentFeaturesDiscovery implements DroppingPolicy {
private static final Logger log = LoggerFactory.getLogger(DDAgentFeaturesDiscovery.class);
private static final JsonAdapter<Map<String, Object>> RESPONSE_ADAPTER =
new Moshi.Builder()
.build()
.adapter(Types.newParameterizedType(Map.class, String.class, Object.class));
// Currently all the endpoints that we probe expect a msgpack body of an array of arrays, v3/v4
// arbitrary size and v5 two elements, so let's give them a two element array of empty arrays
private static final byte[] PROBE_MESSAGE = {
(byte) FIXARRAY | 2, (byte) FIXARRAY, (byte) FIXARRAY
};
public static final String V3_ENDPOINT = "v0.3/traces";
public static final String V4_ENDPOINT = "v0.4/traces";
public static final String V5_ENDPOINT = "v0.5/traces";
public static final String V6_METRICS_ENDPOINT = "v0.6/stats";
public static final String V7_CONFIG_ENDPOINT = "v0.7/config";
public static final String V01_DATASTREAMS_ENDPOINT = "v0.1/pipeline_stats";
public static final String V2_EVP_PROXY_ENDPOINT = "evp_proxy/v2/";
public static final String V4_EVP_PROXY_ENDPOINT = "evp_proxy/v4/";
public static final String DATADOG_AGENT_STATE = "Datadog-Agent-State";
public static final String DEBUGGER_ENDPOINT = "debugger/v1/input";
public static final String DEBUGGER_DIAGNOSTICS_ENDPOINT = "debugger/v1/diagnostics";
public static final String TELEMETRY_PROXY_ENDPOINT = "telemetry/proxy/";
private static final long MIN_FEATURE_DISCOVERY_INTERVAL_MILLIS = 60 * 1000;
private final OkHttpClient client;
private final HttpUrl agentBaseUrl;
private final Recording discoveryTimer;
private final String[] traceEndpoints;
private final String[] metricsEndpoints = {V6_METRICS_ENDPOINT};
private final String[] configEndpoints = {V7_CONFIG_ENDPOINT};
private final boolean metricsEnabled;
private final String[] dataStreamsEndpoints = {V01_DATASTREAMS_ENDPOINT};
// ordered from most recent to least recent, as the logic will stick with the first one that is
// available
private final String[] evpProxyEndpoints = {V4_EVP_PROXY_ENDPOINT, V2_EVP_PROXY_ENDPOINT};
private final String[] telemetryProxyEndpoints = {TELEMETRY_PROXY_ENDPOINT};
private volatile String traceEndpoint;
private volatile String metricsEndpoint;
private volatile String dataStreamsEndpoint;
private volatile boolean supportsLongRunning;
private volatile boolean supportsDropping;
private volatile String state;
private volatile String configEndpoint;
private volatile String debuggerEndpoint;
private volatile String debuggerDiagnosticsEndpoint;
private volatile String evpProxyEndpoint;
private volatile String version;
private volatile String telemetryProxyEndpoint;
private volatile Set<String> peerTags = emptySet();
private volatile Set<String> spanKindsToComputedStats = emptySet();
private long lastTimeDiscovered;
public DDAgentFeaturesDiscovery(
OkHttpClient client,
Monitoring monitoring,
HttpUrl agentUrl,
boolean enableV05Traces,
boolean metricsEnabled) {
this.client = client;
this.agentBaseUrl = agentUrl;
this.metricsEnabled = metricsEnabled;
this.traceEndpoints =
enableV05Traces
? new String[] {V5_ENDPOINT, V4_ENDPOINT, V3_ENDPOINT}
: new String[] {V4_ENDPOINT, V3_ENDPOINT};
this.discoveryTimer = monitoring.newTimer("trace.agent.discovery.time");
}
private void reset() {
traceEndpoint = null;
metricsEndpoint = null;
supportsDropping = false;
supportsLongRunning = false;
state = null;
configEndpoint = null;
debuggerEndpoint = null;
debuggerDiagnosticsEndpoint = null;
dataStreamsEndpoint = null;
evpProxyEndpoint = null;
version = null;
lastTimeDiscovered = 0;
telemetryProxyEndpoint = null;
peerTags = emptySet();
spanKindsToComputedStats = emptySet();
}
/** Run feature discovery, unconditionally. */
public void discover() {
discoverIfOutdated(0);
}
/** Run feature discovery, if it was not run recently. */
public void discoverIfOutdated() {
discoverIfOutdated(getFeaturesDiscoveryMinDelayMillis());
}
protected long getFeaturesDiscoveryMinDelayMillis() {
return MIN_FEATURE_DISCOVERY_INTERVAL_MILLIS;
}
private synchronized void discoverIfOutdated(final long maxElapsedMs) {
final long now = System.currentTimeMillis();
final long elapsed = now - lastTimeDiscovered;
if (elapsed > maxElapsedMs) {
doDiscovery();
lastTimeDiscovered = now;
}
}
private void doDiscovery() {
reset();
// 1. try to fetch info about the agent, if the endpoint is there
// 2. try to parse the response, if it can be parsed, finish
// 3. fallback if the endpoint couldn't be found or the response couldn't be parsed
try (Recording recording = discoveryTimer.start()) {
boolean fallback = true;
final Request.Builder requestBuilder =
new Request.Builder().url(agentBaseUrl.resolve("info").url());
final String containerId = ContainerInfo.get().getContainerId();
if (containerId != null) {
requestBuilder.header(DATADOG_CONTAINER_ID, containerId);
}
try (Response response = client.newCall(requestBuilder.build()).execute()) {
if (response.isSuccessful()) {
processInfoResponseHeaders(response);
fallback = !processInfoResponse(response.body().string());
}
} catch (Throwable error) {
errorQueryingEndpoint("info", error);
}
if (fallback) {
supportsDropping = false;
supportsLongRunning = false;
log.debug("Falling back to probing, client dropping will be disabled");
// disable metrics unless the info endpoint is present, which prevents
// sending metrics to 7.26.0, which has a bug in reporting metric origin
metricsEndpoint = null;
}
// don't want to rewire the traces pipeline
if (null == traceEndpoint) {
traceEndpoint = probeTracesEndpoint(traceEndpoints);
} else if (state == null || state.isEmpty()) {
// Still need to probe so that state is correctly assigned
probeTracesEndpoint(new String[] {traceEndpoint});
}
}
if (log.isDebugEnabled()) {
log.debug(
"discovered traceEndpoint={}, metricsEndpoint={}, supportsDropping={}, supportsLongRunning={}, dataStreamsEndpoint={}, configEndpoint={}, evpProxyEndpoint={}, telemetryProxyEndpoint={}",
traceEndpoint,
metricsEndpoint,
supportsDropping,
supportsLongRunning,
dataStreamsEndpoint,
configEndpoint,
evpProxyEndpoint,
telemetryProxyEndpoint);
}
}
private String probeTracesEndpoint(String[] endpoints) {
for (String candidate : endpoints) {
try (Response response =
client
.newCall(
new Request.Builder()
.put(
OkHttpUtils.msgpackRequestBodyOf(
singletonList(ByteBuffer.wrap(PROBE_MESSAGE))))
.url(agentBaseUrl.resolve(candidate))
.build())
.execute()) {
if (response.code() != 404) {
state = response.header(DATADOG_AGENT_STATE);
return candidate;
}
} catch (Throwable e) {
errorQueryingEndpoint(candidate, e);
}
}
return V3_ENDPOINT;
}
private void processInfoResponseHeaders(Response response) {
ContainerInfo.get().setContainerTagsHash(response.header(DATADOG_CONTAINER_TAGS_HASH));
}
@SuppressWarnings("unchecked")
private boolean processInfoResponse(String response) {
try {
Map<String, Object> map = RESPONSE_ADAPTER.fromJson(response);
discoverStatsDPort(map);
version = (String) map.get("version");
Set<String> endpoints = new HashSet<>((List<String>) map.get("endpoints"));
String foundMetricsEndpoint = null;
if (metricsEnabled) {
for (String endpoint : metricsEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
foundMetricsEndpoint = endpoint;
break;
}
}
}
// This is done outside of the loop to set metricsEndpoint to null if not found
metricsEndpoint = foundMetricsEndpoint;
for (String endpoint : traceEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
traceEndpoint = endpoint;
break;
}
}
for (String endpoint : configEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
configEndpoint = endpoint;
break;
}
}
if (containsEndpoint(endpoints, DEBUGGER_ENDPOINT)) {
debuggerEndpoint = DEBUGGER_ENDPOINT;
}
if (containsEndpoint(endpoints, DEBUGGER_DIAGNOSTICS_ENDPOINT)) {
debuggerDiagnosticsEndpoint = DEBUGGER_DIAGNOSTICS_ENDPOINT;
}
for (String endpoint : dataStreamsEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
dataStreamsEndpoint = endpoint;
break;
}
}
for (String endpoint : evpProxyEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
evpProxyEndpoint = endpoint;
break;
}
}
for (String endpoint : telemetryProxyEndpoints) {
if (containsEndpoint(endpoints, endpoint)) {
telemetryProxyEndpoint = endpoint;
break;
}
}
supportsLongRunning = Boolean.TRUE.equals(map.getOrDefault("long_running_spans", false));
if (metricsEnabled) {
Object canDrop = map.get("client_drop_p0s");
supportsDropping =
null != canDrop
&& ("true".equalsIgnoreCase(String.valueOf(canDrop))
|| Boolean.TRUE.equals(canDrop));
Object peer_tags = map.get("peer_tags");
peerTags =
peer_tags instanceof List
? unmodifiableSet(new HashSet<>((List<String>) peer_tags))
: emptySet();
Object span_kinds = map.get("span_kinds_stats_computed");
spanKindsToComputedStats =
span_kinds instanceof List
? unmodifiableSet(new HashSet<>((List<String>) span_kinds))
: emptySet();
}
try {
state = Strings.sha256(response);
} catch (NoSuchAlgorithmException ex) {
log.debug("Failed to hash trace agent /info response. Will probe {}", traceEndpoint, ex);
}
return true;
} catch (Throwable error) {
log.debug("Error parsing trace agent /info response", error);
}
return false;
}
private static boolean containsEndpoint(Set<String> endpoints, String endpoint) {
return endpoints.contains(endpoint) || endpoints.contains("/" + endpoint);
}
@SuppressWarnings("unchecked")
private static void discoverStatsDPort(final Map<String, Object> info) {
try {
Map<String, ?> config = (Map<String, ?>) info.get("config");
if (config == null) {
log.debug("config missing from trace agent /info response");
return;
}
final Object statsdPortObj = config.get("statsd_port");
if (statsdPortObj == null) {
log.debug("statsd_port missing from trace agent /info response");
return;
}
int statsdPort = ((Number) statsdPortObj).intValue();
DDAgentStatsDClientManager.setDefaultStatsDPort(statsdPort);
} catch (Throwable ex) {
log.debug("statsd_port missing from trace agent /info response", ex);
}
}
public boolean supportsMetrics() {
return metricsEnabled && null != metricsEndpoint;
}
public boolean supportsDebugger() {
return debuggerEndpoint != null;
}
public boolean supportsDebuggerDiagnostics() {
return debuggerDiagnosticsEndpoint != null;
}
boolean supportsDropping() {
return supportsDropping;
}
public boolean supportsLongRunning() {
return supportsLongRunning;
}
public Set<String> peerTags() {
return peerTags;
}
public Set<String> spanKindsToComputedStats() {
return spanKindsToComputedStats;
}
public String getMetricsEndpoint() {
return metricsEndpoint;
}
public String getTraceEndpoint() {
return traceEndpoint;
}
public String getDataStreamsEndpoint() {
return dataStreamsEndpoint;
}
public String getEvpProxyEndpoint() {
return evpProxyEndpoint;
}
public HttpUrl buildUrl(String endpoint) {
return agentBaseUrl.resolve(endpoint);
}
public boolean supportsDataStreams() {
return dataStreamsEndpoint != null;
}
public boolean supportsEvpProxy() {
return evpProxyEndpoint != null;
}
public boolean supportsContentEncodingHeadersWithEvpProxy() {
// content encoding headers are supported in /v4 and above
return evpProxyEndpoint != null && V4_EVP_PROXY_ENDPOINT.compareTo(evpProxyEndpoint) <= 0;
}
public String getConfigEndpoint() {
return configEndpoint;
}
public String getVersion() {
return version;
}
private void errorQueryingEndpoint(String endpoint, Throwable t) {
log.debug(LogCollector.EXCLUDE_TELEMETRY, "Error querying {} at {}", endpoint, agentBaseUrl, t);
}
public String state() {
return state;
}
@Override
public boolean active() {
return supportsMetrics() && supportsDropping;
}
public boolean supportsTelemetryProxy() {
return telemetryProxyEndpoint != null;
}
}