Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect perf counters using Oshi #1482

Merged
merged 21 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ dependencies {
compile ([group: 'com.google.code.gson', name: 'gson', version: '2.8.2'])
// update transitive dependency version
compile ([group: 'com.google.guava', name: 'guava', version: '27.1-android'])
compile ([group: 'com.github.oshi', name:'oshi-core',version:'5.3.1'])
compile 'org.slf4j:slf4j-api:1.7.26'
testCompile group: 'org.hamcrest', name:'hamcrest-core', version:'1.3'
testCompile group: 'org.hamcrest', name:'hamcrest-library', version:'1.3'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* ApplicationInsights-Java
* Copyright (c) Microsoft Corporation
* All rights reserved.
*
* MIT License
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation files (the ""Software""), to deal in the Software
* without restriction, including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
* THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
* FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/

package com.microsoft.applicationinsights.internal.perfcounter;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.telemetry.MetricTelemetry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.software.os.OSProcess;
import oshi.software.os.OperatingSystem;

public class OshiPerformanceCounter implements PerformanceCounter {

private static final Logger logger = LoggerFactory.getLogger(OshiPerformanceCounter.class);
private static final String ID = Constants.PERFORMANCE_COUNTER_PREFIX + "OshiPerformanceCounter";
private final static double MILLIS_IN_SECOND = 1000;
private long prevCollectionInMillis = -1;
private double prevProcessIO;
private OSProcess processInfo;
private double prevTotalProcessorTime;

public OshiPerformanceCounter() {
SystemInfo systemInfo = new SystemInfo();
OperatingSystem osInfo = systemInfo.getOperatingSystem();
processInfo = osInfo.getProcess(osInfo.getProcessId());
}

@Override public String getId() {
return ID;
}

@Override public void report(TelemetryClient telemetryClient) {
processInfo.updateAttributes();

long currentCollectionInMillis = System.currentTimeMillis();
double currentProcessIO = (double) (processInfo.getBytesRead() + processInfo.getBytesWritten());
double currentTotalProcessorTime = processInfo.getUserTime() + processInfo.getKernelTime();
if (prevCollectionInMillis != -1) {
double timeElapsedInSeconds = (currentCollectionInMillis - prevCollectionInMillis) / MILLIS_IN_SECOND;
double processIo = (currentProcessIO - prevProcessIO) / timeElapsedInSeconds;
send(telemetryClient, processIo, Constants.PROCESS_IO_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.PROCESS_IO_PC_METRIC_NAME, processIo);

double processorTime = (currentTotalProcessorTime - prevTotalProcessorTime) / timeElapsedInSeconds;
send(telemetryClient, processorTime, Constants.TOTAL_CPU_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.TOTAL_CPU_PC_METRIC_NAME, processorTime);
}

prevProcessIO = currentProcessIO;
prevTotalProcessorTime = currentTotalProcessorTime;
prevCollectionInMillis = currentCollectionInMillis;
}

private void send(TelemetryClient telemetryClient, double value, String metricName) {
MetricTelemetry telemetry = new MetricTelemetry(metricName, value);
telemetryClient.track(telemetry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,16 @@ private Collection<PerformanceCounter> getWindowsPerformanceCounters() {
ArrayList<PerformanceCounter> performanceCounters = getMutualPerformanceCounters();

try {
WindowsPerformanceCounterAsPC pcWindowsPCs = new WindowsPerformanceCounterAsPC();
performanceCounters.add(pcWindowsPCs);
OshiPerformanceCounter oshiPerfCounter = new OshiPerformanceCounter();
performanceCounters.add(oshiPerfCounter);

WindowsPerformanceCounterAsPC wpc = new WindowsPerformanceCounterAsPC();
performanceCounters.add(wpc);
} catch (ThreadDeath td) {
throw td;
} catch (Throwable e) {
try {
logger.error("Failed to create WindowsPerformanceCounterAsPC: '{}'", e.toString());
logger.error("Failed to create OshiPerformanceCounter: '{}'", e.toString());
} catch (ThreadDeath td) {
throw td;
} catch (Throwable t2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ public final class WindowsPerformanceCounterAsPC extends AbstractWindowsPerforma
public WindowsPerformanceCounterAsPC() throws Throwable {
Preconditions.checkState(SystemInformation.INSTANCE.isWindows(), "Must be used under Windows OS.");

register(Constants.TOTAL_CPU_PC_CATEGORY_NAME, Constants.CPU_PC_COUNTER_NAME, Constants.INSTANCE_NAME_TOTAL, Constants.TOTAL_CPU_PC_METRIC_NAME);
register(Constants.TOTAL_CPU_PC_CATEGORY_NAME, Constants.CPU_PC_COUNTER_NAME, Constants.INSTANCE_NAME_TOTAL, Constants.TOTAL_CPU_PC_METRIC_NAME + " FROM JNI");
register(Constants.PROCESS_CATEGORY, Constants.PROCESS_IO_PC_COUNTER_NAME, JniPCConnector.translateInstanceName(JniPCConnector.PROCESS_SELF_INSTANCE_NAME),
Constants.PROCESS_IO_PC_METRIC_NAME);
Constants.PROCESS_IO_PC_METRIC_NAME + " FROM JNI");

if (pcs.isEmpty()) {
// Failed to register, the performance counter is not needed.
Expand Down Expand Up @@ -130,4 +130,4 @@ private void register(String category, String counter, String instance, String m
}
}
}
}
}