@@ -97,16 +97,198 @@ private static class State {
9797 String telemetryProxyEndpoint ;
9898 Set <String > peerTags = emptySet ();
9999 long lastTimeDiscovered ;
100+
101+ public String getTraceEndpoint () {
102+ return traceEndpoint ;
103+ }
104+
105+ public String getMetricsEndpoint () {
106+ return metricsEndpoint ;
107+ }
108+
109+ public String getDataStreamsEndpoint () {
110+ return dataStreamsEndpoint ;
111+ }
112+
113+ public boolean isSupportsLongRunning () {
114+ return supportsLongRunning ;
115+ }
116+
117+ public boolean isSupportsDropping () {
118+ return supportsDropping ;
119+ }
120+
121+ public String getState () {
122+ return state ;
123+ }
124+
125+ public String getConfigEndpoint () {
126+ return configEndpoint ;
127+ }
128+
129+ public String getDebuggerLogEndpoint () {
130+ return debuggerLogEndpoint ;
131+ }
132+
133+ public String getDebuggerSnapshotEndpoint () {
134+ return debuggerSnapshotEndpoint ;
135+ }
136+
137+ public String getDebuggerDiagnosticsEndpoint () {
138+ return debuggerDiagnosticsEndpoint ;
139+ }
140+
141+ public String getEvpProxyEndpoint () {
142+ return evpProxyEndpoint ;
143+ }
144+
145+ public String getVersion () {
146+ return version ;
147+ }
148+
149+ public String getTelemetryProxyEndpoint () {
150+ return telemetryProxyEndpoint ;
151+ }
152+
153+ public Set <String > getPeerTags () {
154+ return peerTags ;
155+ }
156+
157+ public long getLastTimeDiscovered () {
158+ return lastTimeDiscovered ;
159+ }
160+
161+ void allowIO () {}
162+ }
163+
164+ private class UndiscoveredState extends State {
165+ private boolean ioAllowed ;
166+
167+ public UndiscoveredState (boolean ioAllowed ) {
168+ this .ioAllowed = ioAllowed ;
169+ }
170+
171+ @ Override
172+ void allowIO () {
173+ this .ioAllowed = true ;
174+ }
175+
176+ private void doDiscovery () {
177+ if (ioAllowed ) {
178+ DDAgentFeaturesDiscovery .this .discoverIfOutdated ();
179+ }
180+ }
181+
182+ @ Override
183+ public String getTraceEndpoint () {
184+ doDiscovery ();
185+ return DDAgentFeaturesDiscovery .this .discoveryState .getTraceEndpoint ();
186+ }
187+
188+ @ Override
189+ public String getMetricsEndpoint () {
190+ doDiscovery ();
191+ return DDAgentFeaturesDiscovery .this .discoveryState .getMetricsEndpoint ();
192+ }
193+
194+ @ Override
195+ public String getDataStreamsEndpoint () {
196+ doDiscovery ();
197+ return DDAgentFeaturesDiscovery .this .discoveryState .getDataStreamsEndpoint ();
198+ }
199+
200+ @ Override
201+ public boolean isSupportsLongRunning () {
202+ doDiscovery ();
203+ return DDAgentFeaturesDiscovery .this .discoveryState .isSupportsLongRunning ();
204+ }
205+
206+ @ Override
207+ public boolean isSupportsDropping () {
208+ doDiscovery ();
209+ return DDAgentFeaturesDiscovery .this .discoveryState .isSupportsDropping ();
210+ }
211+
212+ @ Override
213+ public String getState () {
214+ doDiscovery ();
215+ return DDAgentFeaturesDiscovery .this .discoveryState .getState ();
216+ }
217+
218+ @ Override
219+ public String getConfigEndpoint () {
220+ doDiscovery ();
221+ return DDAgentFeaturesDiscovery .this .discoveryState .getConfigEndpoint ();
222+ }
223+
224+ @ Override
225+ public String getDebuggerLogEndpoint () {
226+ doDiscovery ();
227+ return DDAgentFeaturesDiscovery .this .discoveryState .getDebuggerLogEndpoint ();
228+ }
229+
230+ @ Override
231+ public String getDebuggerSnapshotEndpoint () {
232+ doDiscovery ();
233+ return DDAgentFeaturesDiscovery .this .discoveryState .getDebuggerSnapshotEndpoint ();
234+ }
235+
236+ @ Override
237+ public String getDebuggerDiagnosticsEndpoint () {
238+ doDiscovery ();
239+ return DDAgentFeaturesDiscovery .this .discoveryState .getDebuggerDiagnosticsEndpoint ();
240+ }
241+
242+ @ Override
243+ public String getEvpProxyEndpoint () {
244+ doDiscovery ();
245+ return DDAgentFeaturesDiscovery .this .discoveryState .getEvpProxyEndpoint ();
246+ }
247+
248+ @ Override
249+ public String getVersion () {
250+ doDiscovery ();
251+ return DDAgentFeaturesDiscovery .this .discoveryState .getVersion ();
252+ }
253+
254+ @ Override
255+ public String getTelemetryProxyEndpoint () {
256+ doDiscovery ();
257+ return DDAgentFeaturesDiscovery .this .discoveryState .getTelemetryProxyEndpoint ();
258+ }
259+
260+ @ Override
261+ public Set <String > getPeerTags () {
262+ doDiscovery ();
263+ return DDAgentFeaturesDiscovery .this .discoveryState .getPeerTags ();
264+ }
265+
266+ @ Override
267+ public long getLastTimeDiscovered () {
268+ doDiscovery ();
269+ return DDAgentFeaturesDiscovery .this .discoveryState .getLastTimeDiscovered ();
270+ }
100271 }
101272
102273 private volatile State discoveryState ;
103274
104- public DDAgentFeaturesDiscovery (
275+ // kept for testing
276+ protected DDAgentFeaturesDiscovery (
105277 OkHttpClient client ,
106278 Monitoring monitoring ,
107279 HttpUrl agentUrl ,
108280 boolean enableV05Traces ,
109281 boolean metricsEnabled ) {
282+ this (client , monitoring , agentUrl , enableV05Traces , metricsEnabled , true );
283+ }
284+
285+ public DDAgentFeaturesDiscovery (
286+ OkHttpClient client ,
287+ Monitoring monitoring ,
288+ HttpUrl agentUrl ,
289+ boolean enableV05Traces ,
290+ boolean metricsEnabled ,
291+ boolean ioAllowed ) {
110292 this .client = client ;
111293 this .agentBaseUrl = agentUrl ;
112294 this .metricsEnabled = metricsEnabled ;
@@ -115,7 +297,7 @@ public DDAgentFeaturesDiscovery(
115297 ? new String [] {V5_ENDPOINT , V4_ENDPOINT , V3_ENDPOINT }
116298 : new String [] {V4_ENDPOINT , V3_ENDPOINT };
117299 this .discoveryTimer = monitoring .newTimer ("trace.agent.discovery.time" );
118- this .discoveryState = new State ( );
300+ this .discoveryState = new UndiscoveredState ( ioAllowed );
119301 }
120302
121303 /** Run feature discovery, unconditionally. */
@@ -145,6 +327,7 @@ private synchronized void discoverIfOutdated(final long maxElapsedMs) {
145327 }
146328
147329 private void doDiscovery (State newState ) {
330+ discoveryState .allowIO ();
148331 // 1. try to fetch info about the agent, if the endpoint is there
149332 // 2. try to parse the response, if it can be parsed, finish
150333 // 3. fallback if the endpoint couldn't be found or the response couldn't be parsed
@@ -356,93 +539,93 @@ private static void discoverStatsDPort(final Map<String, Object> info) {
356539 }
357540
358541 public boolean supportsMetrics () {
359- return metricsEnabled && null != discoveryState .metricsEndpoint ;
542+ return metricsEnabled && null != discoveryState .getMetricsEndpoint () ;
360543 }
361544
362545 public boolean supportsDebugger () {
363- return discoveryState .debuggerLogEndpoint != null ;
546+ return discoveryState .getDebuggerLogEndpoint () != null ;
364547 }
365548
366549 public String getDebuggerSnapshotEndpoint () {
367- return discoveryState .debuggerSnapshotEndpoint ;
550+ return discoveryState .getDebuggerSnapshotEndpoint () ;
368551 }
369552
370553 public String getDebuggerLogEndpoint () {
371- return discoveryState .debuggerLogEndpoint ;
554+ return discoveryState .getDebuggerLogEndpoint () ;
372555 }
373556
374557 public boolean supportsDebuggerDiagnostics () {
375- return discoveryState .debuggerDiagnosticsEndpoint != null ;
558+ return discoveryState .getDebuggerDiagnosticsEndpoint () != null ;
376559 }
377560
378561 public boolean supportsDropping () {
379- return discoveryState .supportsDropping ;
562+ return discoveryState .isSupportsDropping () ;
380563 }
381564
382565 public boolean supportsLongRunning () {
383- return discoveryState .supportsLongRunning ;
566+ return discoveryState .isSupportsLongRunning () ;
384567 }
385568
386569 public Set <String > peerTags () {
387- return discoveryState .peerTags ;
570+ return discoveryState .getPeerTags () ;
388571 }
389572
390573 public String getMetricsEndpoint () {
391- return discoveryState .metricsEndpoint ;
574+ return discoveryState .getMetricsEndpoint () ;
392575 }
393576
394577 public String getTraceEndpoint () {
395- return discoveryState .traceEndpoint ;
578+ return discoveryState .getTraceEndpoint () ;
396579 }
397580
398581 public String getDataStreamsEndpoint () {
399- return discoveryState .dataStreamsEndpoint ;
582+ return discoveryState .getDataStreamsEndpoint () ;
400583 }
401584
402585 public String getEvpProxyEndpoint () {
403- return discoveryState .evpProxyEndpoint ;
586+ return discoveryState .getEvpProxyEndpoint () ;
404587 }
405588
406589 public HttpUrl buildUrl (String endpoint ) {
407590 return agentBaseUrl .resolve (endpoint );
408591 }
409592
410593 public boolean supportsDataStreams () {
411- return discoveryState .dataStreamsEndpoint != null ;
594+ return discoveryState .getDataStreamsEndpoint () != null ;
412595 }
413596
414597 public boolean supportsEvpProxy () {
415- return discoveryState .evpProxyEndpoint != null ;
598+ return discoveryState .getEvpProxyEndpoint () != null ;
416599 }
417600
418601 public boolean supportsContentEncodingHeadersWithEvpProxy () {
419602 // content encoding headers are supported in /v4 and above
420- final String evpProxyEndpoint = discoveryState .evpProxyEndpoint ;
603+ final String evpProxyEndpoint = discoveryState .getEvpProxyEndpoint () ;
421604 return evpProxyEndpoint != null && V4_EVP_PROXY_ENDPOINT .compareTo (evpProxyEndpoint ) <= 0 ;
422605 }
423606
424607 public String getConfigEndpoint () {
425- return discoveryState .configEndpoint ;
608+ return discoveryState .getConfigEndpoint () ;
426609 }
427610
428611 public String getVersion () {
429- return discoveryState .version ;
612+ return discoveryState .getVersion () ;
430613 }
431614
432615 private void errorQueryingEndpoint (String endpoint , Throwable t ) {
433616 log .debug (LogCollector .EXCLUDE_TELEMETRY , "Error querying {} at {}" , endpoint , agentBaseUrl , t );
434617 }
435618
436619 public String state () {
437- return discoveryState .state ;
620+ return discoveryState .getState () ;
438621 }
439622
440623 @ Override
441624 public boolean active () {
442- return supportsMetrics () && discoveryState .supportsDropping ;
625+ return supportsMetrics () && discoveryState .isSupportsLongRunning () ;
443626 }
444627
445628 public boolean supportsTelemetryProxy () {
446- return discoveryState .telemetryProxyEndpoint != null ;
629+ return discoveryState .getTelemetryProxyEndpoint () != null ;
447630 }
448631}
0 commit comments