Search in sources :

Example 1 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class TracingInvocationEventHandlerTest method testSuccess.

@Test
public void testSuccess() {
    InvocationContext context = handler.preInvocation(instance, method, args);
    assertThat(Tracer.hasTraceId()).isTrue();
    handler.onSuccess(context, null);
    ArgumentCaptor<Span> spanCaptor = ArgumentCaptor.forClass(Span.class);
    verify(mockSpanObserver, times(1)).consume(spanCaptor.capture());
    Span span = spanCaptor.getValue();
    assertThat(span.getDurationNanoSeconds()).isGreaterThan(0L);
    assertThat(Tracer.hasTraceId()).isFalse();
}
Also used : InvocationContext(com.palantir.tritium.event.InvocationContext) Span(com.palantir.tracing.api.Span) Test(org.junit.jupiter.api.Test)

Example 2 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class TracingInvocationEventHandler method preInvocation.

@Override
public InvocationContext preInvocation(@Nonnull Object instance, @Nonnull Method method, @Nonnull Object[] args) {
    InvocationContext context = DefaultInvocationContext.of(instance, method, args);
    String operationName = getOperationName(method);
    Tracer.fastStartSpan(operationName);
    return context;
}
Also used : DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) InvocationContext(com.palantir.tritium.event.InvocationContext)

Example 3 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testReturnNull_failure_compositeHandler.

@Test
@SuppressWarnings("unchecked")
void testReturnNull_failure_compositeHandler() {
    InvocationEventHandler<InvocationContext> handler = Mockito.mock(InvocationEventHandler.class);
    when(handler.isEnabled()).thenReturn(true);
    when(handler.preInvocation(any(), any(), any())).thenReturn(null);
    Runnable wrapped = Instrumentation.builder(Runnable.class, () -> {
        throw new SafeRuntimeException("expected");
    }).withHandler(handler).withTaggedMetrics(new DefaultTaggedMetricRegistry()).build();
    assertThatCode(wrapped::run).isExactlyInstanceOf(SafeRuntimeException.class).hasMessage("expected");
    verify(handler).isEnabled();
    verify(handler).preInvocation(any(), any(), any());
    verify(handler).onFailure(isNull(), any());
    verifyNoMoreInteractions(handler);
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Example 4 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testThrowingHandler_success_composite.

@Test
@SuppressWarnings("unchecked")
void testThrowingHandler_success_composite() {
    InvocationEventHandler<InvocationContext> handler = Mockito.mock(InvocationEventHandler.class);
    when(handler.isEnabled()).thenReturn(true);
    when(handler.preInvocation(any(), any(), any())).thenThrow(new RuntimeException());
    Runnable wrapped = Instrumentation.builder(Runnable.class, Runnables.doNothing()).withHandler(handler).withTaggedMetrics(new DefaultTaggedMetricRegistry()).build();
    assertThatCode(wrapped::run).doesNotThrowAnyException();
    verify(handler).isEnabled();
    verify(handler).preInvocation(any(), any(), any());
    verify(handler).onSuccess(isNull(), any());
}
Also used : SafeRuntimeException(com.palantir.logsafe.exceptions.SafeRuntimeException) DefaultTaggedMetricRegistry(com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Example 5 with InvocationContext

use of com.palantir.tritium.event.InvocationContext in project tritium by palantir.

the class InstrumentationTest method testFilterMatches.

@Test
void testFilterMatches(@Mock InvocationEventHandler<InvocationContext> mockHandler) throws Exception {
    TestInterface delegate = new TestImplementation();
    TestInterface instrumented = Instrumentation.builder(TestInterface.class, delegate).withFilter(methodNameFilter("bulk")).withHandler(mockHandler).build();
    InvocationContext mockContext = mock(InvocationContext.class);
    when(mockHandler.isEnabled()).thenReturn(true);
    when(mockHandler.preInvocation(any(), any(Method.class), any(Object[].class))).thenReturn(mockContext);
    ImmutableSet<String> testSet = ImmutableSet.of("test");
    instrumented.bulk(testSet);
    verify(mockHandler).isEnabled();
    verify(mockHandler).preInvocation(instrumented, TestInterface.class.getDeclaredMethod("bulk", Set.class), new Object[] { testSet });
    verify(mockHandler).onSuccess(mockContext, null);
    verifyNoMoreInteractions(mockHandler);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TestImplementation(com.palantir.tritium.test.TestImplementation) TestInterface(com.palantir.tritium.test.TestInterface) Method(java.lang.reflect.Method) InvocationContext(com.palantir.tritium.event.InvocationContext) DefaultInvocationContext(com.palantir.tritium.event.DefaultInvocationContext) Test(org.junit.jupiter.api.Test)

Aggregations

InvocationContext (com.palantir.tritium.event.InvocationContext)20 Test (org.junit.jupiter.api.Test)16 DefaultInvocationContext (com.palantir.tritium.event.DefaultInvocationContext)14 SafeRuntimeException (com.palantir.logsafe.exceptions.SafeRuntimeException)5 Nullable (javax.annotation.Nullable)4 DefaultTaggedMetricRegistry (com.palantir.tritium.metrics.registry.DefaultTaggedMetricRegistry)3 ImmutableSet (com.google.common.collect.ImmutableSet)2 Span (com.palantir.tracing.api.Span)2 TestImplementation (com.palantir.tritium.test.TestImplementation)2 TestInterface (com.palantir.tritium.test.TestInterface)2 Set (java.util.Set)2 MetricRegistry (com.codahale.metrics.MetricRegistry)1 Throwables (com.google.common.base.Throwables)1 Lists (com.google.common.collect.Lists)1 Goethe (com.palantir.goethe.Goethe)1 GoetheException (com.palantir.goethe.GoetheException)1 Preconditions (com.palantir.logsafe.Preconditions)1 Instrument (com.palantir.tritium.annotations.Instrument)1 InstrumentationBuilder (com.palantir.tritium.annotations.internal.InstrumentationBuilder)1 InstrumentationFilter (com.palantir.tritium.api.event.InstrumentationFilter)1