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
This commit is contained in:
Brian Clozel
2024-01-02 15:12:26 +01:00
parent 2d6b77336b
commit ec5f566ba5
3 changed files with 26 additions and 4 deletions

View File

@@ -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.
@@ -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();
@@ -69,6 +71,21 @@ class DefaultScheduledTaskObservationConventionTests {
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);