Skip to content

Commit

Permalink
Add telemetry/event support
Browse files Browse the repository at this point in the history
This PR fix #430

The OSUtils was renamed to Platform which is more generic (eclipse like
name) which contains information about OS, JVM (name, version, memory).

Signed-off-by: azerr <[email protected]>
  • Loading branch information
angelozerr committed Jul 4, 2019
1 parent 2df4bec commit fbe1f12
Show file tree
Hide file tree
Showing 12 changed files with 432 additions and 29 deletions.
171 changes: 171 additions & 0 deletions org.eclipse.lsp4xml/src/main/java/org/eclipse/lsp4xml/Platform.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* 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.eclipse.lsp4xml;

/**
* PLatform information about OS and JVM.
*/
public class Platform {

private static final OS os = new OS();

public static final boolean isWindows = getOS().isWindows();
public static String SLASH = isWindows ? "\\" : "/";

/**
* OS information
*/
public static class OS {

private final String name;

private final String version;

private final String arch;

private final transient boolean isWindows;

public OS() {
this.name = getSystemProperty("os.name");
this.version = getSystemProperty("os.version");
this.arch = getSystemProperty("os.arch");
isWindows = name != null && name.toLowerCase().indexOf("win") >= 0;
}

/**
* Returns the OS name.
*
* @return the OS name.
*/
public String getName() {
return name;
}

/**
* Returns the OS version.
*
* @return the OS version.
*/
public String getVersion() {
return version;
}

/**
* Returns the OS arch.
*
* @return the OS arch.
*/
public String getArch() {
return arch;
}

public boolean isWindows() {
return isWindows;
}
}

/**
* JVM information
*
*/
public static class JVM {

/**
* JVM memory information
*
*/
public static class Memory {

private final long free;

private final long total;

private final long max;

private Memory() {
super();
this.free = Runtime.getRuntime().freeMemory();
this.total = Runtime.getRuntime().totalMemory();
this.max = Runtime.getRuntime().maxMemory();
}

public long getFree() {
return free;
}

public long getTotal() {
return total;
}

public long getMax() {
return max;
}

}

private final String name;

private final String version;

private final Memory memory;

public JVM() {
this.name = getSystemProperty("java.vm.name");
this.version = getSystemProperty("java.version");
this.memory = new Memory();
}

/**
* Returns the JVM name
*
* @return the JVM name
*/
public String getName() {
return name;
}

/**
* Returns the JVM version
*
* @return the JVM version
*/
public String getVersion() {
return version;
}

public Memory getMemory() {
return memory;
}
}

/**
* Returns the OS information.
*
* @return the OS information.
*/
public static OS getOS() {
return os;
}

/**
* Returns the system property from the given key and null otherwise.
*
* @param key the property system key
* @return the system property from the given key and null otherwise.
*/
private static String getSystemProperty(String key) {
try {
return System.getProperty(key);
} catch (SecurityException e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;
import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesInitializer;
import org.eclipse.lsp4xml.settings.capabilities.XMLCapabilityManager;
import org.eclipse.lsp4xml.telemetry.TelemetryManager;
import org.eclipse.lsp4xml.utils.FilesUtils;

/**
Expand All @@ -72,6 +73,7 @@ public class XMLLanguageServer
private final ScheduledExecutorService delayer;
private Integer parentProcessId;
public XMLCapabilityManager capabilityManager;
private TelemetryManager telemetryManager;

public XMLLanguageServer() {
xmlLanguageService = new XMLLanguageService();
Expand Down Expand Up @@ -111,6 +113,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {
@Override
public void initialized(InitializedParams params) {
capabilityManager.initializeCapabilities();
getTelemetryManager().onInitialized(params);
}

/**
Expand Down Expand Up @@ -207,6 +210,7 @@ public WorkspaceService getWorkspaceService() {
public void setClient(LanguageClient languageClient) {
this.languageClient = languageClient;
capabilityManager = new XMLCapabilityManager(this.languageClient, xmlTextDocumentService);
telemetryManager = new TelemetryManager(languageClient);
}

public LanguageClient getLanguageClient() {
Expand Down Expand Up @@ -242,4 +246,13 @@ public DOMDocument getDocument(String uri) {
ModelTextDocument<DOMDocument> document = xmlTextDocumentService.getDocument(uri);
return document != null ? document.getModel().getNow(null) : null;
}

/**
* Returns the telemetry manager.
*
* @return the telemetry manager.
*/
public TelemetryManager getTelemetryManager() {
return telemetryManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
import org.eclipse.lsp4xml.settings.XMLFormattingOptions;
import org.eclipse.lsp4xml.settings.XMLIncrementalSupportSettings;
import org.eclipse.lsp4xml.settings.XMLSymbolSettings;
import org.eclipse.lsp4xml.settings.capabilities.ServerCapabilitiesConstants;
import org.eclipse.lsp4xml.utils.XMLPositionUtility;

/**
Expand Down Expand Up @@ -144,7 +145,7 @@ public XMLTextDocumentService(XMLLanguageServer xmlLanguageServer) {
}

public void updateClientCapabilities(ClientCapabilities capabilities) {
TextDocumentClientCapabilities textDocumentClientCapabilities = capabilities.getTextDocument();
TextDocumentClientCapabilities textDocumentClientCapabilities = capabilities != null ? capabilities.getTextDocument() : null;
if (textDocumentClientCapabilities != null) {
// Completion settings
sharedSettings.completionSettings.setCapabilities(textDocumentClientCapabilities.getCompletion());
Expand Down Expand Up @@ -220,6 +221,7 @@ public CompletableFuture<List<Either<SymbolInformation, DocumentSymbol>>> docume

@Override
public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormattingParams params) {
xmlLanguageServer.getTelemetryManager().onServiceCall(ServerCapabilitiesConstants.TEXT_DOCUMENT_FORMATTING);
return computeAsync((cancelChecker) -> {
String uri = params.getTextDocument().getUri();
TextDocument document = getDocument(uri);
Expand All @@ -230,6 +232,7 @@ public CompletableFuture<List<? extends TextEdit>> formatting(DocumentFormatting

@Override
public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRangeFormattingParams params) {
xmlLanguageServer.getTelemetryManager().onServiceCall(ServerCapabilitiesConstants.TEXT_DOCUMENT_RANGE_FORMATTING);
return computeAsync((cancelChecker) -> {
String uri = params.getTextDocument().getUri();
TextDocument document = getDocument(uri);
Expand All @@ -240,6 +243,7 @@ public CompletableFuture<List<? extends TextEdit>> rangeFormatting(DocumentRange

@Override
public CompletableFuture<WorkspaceEdit> rename(RenameParams params) {
xmlLanguageServer.getTelemetryManager().onServiceCall(ServerCapabilitiesConstants.TEXT_DOCUMENT_RENAME);
return computeDOMAsync(params.getTextDocument(), (cancelChecker, xmlDocument) -> {
return getXMLLanguageService().doRename(xmlDocument, params.getPosition(), params.getNewName());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*******************************************************************************/
package org.eclipse.lsp4xml.commons;

import static org.eclipse.lsp4xml.utils.OSUtils.isWindows;
import static org.eclipse.lsp4xml.Platform.isWindows;

import java.io.IOException;
import java.util.concurrent.Executors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

package org.eclipse.lsp4xml.extensions.general.completion;

import static org.eclipse.lsp4xml.Platform.isWindows;
import static org.eclipse.lsp4xml.utils.FilesUtils.convertToWindowsPath;
import static org.eclipse.lsp4xml.utils.FilesUtils.getFilePathSlash;
import static org.eclipse.lsp4xml.utils.FilesUtils.getNormalizedPath;
import static org.eclipse.lsp4xml.utils.OSUtils.isWindows;
import static org.eclipse.lsp4xml.utils.StringUtils.isEmpty;

import java.io.File;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* 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.eclipse.lsp4xml.telemetry;

import org.eclipse.lsp4xml.Platform;
import org.eclipse.lsp4xml.Platform.JVM;
import org.eclipse.lsp4xml.Platform.OS;
import org.eclipse.lsp4xml.utils.VersionHelper;

/**
* Telemetry data to collect.
*
* <ul>
* <li>Server Version</li>
* <li>JVM information</li>
* <li>Memory information</li>
* </ul>
*
* @author Angelo ZERR
*
*/
public class ServerInfo {

private final String version;

private OS os;

private JVM jvm;

public ServerInfo() {
this.version = VersionHelper.getVersion();
this.os = Platform.getOS();
this.jvm = new JVM();
}

public String getVersion() {
return version;
}

public OS getOs() {
return os;
}

public JVM getJvm() {
return jvm;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*******************************************************************************
* Copyright (c) 2019 Red Hat Inc. and others.
* All rights reserved. This program and the accompanying materials
* 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.eclipse.lsp4xml.telemetry;

import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.services.LanguageClient;

/**
* Telemetry manager.
*
* @author Angelo ZERRs
*
*/
public class TelemetryManager {

private final LanguageClient languageClient;

public TelemetryManager(LanguageClient languageClient) {
this.languageClient = languageClient;
}

/**
* Send a telemetry event on start of the LSP server
*
* @param params
*/
public void onInitialized(InitializedParams params) {
telemetryEvent(new ServerInfo());
}

/**
* Send a telemetry event on call of a service.
*
* @param serviceName service name.
*/
public void onServiceCall(String serviceName) {
telemetryEvent(serviceName);
}

/**
* The telemetry notification is sent from the server to the client to ask the
* client to log a telemetry event.
*/
public void telemetryEvent(Object object) {
languageClient.telemetryEvent(object);
}

}
Loading

0 comments on commit fbe1f12

Please sign in to comment.