Skip to content

Commit

Permalink
Activate Native JFR monitoring if OTel metrics are used
Browse files Browse the repository at this point in the history
(cherry picked from commit 3a88f3d)
  • Loading branch information
brunobat authored and gsmet committed Nov 19, 2024
1 parent 9b94c6a commit 80ee732
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package io.quarkus.deployment.builditem;

import io.quarkus.builder.item.MultiBuildItem;
import io.quarkus.deployment.pkg.NativeConfig;

/**
* A build item that indicates whether native monitoring is enabled and which option from {@link NativeConfig.MonitoringOption}.
* To be used in the native image generation.
*/
public final class NativeMonitoringBuildItem extends MultiBuildItem {
private final NativeConfig.MonitoringOption option;

public NativeMonitoringBuildItem(NativeConfig.MonitoringOption option) {
this.option = option;
}

public NativeConfig.MonitoringOption getOption() {
return this.option;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand All @@ -24,6 +26,7 @@
import io.quarkus.deployment.annotations.BuildProducer;
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.builditem.NativeImageFeatureBuildItem;
import io.quarkus.deployment.builditem.NativeMonitoringBuildItem;
import io.quarkus.deployment.builditem.SuppressNonRuntimeConfigChangedWarningBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ExcludeConfigBuildItem;
import io.quarkus.deployment.builditem.nativeimage.JPMSExportBuildItem;
Expand Down Expand Up @@ -115,6 +118,7 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig,
List<NativeImageSecurityProviderBuildItem> nativeImageSecurityProviders,
List<NativeImageFeatureBuildItem> nativeImageFeatures,
NativeImageRunnerBuildItem nativeImageRunner,
List<NativeMonitoringBuildItem> nativeMonitoringBuildItems,
CurateOutcomeBuildItem curateOutcomeBuildItem) {

Path outputDir;
Expand Down Expand Up @@ -147,6 +151,7 @@ ArtifactResultBuildItem nativeSourcesResult(NativeConfig nativeConfig,
.setGraalVMVersion(GraalVM.Version.CURRENT)
.setNativeImageFeatures(nativeImageFeatures)
.setContainerBuild(nativeImageRunner.isContainerBuild())
.setNativeMonitoringOptions(nativeMonitoringBuildItems)
.build();
List<String> command = nativeImageArgs.getArgs();

Expand Down Expand Up @@ -196,6 +201,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
Optional<ProcessInheritIODisabledBuildItem> processInheritIODisabledBuildItem,
List<NativeImageFeatureBuildItem> nativeImageFeatures,
Optional<NativeImageAgentConfigDirectoryBuildItem> nativeImageAgentConfigDirectoryBuildItem,
List<NativeMonitoringBuildItem> nativeMonitoringItems,
NativeImageRunnerBuildItem nativeImageRunner) {
if (nativeConfig.debug().enabled()) {
copyJarSourcesToLib(outputTargetBuildItem, curateOutcomeBuildItem);
Expand Down Expand Up @@ -254,6 +260,7 @@ public NativeImageBuildItem build(NativeConfig nativeConfig, LocalesBuildTimeCon
.setBrokenClasspath(incompleteClassPathAllowed.isAllow())
.setNativeImageSecurityProviders(nativeImageSecurityProviders)
.setJPMSExportBuildItems(jpmsExportBuildItems)
.setNativeMonitoringOptions(nativeMonitoringItems)
.setEnableModules(enableModules)
.setNativeMinimalJavaVersions(nativeMinimalJavaVersions)
.setUnsupportedOSes(unsupportedOses)
Expand Down Expand Up @@ -605,6 +612,7 @@ static class Builder {
private List<NativeMinimalJavaVersionBuildItem> nativeMinimalJavaVersions;
private List<UnsupportedOSBuildItem> unsupportedOSes;
private List<NativeImageFeatureBuildItem> nativeImageFeatures;
private List<NativeMonitoringBuildItem> nativeMonitoringItems;
private Path outputDir;
private String runnerJarName;
private String pie = "";
Expand Down Expand Up @@ -713,6 +721,11 @@ public Builder setNativeImageName(String nativeImageName) {
return this;
}

public Builder setNativeMonitoringOptions(List<NativeMonitoringBuildItem> options) {
this.nativeMonitoringItems = options;
return this;
}

@SuppressWarnings("deprecation")
public NativeImageInvokerInfo build() {
List<String> nativeImageArgs = new ArrayList<>();
Expand Down Expand Up @@ -932,17 +945,23 @@ public NativeImageInvokerInfo build() {
nativeImageArgs.add("-march=" + nativeConfig.march().get());
}

List<NativeConfig.MonitoringOption> monitoringOptions = new ArrayList<>();
Set<NativeConfig.MonitoringOption> monitoringOptions = new LinkedHashSet<>();
if (!OS.WINDOWS.isCurrent() || containerBuild) {
// --enable-monitoring=heapdump is not supported on Windows
monitoringOptions.add(NativeConfig.MonitoringOption.HEAPDUMP);
}

if (nativeMonitoringItems != null && !nativeMonitoringItems.isEmpty()) {
monitoringOptions.addAll(nativeMonitoringItems.stream()
.map(NativeMonitoringBuildItem::getOption)
.collect(Collectors.toSet()));
}

if (nativeConfig.monitoring().isPresent()) {
monitoringOptions.addAll(nativeConfig.monitoring().get());
}
if (!monitoringOptions.isEmpty()) {
nativeImageArgs.add("--enable-monitoring=" + monitoringOptions.stream()
.distinct()
.map(o -> o.name().toLowerCase(Locale.ROOT)).collect(Collectors.joining(",")));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
import io.quarkus.deployment.annotations.BuildStep;
import io.quarkus.deployment.annotations.BuildSteps;
import io.quarkus.deployment.builditem.CombinedIndexBuildItem;
import io.quarkus.deployment.builditem.NativeMonitoringBuildItem;
import io.quarkus.deployment.builditem.nativeimage.RuntimeReinitializedClassBuildItem;
import io.quarkus.deployment.pkg.NativeConfig;
import io.quarkus.opentelemetry.runtime.config.build.OTelBuildConfig;
import io.quarkus.opentelemetry.runtime.metrics.cdi.MetricsProducer;
import io.quarkus.opentelemetry.runtime.metrics.instrumentation.JvmMetricsService;
Expand All @@ -33,6 +35,11 @@ public class MetricProcessor {
private static final DotName METRIC_READER = DotName.createSimple(MetricReader.class.getName());
private static final DotName METRIC_PROCESSOR = DotName.createSimple(MetricProcessor.class.getName());

@BuildStep
void addNativeMonitoring(BuildProducer<NativeMonitoringBuildItem> nativeMonitoring) {
nativeMonitoring.produce(new NativeMonitoringBuildItem(NativeConfig.MonitoringOption.JFR));
}

@BuildStep
UnremovableBeanBuildItem ensureProducersAreRetained(
CombinedIndexBuildItem indexBuildItem,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ quarkus.security.users.embedded.plain-text=true
quarkus.security.users.embedded.enabled=true
quarkus.http.auth.basic=true

quarkus.native.monitoring=jfr

0 comments on commit 80ee732

Please sign in to comment.