Skip to content

Commit 8872f5b

Browse files
committed
Improve diagnosability of CDS profile fetch
1 parent ece359d commit 8872f5b

File tree

1 file changed

+12
-26
lines changed

1 file changed

+12
-26
lines changed

core/src/main/java/com/microsoft/applicationinsights/web/internal/correlation/CdsProfileFetcher.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
package com.microsoft.applicationinsights.web.internal.correlation;
2323

24+
import com.google.common.base.Charsets;
2425
import com.microsoft.applicationinsights.TelemetryConfiguration;
2526
import com.microsoft.applicationinsights.internal.util.PeriodicTaskPool;
2627
import com.microsoft.applicationinsights.internal.util.SSLOptionsUtil;
@@ -37,10 +38,10 @@
3738
import org.slf4j.Logger;
3839
import org.slf4j.LoggerFactory;
3940

41+
import java.io.ByteArrayOutputStream;
4042
import java.io.Closeable;
4143
import java.io.IOException;
42-
import java.net.MalformedURLException;
43-
import java.net.URL;
44+
import java.net.URI;
4445
import java.util.concurrent.ConcurrentHashMap;
4546
import java.util.concurrent.ConcurrentMap;
4647
import java.util.concurrent.ExecutionException;
@@ -52,7 +53,6 @@ public class CdsProfileFetcher implements ApplicationIdResolver, Closeable {
5253
private static final Logger logger = LoggerFactory.getLogger(CdsProfileFetcher.class);
5354

5455
private CloseableHttpAsyncClient httpClient;
55-
private String endpointAddress = null;
5656
private static final String PROFILE_QUERY_ENDPOINT_APP_ID_FORMAT = "%s/api/profiles/%s/appId";
5757

5858
// cache of tasks per ikey
@@ -143,7 +143,14 @@ private ProfileFetcherResult internalFetchAppProfile(String instrumentationKey,
143143
try {
144144
HttpResponse response = currentTask.get();
145145

146-
if (response.getStatusLine().getStatusCode() != 200) {
146+
int statusCode = response.getStatusLine().getStatusCode();
147+
if (statusCode != 200) {
148+
URI uri = configuration.getEndpointProvider().getAppIdEndpointURL(instrumentationKey);
149+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
150+
response.getEntity().writeTo(baos);
151+
String responseBody = new String(baos.toByteArray(), Charsets.UTF_8);
152+
// logging the URL to make it easy to diagnose the issue (e.g. in case of local firewall/routing issue)
153+
logger.warn("{} returned {}: {}", uri, statusCode, responseBody);
147154
incrementFailureCount(instrumentationKey);
148155
return new ProfileFetcherResult(null, ProfileFetcherResultTaskStatus.FAILED);
149156
}
@@ -171,29 +178,8 @@ void setHttpClient(CloseableHttpAsyncClient client) {
171178
this.httpClient = client;
172179
}
173180

174-
/**
175-
* @deprecated Set endpoints using {@link TelemetryConfiguration#setConnectionString(String)}.
176-
*/
177-
@Deprecated
178-
public void setEndpointAddress(String endpoint) throws MalformedURLException {
179-
// set endpoint address to the base address (e.g. https://dc.services.visualstudio.com)
180-
// later we will append the profile/ikey segment
181-
URL url = new URL(endpoint);
182-
String urlStr = url.toString();
183-
this.endpointAddress = urlStr.substring(0, urlStr.length() - url.getFile().length());
184-
if (logger.isTraceEnabled()) {
185-
logger.trace("{} endpoint override: {}", CdsProfileFetcher.class.getSimpleName(), this.endpointAddress);
186-
}
187-
}
188-
189181
private Future<HttpResponse> createFetchTask(String instrumentationKey, TelemetryConfiguration configuration) {
190-
final HttpGet request;
191-
if (endpointAddress == null) {
192-
request = new HttpGet(configuration.getEndpointProvider().getAppIdEndpointURL(instrumentationKey));
193-
} else {
194-
request = new HttpGet(String.format(PROFILE_QUERY_ENDPOINT_APP_ID_FORMAT, this.endpointAddress, instrumentationKey));
195-
}
196-
182+
final HttpGet request = new HttpGet(configuration.getEndpointProvider().getAppIdEndpointURL(instrumentationKey));
197183
return this.httpClient.execute(request, null);
198184
}
199185

0 commit comments

Comments
 (0)