-
Notifications
You must be signed in to change notification settings - Fork 38k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Instrument Scheduled methods for observability
This commit enhances the `ScheduledAnnotationBeanPostProcessor` to instrument `@Scheduled` methods declared on beans. This will create `"tasks.scheduled.execution"` observations for each execution of a scheduled method. This supports both blocking and reactive variants. By default, observations are no-ops; developers must configure the current `ObservationRegistry` on the `ScheduledTaskRegistrar` by using a `SchedulingConfigurer`. Closes gh-29883
- Loading branch information
Showing
14 changed files
with
899 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
...mework/docs/integration/observability/tasksscheduled/ObservationSchedulingConfigurer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.docs.integration.observability.tasksscheduled; | ||
|
||
|
||
import io.micrometer.observation.ObservationRegistry; | ||
|
||
import org.springframework.scheduling.annotation.SchedulingConfigurer; | ||
import org.springframework.scheduling.config.ScheduledTaskRegistrar; | ||
|
||
public class ObservationSchedulingConfigurer implements SchedulingConfigurer { | ||
|
||
private final ObservationRegistry observationRegistry; | ||
|
||
public ObservationSchedulingConfigurer(ObservationRegistry observationRegistry) { | ||
this.observationRegistry = observationRegistry; | ||
} | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
taskRegistrar.setObservationRegistry(this.observationRegistry); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
...java/org/springframework/scheduling/config/DefaultScheduledTaskObservationConvention.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
* Copyright 2002-2023 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.scheduling.config; | ||
|
||
import io.micrometer.common.KeyValue; | ||
import io.micrometer.common.KeyValues; | ||
|
||
import org.springframework.util.StringUtils; | ||
|
||
import static org.springframework.scheduling.config.ScheduledTaskObservationDocumentation.LowCardinalityKeyNames; | ||
|
||
/** | ||
* Default implementation for {@link ScheduledTaskObservationConvention}. | ||
* @author Brian Clozel | ||
* @since 6.1.0 | ||
*/ | ||
public class DefaultScheduledTaskObservationConvention implements ScheduledTaskObservationConvention { | ||
|
||
private static final String DEFAULT_NAME = "tasks.scheduled.execution"; | ||
|
||
private static final KeyValue EXCEPTION_NONE = KeyValue.of(LowCardinalityKeyNames.EXCEPTION, KeyValue.NONE_VALUE); | ||
|
||
private static final KeyValue OUTCOME_SUCCESS = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "SUCCESS"); | ||
|
||
private static final KeyValue OUTCOME_ERROR = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "ERROR"); | ||
|
||
private static final KeyValue OUTCOME_UNKNOWN = KeyValue.of(LowCardinalityKeyNames.OUTCOME, "UNKNOWN"); | ||
|
||
@Override | ||
public String getName() { | ||
return DEFAULT_NAME; | ||
} | ||
|
||
@Override | ||
public String getContextualName(ScheduledTaskObservationContext context) { | ||
return "task " + StringUtils.uncapitalize(context.getTargetClass().getSimpleName()) | ||
+ "." + context.getMethod().getName(); | ||
} | ||
|
||
@Override | ||
public KeyValues getLowCardinalityKeyValues(ScheduledTaskObservationContext context) { | ||
return KeyValues.of(exception(context), methodName(context), outcome(context), targetType(context)); | ||
} | ||
|
||
protected KeyValue exception(ScheduledTaskObservationContext context) { | ||
if (context.getError() != null) { | ||
return KeyValue.of(LowCardinalityKeyNames.EXCEPTION, context.getError().getClass().getSimpleName()); | ||
} | ||
return EXCEPTION_NONE; | ||
} | ||
|
||
protected KeyValue methodName(ScheduledTaskObservationContext context) { | ||
return KeyValue.of(LowCardinalityKeyNames.METHOD_NAME, context.getMethod().getName()); | ||
} | ||
|
||
protected KeyValue outcome(ScheduledTaskObservationContext context) { | ||
if (context.getError() != null) { | ||
return OUTCOME_ERROR; | ||
} | ||
else if (!context.isComplete()) { | ||
return OUTCOME_UNKNOWN; | ||
} | ||
return OUTCOME_SUCCESS; | ||
} | ||
|
||
protected KeyValue targetType(ScheduledTaskObservationContext context) { | ||
return KeyValue.of(LowCardinalityKeyNames.TARGET_TYPE, context.getTargetClass().getSimpleName()); | ||
} | ||
|
||
} |
Oops, something went wrong.