Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add application startup metrics support
This commit adds a new `StartupStep` interface and its factory `ApplicationStartup`. Such steps are created, tagged with metadata and thir execution time can be recorded - in order to collect metrics about the application startup. The default implementation is a "no-op" variant and has no side-effect. Other implementations can record and collect events in a dedicated metrics system or profiling tools. We provide here an implementation for recording and storing steps with Java Flight Recorder. This commit also instruments the Spring application context to gather metrics about various phases of the application context, such as: * context refresh phase * bean definition registry post-processing * bean factory post-processing * beans instantiation and post-processing Third part libraries involved in the Spring application context can reuse the same infrastructure to record similar metrics. Closes gh-24878
- Loading branch information
Showing
22 changed files
with
890 additions
and
16 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
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
47 changes: 47 additions & 0 deletions
47
spring-beans/src/main/java/org/springframework/beans/metrics/ApplicationStartup.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,47 @@ | ||
/* | ||
* Copyright 2002-2020 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.beans.metrics; | ||
|
||
/** | ||
* Instruments the application startup phase using {@link StartupStep steps}. | ||
* <p>The core container and its infrastructure components can use the {@code ApplicationStartup} | ||
* to mark steps during the application startup and collect data about the execution context | ||
* or their processing time. | ||
* | ||
* @author Brian Clozel | ||
* @since 5.3.0 | ||
*/ | ||
public interface ApplicationStartup { | ||
|
||
/** | ||
* Return a default "no op" {@code ApplicationStartup} implementation. | ||
* <p>This variant is designed for minimal overhead and does not record data. | ||
*/ | ||
static ApplicationStartup getDefault() { | ||
return new DefaultApplicationStartup(); | ||
} | ||
|
||
/** | ||
* Create a new step and marks its beginning. | ||
* <p>A step name describes the current action or phase. This technical | ||
* name should be "." namespaced and can be reused to describe other instances of | ||
* the same step during application startup. | ||
* @param name the step name | ||
*/ | ||
StartupStep start(String name); | ||
|
||
} |
92 changes: 92 additions & 0 deletions
92
spring-beans/src/main/java/org/springframework/beans/metrics/DefaultApplicationStartup.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,92 @@ | ||
/* | ||
* Copyright 2002-2020 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.beans.metrics; | ||
|
||
import java.util.Collections; | ||
import java.util.Iterator; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
* Default "no op" {@code ApplicationStartup} implementation. | ||
* <p>This variant is designed for minimal overhead and does not record events. | ||
* | ||
* @author Brian Clozel | ||
*/ | ||
class DefaultApplicationStartup implements ApplicationStartup { | ||
|
||
@Override | ||
public DefaultStartupStep start(String name) { | ||
return new DefaultStartupStep(); | ||
} | ||
|
||
static class DefaultStartupStep implements StartupStep { | ||
|
||
boolean recorded = false; | ||
|
||
private final DefaultTags TAGS = new DefaultTags(); | ||
|
||
@Override | ||
public String getName() { | ||
return "default"; | ||
} | ||
|
||
@Override | ||
public long getId() { | ||
return 0L; | ||
} | ||
|
||
@Override | ||
public Long getParentId() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Tags tags() { | ||
return this.TAGS; | ||
} | ||
|
||
@Override | ||
public StartupStep tag(String key, String value) { | ||
if (this.recorded) { | ||
throw new IllegalArgumentException(); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public StartupStep tag(String key, Supplier<String> value) { | ||
if (this.recorded) { | ||
throw new IllegalArgumentException(); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public void end() { | ||
this.recorded = true; | ||
} | ||
|
||
static class DefaultTags implements StartupStep.Tags { | ||
|
||
@Override | ||
public Iterator<StartupStep.Tag> iterator() { | ||
return Collections.emptyIterator(); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.