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

Features/filter #296

Merged
merged 4 commits into from Aug 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -26,6 +26,7 @@

import com.microsoft.applicationinsights.extensibility.ContextInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryProcessor;
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.util.ChannelFetcher;
import com.microsoft.applicationinsights.internal.shutdown.SDKShutdownActivity;
Expand Down Expand Up @@ -370,13 +371,7 @@ public void track(Telemetry telemetry) {
InternalLogger.INSTANCE.error("Exception while telemetry context's initialization: '%s'", t.getMessage());
}

for (TelemetryInitializer initializer : this.configuration.getTelemetryInitializers()) {
try {
initializer.initialize(telemetry);
} catch (Throwable e) {
InternalLogger.INSTANCE.error("Failed during telemetry initialization class '%s', exception: %s", initializer.getClass().getName(), e.getMessage());
}
}
activateInitializers(telemetry);

if (Strings.isNullOrEmpty(telemetry.getContext().getInstrumentationKey())) {
throw new IllegalArgumentException("Instrumentation key cannot be undefined.");
Expand All @@ -388,13 +383,41 @@ public void track(Telemetry telemetry) {
InternalLogger.INSTANCE.error("Exception while sanitizing telemetry: '%s'",t.getMessage());
}

if (!activateProcessors(telemetry)) {
return;
}

try {
getChannel().send(telemetry);
} catch (Throwable t) {
InternalLogger.INSTANCE.error("Exception while sending telemetry: '%s'",t.getMessage());
}
}

private void activateInitializers(Telemetry telemetry) {
for (TelemetryInitializer initializer : this.configuration.getTelemetryInitializers()) {
try {
initializer.initialize(telemetry);
} catch (Throwable e) {
InternalLogger.INSTANCE.error("Failed during telemetry initialization class '%s', exception: %s", initializer.getClass().getName(), e.getMessage());
}
}
}

private boolean activateProcessors(Telemetry telemetry) {
for (TelemetryProcessor processor : configuration.getTelemetryProcessors()) {
try {
if (!processor.process(telemetry)) {
return false;
}
} catch (Throwable t) {
InternalLogger.INSTANCE.error("Exception while processing telemetry: '%s'",t.getMessage());
}
}

return true;
}

