Skip to content

Commit 8c1b947

Browse files
committed
Add timeout on profiler settings pull
1 parent 5fef748 commit 8c1b947

File tree

6 files changed

+37
-11
lines changed

6 files changed

+37
-11
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/configuration/Configuration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1530,6 +1530,7 @@ public static class ProfilerConfiguration {
15301530
public boolean enableDiagnostics = false;
15311531
public boolean enableRequestTriggering = false;
15321532
public List<RequestTrigger> requestTriggerEndpoints = new ArrayList<>();
1533+
public int serviceProfilerHttpTimeoutSeconds = 30;
15331534
}
15341535

15351536
public static class GcEventConfiguration {

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/ProfilingInitializer.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import com.azure.core.http.HttpPipeline;
88
import com.azure.core.http.policy.DefaultRedirectStrategy;
99
import com.azure.core.http.policy.RedirectPolicy;
10+
import com.azure.core.http.policy.TimeoutPolicy;
1011
import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.SystemInformation;
1112
import com.azure.monitor.opentelemetry.autoconfigure.implementation.utils.ThreadPoolUtils;
1213
import com.microsoft.applicationinsights.agent.internal.common.FriendlyException;
@@ -23,6 +24,7 @@
2324
import java.io.File;
2425
import java.net.MalformedURLException;
2526
import java.net.URL;
27+
import java.time.Duration;
2628
import java.util.Arrays;
2729
import java.util.HashSet;
2830
import java.util.concurrent.Executors;
@@ -127,7 +129,8 @@ private synchronized void performInit() {
127129
3,
128130
"Location",
129131
new HashSet<>(
130-
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST)))));
132+
Arrays.asList(HttpMethod.GET, HttpMethod.HEAD, HttpMethod.POST)))),
133+
new TimeoutPolicy(Duration.ofSeconds(configuration.serviceProfilerHttpTimeoutSeconds)));
131134

132135
serviceProfilerExecutorService =
133136
Executors.newScheduledThreadPool(
@@ -140,7 +143,8 @@ private synchronized void performInit() {
140143
getServiceProfilerFrontEndPoint(configuration),
141144
telemetryClient.getInstrumentationKey(),
142145
httpPipeline,
143-
userAgent);
146+
userAgent,
147+
configuration.serviceProfilerHttpTimeoutSeconds);
144148

145149
// Monitor service remains alive permanently due to scheduling an periodic config pull
146150
startPollingForConfigUpdates();

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/profiler/service/ServiceProfilerClient.java

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import java.io.IOException;
1616
import java.net.MalformedURLException;
1717
import java.net.URL;
18+
import java.time.Duration;
1819
import java.util.Date;
1920
import java.util.UUID;
2021
import javax.annotation.Nullable;
@@ -40,21 +41,25 @@ public class ServiceProfilerClient {
4041
private final URL hostUrl;
4142
private final String instrumentationKey;
4243
private final HttpPipeline httpPipeline;
44+
private final int httpTimeoutSeconds;
4345
@Nullable private final String userAgent;
4446

4547
public ServiceProfilerClient(
4648
URL hostUrl,
4749
String instrumentationKey,
4850
HttpPipeline httpPipeline,
49-
@Nullable String userAgent) {
51+
@Nullable String userAgent,
52+
int httpTimeoutSeconds) {
5053
this.hostUrl = hostUrl;
5154
this.instrumentationKey = instrumentationKey;
5255
this.httpPipeline = httpPipeline;
5356
this.userAgent = userAgent;
57+
this.httpTimeoutSeconds = httpTimeoutSeconds;
5458
}
5559

56-
public ServiceProfilerClient(URL hostUrl, String instrumentationKey, HttpPipeline httpPipeline) {
57-
this(hostUrl, instrumentationKey, httpPipeline, null);
60+
public ServiceProfilerClient(
61+
URL hostUrl, String instrumentationKey, HttpPipeline httpPipeline, int httpTimeoutSeconds) {
62+
this(hostUrl, instrumentationKey, httpPipeline, null, httpTimeoutSeconds);
5863
}
5964

6065
/** Obtain permission to upload a profile to service profiler. */
@@ -144,7 +149,18 @@ public Mono<ProfilerConfiguration> getSettings(Date oldTimeStamp) {
144149

145150
HttpRequest request = new HttpRequest(HttpMethod.GET, requestUrl);
146151

147-
return httpPipeline.send(request).flatMap(response -> handle(response, requestUrl));
152+
return httpPipeline
153+
.send(request)
154+
.timeout(
155+
Duration.ofSeconds(httpTimeoutSeconds),
156+
Mono.error(
157+
new HttpResponseException(
158+
"Timed out after "
159+
+ httpTimeoutSeconds
160+
+ " seconds while waiting for response from "
161+
+ requestUrl,
162+
null)))
163+
.flatMap(response -> handle(response, requestUrl));
148164
}
149165

150166
private static Mono<ProfilerConfiguration> handle(HttpResponse response, URL requestUrl) {

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/config/ConfigServiceTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ void pullSettings() throws IOException, InterruptedException {
2727
new ServiceProfilerClient(
2828
new URL("https://agent.azureserviceprofiler.net/"),
2929
"00000000-0000-0000-0000-000000000000",
30-
getHttpPipeline());
30+
getHttpPipeline(),
31+
30);
3132

3233
ConfigService configService = new ConfigService(serviceProfilerClient);
3334

@@ -56,7 +57,8 @@ void badServiceResponseDoesNotProvideReturn() throws MalformedURLException {
5657
new ServiceProfilerClient(
5758
new URL("https://agent.azureserviceprofiler.net/"),
5859
"00000000-0000-0000-0000-000000000000",
59-
getHttpPipeline());
60+
getHttpPipeline(),
61+
30);
6062

6163
ConfigService configService = new ConfigService(serviceProfilerClient);
6264
Mono<ProfilerConfiguration> result = configService.pullSettings();

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/upload/UploadServiceSimpleTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ void roleNameIsCorrectlyAddedToMetaData() throws IOException {
3232
new ServiceProfilerClient(
3333
new URL("https://agent.azureserviceprofiler.net/"),
3434
"00000000-0000-0000-0000-000000000000",
35-
httpPipeline);
35+
httpPipeline,
36+
30);
3637

3738
File tmpFile = createFakeJfrFile();
3839
UUID appId = UUID.randomUUID();
@@ -93,7 +94,8 @@ void uploadWithoutFileThrows() throws MalformedURLException {
9394
new ServiceProfilerClient(
9495
new URL("https://agent.azureserviceprofiler.net/"),
9596
"00000000-0000-0000-0000-000000000000",
96-
httpPipeline);
97+
httpPipeline,
98+
30);
9799

98100
UUID appId = UUID.randomUUID();
99101
UUID profileId = UUID.randomUUID();

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/profiler/upload/UploadServiceTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ void uploadFileGoodPathReturnsExpectedResponse() throws IOException {
3030
new ServiceProfilerClient(
3131
new URL("https://agent.azureserviceprofiler.net/"),
3232
"00000000-0000-0000-0000-000000000000",
33-
httpPipeline);
33+
httpPipeline,
34+
30);
3435

3536
File tmpFile = createFakeJfrFile();
3637
UUID appId = UUID.randomUUID();

0 commit comments

Comments
 (0)