Skip to content

Commit

Permalink
Merge pull request #982 from microsoft/buildme/live-metrics-ikey
Browse files Browse the repository at this point in the history
Fix live metrics to use ikey when it is set programmatically
  • Loading branch information
trask authored Jul 18, 2019
2 parents b34eb8b + 18dadad commit ebdfaf5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<HttpPost> 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<HttpPost> sendQueue, TelemetryConfiguration config,
String instanceName, String quickPulseId) {
this(sendQueue, config, null, instanceName, quickPulseId);
}

@Deprecated
public DefaultQuickPulseDataFetcher(final ArrayBlockingQueue<HttpPost> 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<HttpPost> 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("[{");
Expand Down Expand Up @@ -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);

Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -90,7 +90,7 @@ public void initialize() {

SDKShutdownActivity.INSTANCE.register(this);

QuickPulseDataCollector.INSTANCE.enable(ikey);
QuickPulseDataCollector.INSTANCE.enable(config);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,6 +44,7 @@ public enum QuickPulseDataCollector {
INSTANCE;

private String ikey;
private TelemetryConfiguration config;

static class FinalCounters {
public final double exceptions;
Expand Down Expand Up @@ -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());
}

Expand All @@ -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;
}

Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand Down

0 comments on commit ebdfaf5

Please sign in to comment.