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 1 commit
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,83 @@
/*
* 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.hardware.HWDiskStore;
import oshi.hardware.HardwareAbstractionLayer;
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 NANOS_IN_SECOND = 1000000000.0;
trask marked this conversation as resolved.
Show resolved Hide resolved
private long lastCollectionInNanos = -1;
private double prevProcessIO;

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

@Override public void report(TelemetryClient telemetryClient) {
long currentCollectionInNanos = System.nanoTime();

SystemInfo systemInfo = new SystemInfo();
HardwareAbstractionLayer hal = systemInfo.getHardware();
OperatingSystem osInfo = systemInfo.getOperatingSystem();
OSProcess processInfo = osInfo.getProcess(osInfo.getProcessId());
heyams marked this conversation as resolved.
Show resolved Hide resolved

double currentProcessIO = 0.0;
heyams marked this conversation as resolved.
Show resolved Hide resolved

// system.disk.io
for (HWDiskStore diskStore : hal.getDiskStores()) {
currentProcessIO += (double) (diskStore.getReadBytes() + diskStore.getWriteBytes());
}

if (lastCollectionInNanos != -1) {
trask marked this conversation as resolved.
Show resolved Hide resolved
double timeElapsedInSeconds = ((double)(currentCollectionInNanos - lastCollectionInNanos)) / NANOS_IN_SECOND;
heyams marked this conversation as resolved.
Show resolved Hide resolved
double value = (currentProcessIO - prevProcessIO) / timeElapsedInSeconds;
prevProcessIO = currentProcessIO;
heyams marked this conversation as resolved.
Show resolved Hide resolved
send(telemetryClient, value, Constants.PROCESS_IO_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.PROCESS_IO_PC_METRIC_NAME, value);
}

prevProcessIO = currentProcessIO;
lastCollectionInNanos = currentCollectionInNanos;
heyams marked this conversation as resolved.
Show resolved Hide resolved

// runtime.java.cpu_time
heyams marked this conversation as resolved.
Show resolved Hide resolved
processInfo.updateAttributes();
heyams marked this conversation as resolved.
Show resolved Hide resolved
long totalProcessorTime = (long) ((processInfo.getUserTime() + processInfo.getKernelTime()) * 0.001);
heyams marked this conversation as resolved.
Show resolved Hide resolved
send(telemetryClient, totalProcessorTime, Constants.TOTAL_CPU_PC_METRIC_NAME);
logger.trace("Sent performance counter for '{}': '{}'", Constants.TOTAL_CPU_PC_METRIC_NAME, totalProcessorTime);
}

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,13 @@ private Collection<PerformanceCounter> getWindowsPerformanceCounters() {
ArrayList<PerformanceCounter> performanceCounters = getMutualPerformanceCounters();

try {
WindowsPerformanceCounterAsPC pcWindowsPCs = new WindowsPerformanceCounterAsPC();
performanceCounters.add(pcWindowsPCs);
OshiPerformanceCounter oshiPerfCounter = new OshiPerformanceCounter();
performanceCounters.add(oshiPerfCounter);
} 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

This file was deleted.