Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add safe-logging SafeArg to relevant logging statements #23

Merged
merged 2 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tritium-brave/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ apply from: "${rootDir}/gradle/publish-jars.gradle"
dependencies {
compile project(':tritium-api')
compile project(':tritium-core')
compile 'com.palantir.safe-logging:safe-logging'
compile 'io.zipkin.brave:brave-core'
compile 'org.slf4j:slf4j-api'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

public final class BraveLocalTracingInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(BraveLocalTracingInvocationEventHandler.class);
private static final Logger logger = LoggerFactory.getLogger(BraveLocalTracingInvocationEventHandler.class);

private final Brave brave;
private final String component;
Expand Down Expand Up @@ -71,7 +71,7 @@ public void onFailure(@Nullable InvocationContext context, @Nonnull Throwable ca

private void debugIfNullContext(@Nullable InvocationContext context) {
if (context == null) {
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
logger.debug("Encountered null metric context likely due to exception in preInvocation");
}
}

Expand Down
1 change: 1 addition & 0 deletions tritium-caffeine/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ dependencies {
compile 'com.github.ben-manes.caffeine:caffeine'
compile 'com.google.code.findbugs:annotations'
compile 'com.google.guava:guava'
compile 'com.palantir.safe-logging:safe-logging'
compile 'io.dropwizard.metrics:metrics-core'
compile 'org.slf4j:slf4j-api'

Expand Down
1 change: 1 addition & 0 deletions tritium-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
compile project(':tritium-api')
compile 'com.google.guava:guava'
compile 'com.google.code.findbugs:annotations'
compile 'com.palantir.safe-logging:safe-logging'
compile 'org.slf4j:slf4j-api'

testCompile 'com.google.guava:guava-testlib'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.ImmutableList;
import com.palantir.logsafe.SafeArg;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
Expand All @@ -29,7 +30,7 @@

public final class CompositeInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(CompositeInvocationEventHandler.class);
private static final Logger logger = LoggerFactory.getLogger(CompositeInvocationEventHandler.class);

private final List<InvocationEventHandler<InvocationContext>> handlers;

Expand Down Expand Up @@ -76,7 +77,7 @@ public void onSuccess(@Nullable InvocationContext invocationContext, @Nullable O
}
}
} else {
LOGGER.debug("onSuccess InvocationContext was not a CompositeInvocationContext: {}", invocationContext);
logger.debug("onSuccess InvocationContext was not a CompositeInvocationContext: {}", invocationContext);
}
}

Expand All @@ -93,7 +94,7 @@ public void onFailure(@Nullable InvocationContext invocationContext, @Nonnull Th
}
}
} else {
LOGGER.debug("onFailure InvocationContext was not a CompositeInvocationContext: {}", invocationContext);
logger.debug("onFailure InvocationContext was not a CompositeInvocationContext: {}", invocationContext);
}
}

