From 18dadad9c51b6d49c1e612a5be40239e4449e93a Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Tue, 16 Jul 2019 11:15:59 -0700 Subject: [PATCH] Fix live metrics to use ikey set programmatically Closes #969 --- .../DefaultQuickPulseDataFetcher.java | 28 +++++++++++++++++-- .../internal/quickpulse/QuickPulse.java | 6 ++-- .../quickpulse/QuickPulseDataCollector.java | 20 ++++++++++++- .../DefaultQuickPulseDataFetcherTests.java | 3 +- 4 files changed, 49 insertions(+), 8 deletions(-) diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcher.java b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcher.java index bb8a3626e0c..853125618a7 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcher.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcher.java @@ -24,6 +24,7 @@ import java.util.Date; import java.util.concurrent.ArrayBlockingQueue; +import com.microsoft.applicationinsights.TelemetryConfiguration; import com.microsoft.applicationinsights.internal.util.PropertyHelper; import org.apache.commons.lang3.exception.ExceptionUtils; import org.apache.http.client.methods.HttpPost; @@ -36,15 +37,28 @@ */ final class DefaultQuickPulseDataFetcher implements QuickPulseDataFetcher { private final static String QP_BASE_URI = "https://rt.services.visualstudio.com/QuickPulseService.svc/"; - private final String quickPulsePostUri; private final ArrayBlockingQueue sendQueue; + private final TelemetryConfiguration config; + private final String ikey; private final QuickPulseNetworkHelper networkHelper = new QuickPulseNetworkHelper(); private String postPrefix; private final String sdkVersion; + public DefaultQuickPulseDataFetcher(ArrayBlockingQueue sendQueue, TelemetryConfiguration config, + String instanceName, String quickPulseId) { + this(sendQueue, config, null, instanceName, quickPulseId); + } + + @Deprecated public DefaultQuickPulseDataFetcher(final ArrayBlockingQueue sendQueue, final String ikey, final String instanceName, final String quickPulseId) { - quickPulsePostUri = QP_BASE_URI + "post?ikey=" + ikey; + this(sendQueue, null, ikey, instanceName, quickPulseId); + } + + private DefaultQuickPulseDataFetcher(ArrayBlockingQueue sendQueue, TelemetryConfiguration config, + String ikey, String instanceName, String quickPulseId) { this.sendQueue = sendQueue; + this.config = config; + this.ikey = ikey; sdkVersion = getCurrentSdkVersion(); final StringBuilder sb = new StringBuilder(); sb.append("[{"); @@ -72,7 +86,7 @@ public void prepareQuickPulseDataForSend() { QuickPulseDataCollector.FinalCounters counters = QuickPulseDataCollector.INSTANCE.getAndRestart(); final Date currentDate = new Date(); - final HttpPost request = networkHelper.buildRequest(currentDate, quickPulsePostUri); + final HttpPost request = networkHelper.buildRequest(currentDate, QP_BASE_URI + "post?ikey=" + getInstrumentationKey()); final ByteArrayEntity postEntity = buildPostEntity(counters); @@ -94,6 +108,14 @@ public void prepareQuickPulseDataForSend() { } } + private String getInstrumentationKey() { + if (config != null) { + return config.getInstrumentationKey(); + } else { + return ikey; + } + } + private ByteArrayEntity buildPostEntity(QuickPulseDataCollector.FinalCounters counters) { StringBuilder sb = new StringBuilder(postPrefix); formatMetrics(counters, sb); diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulse.java b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulse.java index d8f201291a3..5abe00cca49 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulse.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulse.java @@ -66,10 +66,10 @@ public void initialize() { instanceName = "Unknown host"; } - final String ikey = TelemetryConfiguration.getActive().getInstrumentationKey(); + final TelemetryConfiguration config = TelemetryConfiguration.getActive(); final QuickPulsePingSender quickPulsePingSender = new DefaultQuickPulsePingSender(apacheSender, instanceName, quickPulseId); - final QuickPulseDataFetcher quickPulseDataFetcher = new DefaultQuickPulseDataFetcher(sendQueue, ikey, instanceName, quickPulseId); + final QuickPulseDataFetcher quickPulseDataFetcher = new DefaultQuickPulseDataFetcher(sendQueue, config, instanceName, quickPulseId); final QuickPulseCoordinatorInitData coordinatorInitData = new QuickPulseCoordinatorInitDataBuilder() @@ -90,7 +90,7 @@ public void initialize() { SDKShutdownActivity.INSTANCE.register(this); - QuickPulseDataCollector.INSTANCE.enable(ikey); + QuickPulseDataCollector.INSTANCE.enable(config); } } } diff --git a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulseDataCollector.java b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulseDataCollector.java index 35564faac58..92f554e7e73 100644 --- a/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulseDataCollector.java +++ b/core/src/main/java/com/microsoft/applicationinsights/internal/quickpulse/QuickPulseDataCollector.java @@ -28,6 +28,7 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; +import com.microsoft.applicationinsights.TelemetryConfiguration; import com.microsoft.applicationinsights.internal.logger.InternalLogger; import com.microsoft.applicationinsights.internal.perfcounter.CpuPerformanceCounterCalculator; import com.microsoft.applicationinsights.telemetry.ExceptionTelemetry; @@ -43,6 +44,7 @@ public enum QuickPulseDataCollector { INSTANCE; private String ikey; + private TelemetryConfiguration config; static class FinalCounters { public final double exceptions; @@ -147,8 +149,16 @@ public synchronized void disable() { counters.set(null); } + @Deprecated public synchronized void enable(final String ikey) { this.ikey = ikey; + this.config = null; + counters.set(new Counters()); + } + + public synchronized void enable(TelemetryConfiguration config) { + this.config = config; + this.ikey = null; counters.set(new Counters()); } @@ -171,7 +181,7 @@ synchronized FinalCounters peek() { } public void add(Telemetry telemetry) { - if (!telemetry.getContext().getInstrumentationKey().equals(ikey)) { + if (!telemetry.getContext().getInstrumentationKey().equals(getInstrumentationKey())) { return; } @@ -185,6 +195,14 @@ public void add(Telemetry telemetry) { } } + private String getInstrumentationKey() { + if (config != null) { + return config.getInstrumentationKey(); + } else { + return ikey; + } + } + private void addDependency(RemoteDependencyTelemetry telemetry) { Counters counters = this.counters.get(); if (counters == null) { diff --git a/core/src/test/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcherTests.java b/core/src/test/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcherTests.java index a97686b3812..7d428df60b7 100644 --- a/core/src/test/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcherTests.java +++ b/core/src/test/java/com/microsoft/applicationinsights/internal/quickpulse/DefaultQuickPulseDataFetcherTests.java @@ -1,5 +1,6 @@ package com.microsoft.applicationinsights.internal.quickpulse; +import com.microsoft.applicationinsights.TelemetryConfiguration; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; @@ -10,7 +11,7 @@ public class DefaultQuickPulseDataFetcherTests { @Test public void testGetCurrentSdkVersion() { - DefaultQuickPulseDataFetcher dataFetcher = new DefaultQuickPulseDataFetcher(null, null, + DefaultQuickPulseDataFetcher dataFetcher = new DefaultQuickPulseDataFetcher(null, (TelemetryConfiguration) null, null, null); String sdkVersion = dataFetcher.getCurrentSdkVersion(); Assert.assertNotNull(sdkVersion);