From 8c2cf0d556b594237bc1685d4bdc9cdd0f5cf0e2 Mon Sep 17 00:00:00 2001 From: Jeff MAURY Date: Wed, 31 Mar 2021 18:20:43 +0200 Subject: [PATCH] feat: add telemetry Fixes #27 Signed-off-by: Jeff MAURY --- USAGE_DATA.md | 9 ++++++ build.gradle | 9 ++++++ .../analytics/AnalyticsLanguageClient.java | 24 +++++++++++++++ .../analytics/GitHubReleaseDownloader.java | 30 +++++++++++++------ .../intellij/analytics/TelemetryService.java | 24 +++++++++++++++ src/main/resources/META-INF/plugin.xml | 1 + 6 files changed, 88 insertions(+), 9 deletions(-) create mode 100644 USAGE_DATA.md create mode 100644 src/main/java/org/jboss/tools/intellij/analytics/TelemetryService.java diff --git a/USAGE_DATA.md b/USAGE_DATA.md new file mode 100644 index 0000000..2997e8a --- /dev/null +++ b/USAGE_DATA.md @@ -0,0 +1,9 @@ +## [Dependency Analytics](https://github.com/redhat-developer/intellij-dependency-analytics) + +### Usage Data + +* when plugin is started +* when server is downloaded +* when a component analysis is done +* when plugin is shut down + diff --git a/build.gradle b/build.gradle index c173d9a..eaa6ed4 100644 --- a/build.gradle +++ b/build.gradle @@ -22,6 +22,7 @@ targetCompatibility = '1.8' intellij { version ideaVersion //for a full list of IntelliJ IDEA releases please see https://www.jetbrains.com/intellij-repository/releases pluginName 'org.jboss.tools.intellij.analytics' + plugins 'com.redhat.devtools.intellij.telemetry:0.0.1.9' updateSinceUntilBuild false } @@ -39,5 +40,13 @@ dependencies { testImplementation("junit:junit:4.12") } +runIde { + systemProperties['com.redhat.devtools.intellij.telemetry.mode'] = 'debug' +} + +runIdeForUiTests { + systemProperties['com.redhat.devtools.intellij.telemetry.mode'] = 'debug' +} + group 'org.jboss.tools.intellij' version projectVersion // Plugin version diff --git a/src/main/java/org/jboss/tools/intellij/analytics/AnalyticsLanguageClient.java b/src/main/java/org/jboss/tools/intellij/analytics/AnalyticsLanguageClient.java index 7a54906..4582ffd 100644 --- a/src/main/java/org/jboss/tools/intellij/analytics/AnalyticsLanguageClient.java +++ b/src/main/java/org/jboss/tools/intellij/analytics/AnalyticsLanguageClient.java @@ -3,11 +3,17 @@ import com.intellij.notification.Notification; import com.intellij.notification.NotificationType; import com.intellij.notification.Notifications; +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage; import org.eclipse.lsp4j.jsonrpc.services.JsonNotification; import org.jetbrains.annotations.NotNull; import org.wso2.lsp4intellij.client.ClientContext; import org.wso2.lsp4intellij.client.DefaultLanguageClient; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.URL; +import java.nio.file.Paths; import java.util.Map; public class AnalyticsLanguageClient extends DefaultLanguageClient { @@ -15,13 +21,31 @@ public AnalyticsLanguageClient(@NotNull ClientContext context) { super(context); } + private static String getFilename(Map info) { + String filename = null; + String url = (String) info.get("uri"); + if (url != null) { + try { + filename = Paths.get(new URI(url)).getFileName().toString(); + } catch (URISyntaxException e) {} + } + return filename; + } + @JsonNotification("caNotification") public void caNotify(Object payload) { if (payload instanceof Map) { Map info = (Map) payload; if (info.containsKey("data") && info.containsKey("diagCount")) { + ActionMessage telemetry = TelemetryService.instance().action("lsp:component_analysis_done"); + String filename = getFilename(info); + if (filename != null) { + telemetry.property("filename", filename); + } + telemetry.send(); Notifications.Bus.notify(new Notification("Analytics", "Analytics", (String) info.get("data"), NotificationType.INFORMATION)); } } } + } diff --git a/src/main/java/org/jboss/tools/intellij/analytics/GitHubReleaseDownloader.java b/src/main/java/org/jboss/tools/intellij/analytics/GitHubReleaseDownloader.java index 4eb3222..55683c9 100644 --- a/src/main/java/org/jboss/tools/intellij/analytics/GitHubReleaseDownloader.java +++ b/src/main/java/org/jboss/tools/intellij/analytics/GitHubReleaseDownloader.java @@ -8,13 +8,17 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.components.Service; import com.intellij.openapi.components.ServiceManager; -import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.extensions.PluginId; import com.intellij.openapi.progress.ProgressIndicator; import com.intellij.openapi.project.Project; import com.intellij.util.io.HttpRequests; +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public final class GitHubReleaseDownloader { + private static Logger LOGGER = LoggerFactory.getLogger(GitHubReleaseDownloader.class); private final IdeaPluginDescriptor descriptor = PluginManager.getPlugin(PluginId.getId("org.jboss.tools.intellij.analytics")); private final String fileName; private final ICookie cookies; @@ -37,14 +41,22 @@ public File download(final ProgressIndicator indicator) throws IOException { if (!isNewRelease(latestReleaseTag) && dest.exists()) { return dest; } - final String url = this.release.getDownloadUri(latestReleaseTag, this.fileName); - HttpRequests - .request(url) - .productNameAsUserAgent() - .saveToFile(dest, indicator); + final ActionMessage telemetry = TelemetryService.instance().action("lsp:download").property("lspVersion", latestReleaseTag); + try { + final String url = this.release.getDownloadUri(latestReleaseTag, this.fileName); + HttpRequests + .request(url) + .productNameAsUserAgent() + .saveToFile(dest, indicator); - dest.setExecutable(true); - cookies.setValue(ICookie.Name.LSPVersion, latestReleaseTag); - return dest; + dest.setExecutable(true); + cookies.setValue(ICookie.Name.LSPVersion, latestReleaseTag); + telemetry.send(); + return dest; + } catch (IOException e) { + telemetry.error(e).send(); + LOGGER.warn(e.getLocalizedMessage(), e); + throw e; + } } } diff --git a/src/main/java/org/jboss/tools/intellij/analytics/TelemetryService.java b/src/main/java/org/jboss/tools/intellij/analytics/TelemetryService.java new file mode 100644 index 0000000..079c782 --- /dev/null +++ b/src/main/java/org/jboss/tools/intellij/analytics/TelemetryService.java @@ -0,0 +1,24 @@ +/******************************************************************************* + * Copyright (c) 2021 Red Hat, Inc. + * Distributed under license by Red Hat, Inc. All rights reserved. + * This program is made available under the terms of the + * Eclipse Public License v2.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v20.html + * + * Contributors: + * Red Hat, Inc. - initial API and implementation + ******************************************************************************/ +package org.jboss.tools.intellij.analytics; + +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; +import com.redhat.devtools.intellij.telemetry.core.util.Lazy; + +public class TelemetryService { + private static final TelemetryService INSTANCE = new TelemetryService(); + + private final Lazy builder = new Lazy<>(() -> new TelemetryMessageBuilder(TelemetryService.class.getClassLoader())); + + public static TelemetryMessageBuilder instance() { + return INSTANCE.builder.get(); + } +} diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index 5503c28..f8743fa 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -88,6 +88,7 @@ com.intellij.modules.lang + com.redhat.devtools.intellij.telemetry