Expand All @@ -118,11 +119,15 @@ public String toString() {

private static void preInvocationFailed(InvocationEventHandler<? extends InvocationContext> handler,
Object instance, Method method, Object[] args, Exception exception) {
LOGGER.warn("Exception handling preInvocation({}): "
logger.warn("Exception handling preInvocation({}): "
+ "invocation of {}.{} with arguments {} on {} threw: {}",
handler,
method.getDeclaringClass().getCanonicalName(), method.getName(), Arrays.toString(args), instance,
exception, exception);
SafeArg.of("class", method.getDeclaringClass().getCanonicalName()),
SafeArg.of("method", method.getName()),
Arrays.toString(args),
instance,
exception,
exception);
}

private void handleSuccess(InvocationEventHandler<?> handler,
Expand All @@ -132,7 +137,7 @@ private void handleSuccess(InvocationEventHandler<?> handler,
try {
handler.onSuccess(context, result);
} catch (RuntimeException e) {
LOGGER.warn("Exception handling onSuccess({}, {}): {}",
logger.warn("Exception handling onSuccess({}, {}): {}",
context, result, e, e);
}
}
Expand All @@ -144,7 +149,7 @@ private void handleFailure(InvocationEventHandler<?> handler,
try {
handler.onFailure(context, cause);
} catch (RuntimeException e) {
LOGGER.warn("Exception handling onFailure({}, {}): {}",
logger.warn("Exception handling onFailure({}, {}): {}",
context, cause, e, e);
}
}
Expand Down
1 change: 1 addition & 0 deletions tritium-lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ dependencies {
compile project(':tritium-slf4j')
compile 'com.google.code.findbugs:annotations'
compile 'com.google.guava:guava'
compile 'com.palantir.safe-logging:safe-logging'
compile 'org.slf4j:slf4j-api'

testCompile project(path: ':tritium-core', configuration: 'testArtifacts')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.reflect.AbstractInvocationHandler;
import com.palantir.logsafe.SafeArg;
import com.palantir.tritium.api.functions.BooleanSupplier;
import com.palantir.tritium.event.CompositeInvocationEventHandler;
import com.palantir.tritium.event.InstrumentationFilter;
Expand All @@ -37,14 +38,15 @@
abstract class InvocationEventProxy<C extends InvocationContext>
extends AbstractInvocationHandler implements InvocationHandler {

private static final Logger LOGGER = LoggerFactory.getLogger(InvocationEventProxy.class);
private static final Logger logger = LoggerFactory.getLogger(InvocationEventProxy.class);
private static final Object[] NO_ARGS = {};

private final InstrumentationFilter filter;
private final InvocationEventHandler<?> eventHandler;

/**
* Always enabled instrumentation handler.
*
* @param handlers event handlers
*/
protected InvocationEventProxy(List<InvocationEventHandler<InvocationContext>> handlers) {
Expand Down Expand Up @@ -81,12 +83,12 @@ private boolean isEnabled(Object instance, Method method, Object[] args) {
return eventHandler.isEnabled()
&& filter.shouldInstrument(instance, method, args);
} catch (Throwable t) {
LOGGER.warn("Exception handling isEnabled({}): {}",
toInvocationDebugString(instance, method, args), t, t);
logInvocationWarning("isEnabled", instance, method, args, t);
return false;
}
}


@Override
@Nullable
@SuppressWarnings("checkstyle:illegalthrows")
Expand All @@ -102,7 +104,7 @@ protected final Object handleInvocation(Object proxy, Method method, Object[] ar
* {@link #invoke} delegates to this method upon any method invocation on the instance, except
* {@link Object#equals}, {@link Object#hashCode} and {@link Object#toString}. The result will be returned as the
* proxied method's return value.
*
* <p>
* <p>
* Unlike {@link #invoke}, {@code args} will never be null. When the method has no parameter, an empty array is
* passed in.
Expand All @@ -126,8 +128,7 @@ final InvocationContext handlePreInvocation(Object instance, Method method, Obje
try {
return eventHandler.preInvocation(instance, method, args);
} catch (RuntimeException e) {
LOGGER.warn("Exception handling preInvocation({}): {}",
toInvocationDebugString(instance, method, args), e, e);
logInvocationWarning("preInvocation", instance, method, args, e);
}
return null;
}
Expand All @@ -147,30 +148,48 @@ final Object handleOnSuccess(@Nullable InvocationContext context, @Nullable Obje
try {
eventHandler.onSuccess(context, result);
} catch (RuntimeException e) {
LOGGER.warn("Exception handling onSuccess({}, {}): {}",
context, result, e, e);
logInvocationWarningOnSuccess(context, result, e);
}
return result;
}

private static void logInvocationWarningOnSuccess(InvocationContext context, Object result, Exception cause) {
logger.warn("{} exception handling onSuccess({}, {}): {}",
safeSimpleClassName("cause", cause), context, safeSimpleClassName("result", result), cause);
}

final Throwable handleOnFailure(@Nullable InvocationContext context, Throwable cause) {
try {
eventHandler.onFailure(context, cause);
} catch (RuntimeException e) {
LOGGER.warn("Exception handling onFailure({}, {}): {}",
context, cause, e, e);
logInvocationWarningOnFailure(context, cause, e);
}
return cause;
}

protected final String toInvocationDebugString(Object instance, Method method, @Nullable Object[] args) {
return "invocation of "
+ method.getDeclaringClass() + '.' + method.getName()
+ " with arguments " + Arrays.toString(nullToEmpty(args))
+ " on " + instance + " via " + this + " : ";
private static void logInvocationWarningOnFailure(InvocationContext context, @Nullable Throwable result,
RuntimeException cause) {
logger.warn("{} exception handling onFailure({}, {}): {}",
safeSimpleClassName("cause", cause), context, safeSimpleClassName("result", result), cause);
}

private static SafeArg<String> safeSimpleClassName(String name, @Nullable Object object) {
return SafeArg.of(name, (object == null) ? "null" : object.getClass().getSimpleName());
}

static void logInvocationWarning(String event, Object instance, Method method, Object[] args,
Throwable cause) {
logger.warn("{} exception handling {} invocation of {}.{} with arguments {} on {}",
safeSimpleClassName("throwable", cause),
event,
SafeArg.of("class", method.getDeclaringClass()),
SafeArg.of("method", method.getName()),
Arrays.toString(nullToEmpty(args)),
instance,
cause);
}

protected static Object[] nullToEmpty(@Nullable Object[] args) {
private static Object[] nullToEmpty(@Nullable Object[] args) {
return (args == null) ? NO_ARGS : args;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ public void testToInvocationDebugString() throws Exception {
InvocationEventProxy<InvocationContext> proxy = createBasicProxy(
Collections.<InvocationEventHandler<InvocationContext>>emptyList());

String debugString = proxy.toInvocationDebugString(this, getToStringMethod(), null);
assertThat(debugString, containsString("x"));
Throwable cause = new RuntimeException("cause");
proxy.logInvocationWarning("test", this, getToStringMethod(), null, cause);
}

private InvocationEventProxy<InvocationContext> createBasicProxy(
Expand Down
1 change: 1 addition & 0 deletions tritium-metrics/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ dependencies {
compile project(':tritium-core')
compile 'com.google.code.findbugs:annotations'
compile 'com.google.guava:guava'
compile 'com.palantir.safe-logging:safe-logging'
compile 'io.dropwizard.metrics:metrics-core'
compile 'org.mpierce.metrics.reservoir:hdrhistogram-metrics-reservoir'
compile 'org.hdrhistogram:HdrHistogram'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*/
public final class MetricsInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(MetricsInvocationEventHandler.class);
private static final Logger logger = LoggerFactory.getLogger(MetricsInvocationEventHandler.class);

private final MetricRegistry metricRegistry;
private final String serviceName;
Expand All @@ -63,7 +63,7 @@ public InvocationContext preInvocation(Object instance, Method method, Object[]
@Override
public void onSuccess(@Nullable InvocationContext context, @Nullable Object result) {
if (context == null) {
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
logger.debug("Encountered null metric context likely due to exception in preInvocation");
return;
}
metricRegistry.timer(getBaseMetricName(context))
Expand All @@ -74,7 +74,7 @@ public void onSuccess(@Nullable InvocationContext context, @Nullable Object resu
public void onFailure(@Nullable InvocationContext context, @Nonnull Throwable cause) {
if (context == null) {
markGlobalFailure();
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation: {}", cause, cause);
logger.debug("Encountered null metric context likely due to exception in preInvocation: {}", cause, cause);
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
*/
public final class MetricRegistries {

private static final Logger LOGGER = LoggerFactory.getLogger(MetricRegistries.class);
private static final Logger logger = LoggerFactory.getLogger(MetricRegistries.class);

/**
* An ISO 8601 date format for pre-Java 8 compatibility without Joda dependency.
Expand Down Expand Up @@ -160,7 +160,7 @@ public static <T extends Metric> T registerSafe(MetricRegistry registry, String
if (existingMetric == null) {
return registry.register(name, metric);
} else {
LOGGER.warn("Metric already registered at this name."
logger.warn("Metric already registered at this name."
+ " Name: {}, existing metric: {}", name, existingMetric);

Set<Class<?>> existingMetricInterfaces = ImmutableSet.copyOf(existingMetric.getClass().getInterfaces());
Expand Down
1 change: 1 addition & 0 deletions tritium-slf4j/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
compile project(':tritium-api')
compile project(':tritium-core')
compile 'com.google.code.findbugs:annotations'
compile 'com.palantir.safe-logging:safe-logging'
compile 'org.slf4j:slf4j-api'

testCompile project(path: ':tritium-core', configuration: 'testArtifacts')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.palantir.logsafe.SafeArg;
import com.palantir.tritium.api.functions.BooleanSupplier;
import com.palantir.tritium.api.functions.LongPredicate;
import com.palantir.tritium.event.AbstractInvocationEventHandler;
Expand All @@ -40,7 +41,7 @@
*/
public class LoggingInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(LoggingInvocationEventHandler.class);
private static final Logger CLASS_LOGGER = LoggerFactory.getLogger(LoggingInvocationEventHandler.class);
private static final List<String> MESSAGE_PATTERNS = generateMessagePatterns(10);

public static final LongPredicate LOG_ALL_DURATIONS = LongPredicate.TRUE;
Expand Down Expand Up @@ -76,7 +77,7 @@ public final InvocationContext preInvocation(Object instance, Method method, Obj
@Override
public final void onSuccess(@Nullable InvocationContext context, @Nullable Object result) {
if (context == null) {
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
CLASS_LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
return;
}

Expand All @@ -87,7 +88,7 @@ public final void onSuccess(@Nullable InvocationContext context, @Nullable Objec
@Override
public final void onFailure(@Nullable InvocationContext context, @Nonnull Throwable cause) {
if (context == null) {
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
CLASS_LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
return;
}

Expand Down Expand Up @@ -191,9 +192,9 @@ static String getMessagePattern(Object[] args) {

static Object[] getLogParams(Method method, Object[] args, long durationNanos, LoggingLevel level) {
Object[] logParams = new Object[3 + args.length];
logParams[0] = method.getDeclaringClass().getSimpleName();
logParams[1] = method.getName();
logParams[logParams.length - 1] = String.format("%.3f", durationNanos / 1000000.0d);
logParams[0] = SafeArg.of("class", method.getDeclaringClass().getSimpleName());
logParams[1] = SafeArg.of("method", method.getName());
logParams[logParams.length - 1] = SafeArg.of("milliseconds", String.format("%.3f", durationNanos / 1000000.0d));

Class<?>[] argTypes = method.getParameterTypes();
if (argTypes.length == 0) {
Expand All @@ -209,7 +210,7 @@ static Object[] getLogParams(Method method, Object[] args, long durationNanos, L
}
}

logParams[2 + i] = argMessage;
logParams[2 + i] = SafeArg.of("type" + i, argMessage);
}

return logParams;
Expand Down
1 change: 1 addition & 0 deletions tritium-tracing/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ dependencies {

compile 'com.google.code.findbugs:annotations'
compile 'com.palantir.remoting1:tracing'
compile 'com.palantir.safe-logging:safe-logging'
compile 'org.slf4j:slf4j-api'

testCompile project(path: ':tritium-core', configuration: 'testArtifacts')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

public final class TracingInvocationEventHandler extends AbstractInvocationEventHandler<InvocationContext> {

private static final Logger LOGGER = LoggerFactory.getLogger(TracingInvocationEventHandler.class);
private static final Logger logger = LoggerFactory.getLogger(TracingInvocationEventHandler.class);

private final String component;

Expand Down Expand Up @@ -66,7 +66,7 @@ public void onFailure(@Nullable InvocationContext context, @Nonnull Throwable ca

private void debugIfNullContext(@Nullable InvocationContext context) {
if (context == null) {
LOGGER.debug("Encountered null metric context likely due to exception in preInvocation");
logger.debug("Encountered null metric context likely due to exception in preInvocation");
}
}

Expand Down
Loading