Skip to content

Commit

Permalink
[WIP] Report telemetry events for text document services.
Browse files Browse the repository at this point in the history
- Resolves eclipse-lemminx#1066
- Report file extensions for didOpen & didClose

Signed-off-by: Roland Grunberg <[email protected]>
  • Loading branch information
rgrunber committed Jul 30, 2021
1 parent 99d860f commit 5c95b46
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public CompletableFuture<InitializeResult> initialize(InitializeParams params) {

this.parentProcessId = params.getProcessId();

xmlLanguageService.setTelemetryManager(getTelemetryManager());
// Update XML language service extensions with InitializeParams
xmlLanguageService.initializeParams(params);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelHoverParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelSymbolsProviderParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.ContentModelTypeDefinitionParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.DocumentTelemetryParticipant;
import org.eclipse.lemminx.extensions.contentmodel.participants.diagnostics.ContentModelDiagnosticsParticipant;
import org.eclipse.lemminx.extensions.contentmodel.settings.ContentModelSettings;
import org.eclipse.lemminx.extensions.contentmodel.settings.XMLValidationSettings;
Expand Down Expand Up @@ -80,6 +81,8 @@ public class ContentModelPlugin implements IXMLExtension {

private XMLValidationSettings currentValidationSettings;

private DocumentTelemetryParticipant documentTelemetryParticipant;

public ContentModelPlugin() {
completionParticipant = new ContentModelCompletionParticipant();
hoverParticipant = new ContentModelHoverParticipant();
Expand Down Expand Up @@ -184,6 +187,8 @@ public void start(InitializeParams params, XMLExtensionsRegistry registry) {
symbolsProviderParticipant = new ContentModelSymbolsProviderParticipant(contentModelManager);
registry.registerSymbolsProviderParticipant(symbolsProviderParticipant);
registry.registerCodeLensParticipant(codeLensParticipant);
documentTelemetryParticipant = new DocumentTelemetryParticipant(registry.getTelemetryManager());
registry.registerDocumentLifecycleParticipant(documentTelemetryParticipant);

// Register custom commands to re-validate XML files
IXMLCommandService commandService = registry.getCommandService();
Expand Down Expand Up @@ -211,6 +216,7 @@ public void stop(XMLExtensionsRegistry registry) {
registry.unregisterTypeDefinitionParticipant(typeDefinitionParticipant);
registry.unregisterSymbolsProviderParticipant(symbolsProviderParticipant);
registry.unregisterCodeLensParticipant(codeLensParticipant);
registry.unregisterDocumentLifecycleParticipant(documentTelemetryParticipant);

// Un-register custom commands to re-validate XML files
IXMLCommandService commandService = registry.getCommandService();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*******************************************************************************
* Copyright (c) 2021 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
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.extensions.contentmodel.participants;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lemminx.services.extensions.IDocumentLifecycleParticipant;
import org.eclipse.lemminx.telemetry.TelemetryManager;

public class DocumentTelemetryParticipant implements IDocumentLifecycleParticipant {

private TelemetryManager telemetryManager;

public DocumentTelemetryParticipant(TelemetryManager telemetryManager) {
this.telemetryManager = telemetryManager;
}

@Override
public void didOpen(DOMDocument document) {
telemetryManager.onDidOpen(document);
}

@Override
public void didChange(DOMDocument document) {}

@Override
public void didSave(DOMDocument document) {}

@Override
public void didClose(DOMDocument document) {
telemetryManager.onDidClose(document);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.eclipse.lemminx.services.extensions.format.IFormatterParticipant;
import org.eclipse.lemminx.services.extensions.save.ISaveContext;
import org.eclipse.lemminx.services.extensions.save.ISaveContext.SaveContextType;
import org.eclipse.lemminx.telemetry.TelemetryManager;
import org.eclipse.lemminx.uriresolver.URIResolverExtensionManager;
import org.eclipse.lsp4j.InitializeParams;

Expand Down Expand Up @@ -74,6 +75,8 @@ public class XMLExtensionsRegistry implements IComponentProvider {

private final Map<Class<?>, Object> components;

private TelemetryManager telemetryManager;

public XMLExtensionsRegistry() {
extensions = new ArrayList<>();
completionParticipants = new ArrayList<>();
Expand All @@ -93,6 +96,7 @@ public XMLExtensionsRegistry() {
documentLifecycleParticipants = new ArrayList<>();
resolverExtensionManager = new URIResolverExtensionManager();
components = new HashMap<>();
telemetryManager = new TelemetryManager(null);
registerComponent(resolverExtensionManager);
}

Expand Down Expand Up @@ -505,4 +509,12 @@ public void setCommandService(IXMLCommandService commandService) {
this.commandService = commandService;
}

public TelemetryManager getTelemetryManager() {
return telemetryManager;
}

public void setTelemetryManager(TelemetryManager telemetryManager) {
this.telemetryManager = telemetryManager;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*******************************************************************************
* Copyright (c) 2021 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
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat Inc. - initial API and implementation
*******************************************************************************/
package org.eclipse.lemminx.telemetry;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.lemminx.dom.DOMDocument;

public class DocumentTelemetryInfo {

private static final String DOC_PROP_EXT = "extension";

public static Map<String, Object> getDocumentTelemetryInfo (DOMDocument doc) {
String uri = doc.getDocumentURI();
int index = uri.lastIndexOf('.');
String fileExtension = uri.substring(index + 1, uri.length());
HashMap<String, Object> props = new HashMap<>();
props.put(DOC_PROP_EXT, fileExtension);
return props;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*******************************************************************************/
package org.eclipse.lemminx.telemetry;

import org.eclipse.lemminx.dom.DOMDocument;
import org.eclipse.lsp4j.InitializedParams;
import org.eclipse.lsp4j.services.LanguageClient;

Expand All @@ -24,6 +25,9 @@ public class TelemetryManager {
*/
private static final String STARTUP_EVENT_NAME = "server.initialized";

private static final String DOC_OPEN_EVENT_NAME = "server.document.open";
private static final String DOC_CLOSE_EVENT_NAME = "server.document.close";

private final LanguageClient languageClient;

private boolean enabled;
Expand Down Expand Up @@ -51,6 +55,14 @@ public void onInitialized(InitializedParams params) {
}
}

public void onDidOpen(DOMDocument document) {
telemetryEvent(DOC_OPEN_EVENT_NAME, DocumentTelemetryInfo.getDocumentTelemetryInfo(document));
}

public void onDidClose(DOMDocument document) {
telemetryEvent(DOC_CLOSE_EVENT_NAME, DocumentTelemetryInfo.getDocumentTelemetryInfo(document));
}

/**
* The telemetry notification is sent from the server to the client to ask the
* client to log a telemetry event.
Expand Down

0 comments on commit 5c95b46

Please sign in to comment.