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

Added plugin to be called prior and after pc collection #328

Merged
merged 1 commit into from
Sep 18, 2016
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.extensibility;

/**
* Created by gupele on 9/13/2016.
*/
public interface PerformanceCountersCollectionPlugin {
void preCollection();

void postCollection();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* 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.extensibility;

import com.microsoft.applicationinsights.internal.perfcounter.PerformanceCounter;

import java.util.Collection;

/**
* Defines the interface that concrete factories should follow.
* The major method is the one that will be able create the needed Performance Counters.
*
* Created by gupele on 3/3/2015.
*/
public interface PerformanceCountersFactory {
/**
* Creates the {@link com.microsoft.applicationinsights.internal.perfcounter.PerformanceCounter}
*
* Note: The method should not throw
*
* @return A collection of {@link com.microsoft.applicationinsights.internal.perfcounter.PerformanceCounter}
*/
Collection<PerformanceCounter> getPerformanceCounters();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public class PerformanceCountersXmlElement {
private boolean useBuiltIn = true;
private long collectionFrequencyInSec = 60;
private PerformanceCounterJvmSectionXmlElement jvmSection;
private String plugin;

private ArrayList<JmxXmlElement> jmxXmlElements;
private ArrayList<WindowsPerformanceCounterXmlElement> windowsPCs;
Expand Down Expand Up @@ -82,4 +83,13 @@ public PerformanceCounterJvmSectionXmlElement getJvmSection() {
public void setJvmSection(PerformanceCounterJvmSectionXmlElement jvmSection) {
this.jvmSection = jvmSection;
}

public String getPlugin() {
return plugin;
}

@XmlElement(name="Plugin")
public void setPlugin(String plugin) {
this.plugin = plugin;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,9 @@
import java.util.HashMap;

import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.extensibility.ContextInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryInitializer;
import com.microsoft.applicationinsights.extensibility.*;
import com.microsoft.applicationinsights.channel.concrete.inprocess.InProcessTelemetryChannel;
import com.microsoft.applicationinsights.channel.TelemetryChannel;
import com.microsoft.applicationinsights.extensibility.TelemetryModule;
import com.microsoft.applicationinsights.extensibility.TelemetryProcessor;
import com.microsoft.applicationinsights.extensibility.initializer.DeviceInfoContextInitializer;
import com.microsoft.applicationinsights.extensibility.initializer.SdkVersionContextInitializer;
import com.microsoft.applicationinsights.internal.annotation.AnnotationPackageScanner;
Expand Down Expand Up @@ -263,6 +260,10 @@ private List<TelemetryProcessor> getTelemetryProcessors() {
@SuppressWarnings("unchecked")
private List<TelemetryModule> getPerformanceModules(PerformanceCountersXmlElement performanceConfigurationData) {
PerformanceCounterContainer.INSTANCE.setCollectionFrequencyInSec(performanceConfigurationData.getCollectionFrequencyInSec());
String pluginName = performanceConfigurationData.getPlugin();

PerformanceCountersCollectionPlugin plugin = ReflectionUtils.createInstance(pluginName, PerformanceCountersCollectionPlugin.class);
PerformanceCounterContainer.INSTANCE.setPlugin(plugin);

ArrayList<TelemetryModule> modules = new ArrayList<TelemetryModule>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import com.google.common.base.Strings;

import com.microsoft.applicationinsights.TelemetryClient;
import com.microsoft.applicationinsights.extensibility.PerformanceCountersCollectionPlugin;
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.shutdown.SDKShutdownActivity;
import com.microsoft.applicationinsights.internal.shutdown.Stoppable;
Expand Down Expand Up @@ -69,6 +70,8 @@ public enum PerformanceCounterContainer implements Stoppable {

private volatile boolean initialized = false;

private PerformanceCountersCollectionPlugin plugin;

private long startCollectingDelayInMillis = START_COLLECTING_DELAY_IN_MILLIS;
private long collectionFrequencyInMS = DEFAULT_COLLECTION_FREQUENCY_IN_SEC * 1000;

Expand Down Expand Up @@ -217,13 +220,27 @@ public void run() {
telemetryClient = new TelemetryClient();
}

if (plugin != null) {
try {
plugin.preCollection();
} catch (Throwable t) {
}
}

for (PerformanceCounter performanceCounter : performanceCounters.values()) {
try {
performanceCounter.report(telemetryClient);
} catch (Throwable e) {
InternalLogger.INSTANCE.error("Exception while reporting performance counter '%s': '%s'", performanceCounter.getId(), e.getMessage());
}
}

if (plugin != null) {
try {
plugin.postCollection();
} catch (Throwable t) {
}
}
}
},
startCollectingDelayInMillis,
Expand All @@ -245,4 +262,8 @@ public Thread newThread(Runnable r) {
}
});
}

public void setPlugin(PerformanceCountersCollectionPlugin plugin) {
this.plugin = plugin;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.microsoft.applicationinsights.sample;

import com.microsoft.applicationinsights.extensibility.PerformanceCountersCollectionPlugin;

/**
* A sample for creating a concrete plugin that will be called before and after Performance Counters are collected.
*
* To activate this class you need to add it to the ApplicationInsights.xml configuration file with the 'Plugin' tag:
*
* <pre>
* {@code
*
* <PerformanceCounters>
* <Plugin>full.path.to.your.Plugin</Plugin>
* </PerformanceCounters>
* }
* </pre>
*
* Created by gupele on 9/13/2016.
*/
public class SamplePCPlugin implements PerformanceCountersCollectionPlugin {
@Override
public void preCollection() {
System.out.println("pre collection called");
}

@Override
public void postCollection() {
System.out.println("post collection called");
}
}