Feature usage log: implement empty logger to use if statistic is disabled

This commit is contained in:
Svetlana.Zemlyanskaya
2018-02-14 18:26:11 +01:00
parent b9ac280ec1
commit 692ba0b4bf
5 changed files with 38 additions and 19 deletions
@@ -1,8 +1,8 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.featureStatistics;
import com.intellij.internal.statistic.eventLog.FeatureUsageLogger;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.internal.statistic.eventLog.FeatureUsageEventLogger;
import com.intellij.openapi.components.PersistentStateComponent;
import com.intellij.openapi.components.RoamingType;
import com.intellij.openapi.components.State;
@@ -177,7 +177,7 @@ public class FeatureUsageTrackerImpl extends FeatureUsageTracker implements Pers
// TODO: LOG.error("Feature '" + featureId +"' must be registered prior triggerFeatureUsed() is called");
}
else {
FeatureUsageEventLogger.INSTANCE.log(descriptor.getGroupId(), descriptor.getId());
FeatureUsageLogger.INSTANCE.log(descriptor.getGroupId(), descriptor.getId());
descriptor.triggerUsed();
}
}
@@ -4,11 +4,10 @@ package com.intellij.internal.statistic;
import com.intellij.internal.statistic.beans.ConvertUsagesUtil;
import com.intellij.internal.statistic.beans.GroupDescriptor;
import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.internal.statistic.eventLog.FeatureUsageLogger;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.internal.statistic.eventLog.FeatureUsageEventLogger;
import com.intellij.internal.statistic.service.fus.collectors.ApplicationUsagesCollector;
import com.intellij.internal.statistic.service.fus.collectors.FUStatisticsDifferenceSender;
import com.intellij.internal.statistic.service.fus.collectors.FeatureUsagesCollector;
import com.intellij.openapi.components.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.xmlb.annotations.MapAnnotation;
@@ -37,7 +36,7 @@ public class UsageTrigger implements PersistentStateComponent<UsageTrigger.State
private State myState = new State();
public static void trigger(@NotNull @NonNls String feature) {
FeatureUsageEventLogger.INSTANCE.log("feature-usage-stats", feature);
FeatureUsageLogger.INSTANCE.log("feature-usage-stats", feature);
getInstance().doTrigger(feature);
}
@@ -6,7 +6,7 @@ import com.intellij.internal.statistic.UsagesCollector;
import com.intellij.internal.statistic.beans.ConvertUsagesUtil;
import com.intellij.internal.statistic.beans.GroupDescriptor;
import com.intellij.internal.statistic.beans.UsageDescriptor;
import com.intellij.internal.statistic.eventLog.FeatureUsageEventLogger;
import com.intellij.internal.statistic.eventLog.FeatureUsageLogger;
import com.intellij.internal.statistic.persistence.UsageStatisticsPersistenceComponent;
import com.intellij.internal.statistic.service.fus.collectors.ApplicationUsagesCollector;
import com.intellij.internal.statistic.service.fus.collectors.FUStatisticsDifferenceSender;
@@ -38,7 +38,7 @@ public class ActionsCollectorImpl extends ActionsCollector implements Persistent
if (state == null) return;
String key = ConvertUsagesUtil.escapeDescriptorName(actionId);
FeatureUsageEventLogger.INSTANCE.log("action-stats", key);
FeatureUsageLogger.INSTANCE.log("action-stats", key);
final Integer count = state.myValues.get(key);
int value = count == null ? 1 : count + 1;
state.myValues.put(key, value);
@@ -7,7 +7,6 @@ package com.intellij.internal.statistic.eventLog
import com.intellij.openapi.application.ApplicationAdapter
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.PathManager
import com.intellij.openapi.util.registry.Registry
import org.apache.log4j.Level
import org.apache.log4j.Logger
import org.apache.log4j.PatternLayout
@@ -17,12 +16,12 @@ import java.io.IOException
import java.nio.file.Paths
import java.util.*
object FeatureUsageEventLogger {
class FeatureUsageFileEventLogger : FeatureUsageEventLogger {
private val sessionId = UUID.randomUUID().toString().shortedUUID()
private val bucket = "-1"
private val recorderVersion = "1"
private val eventLogger = if (Registry.`is`("feature.usage.event.log.collect.and.upload")) createLogger() else null
private val eventLogger = createLogger()
private var lastEvent: LogEvent? = null
private var lastEventTime: Long = 0
@@ -31,9 +30,7 @@ object FeatureUsageEventLogger {
init {
ApplicationManager.getApplication().addApplicationListener(object : ApplicationAdapter() {
override fun applicationExiting() {
if (eventLogger != null) {
dispose(eventLogger)
}
dispose(eventLogger)
}
})
}
@@ -46,10 +43,8 @@ object FeatureUsageEventLogger {
return this
}
fun log(recorderId: String, action: String) {
if (eventLogger != null) {
log(eventLogger, LogEvent(sessionId, bucket, recorderId, recorderVersion, action))
}
override fun log(recorderId: String, action: String) {
log(eventLogger, LogEvent(sessionId, bucket, recorderId, recorderVersion, action))
}
private fun log(logger: Logger, event: LogEvent) {
@@ -80,7 +75,7 @@ object FeatureUsageEventLogger {
count = 1
}
private fun createLogger(): Logger? {
private fun createLogger(): Logger {
val path = Paths.get(PathManager.getSystemPath()).resolve("event-log").resolve("feature-usage-event.log")
val file = File(path.toUri())
@@ -100,4 +95,4 @@ object FeatureUsageEventLogger {
}
return logger
}
}
}
@@ -0,0 +1,25 @@
// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.internal.statistic.eventLog
import com.intellij.openapi.util.registry.Registry
object FeatureUsageLogger {
private val ourLogger = if (isEnabled()) FeatureUsageFileEventLogger() else FeatureUsageEmptyEventLogger()
fun log(recorderId: String, action: String) {
return ourLogger.log(recorderId, action)
}
fun isEnabled() : Boolean {
return Registry.`is`("feature.usage.event.log.collect.and.upload")
}
}
interface FeatureUsageEventLogger {
fun log(recorderId: String, action: String)
}
class FeatureUsageEmptyEventLogger : FeatureUsageEventLogger {
override fun log(recorderId: String, action: String) {
}
}