Skip to content

Commit c3d5eac

Browse files
authored
removed usage of -1 as a default value for some measurements (#651)
* removed usage of -1 as a default value for measurements. This just causes confusion when seen in the cloud. If you can't measure something, don't report it as some garbage value * fix log message; instead of waiting for N counters, just wait for each kind individually * added support to delay before calling TargetUri. cleaned up test code
1 parent 4e8128e commit c3d5eac

File tree

13 files changed

+165
-161
lines changed

13 files changed

+165
-161
lines changed

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/AbstractUnixPerformanceCounter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ protected AbstractUnixPerformanceCounter(String path) {
4545
this.path = path;
4646
processFile = new File(path);
4747
if (!processFile.canRead()) {
48-
logError("Can not read");
48+
logPerfCounterErrorError("Can not read");
4949
}
5050
}
5151

52-
protected void logError(String format, Object... args) {
52+
protected void logPerfCounterErrorError(String format, Object... args) {
5353
format = "Performance Counter " + getId() + ": Error in file '" + path + "': " + format;
5454
InternalLogger.INSTANCE.error(format, args);
5555
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/Constants.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ public final class Constants {
5151

5252
public final static String PROCESS_CATEGORY = "Process";
5353

54-
public final static double DEFAULT_DOUBLE_VALUE = -1.0;
55-
5654
private Constants() {
5755
}
5856
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/CpuPerformanceCounterCalculator.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public CpuPerformanceCounterCalculator() {
4747

4848
}
4949

50-
public double getProcessCpuUsage() {
51-
double processCpuUsage;
50+
public Double getProcessCpuUsage() {
51+
Double processCpuUsage = null;
5252
try {
5353
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
5454

@@ -58,14 +58,12 @@ public double getProcessCpuUsage() {
5858
if (prevUpTime > 0L && upTime > prevUpTime) {
5959
long elapsedCpu = processCpuTime - prevProcessCpuTime;
6060
long elapsedTime = upTime - prevUpTime;
61-
processCpuUsage = Math.min(99F, elapsedCpu / (elapsedTime * 10000F * numberOfCpus));
62-
} else {
63-
processCpuUsage = Constants.DEFAULT_DOUBLE_VALUE;
61+
processCpuUsage = Math.min(99.999, elapsedCpu / (elapsedTime * 10_000.0 * numberOfCpus)); // if this looks weird, here's another way to write it: (elapsedCpu / 1000000.0) / elapsedTime / numberOfCpus * 100.0
6462
}
6563
prevUpTime = upTime;
6664
prevProcessCpuTime = processCpuTime;
6765
} catch (Exception e) {
68-
processCpuUsage = Constants.DEFAULT_DOUBLE_VALUE;
66+
processCpuUsage = null;
6967
InternalLogger.INSTANCE.error("Error in getProcessCPUUsage");
7068
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
7169
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/JmxPerformanceCounter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ public JmxPerformanceCounter(String categoryName, String counterName, Map<String
5959
Preconditions.checkArgument(!objectToAttributes.isEmpty(), "objectToAttributes should be not be empty");
6060

6161
id = categoryName + "." + counterName;
62-
telemetry = new PerformanceCounterTelemetry(categoryName, counterName, SystemInformation.INSTANCE.getProcessId(), Constants.DEFAULT_DOUBLE_VALUE);
62+
telemetry = new PerformanceCounterTelemetry();
63+
telemetry.setCategoryName(categoryName);
64+
telemetry.setCounterName(counterName);
65+
telemetry.setInstanceName(SystemInformation.INSTANCE.getProcessId());
6366
this.objectToAttributes = objectToAttributes;
6467
}
6568

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/ProcessCpuPerformanceCounter.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ public void report(TelemetryClient telemetryClient) {
6767
if (cpuPerformanceCounterCalculator == null) {
6868
return;
6969
}
70-
double processCpuUsage = cpuPerformanceCounterCalculator.getProcessCpuUsage();
70+
Double processCpuUsage = cpuPerformanceCounterCalculator.getProcessCpuUsage();
71+
if (processCpuUsage == null) {
72+
return;
73+
}
7174

7275
InternalLogger.INSTANCE.trace("Performance Counter: %s %s: %s", getProcessCategoryName(), Constants.CPU_PC_COUNTER_NAME, processCpuUsage);
7376
Telemetry telemetry = new PerformanceCounterTelemetry(

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/UnixProcessIOPerformanceCounter.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,10 @@ public String getId() {
6060
public void report(TelemetryClient telemetryClient) {
6161
long currentCollectionInNanos = System.nanoTime();
6262

63-
double processIO = getCurrentIOForCurrentProcess();
63+
Double processIO = getCurrentIOForCurrentProcess();
64+
if (processIO == null) {
65+
return;
66+
}
6467
if (lastCollectionInNanos != -1) {
6568
// Not the first time
6669

@@ -83,10 +86,14 @@ public void report(TelemetryClient telemetryClient) {
8386
lastCollectionInNanos = currentCollectionInNanos;
8487
}
8588

86-
public double getCurrentIOForCurrentProcess() {
89+
/**
90+
*
91+
* @return the current IO for current process, or null if the datum could not be measured.
92+
*/
93+
public Double getCurrentIOForCurrentProcess() {
8794
BufferedReader bufferedReader = null;
8895

89-
double result = Constants.DEFAULT_DOUBLE_VALUE;
96+
Double result = null;
9097
UnixProcessIOtParser parser = new UnixProcessIOtParser();
9198
try {
9299
bufferedReader = new BufferedReader(new FileReader(getProcessFile()));
@@ -97,15 +104,15 @@ public double getCurrentIOForCurrentProcess() {
97104

98105
result = parser.getValue();
99106
} catch (Exception e) {
100-
result = Constants.DEFAULT_DOUBLE_VALUE;
101-
logError("Error while parsing file: '%s'", getId(), e.toString());
107+
result = null;
108+
logPerfCounterErrorError("Error while parsing file: '%s'", getId());
102109
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
103110
} finally {
104111
if (bufferedReader != null ) {
105112
try {
106113
bufferedReader.close();
107114
} catch (Exception e) {
108-
logError("Error while closing file : '%s'", e.toString());
115+
logPerfCounterErrorError("Error while closing file : '%s'", e.toString());
109116
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
110117
}
111118
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/UnixTotalCpuPerformanceCounter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ private String getLineOfData() {
9999
bufferedReader = new BufferedReader(new FileReader(getProcessFile()));
100100
line = bufferedReader.readLine();
101101
} catch (Exception e) {
102-
logError("Error while parsing file: '%s'", e.toString());
102+
logPerfCounterErrorError("Error while parsing file: '%s'", e.toString());
103103
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
104104
} finally {
105105
if (bufferedReader != null ) {
106106
try {
107107
bufferedReader.close();
108108
} catch (Exception e) {
109-
logError("Error while closing file : '%s'", e.toString());
109+
logPerfCounterErrorError("Error while closing file : '%s'", e.toString());
110110
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
111111
}
112112
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/UnixTotalMemoryPerformanceCounter.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,10 @@ public String getId() {
4949

5050
@Override
5151
public void report(TelemetryClient telemetryClient) {
52-
double totalAvailableMemory = getTotalAvailableMemory();
52+
Double totalAvailableMemory = getTotalAvailableMemory();
53+
if (totalAvailableMemory == null) {
54+
return;
55+
}
5356

5457
InternalLogger.INSTANCE.trace("Sending Performance Counter: %s %s: %s", Constants.TOTAL_MEMORY_PC_CATEGORY_NAME, Constants.TOTAL_MEMORY_PC_COUNTER_NAME, totalAvailableMemory);
5558
Telemetry telemetry = new PerformanceCounterTelemetry(
@@ -61,10 +64,10 @@ public void report(TelemetryClient telemetryClient) {
6164
telemetryClient.track(telemetry);
6265
}
6366

64-
private double getTotalAvailableMemory() {
67+
private Double getTotalAvailableMemory() {
6568
BufferedReader bufferedReader = null;
6669

67-
double result = Constants.DEFAULT_DOUBLE_VALUE;
70+
Double result = null;
6871
UnixTotalMemInfoParser reader = new UnixTotalMemInfoParser();
6972
try {
7073
bufferedReader = new BufferedReader(new FileReader(getProcessFile()));
@@ -76,14 +79,14 @@ private double getTotalAvailableMemory() {
7679
// The value we get is in KB so we need to translate that to bytes.
7780
result = reader.getValue() * KB;
7881
} catch (Exception e) {
79-
result = Constants.DEFAULT_DOUBLE_VALUE;
80-
logError("Error while parsing file: '%s'", e.toString());
82+
result = null;
83+
logPerfCounterErrorError("Error while parsing file: '%s'", e.toString());
8184
} finally {
8285
if (bufferedReader != null ) {
8386
try {
8487
bufferedReader.close();
8588
} catch (Exception e) {
86-
logError("Error while closing file : '%s'", e.toString());
89+
logPerfCounterErrorError("Error while closing file : '%s'", e.toString());
8790
}
8891
}
8992
}

core/src/main/java/com/microsoft/applicationinsights/internal/perfcounter/WindowsPerformanceCounterAsMetric.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public final class WindowsPerformanceCounterAsMetric extends AbstractWindowsPerf
4242
private static final String ID = Constants.PERFORMANCE_COUNTER_PREFIX + "WindowsPerformanceCounterAsMetric";
4343

4444
private final HashMap<String, String> keyToDisplayName = new HashMap<String, String>();
45-
private final MetricTelemetry telemetry = new MetricTelemetry("placeholder", Constants.DEFAULT_DOUBLE_VALUE);
45+
private final MetricTelemetry telemetry = new MetricTelemetry();
4646

4747
/**
4848
* Registers the argument's data into performance counters.

core/src/test/java/com/microsoft/applicationinsights/internal/config/ConfigurationFileLocatorTest.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
package com.microsoft.applicationinsights.internal.config;
2323

24-
import org.junit.Test;
24+
import org.junit.*;
2525

2626
import java.io.File;
2727
import java.io.IOException;
@@ -39,6 +39,11 @@ public final class ConfigurationFileLocatorTest {
3939
private final static String MOCK_CONF_FILE = "MockApplicationInsights.xml";
4040
private final static String EXISTING_CONF_TEST_FILE = "ApplicationInsights.xml";
4141

42+
@Before
43+
public void clearProp() {
44+
System.clearProperty(ConfigurationFileLocator.CONFIG_DIR_PROPERTY);
45+
}
46+
4247
@Test(expected = IllegalArgumentException.class)
4348
public void testCtorWithNull() {
4449
new ConfigurationFileLocator(null);

0 commit comments

Comments
 (0)