org.glowroot.agent.model.ErrorMessage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of glowroot-agent-it-harness Show documentation
Show all versions of glowroot-agent-it-harness Show documentation
Glowroot Agent Integration Test Harness
/*
* Copyright 2015-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.glowroot.agent.model;
import java.util.Arrays;
import java.util.List;
import java.util.ListIterator;
import java.util.concurrent.atomic.AtomicInteger;
import org.glowroot.agent.shaded.org.glowroot.agent.it.harness.shaded.com.google.common.base.Strings;
import org.glowroot.agent.shaded.org.glowroot.agent.it.harness.shaded.com.google.common.collect.ImmutableList;
import org.glowroot.agent.shaded.org.checkerframework.checker.nullness.qual.Nullable;
import org.immutables.value.Value;
import org.glowroot.agent.shaded.org.glowroot.common.util.Styles;
import org.glowroot.agent.shaded.org.glowroot.wire.api.model.Proto;
import org.glowroot.agent.shaded.org.glowroot.wire.api.model.Proto.Throwable;
@Value.Immutable
@Styles.AllParameters
public abstract class ErrorMessage {
private static final int TRANSACTION_THROWABLE_FRAME_LIMIT =
Integer.getInteger("glowroot.transaction.throwable.frame.limit", 100000);
public abstract String message();
// cannot use Proto. /*@Nullable*/ Throwable
// or org.glowroot.agent.shaded.org.glowroot.wire.api.model.Proto. /*@Nullable*/ Throwable here because Immutables needs
// to be able to see the annotation
public abstract @Nullable Throwable throwable();
// accepts null values so callers don't have to check if passing it in from elsewhere
public static ErrorMessage create(@Nullable String message,
java.lang. /*@Nullable*/ Throwable t, AtomicInteger transactionThrowableFrameCount) {
if (t == null) {
return ImmutableErrorMessage.of(Strings.nullToEmpty(message), null);
} else {
return fromThrowable(message, t, transactionThrowableFrameCount);
}
}
private static ErrorMessage fromThrowable(@Nullable String message, java.lang.Throwable t,
AtomicInteger transactionThrowableFrameCount) {
String msg = Strings.nullToEmpty(message);
if (msg.isEmpty()) {
msg = Strings.nullToEmpty(t.getMessage());
}
if (msg.isEmpty()) {
msg = Strings.nullToEmpty(t.getClass().getName());
}
return ImmutableErrorMessage.of(msg,
buildThrowableInfo(t, null, transactionThrowableFrameCount, 0));
}
private static Proto.Throwable buildThrowableInfo(java.lang.Throwable t,
@Nullable List causedStackTrace,
AtomicInteger transactionThrowableFrameCount, int recursionDepth) {
int framesInCommonWithEnclosing = 0;
ImmutableList stackTrace = ImmutableList.of();
if (transactionThrowableFrameCount.get() < TRANSACTION_THROWABLE_FRAME_LIMIT) {
stackTrace = ImmutableList.copyOf(t.getStackTrace());
if (causedStackTrace != null) {
ListIterator i = stackTrace.listIterator(stackTrace.size());
ListIterator j =
causedStackTrace.listIterator(causedStackTrace.size());
while (i.hasPrevious() && j.hasPrevious()) {
StackTraceElement element = i.previous();
StackTraceElement causedElement = j.previous();
if (!element.equals(causedElement)) {
break;
}
framesInCommonWithEnclosing++;
}
if (framesInCommonWithEnclosing > 0) {
// strip off common frames
stackTrace =
stackTrace.subList(0, stackTrace.size() - framesInCommonWithEnclosing);
}
}
transactionThrowableFrameCount.addAndGet(stackTrace.size());
}
Proto.Throwable.Builder builder = Proto.Throwable.newBuilder()
.setClassName(t.getClass().getName());
String message = t.getMessage();
if (message != null) {
builder.setMessage(message);
}
for (StackTraceElement element : stackTrace) {
builder.addStackTraceElement(toProto(element));
}
builder.setFramesInCommonWithEnclosing(framesInCommonWithEnclosing);
java.lang.Throwable cause = t.getCause();
if (cause == null) {
return builder.build();
}
if (transactionThrowableFrameCount.get() > TRANSACTION_THROWABLE_FRAME_LIMIT) {
builder.setCause(Proto.Throwable.newBuilder()
.setMessage("Throwable frame capture limit exceeded")
.build());
} else if (recursionDepth == 80) {
// this was the 80th nested cause
// protobuf limits to 100 total levels of nesting by default
builder.setCause(Proto.Throwable.newBuilder()
.setMessage(
"The rest of the causal chain for this exception has been truncated")
.build());
} else {
// pass t's original stack trace to construct the nested cause
// (not stackTraces, which now has common frames removed)
builder.setCause(buildThrowableInfo(cause, Arrays.asList(t.getStackTrace()),
transactionThrowableFrameCount, recursionDepth + 1));
}
return builder.build();
}
public static Proto.StackTraceElement toProto(StackTraceElement ste) {
Proto.StackTraceElement.Builder builder = Proto.StackTraceElement.newBuilder()
.setClassName(ste.getClassName());
String methodName = ste.getMethodName();
if (methodName != null) {
builder.setMethodName(methodName);
}
String fileName = ste.getFileName();
if (fileName != null) {
builder.setFileName(fileName);
}
return builder.setLineNumber(ste.getLineNumber())
.build();
}
}