Skip to content

Commit

Permalink
Merge pull request #920 from microsoft/fixDuplicatePcJni
Browse files Browse the repository at this point in the history
Fix duplicate pc jni
  • Loading branch information
littleaj authored May 14, 2019
2 parents 68f3514 + 68c45ce commit 07f1a65
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 23 deletions.
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ sourceSets {

clean {
delete 'src/main/generated'
delete "${System.properties['java.io.tmpdir']}/AISDK/native" // see JniPCConnector.java for jni directory
}

import org.apache.tools.ant.taskdefs.condition.Os
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
public abstract class AbstractWindowsPerformanceCounter implements PerformanceCounter {

protected void reportError(double value, String displayName) {
if (!InternalLogger.INSTANCE.isErrorEnabled()) {
return;
}

if (value == -1) {
InternalLogger.INSTANCE.error("Native code exception in wrapper while fetching counter value '%s'", displayName);
} else if (value == -4) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,11 @@
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.system.SystemInformation;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.microsoft.applicationinsights.internal.util.LocalFileSystemUtils;
import com.microsoft.applicationinsights.internal.util.PropertyHelper;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -99,13 +98,24 @@ public static boolean initialize() {
* @param category The category must be non null non empty value.
* @param counter The counter must be non null non empty value.
* @param instance The instance.
* @return True on success.
* @return The key for retrieving counter data.
*/
public static String addPerformanceCounter(String category, String counter, String instance) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(category), "category must be non-null non empty string.");
Preconditions.checkArgument(!Strings.isNullOrEmpty(counter), "counter must be non-null non empty string.");
if (StringUtils.isEmpty(category)) {
throw new IllegalArgumentException("category must be non-null non empty string.");
}
if (StringUtils.isEmpty(counter)) {
throw new IllegalArgumentException("counter must be non-null non empty string.");
}

return addCounter(category, counter, instance);
if (InternalLogger.INSTANCE.isTraceEnabled()) {
InternalLogger.INSTANCE.trace("Registering performance counter: %s\\%s [%s]", category, counter, StringUtils.trimToEmpty(instance));
}
final String s = addCounter(category, counter, instance);
if (StringUtils.isEmpty(s) && InternalLogger.INSTANCE.isWarnEnabled()) {
InternalLogger.INSTANCE.warn("Performance coutner registration failed for %s\\%s [%s]", category, counter, StringUtils.trimToEmpty(instance));
}
return s;
}

/**
Expand All @@ -118,7 +128,7 @@ public static String addPerformanceCounter(String category, String counter, Stri
*/
public static String translateInstanceName(String instanceName) throws Exception {
if (PROCESS_SELF_INSTANCE_NAME.equals(instanceName)) {
if (Strings.isNullOrEmpty(currentInstanceName)) {
if (StringUtils.isEmpty(currentInstanceName)) {
throw new Exception("Cannot translate instance name: Unknown current instance name");
}

Expand All @@ -134,7 +144,9 @@ public static String translateInstanceName(String instanceName) throws Exception
* @return The current value.
*/
public static double getValueOfPerformanceCounter(String name) {
Preconditions.checkArgument(!Strings.isNullOrEmpty(name), "name must be non-null non empty value.");
if(StringUtils.isEmpty(name)) {
throw new IllegalArgumentException("name must be non-null non empty value.");
}

return getPerformanceCounterValue(name);
}
Expand All @@ -143,7 +155,7 @@ private static void initNativeCode() {
int processId = Integer.parseInt(SystemInformation.INSTANCE.getProcessId());

currentInstanceName = getInstanceName(processId);
if (Strings.isNullOrEmpty(currentInstanceName)) {
if (StringUtils.isEmpty(currentInstanceName)) {
InternalLogger.INSTANCE.error("Failed to fetch current process instance name, process counters for for the process level will not be activated.");
} else {
InternalLogger.INSTANCE.trace("Java process name is set to '%s'", currentInstanceName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@
import com.microsoft.applicationinsights.internal.system.SystemInformation;
import com.microsoft.applicationinsights.telemetry.MetricTelemetry;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;

/**
Expand All @@ -51,8 +50,12 @@ public final class WindowsPerformanceCounterAsMetric extends AbstractWindowsPerf
* connect to the native code. or Exception if the constructor is not called under Windows OS.
*/
public WindowsPerformanceCounterAsMetric(Iterable<WindowsPerformanceCounterData> pcsData) throws Throwable {
Preconditions.checkState(SystemInformation.INSTANCE.isWindows(), "Must be used under Windows OS.");
Preconditions.checkNotNull(pcsData, "pcsData must be non-null value.");
if (!SystemInformation.INSTANCE.isWindows()) {
throw new IllegalStateException("Must be used under Windows OS.");
}
if (pcsData == null) {
throw new NullPointerException("pcsData must be non-null value.");
}

// indicate that this is used for performance counters, not custom metrics.
telemetry.markAsCustomPerfCounter();
Expand Down Expand Up @@ -107,7 +110,7 @@ private void register(Iterable<WindowsPerformanceCounterData> pcsData) {
for (WindowsPerformanceCounterData data : pcsData) {
try {
String key = JniPCConnector.addPerformanceCounter(data.categoryName, data.counterName, data.instanceName);
if (!Strings.isNullOrEmpty(key)) {
if (!StringUtils.isEmpty(key)) {
keyToDisplayName.put(key, data.displayName);
}
} catch (ThreadDeath td) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,9 @@ public void report(TelemetryClient telemetryClient) {
throw td;
} catch (Throwable e) {
try {
InternalLogger.INSTANCE.error("Failed to send performance counter for '%s': '%s'", entry.getValue().displayName, e.toString());
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isErrorEnabled()) {
InternalLogger.INSTANCE.error("Failed to send performance counter for '%s': %s", entry.getValue().displayName, ExceptionUtils.getStackTrace(e));
}
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
Expand Down Expand Up @@ -123,8 +124,9 @@ private void register(String category, String counter, String instance) {
throw td;
} catch (Throwable e) {
try {
InternalLogger.INSTANCE.error("Exception while registering windows performance counter as PC");
InternalLogger.INSTANCE.trace("Stack trace generated is %s", ExceptionUtils.getStackTrace(e));
if (InternalLogger.INSTANCE.isErrorEnabled()) {
InternalLogger.INSTANCE.error("Exception while registering windows performance counter as PC: %s", ExceptionUtils.getStackTrace(e));
}
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
Expand Down
14 changes: 9 additions & 5 deletions core/src/windows/cpp/CppPerfCounters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ref class PerfCountersUtils
String^ key = performanceCounterCategoryName + performanceCounterCounterName + performanceCounterInstanceName;
if (pcDictionary->ContainsKey(key))
{
return nullptr;
return key;
}

PerformanceCounter^ pc = gcnew PerformanceCounter(performanceCounterCategoryName, performanceCounterCounterName, performanceCounterInstanceName);
Expand Down Expand Up @@ -218,10 +218,14 @@ JNIEXPORT jstring JNICALL Java_com_microsoft_applicationinsights_internal_perfco
env->ReleaseStringUTFChars(rawPerformanceCounterCounterName, performanceCounterCounterName);
env->ReleaseStringUTFChars(rawPerformanceCounterInstance, performanceCounterInstance);

std::string a;
MarshalString(value, a);
jstring r = env->NewStringUTF(a.c_str());
return r;
if (value == nullptr) {
return nullptr;
} else {
std::string a;
MarshalString(value, a);
jstring r = env->NewStringUTF(a.c_str());
return r;
}
}
catch (...)
{
Expand Down

0 comments on commit 07f1a65

Please sign in to comment.