/**
* Flushes possible pending Telemetries in the channel.
*/
Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.microsoft.applicationinsights.extensibility.ContextInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryModule;
import com.microsoft.applicationinsights.extensibility.TelemetryProcessor;
import com.microsoft.applicationinsights.internal.config.TelemetryConfigurationFactory;
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
import com.microsoft.applicationinsights.internal.util.Sanitizer;
Expand All @@ -53,6 +54,7 @@ public final class TelemetryConfiguration {
private final ArrayList<ContextInitializer> contextInitializers = new ArrayList<ContextInitializer>();
private final ArrayList<TelemetryInitializer> telemetryInitializers = new ArrayList<TelemetryInitializer>();
private final ArrayList<TelemetryModule> telemetryModules = new ArrayList<TelemetryModule>();
private final ArrayList<TelemetryProcessor> telemetryProcessors = new ArrayList<TelemetryProcessor>();

private TelemetryChannel channel;

Expand Down Expand Up @@ -158,6 +160,10 @@ public List<TelemetryModule> getTelemetryModules() {
return telemetryModules;
}

public List<TelemetryProcessor> getTelemetryProcessors() {
return telemetryProcessors;
}

/**
* Gets or sets the default instrumentation key for the application.
*
Expand Down
@@ -0,0 +1,52 @@
/*
* 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.telemetry.Telemetry;

/**
* The method gets a {@link Telemetry} instance that is ready to be sent. This is your chance to approve
* or deny it. Returning 'false' means that the Telemetry will not be sent while 'true' means you approve it.
*
* The Telemetry might go through other filters though, that might deny its sending.
*
* To enable this processor you need to add it in the ApplicationInsights.xml like this:
*
<TelemetryProcessors>

<CustomProcessors>
<Processor type="full.path.to.Filter">
<Add name="Property" value="stringValue"/>
</Processor>
</CustomProcessors>

</TelemetryProcessors>
*
* Note that the class defines a property named 'Property' which is configured too.
* Every property that you wish to configure needs to have a 'setX' public method like 'setProperty' in this example
* <b>Exceptions thrown from the 'setX' methods will be caught by the framework that will ignore the filter</b>
*
* Created by gupele on 7/26/2016.
*/
public interface TelemetryProcessor {
boolean process(Telemetry telemetry);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth adding documentation about the meaning of the return value

}
@@ -0,0 +1,36 @@
/*
* 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.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Created by gupele on 7/26/2016.
*/
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface BuiltInProcessor {
String value();
}
Expand Up @@ -35,6 +35,7 @@ public class ApplicationInsightsXmlConfiguration {
private boolean disableTelemetry = false;

private TelemetryInitializersXmlElement telemetryInitializers;
private TelemetryProcessorsXmlElement telemetryProcessors;
private ContextInitializersXmlElement contextInitializers;
private ChannelXmlElement channel = new ChannelXmlElement();
private TelemetryModulesXmlElement modules;
Expand Down Expand Up @@ -74,6 +75,15 @@ public ContextInitializersXmlElement getContextInitializers() {
return contextInitializers;
}

@XmlElement(name="TelemetryProcessors")
public void setTelemetryProcessors(TelemetryProcessorsXmlElement telemetryProcessors) {
this.telemetryProcessors = telemetryProcessors;
}

public TelemetryProcessorsXmlElement getTelemetryProcessors() {
return telemetryProcessors;
}

@XmlElement(name="ContextInitializers")
public void setContextInitializers(ContextInitializersXmlElement contextInitializers) {
this.contextInitializers = contextInitializers;
Expand Down
Expand Up @@ -55,6 +55,8 @@ public ApplicationInsightsXmlConfiguration build(InputStream resourceFile) {
} else {
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.ERROR, "Failed to parse configuration file: '%s'", e.getMessage());
}
} catch (NullPointerException e) {
e.printStackTrace();
} finally {
try {
resourceFile.close();
Expand Down
Expand Up @@ -23,21 +23,27 @@

import java.io.InputStream;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;
import java.util.List;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;

import com.microsoft.applicationinsights.TelemetryConfiguration;
import com.microsoft.applicationinsights.agent.internal.common.StringUtils;
import com.microsoft.applicationinsights.extensibility.ContextInitializer;
import com.microsoft.applicationinsights.extensibility.TelemetryInitializer;
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;
import com.microsoft.applicationinsights.internal.annotation.BuiltInProcessor;
import com.microsoft.applicationinsights.internal.annotation.PerformanceModule;
import com.microsoft.applicationinsights.internal.jmx.JmxAttributeData;
import com.microsoft.applicationinsights.internal.logger.InternalLogger;
Expand Down Expand Up @@ -104,6 +110,7 @@ public final void initialize(TelemetryConfiguration configuration) {
setContextInitializers(applicationInsightsConfig.getContextInitializers(), configuration);
setTelemetryInitializers(applicationInsightsConfig.getTelemetryInitializers(), configuration);
setTelemetryModules(applicationInsightsConfig, configuration);
setTelemetryProcessors(applicationInsightsConfig, configuration);

initializeComponents(configuration);
} catch (Exception e) {
Expand Down Expand Up @@ -172,6 +179,37 @@ private void setTelemetryModules(ApplicationInsightsXmlConfiguration appConfigur
modules.addAll(pcModules);
}

private void setTelemetryProcessors(ApplicationInsightsXmlConfiguration appConfiguration, TelemetryConfiguration configuration) {
TelemetryProcessorsXmlElement configurationProcessors = appConfiguration.getTelemetryProcessors();
List<TelemetryProcessor> processors = configuration.getTelemetryProcessors();

if (configurationProcessors != null) {
ArrayList<TelemetryProcessorXmlElement> b = configurationProcessors.getBuiltInTelemetryProcessors();
if (!b.isEmpty()) {
final List<String> processorsBuiltInNames =
new AnnotationPackageScanner().scanForClassAnnotations(new Class[]{BuiltInProcessor.class}, performanceCountersSection);
final HashMap<String, String> builtInMap = new HashMap<String, String>();
for (String processorsBuiltInName : processorsBuiltInNames) {
builtInMap.put(processorsBuiltInName.substring(processorsBuiltInName.lastIndexOf(".") + 1), processorsBuiltInName);
}
ArrayList<TelemetryProcessorXmlElement> validProcessors = new ArrayList<TelemetryProcessorXmlElement>();
for (TelemetryProcessorXmlElement element : b) {
String fullTypeName = builtInMap.get(element.getType());
if (StringUtils.isNullOrEmpty(fullTypeName)) {
InternalLogger.INSTANCE.error("Failed to find built in processor: '%s', ignored", element.getType());
continue;
}
element.setType(fullTypeName);
validProcessors.add(element);
}
loadProcessorComponents(TelemetryProcessor.class, processors, validProcessors);
}
ArrayList<TelemetryProcessorXmlElement> customs = configurationProcessors.getCustomTelemetryProcessors();
loadProcessorComponents(TelemetryProcessor.class, processors, customs);
}
}


/**
* Setting an instrumentation key:
* First we try the system property '-DAPPLICATION_INSIGHTS_IKEY=i_key'
Expand Down Expand Up @@ -216,6 +254,12 @@ private void setInstrumentationKey(ApplicationInsightsXmlConfiguration userConfi
}
}

private List<TelemetryProcessor> getTelemetryProcessors() {
ArrayList<TelemetryProcessor> processors = new ArrayList<TelemetryProcessor>();

return processors;
}

@SuppressWarnings("unchecked")
private List<TelemetryModule> getPerformanceModules(PerformanceCountersXmlElement performanceConfigurationData) {
ArrayList<TelemetryModule> modules = new ArrayList<TelemetryModule>();
Expand Down Expand Up @@ -401,6 +445,31 @@ private <T> void loadComponents(
}
}

private <T> void loadProcessorComponents(
Class<T> clazz,
List<T> list,
Collection<TelemetryProcessorXmlElement> classNames) {
if (classNames == null) {
return;
}

for (TelemetryProcessorXmlElement className : classNames) {
T initializer = null;

initializer = createInstance(className.getType(), clazz);
for (ParamXmlElement param : className.getAdds()){
String methodName = "set" + param.getName();
try {
if (activateMethod(initializer, methodName, param.getValue(), String.class)) {
list.add(initializer);
}
} catch (Throwable t) {
InternalLogger.INSTANCE.logAlways(InternalLogger.LoggingLevel.ERROR, className + ": failed to activate method " + methodName + ", exception: " + t.getMessage() + ", the class will not be used.");
}
}
}
}

/**
* Creates an instance from its name. We suppress Java compiler warnings for Generic casting
*
Expand Down Expand Up @@ -491,6 +560,26 @@ private void initializeComponents(TelemetryConfiguration configuration) {
}
}

private static <V, A> boolean activateMethod(Object object, String methodName, V value, A argumentClass) {
Class<?> clazz = object.getClass();
Method method = null;
try {
method = clazz.getDeclaredMethod(methodName, String.class);
method.invoke(object, value);
return true;
} catch (NoSuchMethodException e) {
InternalLogger.INSTANCE.error(
"Failed to call method " + methodName + ". NoSuchMethodException");
} catch (InvocationTargetException e) {
InternalLogger.INSTANCE.error(
"Failed to call method " + methodName + ". InvocationTargetException");
} catch (IllegalAccessException e) {
InternalLogger.INSTANCE.error(
"Failed to call method " + methodName + ". IllegalAccessException");
}
return false;
}

void setPerformanceCountersSection(String performanceCountersSection) {
this.performanceCountersSection = performanceCountersSection;
}
Expand Down