Skip to content

Commit 8e20bfb

Browse files
authored
Fix ingestion error on missing exception message (#2064)
* Fix ingestion error on missing exception message * Much better
1 parent 5ca2aa1 commit 8e20bfb

File tree

3 files changed

+23
-28
lines changed

3 files changed

+23
-28
lines changed

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exceptions.java

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
import static java.util.Collections.singletonList;
2525

26-
import com.microsoft.applicationinsights.agent.internal.common.Strings;
2726
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionDetails;
2827
import java.util.ArrayList;
2928
import java.util.List;
@@ -45,10 +44,17 @@ public static List<TelemetryExceptionDetails> minimalParse(String str) {
4544
}
4645
// at the end of the loop, current will be end of the first line
4746
if (separator != -1) {
48-
details.setTypeName(str.substring(0, separator));
49-
details.setMessage(Strings.trimAndEmptyToNull(str.substring(separator + 1, current)));
47+
String typeName = str.substring(0, separator);
48+
String message = str.substring(separator + 1, current).trim();
49+
if (message.isEmpty()) {
50+
message = typeName;
51+
}
52+
details.setTypeName(typeName);
53+
details.setMessage(message);
5054
} else {
51-
details.setTypeName(str.substring(0, current));
55+
String typeName = str.substring(0, current);
56+
details.setTypeName(typeName);
57+
details.setMessage(typeName);
5258
}
5359
details.setStack(str);
5460
return singletonList(details);
@@ -83,10 +89,16 @@ void process(String line) {
8389
current = new TelemetryExceptionDetails();
8490
int index = line.indexOf(':');
8591
if (index != -1) {
86-
current.setTypeName(line.substring(0, index));
87-
current.setMessage(Strings.trimAndEmptyToNull(line.substring(index + 1)));
92+
String typeName = line.substring(0, index);
93+
String message = line.substring(index + 1).trim();
94+
if (message.isEmpty()) {
95+
message = typeName;
96+
}
97+
current.setTypeName(typeName);
98+
current.setMessage(message);
8899
} else {
89100
current.setTypeName(line);
101+
current.setMessage(line);
90102
}
91103
}
92104
}

agent/agent-tooling/src/main/java/com/microsoft/applicationinsights/agent/internal/exporter/Exporter.java

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData;
3535
import com.microsoft.applicationinsights.agent.internal.exporter.models.SeverityLevel;
3636
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionData;
37-
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionDetails;
3837
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem;
3938
import com.microsoft.applicationinsights.agent.internal.telemetry.FormattedDuration;
4039
import com.microsoft.applicationinsights.agent.internal.telemetry.FormattedTime;
@@ -229,22 +228,6 @@ private void internalExport(SpanData span) {
229228
}
230229
}
231230

232-
private static List<TelemetryExceptionDetails> minimalParse(String errorStack) {
233-
TelemetryExceptionDetails details = new TelemetryExceptionDetails();
234-
String line = errorStack.split(System.lineSeparator())[0];
235-
int index = line.indexOf(": ");
236-
237-
if (index != -1) {
238-
details.setTypeName(line.substring(0, index));
239-
details.setMessage(line.substring(index + 2));
240-
} else {
241-
details.setTypeName(line);
242-
}
243-
// TODO (trask): map OpenTelemetry exception to Application Insights exception better
244-
details.setStack(errorStack);
245-
return Collections.singletonList(details);
246-
}
247-
248231
private void exportRemoteDependency(SpanData span, boolean inProc) {
249232
TelemetryItem telemetry = new TelemetryItem();
250233
RemoteDependencyData data = new RemoteDependencyData();
@@ -994,7 +977,7 @@ private void trackException(
994977
setSampleRate(telemetry, samplingPercentage);
995978

996979
// set exception-specific properties
997-
data.setExceptions(minimalParse(errorStack));
980+
data.setExceptions(Exceptions.minimalParse(errorStack));
998981

999982
telemetryClient.trackAsync(telemetry);
1000983
}

agent/agent-tooling/src/test/java/com/microsoft/applicationinsights/agent/internal/exporter/ExceptionsTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void testMinimalParseWithNoMessage() {
6060

6161
TelemetryExceptionDetails details = list.get(0);
6262
assertThat(details.getTypeName()).isEqualTo(IllegalStateException.class.getName());
63-
assertThat(details.getMessage()).isNull();
63+
assertThat(details.getMessage()).isEqualTo(IllegalStateException.class.getName());
6464
}
6565

6666
@Test
@@ -76,7 +76,7 @@ void testMinimalParseWithProblematicMessage() {
7676

7777
TelemetryExceptionDetails details = list.get(0);
7878
assertThat(details.getTypeName()).isEqualTo(ProblematicException.class.getName());
79-
assertThat(details.getMessage()).isNull();
79+
assertThat(details.getMessage()).isEqualTo(ProblematicException.class.getName());
8080
}
8181

8282
@Test
@@ -108,7 +108,7 @@ void testFullParseWithNoMessage() {
108108

109109
TelemetryExceptionDetails details = list.get(0);
110110
assertThat(details.getTypeName()).isEqualTo(IllegalStateException.class.getName());
111-
assertThat(details.getMessage()).isNull();
111+
assertThat(details.getMessage()).isEqualTo(IllegalStateException.class.getName());
112112
}
113113

114114
@Test
@@ -124,7 +124,7 @@ void testFullParseWithProblematicMessage() {
124124

125125
TelemetryExceptionDetails details = list.get(0);
126126
assertThat(details.getTypeName()).isEqualTo(ProblematicException.class.getName());
127-
assertThat(details.getMessage()).isNull();
127+
assertThat(details.getMessage()).isEqualTo(ProblematicException.class.getName());
128128
}
129129

130130
@Test

0 commit comments

Comments
 (0)