From 71bbdf7ac53f3c7ac4b809167d514468df9dc3b6 Mon Sep 17 00:00:00 2001 From: SylvainJuge <763082+SylvainJuge@users.noreply.github.com> Date: Mon, 10 Jul 2023 15:30:30 +0200 Subject: [PATCH] Ignore gc alloc metrics when unsupported (#3225) * simply ignore when it's unsupported --- CHANGELOG.asciidoc | 1 + .../apm/agent/metrics/builtin/JvmGcMetrics.java | 10 ++++++++-- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 510cabdb58..b9a2a26ae5 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -35,6 +35,7 @@ Use subheadings with the "=====" level for adding notes for unreleased changes: ===== Bug fixes * Fixed agent programmatic attach with immutable config - {pull}3170[#3170] * Prevent overriding `ELASTIC_APM_AWS_LAMBDA_HANDLER` in AWS lambda execution when explicitly set - {pull}3205[#3205] +* Ignore gc allocation metrics when unsupported - {pull}3225[#3225] [[release-notes-1.x]] === Java Agent version 1.x diff --git a/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java b/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java index 7899bf8289..4cd140aced 100644 --- a/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java +++ b/apm-agent-core/src/main/java/co/elastic/apm/agent/metrics/builtin/JvmGcMetrics.java @@ -62,11 +62,17 @@ public double get() { // J9 does contain com.sun.management.ThreadMXBean in classpath // but the actual MBean it uses (com.ibm.lang.management.internal.ExtendedThreadMXBeanImpl) does not implement it if (sunBeanClass.isInstance(ManagementFactory.getThreadMXBean())) { + + DoubleSupplier supplier = (DoubleSupplier) Class.forName(getClass().getName() + "$HotspotAllocationSupplier").getEnumConstants()[0]; + + // attempt to read it at least once before registering + supplier.get(); + // in reference to JMH's GC profiler (gc.alloc.rate) - registry.add("jvm.gc.alloc", Labels.EMPTY, - (DoubleSupplier) Class.forName(getClass().getName() + "$HotspotAllocationSupplier").getEnumConstants()[0]); + registry.add("jvm.gc.alloc", Labels.EMPTY, supplier); } } catch (ClassNotFoundException ignore) { + } catch (UnsupportedOperationException ignore){ } }