Skip to content

Commit ec5f566

Browse files
committed
Fix Scheduled observation convention for lambdas
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
1 parent 2d6b773 commit ec5f566

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

framework-docs/modules/ROOT/pages/integration/observability.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ By default, the following `KeyValues` are created:
108108
|===
109109
|Name | Description
110110
|`code.function` _(required)_|Name of Java `Method` that is scheduled for execution.
111-
|`code.namespace` _(required)_|Canonical name of the class of the bean instance that holds the scheduled method.
111+
|`code.namespace` _(required)_|Canonical name of the class of the bean instance that holds the scheduled method, or `"ANONYMOUS"` for anonymous classes.
112112
|`error` _(required)_|Class name of the exception thrown during the execution, or `"none"` if no exception happened.
113113
|`exception` _(deprecated)_|Duplicates the `error` key and might be removed in the future.
114114
|`outcome` _(required)_|Outcome of the method execution. Can be `"SUCCESS"`, `"ERROR"` or `"UNKNOWN"` (if for example the operation was cancelled during execution).

spring-context/src/main/java/org/springframework/scheduling/support/DefaultScheduledTaskObservationConvention.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -40,6 +40,8 @@ public class DefaultScheduledTaskObservationConvention implements ScheduledTaskO
4040

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

43+
private static final KeyValue CODE_NAMESPACE_ANONYMOUS = KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, "ANONYMOUS");
44+
4345
@Override
4446
public String getName() {
4547
return DEFAULT_NAME;
@@ -61,7 +63,10 @@ protected KeyValue codeFunction(ScheduledTaskObservationContext context) {
6163
}
6264

6365
protected KeyValue codeNamespace(ScheduledTaskObservationContext context) {
64-
return KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, context.getTargetClass().getCanonicalName());
66+
if (context.getTargetClass().getCanonicalName() != null) {
67+
return KeyValue.of(LowCardinalityKeyNames.CODE_NAMESPACE, context.getTargetClass().getCanonicalName());
68+
}
69+
return CODE_NAMESPACE_ANONYMOUS;
6570
}
6671

6772
protected KeyValue exception(ScheduledTaskObservationContext context) {

spring-context/src/test/java/org/springframework/scheduling/support/DefaultScheduledTaskObservationConventionTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,6 +35,8 @@ class DefaultScheduledTaskObservationConventionTests {
3535

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

38+
private final Method runMethod = ClassUtils.getMethod(Runnable.class, "run");
39+
3840
private final ScheduledTaskObservationConvention convention = new DefaultScheduledTaskObservationConvention();
3941

4042

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

74+
@Test
75+
void observationShouldHaveTargetTypeForAnonymousClass() {
76+
Runnable runnable = () -> { };
77+
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(runnable, runMethod);
78+
assertThat(convention.getLowCardinalityKeyValues(context))
79+
.contains(KeyValue.of("code.namespace", "ANONYMOUS"));
80+
}
81+
82+
@Test
83+
void observationShouldHaveMethodNameForAnonymousClass() {
84+
Runnable runnable = () -> { };
85+
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(runnable, runMethod);
86+
assertThat(convention.getLowCardinalityKeyValues(context)).contains(KeyValue.of("code.function", "run"));
87+
}
88+
7289
@Test
7390
void observationShouldHaveSuccessfulOutcome() {
7491
ScheduledTaskObservationContext context = new ScheduledTaskObservationContext(new BeanWithScheduledMethods(), taskMethod);

0 commit comments

Comments
 (0)