Skip to content

Commit 67c06bd

Browse files
authored
[RP Integration] Stop sending requests to all endpoints when ikey is empty (#2061)
* Turn off breeze profiler api when ikey is empty * Stop sending persisted file when ikey is empty * Turn off live metric when ikey is empty * Stop sending perfcounters when ikey is empty * Add nullable to input parameter * Reset url endpoints for ingestion, livemetric and profiler * Fix spotless * Add a comment * Refactor * Set null * Change to private * Add unit test * Fix spotless * Test one more code path * Revert spotless on samplingOverrides * Fix tests * Fix spotless
1 parent f398df4 commit 67c06bd

File tree

9 files changed

+83
-28
lines changed

9 files changed

+83
-28
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/init/RpConfigurationPolling.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import static java.util.concurrent.TimeUnit.SECONDS;
2525

26+
import com.microsoft.applicationinsights.agent.internal.common.Strings;
2627
import com.microsoft.applicationinsights.agent.internal.common.ThreadPoolUtils;
2728
import com.microsoft.applicationinsights.agent.internal.configuration.Configuration;
2829
import com.microsoft.applicationinsights.agent.internal.configuration.ConfigurationBuilder;
@@ -101,7 +102,9 @@ public void run() {
101102
logger.debug(
102103
"Connection string from the JSON config file is overriding the previously configured connection string.");
103104
telemetryClient.setConnectionString(newRpConfiguration.connectionString);
104-
appIdSupplier.startAppIdRetrieval();
105+
if (!Strings.isNullOrEmpty(newRpConfiguration.connectionString)) {
106+
appIdSupplier.startAppIdRetrieval();
107+
}
105108
}
106109

107110
if (newRpConfiguration.sampling.percentage != rpConfiguration.sampling.percentage) {

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/localstorage/LocalFileSender.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@
2121

2222
package com.microsoft.applicationinsights.agent.internal.localstorage;
2323

24+
import com.microsoft.applicationinsights.agent.internal.common.Strings;
2425
import com.microsoft.applicationinsights.agent.internal.common.ThreadPoolUtils;
2526
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryChannel;
27+
import com.microsoft.applicationinsights.agent.internal.telemetry.TelemetryClient;
2628
import io.opentelemetry.sdk.common.CompletableResultCode;
2729
import java.util.concurrent.Executors;
2830
import java.util.concurrent.ScheduledExecutorService;
@@ -58,6 +60,10 @@ private LocalFileSender(LocalFileLoader localFileLoader, TelemetryChannel teleme
5860
public void run() {
5961
// TODO (heya) load all persisted files on disk in one or more batch per batch capacity?
6062
try {
63+
if (Strings.isNullOrEmpty(TelemetryClient.getActive().getInstrumentationKey())) {
64+
return;
65+
}
66+
6167
LocalFileLoader.PersistedFile persistedFile = localFileLoader.loadTelemetriesFromDisk();
6268
if (persistedFile != null) {
6369
CompletableResultCode resultCode =

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/quickpulse/QuickPulsePingSender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public QuickPulsePingSender(
9191

9292
public QuickPulseHeaderInfo ping(String redirectedEndpoint) {
9393
String instrumentationKey = getInstrumentationKey();
94-
if (instrumentationKey == null && "java".equals(System.getenv("FUNCTIONS_WORKER_RUNTIME"))) {
94+
if (Strings.isNullOrEmpty(instrumentationKey)) {
9595
// Quick Pulse Ping uri will be null when the instrumentation key is null. When that happens,
9696
// turn off quick pulse.
9797
return new QuickPulseHeaderInfo(QuickPulseStatus.QP_IS_OFF);

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/telemetry/ConnectionString.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,16 @@ public class ConnectionString {
3939

4040
private ConnectionString() {}
4141

42-
public static void parseInto(String connectionString, TelemetryClient targetConfig)
43-
throws InvalidConnectionStringException {
44-
mapToConnectionConfiguration(getKeyValuePairs(connectionString), targetConfig);
42+
public static void parseInto(String connectionString, TelemetryClient telemetryClient)
43+
throws InvalidConnectionStringException, MalformedURLException {
44+
// reset endpoint to default for ingestion, live metric and profiler
45+
telemetryClient.getEndpointProvider().resetEndpointUrls();
46+
47+
if (Strings.isNullOrEmpty(connectionString)) {
48+
telemetryClient.setInstrumentationKey(null);
49+
} else {
50+
mapToConnectionConfiguration(getKeyValuePairs(connectionString), telemetryClient);
51+
}
4552
}
4653

4754
public static void updateStatsbeatConnectionString(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/telemetry/EndpointProvider.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,7 @@ public class EndpointProvider {
4545

4646
public EndpointProvider() {
4747
try {
48-
ingestionEndpoint = new URL(DefaultEndpoints.INGESTION_ENDPOINT);
49-
ingestionEndpointUrl = buildIngestionUrl(ingestionEndpoint);
50-
liveEndpointUrl = buildLiveUri(new URL(DefaultEndpoints.LIVE_ENDPOINT));
51-
profilerEndpoint = new URL(DefaultEndpoints.PROFILER_ENDPOINT);
48+
resetEndpointUrls();
5249
snapshotEndpoint = new URL(DefaultEndpoints.SNAPSHOT_ENDPOINT);
5350
statsbeatEndpointUrl = buildIngestionUrl(ingestionEndpointUrl);
5451
} catch (MalformedURLException e) {
@@ -76,6 +73,14 @@ public URL getAppIdEndpointUrl(String instrumentationKey) {
7673
return buildAppIdUrl(instrumentationKey);
7774
}
7875

76+
// reset endpoint URLs for ingestion, live metric and profiler
77+
public void resetEndpointUrls() throws MalformedURLException {
78+
ingestionEndpoint = new URL(DefaultEndpoints.INGESTION_ENDPOINT);
79+
ingestionEndpointUrl = buildIngestionUrl(ingestionEndpoint);
80+
liveEndpointUrl = buildLiveUri(new URL(DefaultEndpoints.LIVE_ENDPOINT));
81+
profilerEndpoint = new URL(DefaultEndpoints.PROFILER_ENDPOINT);
82+
}
83+
7984
private URL buildAppIdUrl(String instrumentationKey) {
8085
try {
8186
return buildUrl(

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/telemetry/TelemetryClient.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import io.opentelemetry.instrumentation.api.cache.Cache;
5050
import io.opentelemetry.sdk.common.CompletableResultCode;
5151
import java.io.File;
52+
import java.net.MalformedURLException;
5253
import java.util.ArrayList;
5354
import java.util.HashMap;
5455
import java.util.HashSet;
@@ -74,7 +75,7 @@ public class TelemetryClient {
7475

7576
private final Set<String> nonFilterableMetricNames = new HashSet<>();
7677

77-
private volatile @MonotonicNonNull String instrumentationKey;
78+
@Nullable private volatile String instrumentationKey;
7879
private volatile @MonotonicNonNull String roleName;
7980
private volatile @MonotonicNonNull String roleInstance;
8081
private volatile @MonotonicNonNull String statsbeatInstrumentationKey;
@@ -147,6 +148,10 @@ public static void setActive(TelemetryClient telemetryClient) {
147148
}
148149

149150
public void trackAsync(TelemetryItem telemetry) {
151+
if (Strings.isNullOrEmpty(instrumentationKey)) {
152+
return;
153+
}
154+
150155
MonitorDomain data = telemetry.getData().getBaseData();
151156
if (data instanceof MetricsData) {
152157
MetricsData metricsData = (MetricsData) data;
@@ -305,13 +310,7 @@ public String getInstrumentationKey() {
305310
}
306311

307312
/** Gets or sets the default instrumentation key for the application. */
308-
public void setInstrumentationKey(String key) {
309-
310-
// A non null, non empty instrumentation key is a must
311-
if (Strings.isNullOrEmpty(key)) {
312-
throw new IllegalArgumentException("key");
313-
}
314-
313+
public void setInstrumentationKey(@Nullable String key) {
315314
instrumentationKey = key;
316315
}
317316

@@ -347,6 +346,8 @@ public void setConnectionString(String connectionString) {
347346
ConnectionString.parseInto(connectionString, this);
348347
} catch (InvalidConnectionStringException e) {
349348
throw new IllegalArgumentException("Invalid connection string", e);
349+
} catch (MalformedURLException e) {
350+
throw new IllegalStateException("Invalid endpoint urls.", e);
350351
}
351352
}
352353

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/quickpulse/QuickPulseDataFetcherTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ void endpointChangesWithRedirectHeaderAndGetNewPingInterval() {
8080
headers.put("x-ms-qps-service-endpoint-redirect", "https://new.endpoint.com");
8181
headers.put("x-ms-qps-subscribed", "true");
8282
HttpHeaders httpHeaders = new HttpHeaders(headers);
83+
TelemetryClient telemetryClient = TelemetryClient.createForTest();
84+
telemetryClient.setInstrumentationKey("fake-key");
8385
HttpPipeline httpPipeline =
8486
new HttpPipelineBuilder()
8587
.httpClient(request -> Mono.just(new MockHttpResponse(request, 200, httpHeaders)))
8688
.build();
8789
QuickPulsePingSender quickPulsePingSender =
88-
new QuickPulsePingSender(
89-
httpPipeline, TelemetryClient.createForTest(), "machine1", "instance1", "qpid123");
90-
90+
new QuickPulsePingSender(httpPipeline, telemetryClient, "machine1", "instance1", "qpid123");
9191
QuickPulseHeaderInfo quickPulseHeaderInfo = quickPulsePingSender.ping(null);
9292
assertThat(QuickPulseStatus.QP_IS_ON).isEqualTo(quickPulseHeaderInfo.getQuickPulseStatus());
9393
assertThat(1000).isEqualTo(quickPulseHeaderInfo.getQpsServicePollingInterval());

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/quickpulse/QuickPulsePingSenderTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,14 @@ void endpointChangesWithRedirectHeaderAndGetNewPingInterval() {
7878
headers.put("x-ms-qps-service-endpoint-redirect", "https://new.endpoint.com");
7979
headers.put("x-ms-qps-subscribed", "true");
8080
HttpHeaders httpHeaders = new HttpHeaders(headers);
81+
TelemetryClient telemetryClient = TelemetryClient.createForTest();
82+
telemetryClient.setInstrumentationKey("fake-ikey");
8183
HttpPipeline httpPipeline =
8284
new HttpPipelineBuilder()
8385
.httpClient(request -> Mono.just(new MockHttpResponse(request, 200, httpHeaders)))
8486
.build();
8587
QuickPulsePingSender quickPulsePingSender =
86-
new QuickPulsePingSender(
87-
httpPipeline, TelemetryClient.createForTest(), "machine1", "instance1", "qpid123");
88+
new QuickPulsePingSender(httpPipeline, telemetryClient, "machine1", "instance1", "qpid123");
8889
QuickPulseHeaderInfo quickPulseHeaderInfo = quickPulsePingSender.ping(null);
8990
assertThat(QuickPulseStatus.QP_IS_ON).isEqualTo(quickPulseHeaderInfo.getQuickPulseStatus());
9091
assertThat(1000).isEqualTo(quickPulseHeaderInfo.getQpsServicePollingInterval());

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/telemetry/ConnectionStringParsingTests.java

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -457,12 +457,6 @@ void emptyIkeyValueIsInvalid() {
457457
.isInstanceOf(InvalidConnectionStringException.class);
458458
}
459459

460-
@Test
461-
void emptyStringIsInvalid() {
462-
assertThatThrownBy(() -> ConnectionString.parseInto("", telemetryClient))
463-
.isInstanceOf(InvalidConnectionStringException.class);
464-
}
465-
466460
@Test
467461
void nonKeyValueStringIsInvalid() {
468462
assertThatThrownBy(
@@ -500,4 +494,42 @@ void giantValuesAreNotAllowed() {
500494
.isInstanceOf(InvalidConnectionStringException.class)
501495
.hasMessageContaining(Integer.toString(ConnectionString.CONNECTION_STRING_MAX_LENGTH));
502496
}
497+
498+
@Test
499+
void resetEndpointUrlTest() throws MalformedURLException, InvalidConnectionStringException {
500+
String fakeConnectionString =
501+
"InstrumentationKey=fake-key;IngestionEndpoint=https://ingestion.example.com/;LiveEndpoint=https://live.example.com/";
502+
ConnectionString.parseInto(fakeConnectionString, telemetryClient);
503+
504+
assertThat(telemetryClient.getEndpointProvider().getIngestionEndpointUrl().toString())
505+
.isEqualTo("https://ingestion.example.com/v2.1/track");
506+
assertThat(telemetryClient.getEndpointProvider().getLiveEndpointUrl().toString())
507+
.isEqualTo("https://live.example.com/QuickPulseService.svc");
508+
509+
String emptyConnectionString = "";
510+
ConnectionString.parseInto(emptyConnectionString, telemetryClient);
511+
512+
assertThat(telemetryClient.getEndpointProvider().getIngestionEndpointUrl().toString())
513+
.isEqualTo("https://dc.services.visualstudio.com/v2.1/track");
514+
assertThat(telemetryClient.getEndpointProvider().getLiveEndpointUrl().toString())
515+
.isEqualTo("https://rt.services.visualstudio.com/QuickPulseService.svc");
516+
517+
String newFakeConnectionString =
518+
"InstrumentationKey=new-fake-key;IngestionEndpoint=https://new-ingestion.example.com/;LiveEndpoint=https://new-live.example.com/";
519+
ConnectionString.parseInto(newFakeConnectionString, telemetryClient);
520+
521+
assertThat(telemetryClient.getEndpointProvider().getIngestionEndpointUrl().toString())
522+
.isEqualTo("https://new-ingestion.example.com/v2.1/track");
523+
assertThat(telemetryClient.getEndpointProvider().getLiveEndpointUrl().toString())
524+
.isEqualTo("https://new-live.example.com/QuickPulseService.svc");
525+
526+
String newerFakeConnectionString =
527+
"InstrumentationKey=newer-fake-key;IngestionEndpoint=https://newer-ingestion.example.com/;LiveEndpoint=https://newer-live.example.com/";
528+
ConnectionString.parseInto(newerFakeConnectionString, telemetryClient);
529+
530+
assertThat(telemetryClient.getEndpointProvider().getIngestionEndpointUrl().toString())
531+
.isEqualTo("https://newer-ingestion.example.com/v2.1/track");
532+
assertThat(telemetryClient.getEndpointProvider().getLiveEndpointUrl().toString())
533+
.isEqualTo("https://newer-live.example.com/QuickPulseService.svc");
534+
}
503535
}

0 commit comments

Comments
 (0)