Skip to content

Commit

Permalink
refactor: Update the telemetry schema
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo committed Sep 21, 2023
1 parent 213b6de commit 0df6066
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@
/**
* The Object passed to the logger.
*/
public class LogEntity {
public class BspTraceEntity {
private final String kind;
private final String schemaVersion;
private final String buildServerVersion;
private final String operationName;
private final String duration;
private final String trace;
private final String rootCauseMessage;

private LogEntity(Builder builder) {
this.buildServerVersion = builder.buildServerVersion;
private BspTraceEntity(Builder builder) {
this.kind = "bsptrace";
this.schemaVersion = "1.0";
this.buildServerVersion = Constants.SERVER_VERSION;
this.operationName = builder.operationName;
this.duration = builder.duration;
this.trace = builder.trace;
Expand Down Expand Up @@ -47,16 +51,11 @@ public String getDuration() {
* Builder.
*/
public static class Builder {
private final String buildServerVersion;
private String rootCauseMessage;
private String trace;
private String operationName;
private String duration;

public Builder() {
this.buildServerVersion = Constants.SERVER_VERSION;
}

public Builder rootCauseMessage(String rootCauseMessage) {
this.rootCauseMessage = rootCauseMessage;
return this;
Expand All @@ -77,8 +76,8 @@ public Builder duration(String duration) {
return this;
}

public LogEntity build() {
return new LogEntity(this);
public BspTraceEntity build() {
return new BspTraceEntity(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

package com.microsoft.java.bs.core.internal.log;

import java.util.Map;
import java.util.logging.Handler;
import java.util.logging.LogRecord;

Expand All @@ -20,13 +21,12 @@ public class TelemetryHandler extends Handler {
@Override
public void publish(LogRecord logRecord) {
Object[] property = logRecord.getParameters();
if (property == null || property.length == 0 || !(property[0] instanceof LogEntity)) {
if (property == null || property.length == 0 || (!(property[0] instanceof BspTraceEntity)
&& !(property[0] instanceof Map))) {
return;
}

LogEntity entity = (LogEntity) property[0];

String jsonStr = new Gson().toJson(entity);
String jsonStr = new Gson().toJson(property[0]);
Launcher.client.onBuildLogMessage(new LogMessageParams(MessageType.LOG, jsonStr));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import org.eclipse.lsp4j.jsonrpc.messages.ResponseError;
import org.eclipse.lsp4j.jsonrpc.messages.ResponseErrorCode;

import com.microsoft.java.bs.core.internal.log.LogEntity;
import com.microsoft.java.bs.core.internal.log.BspTraceEntity;
import com.microsoft.java.bs.core.internal.services.BuildTargetService;
import com.microsoft.java.bs.core.internal.services.LifecycleService;

Expand Down Expand Up @@ -176,7 +176,7 @@ public CompletableFuture<JavacOptionsResult> buildTargetJavacOptions(JavacOption
}

private void handleNotification(String methodName, Runnable runnable, boolean async) {
LogEntity entity = new LogEntity.Builder()
BspTraceEntity entity = new BspTraceEntity.Builder()
.operationName(escapeMethodName(methodName))
.build();
LOGGER.log(Level.INFO, "Received notification '" + methodName + "'.", entity);
Expand Down Expand Up @@ -212,7 +212,7 @@ private <T> CompletableFuture<T> runAsync(String methodName, Function<CancelChec
}

private <T> CompletableFuture<T> success(String methodName, T response, long elapsedTime) {
LogEntity entity = new LogEntity.Builder()
BspTraceEntity entity = new BspTraceEntity.Builder()
.operationName(escapeMethodName(methodName))
.duration(String.valueOf(elapsedTime))
.build();
Expand All @@ -226,7 +226,7 @@ private <T> CompletableFuture<T> failure(String methodName, Throwable throwable)
String stackTrace = ExceptionUtils.getStackTrace(throwable);
Throwable rootCause = ExceptionUtils.getRootCause(throwable);
String rootCauseMessage = rootCause != null ? rootCause.getMessage() : null;
LogEntity entity = new LogEntity.Builder()
BspTraceEntity entity = new BspTraceEntity.Builder()
.operationName(escapeMethodName(methodName))
.trace(stackTrace)
.rootCauseMessage(rootCauseMessage)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.Arrays;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import org.apache.commons.lang3.StringUtils;

Expand All @@ -23,6 +24,7 @@
import com.microsoft.java.bs.core.internal.managers.PreferenceManager;
import com.microsoft.java.bs.core.internal.model.Preferences;
import com.microsoft.java.bs.core.internal.utils.JsonUtils;
import com.microsoft.java.bs.core.internal.utils.TelemetryUtils;
import com.microsoft.java.bs.core.internal.utils.UriUtils;
import com.microsoft.java.bs.gradle.model.GradleSourceSets;

Expand Down Expand Up @@ -151,6 +153,8 @@ private void updateGradleJavaHomeIfNecessary(URI rootUri, Preferences preference
}

if (StringUtils.isNotBlank(gradleVersion)) {
Map<String, String> map = TelemetryUtils.getMetadataMap("gradleVersion", gradleVersion);
LOGGER.log(Level.INFO, "Gradle version: " + gradleVersion, map);
String highestJavaVersion = Utils.getHighestCompatibleJavaVersion(gradleVersion);
File jdkInstallation = getJdkToLaunchDaemon(preferences.getJdks(), highestJavaVersion);
if (jdkInstallation != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

package com.microsoft.java.bs.core.internal.utils;

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

/**
* Utils for telemetry.
*/
public class TelemetryUtils {
private TelemetryUtils() {}

/**
* Return a map suitable for metadata telemetry.
*/
public static Map<String, String> getMetadataMap(String kind, String data) {
Map<String, String> map = new HashMap<>();
map.put("kind", kind);
map.put("data", data);
return map;
}
}

0 comments on commit 0df6066

Please sign in to comment.