Skip to content

Commit

Permalink
fix: Don't crash language servers if telemetry cannot be initialized
Browse files Browse the repository at this point in the history
Fixes #1384

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Sep 5, 2024
1 parent 66f8c8e commit 61379ce
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -92,6 +94,7 @@ public CompletableFuture<Void> 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);
Expand All @@ -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<VirtualFile>[] 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);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ private Set<MavenArtifact> resolveDeploymentArtifacts(Module module, MavenProjec
}
}
}
} catch (MavenProcessCanceledException | RuntimeException e) {
} catch (Exception e) {
LOGGER.warn(e.getLocalizedMessage(), e);
}
return deploymentArtifacts;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -81,17 +84,21 @@ 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) {
@Override
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
}
Original file line number Diff line number Diff line change
@@ -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 <a href="https://github.com/redhat-developer/intellij-redhat-telemetry">intellij-redhat-telemetry</a> is available
*/
public class RedHatTelemetryService implements TelemetryService {

private static final Logger LOGGER = LoggerFactory.getLogger(RedHatTelemetryService.class);

private final Lazy<TelemetryMessageBuilder> builder = new Lazy<>(() -> new TelemetryMessageBuilder(PluginManager.getPluginByClass(this.getClass())));

private boolean hasError;
@Override
public void send(TelemetryEventName eventName, Map<String, String> properties) {
TelemetryMessageBuilder.ActionMessage action = action(eventName, properties);
if (action == null) {
return;
}
asyncSend(action);
}

@Override
public @Nullable TelemetryMessageBuilder.ActionMessage action(TelemetryEventName eventName, Map<String, String> 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());
}
});
}

}
Loading

0 comments on commit 61379ce

Please sign in to comment.