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.
@@ -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;
@@ -61,7 +63,10 @@ public class DefaultScheduledTaskObservationConvention implements ScheduledTaskO
}
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) {