Skip to content

Commit 4c866ec

Browse files
Rui Fangt-rufang
authored andcommitted
Fix UnsupportedOperationException for telemetry code
1 parent 0024056 commit 4c866ec

File tree

4 files changed

+40
-56
lines changed

4 files changed

+40
-56
lines changed

PluginsAndFeatures/azure-toolkit-for-intellij/Test/java/com/microsoft/intellij/telemetry/ContainerTelemetryExtensionTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class FormBuilderPanel {
134134
}
135135
}
136136

137-
class IdeaTelemetryUtilsTest : LightProjectDescriptor() {
137+
class ContainerTelemetryExtensionTest : LightProjectDescriptor() {
138138
override fun getSdk(): Sdk? {
139139
return IdeaTestUtil.getMockJdk18()
140140
}

Utils/azuretools-core/src/com/microsoft/azuretools/telemetrywrapper/CommonUtil.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import com.microsoft.applicationinsights.TelemetryClient;
2626
import com.microsoft.azuretools.adauth.StringUtils;
27+
2728
import java.util.HashMap;
2829
import java.util.Map;
2930

@@ -50,11 +51,12 @@ public static Map<String, String> mergeProperties(Map<String, String> properties
5051

5152
public synchronized static void sendTelemetry(EventType eventType, String serviceName, Map<String, String> properties,
5253
Map<String, Double> metrics) {
54+
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
5355
if (client != null) {
5456
if (!StringUtils.isNullOrEmpty(serviceName)) {
55-
properties.put(SERVICE_NAME, serviceName);
57+
mutableProps.put(SERVICE_NAME, serviceName);
5658
}
57-
client.trackEvent(getFullEventName(eventType), properties, metrics);
59+
client.trackEvent(getFullEventName(eventType), mutableProps, metrics);
5860
client.flush();
5961
}
6062
}

Utils/azuretools-core/src/com/microsoft/azuretools/telemetrywrapper/DefaultOperation.java

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,12 @@
2222

2323
package com.microsoft.azuretools.telemetrywrapper;
2424

25-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.DURATION;
26-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.ERROR_CLASSNAME;
27-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.ERROR_CODE;
28-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.ERROR_MSG;
29-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.ERROR_TYPE;
30-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.OPERATION_ID;
31-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.OPERATION_NAME;
32-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.mergeProperties;
33-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.sendTelemetry;
34-
3525
import java.util.HashMap;
3626
import java.util.Map;
3727
import java.util.UUID;
3828

29+
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.*;
30+
3931
public class DefaultOperation implements Operation {
4032

4133
private long timeStart;
@@ -63,19 +55,15 @@ public void logEvent(EventType eventType, Map<String, String> properties, Map<St
6355
if (eventType == EventType.opStart || eventType == EventType.opEnd) {
6456
return;
6557
}
66-
if (properties == null) {
67-
properties = new HashMap<>();
68-
}
69-
if (metrics == null) {
70-
metrics = new HashMap<>();
71-
}
72-
properties.put(OPERATION_ID, operationId);
73-
properties.put(OPERATION_NAME, operationName);
58+
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
59+
mutableProps.put(OPERATION_ID, operationId);
60+
mutableProps.put(OPERATION_NAME, operationName);
7461

62+
Map<String, Double> mutableMetrics = properties == null ? new HashMap<>() : new HashMap<>(metrics);
7563
if (eventType == EventType.step) {
76-
metrics.put(DURATION, Double.valueOf(System.currentTimeMillis() - timeStart));
64+
mutableMetrics.put(DURATION, Double.valueOf(System.currentTimeMillis() - timeStart));
7765
}
78-
sendTelemetry(eventType, serviceName, mergeProperties(properties), metrics);
66+
sendTelemetry(eventType, serviceName, mergeProperties(mutableProps), mutableMetrics);
7967
} catch (Exception ignore) {
8068
}
8169
}
@@ -86,26 +74,23 @@ public synchronized void logError(ErrorType errorType, Throwable e, Map<String,
8674
if (isComplete) {
8775
return;
8876
}
89-
if (properties == null) {
90-
properties = new HashMap<>();
91-
}
92-
if (metrics == null) {
93-
metrics = new HashMap<>();
94-
}
77+
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
78+
Map<String, Double> mutableMetrics = properties == null ? new HashMap<>() : new HashMap<>(metrics);
79+
9580
error = new Error();
9681
error.errorType = errorType == null ? ErrorType.systemError : errorType;
9782
error.errMsg = e == null ? "" : e.getMessage();
9883
error.className = e == null ? "" : e.getClass().getName();
9984

100-
properties.put(ERROR_CODE, "1");
101-
properties.put(ERROR_MSG, error.errMsg);
102-
properties.put(ERROR_TYPE, error.errorType.name());
103-
properties.put(ERROR_CLASSNAME, error.className);
104-
properties.put(OPERATION_ID, operationId);
105-
properties.put(OPERATION_NAME, operationName);
85+
mutableProps.put(ERROR_CODE, "1");
86+
mutableProps.put(ERROR_MSG, error.errMsg);
87+
mutableProps.put(ERROR_TYPE, error.errorType.name());
88+
mutableProps.put(ERROR_CLASSNAME, error.className);
89+
mutableProps.put(OPERATION_ID, operationId);
90+
mutableProps.put(OPERATION_NAME, operationName);
10691

107-
metrics.put(DURATION, Double.valueOf(System.currentTimeMillis() - timeStart));
108-
sendTelemetry(EventType.error, serviceName, mergeProperties(properties), metrics);
92+
mutableMetrics.put(DURATION, Double.valueOf(System.currentTimeMillis() - timeStart));
93+
sendTelemetry(EventType.error, serviceName, mergeProperties(mutableProps), mutableMetrics);
10994
} catch (Exception ignore) {
11095
}
11196
}

Utils/azuretools-core/src/com/microsoft/azuretools/telemetrywrapper/EventUtil.java

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,25 +22,24 @@
2222

2323
package com.microsoft.azuretools.telemetrywrapper;
2424

25-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.mergeProperties;
26-
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.sendTelemetry;
27-
2825
import java.util.HashMap;
2926
import java.util.Map;
3027
import java.util.UUID;
3128
import java.util.function.Consumer;
3229

30+
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.mergeProperties;
31+
import static com.microsoft.azuretools.telemetrywrapper.CommonUtil.sendTelemetry;
32+
3333
public class EventUtil {
3434

3535
public static void logEvent(EventType eventType, String serviceName, String operName, Map<String, String> properties,
3636
Map<String, Double> metrics) {
3737
try {
38-
if (properties == null) {
39-
properties = new HashMap<>();
40-
}
41-
properties.put(CommonUtil.OPERATION_NAME, operName);
42-
properties.put(CommonUtil.OPERATION_ID, UUID.randomUUID().toString());
43-
sendTelemetry(eventType, serviceName, mergeProperties(properties), metrics);
38+
// Parameter properties might be a ImmutableMap, which means calling properties.put will lead to UnsupportedOperationException
39+
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
40+
mutableProps.put(CommonUtil.OPERATION_NAME, operName);
41+
mutableProps.put(CommonUtil.OPERATION_ID, UUID.randomUUID().toString());
42+
sendTelemetry(eventType, serviceName, mergeProperties(mutableProps), metrics);
4443
} catch (Exception ignore) {
4544
}
4645
}
@@ -53,16 +52,14 @@ public static void logEvent(EventType eventType, String serviceName, String oper
5352
public static void logError(String serviceName, String operName, ErrorType errorType, Exception e,
5453
Map<String, String> properties, Map<String, Double> metrics) {
5554
try {
56-
if (properties == null) {
57-
properties = new HashMap<>();
58-
}
59-
properties.put(CommonUtil.OPERATION_NAME, operName);
60-
properties.put(CommonUtil.OPERATION_ID, UUID.randomUUID().toString());
61-
properties.put(CommonUtil.ERROR_CODE, "1");
62-
properties.put(CommonUtil.ERROR_MSG, e != null ? e.getMessage() : "");
63-
properties.put(CommonUtil.ERROR_CLASSNAME, e != null ? e.getClass().getName() : "");
64-
properties.put(CommonUtil.ERROR_TYPE, errorType.name());
65-
sendTelemetry(EventType.error, serviceName, mergeProperties(properties), metrics);
55+
Map<String, String> mutableProps = properties == null ? new HashMap<>() : new HashMap<>(properties);
56+
mutableProps.put(CommonUtil.OPERATION_NAME, operName);
57+
mutableProps.put(CommonUtil.OPERATION_ID, UUID.randomUUID().toString());
58+
mutableProps.put(CommonUtil.ERROR_CODE, "1");
59+
mutableProps.put(CommonUtil.ERROR_MSG, e != null ? e.getMessage() : "");
60+
mutableProps.put(CommonUtil.ERROR_CLASSNAME, e != null ? e.getClass().getName() : "");
61+
mutableProps.put(CommonUtil.ERROR_TYPE, errorType.name());
62+
sendTelemetry(EventType.error, serviceName, mergeProperties(mutableProps), metrics);
6663
} catch (Exception ignore) {
6764
}
6865
}

0 commit comments

Comments
 (0)