Skip to content

Commit

Permalink
Add boot clock to CamelContext to capture boot time
Browse files Browse the repository at this point in the history
Fixes #6630
  • Loading branch information
jamesnetherton committed Dec 4, 2024
1 parent c46acba commit ae5de89
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.ExecutionTime;
import io.quarkus.deployment.annotations.Record;
import io.quarkus.deployment.pkg.steps.NativeOrNativeSourcesBuild;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.CamelContext;
import org.apache.camel.quarkus.core.CamelConfig;
import org.apache.camel.quarkus.core.CamelContextRecorder;
import org.apache.camel.quarkus.core.deployment.spi.CamelBootClockBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelComponentNameResolverBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem;
import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
Expand Down Expand Up @@ -81,6 +83,7 @@ CamelContextBuildItem context(
CamelComponentNameResolverBuildItem componentNameResolver,
CamelPackageScanClassResolverBuildItem packageScanClassResolver,
CamelModelReifierFactoryBuildItem modelReifierFactory,
CamelBootClockBuildItem camelBootClock,
CamelConfig config) {

RuntimeValue<CamelContext> context = recorder.createContext(
Expand All @@ -93,6 +96,7 @@ CamelContextBuildItem context(
componentNameResolver.getComponentNameResolver(),
packageScanClassResolver.getPackageScanClassResolver(),
modelReifierFactory.getModelReifierFactory(),
camelBootClock.getClock(),
beanContainer.getValue(),
CamelSupport.getCamelVersion(),
config);
Expand Down Expand Up @@ -175,6 +179,18 @@ public void enableCamelTrace(
producer.produce(new CamelContextCustomizerBuildItem(recorder.createBacklogTracerCustomizer(config)));
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIf = NativeOrNativeSourcesBuild.class)
CamelBootClockBuildItem createCamelBootClock(CamelContextRecorder recorder) {
return new CamelBootClockBuildItem(recorder.createBootClock(true));
}

@Record(ExecutionTime.STATIC_INIT)
@BuildStep(onlyIfNot = NativeOrNativeSourcesBuild.class)
CamelBootClockBuildItem createCamelNativeModeBootClock(CamelContextRecorder recorder) {
return new CamelBootClockBuildItem(recorder.createBootClock(false));
}

public static final class EventBridgeEnabled implements BooleanSupplier {
CamelConfig config;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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
*
* http://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.apache.camel.quarkus.core.deployment.spi;

import io.quarkus.builder.item.SimpleBuildItem;
import io.quarkus.runtime.RuntimeValue;
import org.apache.camel.clock.Clock;

public final class CamelBootClockBuildItem extends SimpleBuildItem {
private RuntimeValue<Clock> bootClockRuntimeValue;

public CamelBootClockBuildItem(RuntimeValue<Clock> bootClockRuntimeValue) {
this.bootClockRuntimeValue = bootClockRuntimeValue;
}

public RuntimeValue<Clock> getClock() {
return bootClockRuntimeValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import io.quarkus.runtime.RuntimeValue;
import io.quarkus.runtime.annotations.Recorder;
import org.apache.camel.CamelContext;
import org.apache.camel.ContextEvents;
import org.apache.camel.ExtendedCamelContext;
import org.apache.camel.catalog.RuntimeCamelCatalog;
import org.apache.camel.clock.Clock;
import org.apache.camel.impl.debugger.BacklogTracer;
import org.apache.camel.impl.engine.DefaultVariableRepositoryFactory;
import org.apache.camel.quarkus.core.devmode.NoOpModelineFactory;
Expand All @@ -41,6 +43,7 @@
import org.apache.camel.spi.Registry;
import org.apache.camel.spi.TypeConverterRegistry;
import org.apache.camel.spi.VariableRepositoryFactory;
import org.apache.camel.support.ResetableClock;
import org.eclipse.microprofile.config.ConfigProvider;

@Recorder
Expand All @@ -55,6 +58,7 @@ public RuntimeValue<CamelContext> createContext(
RuntimeValue<ComponentNameResolver> componentNameResolver,
RuntimeValue<PackageScanClassResolver> packageScanClassResolver,
RuntimeValue<ModelReifierFactory> modelReifierFactory,
RuntimeValue<Clock> bootClock,
BeanContainer beanContainer,
String version,
CamelConfig config) {
Expand All @@ -64,6 +68,8 @@ public RuntimeValue<CamelContext> createContext(
xmlModelDumper.getValue(),
yamlModelDumper.getValue());

context.getClock().add(ContextEvents.BOOT, bootClock.getValue());

final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
// Set ClassLoader first as some actions depend on it being available
ExtendedCamelContext extendedCamelContext = context.getCamelContextExtension();
Expand Down Expand Up @@ -155,4 +161,25 @@ public RuntimeValue<CamelContextCustomizer> createBacklogTracerCustomizer(CamelC
context.getCamelContextExtension().addContextPlugin(BacklogTracer.class, tracer);
});
}

public RuntimeValue<Clock> createBootClock(boolean isNativeImage) {
Clock clock;
if (isNativeImage) {
// Avoid recording boot times since 'boot' is done during the native image build
clock = new Clock() {
@Override
public long elapsed() {
return 0;
}

@Override
public long getCreated() {
return 0;
}
};
} else {
clock = new ResetableClock();
}
return new RuntimeValue<>(clock);
}
}

0 comments on commit ae5de89

Please sign in to comment.