Skip to content

Commit

Permalink
Introduce withParentOnly for @observed
Browse files Browse the repository at this point in the history
Take tracing for example, we may no want to create root span by @observed, they should be created if a root http span present.
  • Loading branch information
quaff committed May 18, 2023
1 parent 77d9cdb commit fcf9953
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Annotation to mark classes and methods that you want to observe.
*
* @author Jonatan Ivanov
* @author Yanming Zhou
* @since 1.10.0
*/
@Target({ ElementType.ANNOTATION_TYPE, ElementType.TYPE, ElementType.METHOD })
Expand All @@ -52,4 +53,10 @@
*/
String[] lowCardinalityKeyValues() default {};

/**
* Returns {@code true} if {@link Observation} should not be started without parent.
* @return {@code true} if {@link Observation} should not be started without parent.
*/
boolean withParentOnly() default false;

}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* </pre>
*
* @author Jonatan Ivanov
* @author Yanming Zhou
* @since 1.10.0
*/
@Aspect
Expand Down Expand Up @@ -131,6 +132,9 @@ public Object observeMethod(ProceedingJoinPoint pjp) throws Throwable {
private Object observe(ProceedingJoinPoint pjp, Method method, Observed observed) throws Throwable {
Observation observation = ObservedAspectObservationDocumentation.of(pjp, observed, this.registry,
this.observationConvention);
if (observed.withParentOnly() && observation.getContextView().getParentObservation() == null) {
return pjp.proceed();
}
if (CompletionStage.class.isAssignableFrom(method.getReturnType())) {
observation.start();
Observation.Scope scope = observation.openScope();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,35 @@ void skipPredicateShouldTakeEffectForClass() {
TestObservationRegistryAssert.assertThat(registry).doesNotHaveAnyObservation();
}

@Test
void withParentOnlyObservationShouldNotBeStartedWithoutParent() {
registry.observationConfig().observationHandler(new ObservationTextPublisher());

AspectJProxyFactory pf = new AspectJProxyFactory(new InternalService());
pf.addAspect(new ObservedAspect(registry));

InternalService internalService = pf.getProxy();
internalService.call();

TestObservationRegistryAssert.assertThat(registry).doesNotHaveAnyObservation();
}

@Test
void withParentOnlyObservationShouldBeStartedIfParentPresent() {
registry.observationConfig().observationHandler(new ObservationTextPublisher());

AspectJProxyFactory pf = new AspectJProxyFactory(new InternalService());
pf.addAspect(new ObservedAspect(registry));
InternalService internalService = pf.getProxy();
pf = new AspectJProxyFactory(new ExternalService(internalService));
pf.addAspect(new ObservedAspect(registry));

ExternalService externalService = pf.getProxy();
externalService.call();

TestObservationRegistryAssert.assertThat(registry).hasNumberOfObservationsEqualTo(2);
}

static class ObservedService {

@Observed(name = "test.call", contextualName = "test#call",
Expand Down Expand Up @@ -443,4 +472,28 @@ public boolean supportsContext(@NonNull Observation.Context context) {

}

static class ExternalService {

private final InternalService internalService;

ExternalService(InternalService internalService) {
this.internalService = internalService;
}

@Observed
void call() {
internalService.call();
}

}

static class InternalService {

@Observed(withParentOnly = true)
void call() {
System.out.println("call");
}

}

}

0 comments on commit fcf9953

Please sign in to comment.