From 61379ce7378512dccc519aeb25449f335cc3e269 Mon Sep 17 00:00:00 2001 From: azerr Date: Wed, 4 Sep 2024 22:05:59 +0200 Subject: [PATCH] fix: Don't crash language servers if telemetry cannot be initialized Fixes #1384 Signed-off-by: azerr --- .../quarkus/QuarkusDeploymentSupport.java | 21 +++-- .../intellij/quarkus/TelemetryService.java | 35 -------- .../buildtool/maven/MavenToolDelegate.java | 2 +- .../intellij/quarkus/lsp/QuarkusServer.java | 12 ++- .../projectWizard/QuarkusModuleBuilder.java | 16 +++- .../quarkus/run/QuarkusRunConfiguration.java | 27 ++++-- .../QuarkusRunDashboardCustomizer.java | 19 ++-- .../telemetry/NoOpTelemetryService.java | 20 +++++ .../telemetry/RedHatTelemetryService.java | 89 +++++++++++++++++++ .../quarkus/telemetry/TelemetryEventName.java | 53 +++++++++++ .../quarkus/telemetry/TelemetryManager.java | 62 +++++++++++++ .../quarkus/telemetry/TelemetryService.java | 63 +++++++++++++ .../intellij/qute/lsp/QuteServer.java | 12 ++- src/main/resources/META-INF/plugin.xml | 2 + 14 files changed, 357 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/TelemetryService.java create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/NoOpTelemetryService.java create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/RedHatTelemetryService.java create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryEventName.java create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryManager.java create mode 100644 src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryService.java diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusDeploymentSupport.java b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusDeploymentSupport.java index e07646b34..14a93a706 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusDeploymentSupport.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/QuarkusDeploymentSupport.java @@ -35,6 +35,8 @@ import com.redhat.devtools.intellij.lsp4mp4ij.classpath.ClasspathResourceChangedManager; import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; import com.redhat.devtools.intellij.quarkus.search.QuarkusModuleComponent; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.Logger; @@ -92,6 +94,7 @@ public CompletableFuture updateClasspathWithQuarkusDeploymentAsync(Module public static void updateClasspathWithQuarkusDeployment(Module module, ProgressIndicator progressIndicator) { if (module.isDisposed()) return; + Project project = module.getProject(); LOGGER.info("Ensuring library to " + module.getName()); long start = System.currentTimeMillis(); BuildToolDelegate toolDelegate = BuildToolDelegate.getDelegate(module); @@ -110,22 +113,24 @@ public static void updateClasspathWithQuarkusDeployment(Module module, ProgressI Library library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME); while (library != null) { table.removeLibrary(library); - try { - TelemetryService.instance().action(TelemetryService.MODEL_PREFIX + "removeLibrary"); - } catch (Exception e) { - } + // Send "model-removeLibrary" telemetry event + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.MODEL_REMOVE_LIBRARY); + library = table.getLibraryByName(QuarkusConstants.QUARKUS_DEPLOYMENT_LIBRARY_NAME); } progressIndicator.checkCanceled(); progressIndicator.setText("Adding in ''" + module.getName() + "'' Quarkus deployment dependencies to classpath..."); List[] files = toolDelegate.getDeploymentFiles(module, progressIndicator); LOGGER.info("Adding library to " + module.getName() + " previousHash=" + previousHash + " newHash=" + actualHash); - try { - TelemetryService.instance().action(TelemetryService.MODEL_PREFIX + "addLibrary").send(); - } catch (Exception e) { - } + // Send "model-addLibrary" telemetry event + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.MODEL_ADD_LIBRARY); + addLibrary(model, files); }); component.setHash(actualHash); diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/TelemetryService.java b/src/main/java/com/redhat/devtools/intellij/quarkus/TelemetryService.java deleted file mode 100644 index 411e83baa..000000000 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/TelemetryService.java +++ /dev/null @@ -1,35 +0,0 @@ -/******************************************************************************* - * 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 com.redhat.devtools.intellij.quarkus; - -import com.intellij.ide.plugins.PluginManager; -import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; -import com.redhat.devtools.intellij.telemetry.core.util.Lazy; - -public class TelemetryService { - public static final String LSP_PREFIX = "lsp-"; - - public static final String UI_PREFIX = "ui-"; - - public static final String MODEL_PREFIX = "model-"; - - public static final String RUN_PREFIX = "run-"; - - private static final TelemetryService INSTANCE = new TelemetryService(); - - private final Lazy builder = new Lazy<>(() -> - new TelemetryMessageBuilder(PluginManager.getPluginByClass(this.getClass())) - ); - - public static TelemetryMessageBuilder instance() { - return INSTANCE.builder.get(); - } -} diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java index c45db09b8..b6b61d15d 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/buildtool/maven/MavenToolDelegate.java @@ -203,7 +203,7 @@ private Set resolveDeploymentArtifacts(Module module, MavenProjec } } } - } catch (MavenProcessCanceledException | RuntimeException e) { + } catch (Exception e) { LOGGER.warn(e.getLocalizedMessage(), e); } return deploymentArtifacts; diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/QuarkusServer.java b/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/QuarkusServer.java index b71269a5d..76725e916 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/QuarkusServer.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/lsp/QuarkusServer.java @@ -15,14 +15,14 @@ import com.intellij.openapi.extensions.PluginId; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; import com.redhat.devtools.lsp4ij.server.JavaProcessCommandBuilder; import com.redhat.devtools.lsp4ij.server.ProcessStreamConnectionProvider; import com.redhat.devtools.intellij.lsp4mp4ij.settings.UserDefinedMicroProfileSettings; -import com.redhat.devtools.intellij.quarkus.TelemetryService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.net.URI; import java.nio.file.Path; import java.util.Arrays; import java.util.HashMap; @@ -55,11 +55,9 @@ public QuarkusServer(Project project) { commands.add("-DrunAsync=true"); super.setCommands(commands); - try { - TelemetryService.instance().action(TelemetryService.LSP_PREFIX + "start").send(); - } catch (Exception e) { - LOGGER.error("Error while consuming telemetry service", e); - } + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.LSP_START_MICROPROFILE_SERVER); } @Override diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusModuleBuilder.java b/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusModuleBuilder.java index ee3d955ce..8e1f2cf69 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusModuleBuilder.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/projectWizard/QuarkusModuleBuilder.java @@ -23,7 +23,9 @@ import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.RefreshQueue; import com.redhat.devtools.intellij.quarkus.QuarkusConstants; -import com.redhat.devtools.intellij.quarkus.TelemetryService; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService; import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; import org.jdom.JDOMException; import org.jetbrains.annotations.NotNull; @@ -86,15 +88,21 @@ public ModuleWizardStep modifySettingsStep(@NotNull SettingsStep settingsStep) { @NotNull @Override public Module createModule(@NotNull ModifiableModuleModel moduleModel) throws InvalidDataException, IOException, ModuleWithNameAlreadyExists, JDOMException, ConfigurationException { - TelemetryMessageBuilder.ActionMessage telemetry = TelemetryService.instance().action(TelemetryService.UI_PREFIX + "wizard"); + TelemetryService telemetryService = TelemetryManager.getInstance(moduleModel.getProject()) + .getTelemetryService(); + TelemetryMessageBuilder.ActionMessage telemetryMessage = + telemetryService + .action(TelemetryEventName.UI_WIZARD); try { processDownload(); Module module = super.createModule(moduleModel); wizardContext.getUserData(QuarkusConstants.WIZARD_TOOL_KEY).processImport(module); - telemetry.send(); + // Send "ui-wizard" telemetry event with no error + telemetryService.asyncSend(telemetryMessage); return module; } catch (IOException | InvalidDataException | ModuleWithNameAlreadyExists | JDOMException | ConfigurationException e) { - telemetry.error(e).send(); + // Send "ui-wizard" telemetry event with error + telemetryService.asyncSend(telemetryMessage.error(e)); throw e; } } diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfiguration.java b/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfiguration.java index 32164f4f5..90bc483ae 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfiguration.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/run/QuarkusRunConfiguration.java @@ -33,8 +33,10 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.Messages; import com.redhat.devtools.intellij.quarkus.QuarkusModuleUtil; -import com.redhat.devtools.intellij.quarkus.TelemetryService; import com.redhat.devtools.intellij.quarkus.buildtool.BuildToolDelegate; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService; import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -121,23 +123,32 @@ private void allocateLocalPort() { @Nullable @Override public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment environment) throws ExecutionException { - TelemetryMessageBuilder.ActionMessage telemetry = TelemetryService.instance().action(TelemetryService.RUN_PREFIX + "run"); - telemetry.property("kind", executor.getId()); - BuildToolDelegate toolDelegate = BuildToolDelegate.getDelegate(getModule()); + Module module = getModule(); + + TelemetryService telemetryService = TelemetryManager.getInstance(module.getProject()) + .getTelemetryService(); + TelemetryMessageBuilder.ActionMessage telemetryMessage = + telemetryService + .action(TelemetryEventName.RUN_RUN); + + telemetryMessage.property("kind", executor.getId()); + BuildToolDelegate toolDelegate = BuildToolDelegate.getDelegate(module); allocateLocalPort(); RunProfileState state = null; if (toolDelegate != null) { - telemetry.property("tool", toolDelegate.getDisplay()); + telemetryMessage.property("tool", toolDelegate.getDisplay()); // Create a Gradle or Maven run configuration in memory - RunnerAndConfigurationSettings settings = toolDelegate.getConfigurationDelegate(getModule(), this); + RunnerAndConfigurationSettings settings = toolDelegate.getConfigurationDelegate(module, this); if (settings != null) { long groupId = ExecutionEnvironment.getNextUnusedExecutionId(); state = doRunConfiguration(settings, executor, DefaultExecutionTarget.INSTANCE, groupId, null); } } else { - telemetry.property("tool", "not found"); + telemetryMessage.property("tool", "not found"); } - telemetry.send(); + // Send "run-run" telemetry event + telemetryService.asyncSend(telemetryMessage); + if (executor.getId() == DefaultDebugExecutor.EXECUTOR_ID) { ProgressManager.getInstance().run(new Task.Backgroundable(getProject(), QUARKUS_CONFIGURATION, false) { @Override diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/run/dashboard/QuarkusRunDashboardCustomizer.java b/src/main/java/com/redhat/devtools/intellij/quarkus/run/dashboard/QuarkusRunDashboardCustomizer.java index 0042db922..c96479a33 100644 --- a/src/main/java/com/redhat/devtools/intellij/quarkus/run/dashboard/QuarkusRunDashboardCustomizer.java +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/run/dashboard/QuarkusRunDashboardCustomizer.java @@ -17,13 +17,15 @@ import com.intellij.execution.ui.RunContentDescriptor; import com.intellij.ide.projectView.PresentationData; import com.intellij.openapi.module.Module; +import com.intellij.openapi.project.Project; import com.intellij.ui.SimpleColoredComponent; import com.intellij.ui.SimpleTextAttributes; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project.PsiMicroProfileProject; import com.redhat.devtools.intellij.lsp4mp4ij.psi.core.project.PsiMicroProfileProjectManager; import com.redhat.devtools.intellij.quarkus.QuarkusModuleUtil; -import com.redhat.devtools.intellij.quarkus.TelemetryService; import com.redhat.devtools.intellij.quarkus.run.QuarkusRunConfiguration; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -59,7 +61,8 @@ public boolean updatePresentation(@NotNull PresentationData presentation, @NotNu QuarkusRunConfiguration quarkusRunConfiguration = (QuarkusRunConfiguration) node.getConfigurationSettings().getConfiguration(); Module module = quarkusRunConfiguration.getModule(); if (QuarkusModuleUtil.isQuarkusWebAppModule(module)) { - PsiMicroProfileProject mpProject = PsiMicroProfileProjectManager.getInstance(module.getProject()).getMicroProfileProject(module); + Project project = module.getProject(); + PsiMicroProfileProject mpProject = PsiMicroProfileProjectManager.getInstance(project).getMicroProfileProject(module); // It is a Web application, add links for: // - Opening quarkus application in a browser @@ -81,8 +84,10 @@ public boolean updatePresentation(@NotNull PresentationData presentation, @NotNu public void run() { // Open Quarkus application in a Web Browser super.run(); - // Send event with telemetry - TelemetryService.instance().action(TelemetryService.UI_PREFIX + "openApplication").send(); + // Send "ui-openApplication" telemetry event + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.UI_OPEN_APPLICATION); } }); links.put(devUILabel, new SimpleColoredComponent.BrowserLauncherTag(devUIUrl) { @@ -90,8 +95,10 @@ public void run() { public void run() { // Open DevUI in a Web Browser super.run(); - // Send event with telemetry - TelemetryService.instance().action(TelemetryService.UI_PREFIX + "openDevUI").send(); + // Send "ui-openDevUI" telemetry event + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.UI_OPEN_DEV_UI); } }); node.putUserData(RunDashboardCustomizer.NODE_LINKS, links); diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/NoOpTelemetryService.java b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/NoOpTelemetryService.java new file mode 100644 index 000000000..a4324eac8 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/NoOpTelemetryService.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.telemetry; + +/** + * No-Op implementation of telemetry service + */ +public class NoOpTelemetryService implements TelemetryService { +} diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/RedHatTelemetryService.java b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/RedHatTelemetryService.java new file mode 100644 index 000000000..bcd9d81ac --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/RedHatTelemetryService.java @@ -0,0 +1,89 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.telemetry; + +import com.intellij.ide.plugins.PluginManager; +import com.intellij.openapi.application.ApplicationManager; +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; +import com.redhat.devtools.intellij.telemetry.core.util.Lazy; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Red Hat Telemetry Service, used when intellij-redhat-telemetry is available + */ +public class RedHatTelemetryService implements TelemetryService { + + private static final Logger LOGGER = LoggerFactory.getLogger(RedHatTelemetryService.class); + + private final Lazy builder = new Lazy<>(() -> new TelemetryMessageBuilder(PluginManager.getPluginByClass(this.getClass()))); + + private boolean hasError; + @Override + public void send(TelemetryEventName eventName, Map properties) { + TelemetryMessageBuilder.ActionMessage action = action(eventName, properties); + if (action == null) { + return; + } + asyncSend(action); + } + + @Override + public @Nullable TelemetryMessageBuilder.ActionMessage action(TelemetryEventName eventName, Map properties) { + TelemetryMessageBuilder builder = getMessageBuilder(); + if (builder == null) { + return null; + } + TelemetryMessageBuilder.ActionMessage action = builder.action(eventName.getEventName()); + if (properties != null) { + properties.forEach((k, v) -> { + action.property(k, v); + }); + } + return action; + } + + @Nullable + private TelemetryMessageBuilder getMessageBuilder() { + if (hasError) { + return null; + } + try { + return builder.get(); + } + catch(Exception e) { + hasError = true; + return null; + } + } + + @Override + public void asyncSend(@Nullable TelemetryMessageBuilder.ActionMessage message) { + if (message == null) { + return; + } + ApplicationManager.getApplication().executeOnPooledThread(() -> { + try{ + message.send(); + } catch (Exception e) { + LOGGER.warn("Failed to send Telemetry data : {}", e.getMessage()); + } + }); + } + +} diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryEventName.java b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryEventName.java new file mode 100644 index 000000000..852df2dfc --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryEventName.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.telemetry; + +import static com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService.LSP_PREFIX; +import static com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService.MODEL_PREFIX; +import static com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService.UI_PREFIX; +import static com.redhat.devtools.intellij.quarkus.telemetry.TelemetryService.RUN_PREFIX; + +/** + * Quarkus telemetry event name. + */ +public enum TelemetryEventName { + + // LSP event names + LSP_START_MICROPROFILE_SERVER(LSP_PREFIX + "start"), + LSP_START_QUTE_SERVER(LSP_PREFIX + "startQute"), + + // Model event names + MODEL_REMOVE_LIBRARY(MODEL_PREFIX + "removeLibrary"), + MODEL_ADD_LIBRARY(MODEL_PREFIX + "addLibrary"), + + // UI event names + UI_WIZARD(UI_PREFIX + "wizard"), + UI_OPEN_APPLICATION(UI_PREFIX + "openApplication"), + UI_OPEN_DEV_UI(UI_PREFIX + "openDevUI"), + + // Run event names + RUN_RUN(RUN_PREFIX + "run"); + + private final String eventName; + + TelemetryEventName(String eventName) { + this.eventName = eventName; + } + + + public String getEventName() { + return eventName; + } + +} \ No newline at end of file diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryManager.java b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryManager.java new file mode 100644 index 000000000..2de430066 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryManager.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2023 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.telemetry; + +import com.intellij.openapi.Disposable; +import com.intellij.openapi.project.Project; +import com.redhat.devtools.lsp4ij.LanguageServersRegistry; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplate; +import com.redhat.devtools.lsp4ij.launching.templates.LanguageServerTemplateManager; +import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinition; +import com.redhat.devtools.lsp4ij.server.definition.LanguageServerDefinitionListener; +import com.redhat.devtools.lsp4ij.server.definition.extension.ExtensionLanguageServerDefinition; +import com.redhat.devtools.lsp4ij.server.definition.launching.UserDefinedLanguageServerDefinition; +import org.jetbrains.annotations.NotNull; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +/** + * Helper to initialize Telemetry. A Telemetry service based on intellij-redhat-telemetry, + * will be instantiated if that plugin is installed, a No-Op Telemetry stub will be used otherwise. + */ +public class TelemetryManager implements Disposable { + + private final Project project; + private TelemetryService telemetryService; + private LanguageServerDefinitionListener telemetryListener; + + public TelemetryManager(Project project) { + this.project = project; + try { + Class.forName("com.redhat.devtools.intellij.telemetry.core.service.TelemetryService"); + telemetryService = new RedHatTelemetryService(); + } catch (Exception ignore) { + telemetryService = new NoOpTelemetryService(); + } + } + + public static TelemetryManager getInstance(@NotNull Project project) { + return project.getService(TelemetryManager.class); + } + + public TelemetryService getTelemetryService() { + return telemetryService; + } + + @Override + public void dispose() { + } +} diff --git a/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryService.java b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryService.java new file mode 100644 index 000000000..680d72e43 --- /dev/null +++ b/src/main/java/com/redhat/devtools/intellij/quarkus/telemetry/TelemetryService.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2024 Red Hat Inc. and others. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0 which is available at + * http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 + * which is available at https://www.apache.org/licenses/LICENSE-2.0. + * + * SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 + * + * Contributors: + * Red Hat Inc. - initial API and implementation + *******************************************************************************/ +package com.redhat.devtools.intellij.quarkus.telemetry; + +import com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.Map; + +/** + * Telemetry Service facade + */ +public interface TelemetryService { + + String LSP_PREFIX = "lsp-"; + + String UI_PREFIX = "ui-"; + + String MODEL_PREFIX = "model-"; + + String RUN_PREFIX = "run-"; + + /** + * Sends a tracking event without additional properties. + * @param eventName the name of the event + */ + default void send(TelemetryEventName eventName){ + send(eventName, null); + } + + /** + * Sends a tracking event with additional properties. + * @param eventName the name of the event + * @param properties the properties of the event + */ + default void send(TelemetryEventName eventName, Map properties){} + + @Nullable + default TelemetryMessageBuilder.ActionMessage action(TelemetryEventName eventName) { + return action(eventName, null); + } + + @Nullable + default TelemetryMessageBuilder.ActionMessage action(TelemetryEventName eventName, Map properties){ + return null; + } + + default void asyncSend(@Nullable TelemetryMessageBuilder.ActionMessage message) {} +} diff --git a/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteServer.java b/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteServer.java index eea161650..64f0db915 100644 --- a/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteServer.java +++ b/src/main/java/com/redhat/devtools/intellij/qute/lsp/QuteServer.java @@ -15,9 +15,10 @@ import com.intellij.openapi.extensions.PluginId; import com.intellij.openapi.project.Project; import com.intellij.openapi.vfs.VirtualFile; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryEventName; +import com.redhat.devtools.intellij.quarkus.telemetry.TelemetryManager; import com.redhat.devtools.lsp4ij.server.JavaProcessCommandBuilder; import com.redhat.devtools.lsp4ij.server.ProcessStreamConnectionProvider; -import com.redhat.devtools.intellij.quarkus.TelemetryService; import com.redhat.devtools.intellij.qute.settings.UserDefinedQuteSettings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -52,12 +53,9 @@ public QuteServer(Project project) { commands.add("-DrunAsync=true"); super.setCommands(commands); - try { - TelemetryService.instance().action(TelemetryService.LSP_PREFIX + "startQute").send(); - } - catch(Exception e) { - LOGGER.error("Error while consuming telemetry service", e); - } + TelemetryManager.getInstance(project) + .getTelemetryService() + .send(TelemetryEventName.LSP_START_QUTE_SERVER); } @Override diff --git a/src/main/resources/META-INF/plugin.xml b/src/main/resources/META-INF/plugin.xml index df9026641..fc8d47f66 100644 --- a/src/main/resources/META-INF/plugin.xml +++ b/src/main/resources/META-INF/plugin.xml @@ -365,6 +365,8 @@ +