Skip to content

Commit

Permalink
Fix Scheduled observation convention for lambdas
Browse files Browse the repository at this point in the history
Prior to this commit, the `DefaultScheduledTaskObservationConvention`
would fail as it tried to add a `KeyValue` to the observation context
that is `null`. This is rejected by the observation registry and should
be prevented. This happened when registered scheduled methods were
lambdas or part of anonymous classes. Those types do not have a
canonical name and return `null` as a value there.

This commit ensures that for these cases, the default convetion uses a
`"ANONYMOUS"` value as the `"code.namespace"` keyvalue.

Fixes gh-31918
  • Loading branch information
bclozel committed Jan 2, 2024
1 parent 2d6b773 commit ec5f566
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ By default, the following `KeyValues` are created:
|===
|Name | Description
|`code.function` _(required)_|Name of Java `Method` that is scheduled for execution.
|`code.namespace` _(required)_|Canonical name of the class of the bean instance that holds the scheduled method.
|`code.namespace` _(required)_|Canonical name of the class of the bean instance that holds the scheduled method, or `"ANONYMOUS"` for anonymous classes.
|`error` _(required)_|Class name of the exception thrown during the execution, or `"none"` if no exception happened.
|`exception` _(deprecated)_|Duplicates the `error` key and might be removed in the future.
|`outcome` _(required)_|Outcome of the method execution. Can be `"SUCCESS"`, `"ERROR"` or `"UNKNOWN"` (if for example the operation was cancelled during execution).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -40,6 +40,8 @@ public class DefaultScheduledTaskObservationConvention implements ScheduledTaskO

private static final KeyValue OUTCOME_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "UNKNOWN");

private static final KeyValue CODE_NAMESPACE_ANONYMOUS = KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, "ANONYMOUS");

@Override
public String getName() {
return DEFAULT_NAME;
Expand All @@ -61,7 +63,10 @@ protected KeyValue codeFunction(ScheduledTaskObservationContext context) {
}

protected KeyValue codeNamespace(ScheduledTaskObservationContext context) {
return KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, context.getTargetClass().getCanonicalName());
if (context.getTargetClass().getCanonicalName() != null) {
return KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, context.getTargetClass().getCanonicalName());
}
return CODE_NAMESPACE_ANONYMOUS;
}

protected KeyValue exception(ScheduledTaskObservationContext context) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 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.
Expand Down Expand Up @@ -35,6 +35,8 @@ class DefaultScheduledTaskObservationConventionTests {

private final Method taskMethod = ClassUtils.getMethod(BeanWithScheduledMethods.class, "process");

private final Method runMethod = ClassUtils.getMethod(Runnable.class, "run");

private final ScheduledTaskObservationConvention convention = new DefaultScheduledTaskObservationConvention();


Expand Down Expand Up @@ -69,6 +71,21 @@ void observationShouldHaveMethodName() {
assertThat(convention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("code.function", "process"));
}

@Test
void observationShouldHaveTargetTypeForAnonymousClass() {
Runnable runnable = () -> { };
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(runnable, runMethod);
assertThat(convention.getLowCardinalityKeyValues(context))
.contains(KeyValue.of("code.namespace", "ANONYMOUS"));
}

@Test
void observationShouldHaveMethodNameForAnonymousClass() {
Runnable runnable = () -> { };
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(runnable, runMethod);
assertThat(convention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("code.function", "run"));
}

@Test
void observationShouldHaveSuccessfulOutcome() {
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(new BeanWithScheduledMethods(), taskMethod);
Expand Down

0 comments on commit ec5f566

Please sign in to comment.