Skip to content
This repository has been archived by the owner on Nov 23, 2021. It is now read-only.

Commit

Permalink
Default disable tracing some ai events (#38)
Browse files Browse the repository at this point in the history
* Default disable tracing some ai events

* Fix checkstyle issue

* Add switch for restart trace
  • Loading branch information
gavinfish committed May 10, 2019
1 parent e4279a1 commit 9724a4c
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 14 deletions.
9 changes: 8 additions & 1 deletion azure-commons-plugin/pom.xml
Expand Up @@ -15,6 +15,13 @@
<description>Common APIs for Azure related Jenkins plugins</description>
<url>https://wiki.jenkins-ci.org/display/JENKINS/Azure+Commons+Plugin</url>

<properties>
<enableAzureApiTrace>false</enableAzureApiTrace>
<enablePageDecorator>false</enablePageDecorator>
<enableRestartTrace>false</enableRestartTrace>
<filteredEvents/>
</properties>

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
Expand Down Expand Up @@ -62,7 +69,7 @@
<configuration>
<minimumJavaVersion>1.8</minimumJavaVersion>
<maskClasses>
com.google.common.base.
com.google.common.
</maskClasses>
</configuration>
</plugin>
Expand Down
6 changes: 5 additions & 1 deletion azure-commons-plugin/src/main/ai-resources/ai.properties
@@ -1 +1,5 @@
InstrumentationKey=@instrkey@
InstrumentationKey=@instrkey@
EnableAzureApiTrace=@enableAzureApiTrace@
EnablePageDecorator=@enablePageDecorator@
EnableRestartTrace=@enableRestartTrace@
FilteredEvents=@filteredEvents@
@@ -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() {
}
}
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -83,6 +75,10 @@ public void sendEvent(String item, String action, Map<String, String> 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<String, String> formalizedProperties = formalizeProperties(properties);

final JenkinsTelemetryClient client = getTelemetryClient();
Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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) {
Expand Down

0 comments on commit 9724a4c

Please sign in to comment.