diff --git a/azure-commons-plugin/pom.xml b/azure-commons-plugin/pom.xml index 89aa723..8f80059 100644 --- a/azure-commons-plugin/pom.xml +++ b/azure-commons-plugin/pom.xml @@ -15,6 +15,13 @@ Common APIs for Azure related Jenkins plugins https://wiki.jenkins-ci.org/display/JENKINS/Azure+Commons+Plugin + + false + false + false + + + ${project.groupId} @@ -62,7 +69,7 @@ 1.8 - com.google.common.base. + com.google.common. diff --git a/azure-commons-plugin/src/main/ai-resources/ai.properties b/azure-commons-plugin/src/main/ai-resources/ai.properties index abbff03..b55bbb3 100644 --- a/azure-commons-plugin/src/main/ai-resources/ai.properties +++ b/azure-commons-plugin/src/main/ai-resources/ai.properties @@ -1 +1,5 @@ -InstrumentationKey=@instrkey@ \ No newline at end of file +InstrumentationKey=@instrkey@ +EnableAzureApiTrace=@enableAzureApiTrace@ +EnablePageDecorator=@enablePageDecorator@ +EnableRestartTrace=@enableRestartTrace@ +FilteredEvents=@filteredEvents@ \ No newline at end of file diff --git a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AiProperties.java b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AiProperties.java new file mode 100644 index 0000000..c2d3291 --- /dev/null +++ b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AiProperties.java @@ -0,0 +1,50 @@ +package com.microsoft.jenkins.azurecommons.telemetry; + +import java.io.IOException; +import java.util.Properties; +import java.util.logging.Logger; + +public final class AiProperties { + private static final Logger LOGGER = Logger.getLogger(AiProperties.class.getName()); + private static final String INSTRUMENTATION_KEY_NAME = "InstrumentationKey"; + private static final String ENABLE_AZURE_API_TRACE_NAME = "EnableAzureApiTrace"; + private static final String ENABLE_PAGE_DECORATOR_NAME = "EnablePageDecorator"; + private static final String ENABLE_RESTART_TRACE_NAME = "EnableRestartTrace"; + private static final String FILTERED_EVENTS_NAME = "FilteredEvents"; + + private static final Properties PROP = new Properties(); + + static { + try { + PROP.load(AiProperties.class.getClassLoader().getResourceAsStream("ai.properties")); + } catch (IOException e) { + LOGGER.severe("Failed to load Ai property file."); + } + } + + public static String getInstrumentationKey() { + return PROP.getProperty(INSTRUMENTATION_KEY_NAME); + } + + public static boolean enableAzureApiTrace() { + String property = PROP.getProperty(ENABLE_AZURE_API_TRACE_NAME); + return Boolean.parseBoolean(property); + } + + public static boolean enablePageDecorator() { + String property = PROP.getProperty(ENABLE_PAGE_DECORATOR_NAME); + return Boolean.parseBoolean(property); + } + + public static boolean enableRestartTrace() { + String property = PROP.getProperty(ENABLE_RESTART_TRACE_NAME); + return Boolean.parseBoolean(property); + } + + public static String getFilteredEvents() { + return PROP.getProperty(FILTERED_EVENTS_NAME); + } + + private AiProperties() { + } +} diff --git a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsClient.java b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsClient.java index ad86c13..fc4e375 100644 --- a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsClient.java +++ b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsClient.java @@ -14,11 +14,9 @@ import jenkins.model.Jenkins; import org.apache.commons.lang.StringUtils; -import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.Map; -import java.util.Properties; import java.util.logging.Logger; import static com.google.common.base.Preconditions.checkNotNull; @@ -67,13 +65,7 @@ public AppInsightsClient(Plugin plugin) { this.instrumentationKey = ikey; LOGGER.info("Use AI instrumentation key from system properties or environment variables."); } else { - Properties prop = new Properties(); - try { - prop.load(AppInsightsClient.class.getClassLoader().getResourceAsStream("ai.properties")); - this.instrumentationKey = prop.getProperty(INSTRUMENTATION_KEY_NAME); - } catch (IOException e) { - LOGGER.severe("Failed to load application insights configuration file."); - } + this.instrumentationKey = AiProperties.getInstrumentationKey(); } } telemetryClient = new JenkinsTelemetryClient(this.instrumentationKey); @@ -83,6 +75,10 @@ public void sendEvent(String item, String action, Map properties try { if (this.plugin != null && (AppInsightsGlobalConfig.get().isAppInsightsEnabled() || force)) { final String eventName = buildEventName(item, action); + if (AiProperties.getFilteredEvents().contains(eventName)) { + LOGGER.fine(String.format("Ignore event %s for App Insights.", eventName)); + return; + } final Map formalizedProperties = formalizeProperties(properties); final JenkinsTelemetryClient client = getTelemetryClient(); diff --git a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPageDecorator.java b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPageDecorator.java index 9fb7183..b0b31b2 100644 --- a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPageDecorator.java +++ b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPageDecorator.java @@ -26,7 +26,8 @@ public class AppInsightsPageDecorator extends PageDecorator { */ public boolean isDue() { // user opted out. no data collection. - if (!AppInsightsGlobalConfig.get().isAppInsightsEnabled()) { + if (!AppInsightsGlobalConfig.get().isAppInsightsEnabled() + || !AiProperties.enablePageDecorator()) { return false; } diff --git a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPluginLoadListener.java b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPluginLoadListener.java index eaa6cd7..ed6571a 100644 --- a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPluginLoadListener.java +++ b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AppInsightsPluginLoadListener.java @@ -21,7 +21,9 @@ public class AppInsightsPluginLoadListener extends RestartListener { @Override public boolean isReadyToRestart() throws IOException, InterruptedException { - AzureCommonsPlugin.sendEvent(AppInsightsConstants.JENKINS, AppInsightsConstants.RESTART); + if (AiProperties.enableRestartTrace()) { + AzureCommonsPlugin.sendEvent(AppInsightsConstants.JENKINS, AppInsightsConstants.RESTART); + } return true; } diff --git a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AzureHttpRecorder.java b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AzureHttpRecorder.java index c31312b..930e57a 100644 --- a/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AzureHttpRecorder.java +++ b/azure-commons-plugin/src/main/java/com/microsoft/jenkins/azurecommons/telemetry/AzureHttpRecorder.java @@ -34,7 +34,7 @@ public AzureHttpRecorder(AppInsightsClient client) { } public void record(HttpRecordable recordable) throws IOException { - if (appInsightsClient != null) { + if (appInsightsClient != null && AiProperties.enableAzureApiTrace()) { try { sendTelemetry(recordable); } catch (Exception e) {