|
| 1 | +/* |
| 2 | + * Copyright 2002-2020 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.context.metrics.buffering; |
| 18 | + |
| 19 | +import java.time.Instant; |
| 20 | +import java.util.ArrayDeque; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Deque; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.BlockingQueue; |
| 25 | +import java.util.concurrent.LinkedBlockingQueue; |
| 26 | +import java.util.function.Predicate; |
| 27 | + |
| 28 | +import org.springframework.core.metrics.ApplicationStartup; |
| 29 | +import org.springframework.core.metrics.StartupStep; |
| 30 | +import org.springframework.util.Assert; |
| 31 | + |
| 32 | +/** |
| 33 | + * {@link ApplicationStartup} implementation that buffers {@link StartupStep steps} and |
| 34 | + * records their timestamp as well as their processing time. |
| 35 | + * <p> |
| 36 | + * Once recording has been {@link #startRecording() started}, steps are buffered up until |
| 37 | + * the configured {@link #BufferingApplicationStartup(int) capacity}; after that, new |
| 38 | + * steps are not recorded. |
| 39 | + * <p> |
| 40 | + * There are several ways to keep the buffer size low: |
| 41 | + * <ul> |
| 42 | + * <li>configuring {@link #addFilter(Predicate) filters} to only record steps that are |
| 43 | + * relevant to us. |
| 44 | + * <li>{@link #drainBufferedTimeline() draining} the buffered steps. |
| 45 | + * </ul> |
| 46 | + * |
| 47 | + * @author Brian Clozel |
| 48 | + * @since 2.4.0 |
| 49 | + */ |
| 50 | +public class BufferingApplicationStartup implements ApplicationStartup { |
| 51 | + |
| 52 | + private Instant recordingStartTime; |
| 53 | + |
| 54 | + private long recordingStartNanos; |
| 55 | + |
| 56 | + private long currentSequenceId = 0; |
| 57 | + |
| 58 | + private final Deque<Long> currentSteps; |
| 59 | + |
| 60 | + private final BlockingQueue<BufferedStartupStep> recordedSteps; |
| 61 | + |
| 62 | + private Predicate<StartupStep> stepFilters = (step) -> true; |
| 63 | + |
| 64 | + /** |
| 65 | + * Create a new buffered {@link ApplicationStartup} with a limited capacity and starts |
| 66 | + * the recording of steps. |
| 67 | + * @param capacity the configured capacity; once reached, new steps are not recorded. |
| 68 | + */ |
| 69 | + public BufferingApplicationStartup(int capacity) { |
| 70 | + this.currentSteps = new ArrayDeque<>(); |
| 71 | + this.currentSteps.offerFirst(this.currentSequenceId); |
| 72 | + this.recordedSteps = new LinkedBlockingQueue<>(capacity); |
| 73 | + startRecording(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Start the recording of steps and mark the beginning of the {@link StartupTimeline}. |
| 78 | + * The class constructor already implicitly calls this, but it is possible to reset it |
| 79 | + * as long as steps have not been recorded already. |
| 80 | + * @throws IllegalStateException if called and {@link StartupStep} have been recorded |
| 81 | + * already. |
| 82 | + */ |
| 83 | + public void startRecording() { |
| 84 | + Assert.state(this.recordedSteps.isEmpty(), "Cannot restart recording once steps have been buffered."); |
| 85 | + this.recordingStartTime = Instant.now(); |
| 86 | + this.recordingStartNanos = getCurrentTime(); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Add a predicate filter to the list of existing ones. |
| 91 | + * <p> |
| 92 | + * A {@link StartupStep step} that doesn't match all filters will not be recorded. |
| 93 | + * @param filter the predicate filter to add. |
| 94 | + */ |
| 95 | + public void addFilter(Predicate<StartupStep> filter) { |
| 96 | + this.stepFilters = this.stepFilters.and(filter); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Return the {@link StartupTimeline timeline} as a snapshot of currently buffered |
| 101 | + * steps. |
| 102 | + * <p> |
| 103 | + * This will not remove steps from the buffer, see {@link #drainBufferedTimeline()} |
| 104 | + * for its counterpart. |
| 105 | + * @return a snapshot of currently buffered steps. |
| 106 | + */ |
| 107 | + public StartupTimeline getBufferedTimeline() { |
| 108 | + return new StartupTimeline(this.recordingStartTime, this.recordingStartNanos, this.recordedSteps); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Return the {@link StartupTimeline timeline} by pulling steps from the buffer. |
| 113 | + * <p> |
| 114 | + * This removes steps from the buffer, see {@link #getBufferedTimeline()} for its |
| 115 | + * read-only counterpart. |
| 116 | + * @return buffered steps drained from the buffer. |
| 117 | + */ |
| 118 | + public StartupTimeline drainBufferedTimeline() { |
| 119 | + List<BufferedStartupStep> steps = new ArrayList<>(this.recordedSteps.size()); |
| 120 | + this.recordedSteps.drainTo(steps); |
| 121 | + return new StartupTimeline(this.recordingStartTime, this.recordingStartNanos, steps); |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public StartupStep start(String name) { |
| 126 | + BufferedStartupStep step = new BufferedStartupStep(++this.currentSequenceId, name, |
| 127 | + this.currentSteps.peekFirst(), this::record); |
| 128 | + step.recordStartTime(getCurrentTime()); |
| 129 | + this.currentSteps.offerFirst(this.currentSequenceId); |
| 130 | + return step; |
| 131 | + } |
| 132 | + |
| 133 | + private void record(BufferedStartupStep step) { |
| 134 | + step.recordEndTime(getCurrentTime()); |
| 135 | + if (this.stepFilters.test(step)) { |
| 136 | + this.recordedSteps.offer(step); |
| 137 | + } |
| 138 | + this.currentSteps.removeFirst(); |
| 139 | + } |
| 140 | + |
| 141 | + private long getCurrentTime() { |
| 142 | + return System.nanoTime(); |
| 143 | + } |
| 144 | + |
| 145 | +} |
0 commit comments