Skip to content

Commit

Permalink
Fix ingestion error on missing exception message (#2064)
Browse files Browse the repository at this point in the history
* Fix ingestion error on missing exception message

* Much better
  • Loading branch information
trask authored Jan 26, 2022
1 parent 5ca2aa1 commit 8e20bfb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import static java.util.Collections.singletonList;

import com.microsoft.applicationinsights.agent.internal.common.Strings;
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionDetails;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -45,10 +44,17 @@ public static List<TelemetryExceptionDetails> minimalParse(String str) {
}
// at the end of the loop, current will be end of the first line
if (separator != -1) {
details.setTypeName(str.substring(0, separator));
details.setMessage(Strings.trimAndEmptyToNull(str.substring(separator + 1, current)));
String typeName = str.substring(0, separator);
String message = str.substring(separator + 1, current).trim();
if (message.isEmpty()) {
message = typeName;
}
details.setTypeName(typeName);
details.setMessage(message);
} else {
details.setTypeName(str.substring(0, current));
String typeName = str.substring(0, current);
details.setTypeName(typeName);
details.setMessage(typeName);
}
details.setStack(str);
return singletonList(details);
Expand Down Expand Up @@ -83,10 +89,16 @@ void process(String line) {
current = new TelemetryExceptionDetails();
int index = line.indexOf(':');
if (index != -1) {
current.setTypeName(line.substring(0, index));
current.setMessage(Strings.trimAndEmptyToNull(line.substring(index + 1)));
String typeName = line.substring(0, index);
String message = line.substring(index + 1).trim();
if (message.isEmpty()) {
message = typeName;
}
current.setTypeName(typeName);
current.setMessage(message);
} else {
current.setTypeName(line);
current.setMessage(line);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import com.microsoft.applicationinsights.agent.internal.exporter.models.RequestData;
import com.microsoft.applicationinsights.agent.internal.exporter.models.SeverityLevel;
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionData;
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryExceptionDetails;
import com.microsoft.applicationinsights.agent.internal.exporter.models.TelemetryItem;
import com.microsoft.applicationinsights.agent.internal.telemetry.FormattedDuration;
import com.microsoft.applicationinsights.agent.internal.telemetry.FormattedTime;
Expand Down Expand Up @@ -229,22 +228,6 @@ private void internalExport(SpanData span) {
}
}

private static List<TelemetryExceptionDetails> minimalParse(String errorStack) {
TelemetryExceptionDetails details = new TelemetryExceptionDetails();
String line = errorStack.split(System.lineSeparator())[0];
int index = line.indexOf(": ");

if (index != -1) {
details.setTypeName(line.substring(0, index));
details.setMessage(line.substring(index + 2));
} else {
details.setTypeName(line);
}
// TODO (trask): map OpenTelemetry exception to Application Insights exception better
details.setStack(errorStack);
return Collections.singletonList(details);
}

private void exportRemoteDependency(SpanData span, boolean inProc) {
TelemetryItem telemetry = new TelemetryItem();
RemoteDependencyData data = new RemoteDependencyData();
Expand Down Expand Up @@ -994,7 +977,7 @@ private void trackException(
setSampleRate(telemetry, samplingPercentage);

// set exception-specific properties
data.setExceptions(minimalParse(errorStack));
data.setExceptions(Exceptions.minimalParse(errorStack));

telemetryClient.trackAsync(telemetry);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void testMinimalParseWithNoMessage() {

TelemetryExceptionDetails details = list.get(0);
assertThat(details.getTypeName()).isEqualTo(IllegalStateException.class.getName());
assertThat(details.getMessage()).isNull();
assertThat(details.getMessage()).isEqualTo(IllegalStateException.class.getName());
}

@Test
Expand All @@ -76,7 +76,7 @@ void testMinimalParseWithProblematicMessage() {

TelemetryExceptionDetails details = list.get(0);
assertThat(details.getTypeName()).isEqualTo(ProblematicException.class.getName());
assertThat(details.getMessage()).isNull();
assertThat(details.getMessage()).isEqualTo(ProblematicException.class.getName());
}

@Test
Expand Down Expand Up @@ -108,7 +108,7 @@ void testFullParseWithNoMessage() {

TelemetryExceptionDetails details = list.get(0);
assertThat(details.getTypeName()).isEqualTo(IllegalStateException.class.getName());
assertThat(details.getMessage()).isNull();
assertThat(details.getMessage()).isEqualTo(IllegalStateException.class.getName());
}

@Test
Expand All @@ -124,7 +124,7 @@ void testFullParseWithProblematicMessage() {

TelemetryExceptionDetails details = list.get(0);
assertThat(details.getTypeName()).isEqualTo(ProblematicException.class.getName());
assertThat(details.getMessage()).isNull();
assertThat(details.getMessage()).isEqualTo(ProblematicException.class.getName());
}

@Test
Expand Down

0 comments on commit 8e20bfb

Please sign in to comment.