diff --git a/ibm-mq-metrics/build.gradle.kts b/ibm-mq-metrics/build.gradle.kts index 71cddae840..01d9c298fc 100644 --- a/ibm-mq-metrics/build.gradle.kts +++ b/ibm-mq-metrics/build.gradle.kts @@ -20,6 +20,7 @@ val ibmClientJar: Configuration by configurations.creating { } dependencies { + api("com.google.auto.value:auto-value-annotations:1.11.1") api("com.google.code.findbugs:jsr305:3.0.2") api("io.swagger:swagger-annotations:1.6.16") api("org.jetbrains:annotations:26.1.0") @@ -33,6 +34,7 @@ dependencies { implementation("org.slf4j:slf4j-simple:2.0.17") testImplementation("com.google.guava:guava") testImplementation("io.opentelemetry:opentelemetry-sdk-testing") + annotationProcessor("com.google.auto.value:auto-value:1.11.1") ibmClientJar("com.ibm.mq:com.ibm.mq.allclient:9.4.5.1") { artifact { name = "com.ibm.mq.allclient" diff --git a/ibm-mq-metrics/config.yml b/ibm-mq-metrics/config.yml index 69e99e6cf7..d0a3c85449 100644 --- a/ibm-mq-metrics/config.yml +++ b/ibm-mq-metrics/config.yml @@ -214,4 +214,4 @@ sslConnection: # Configure the OTLP exporter using system properties keys following the specification https://opentelemetry.io/docs/languages/java/configuration/ otlpExporter: - otel.exporter.otlp.endpoint: http://localhost:4318 + otel.exporter.otlp.endpoint: http://localhost:4318 \ No newline at end of file diff --git a/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/TestWMQMonitor.java b/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/TestWMQMonitor.java index 8221c9353a..fd4ef63ccd 100644 --- a/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/TestWMQMonitor.java +++ b/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/TestWMQMonitor.java @@ -8,9 +8,9 @@ import static org.assertj.core.api.Assertions.assertThat; import com.fasterxml.jackson.databind.ObjectMapper; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.WmqMonitor; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; import java.util.List; import java.util.Map; @@ -26,12 +26,12 @@ class TestWMQMonitor { private final ConfigWrapper config; private final ExecutorService threadPool; - private final Meter meter; + private final MetricProducer producer; - TestWMQMonitor(ConfigWrapper config, Meter meter, ExecutorService service) { + TestWMQMonitor(ConfigWrapper config, MetricProducer producer, ExecutorService service) { this.config = config; this.threadPool = service; - this.meter = meter; + this.producer = producer; } /** @@ -47,7 +47,7 @@ void runTest() { assertThat(queueManagers).isNotNull(); ObjectMapper mapper = new ObjectMapper(); - WmqMonitor wmqTask = new WmqMonitor(config, threadPool, meter); + WmqMonitor wmqTask = new WmqMonitor(config, threadPool, producer); // we override this helper to pass in our opentelemetry helper instead. for (Map queueManager : queueManagers) { diff --git a/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/WMQMonitorIntegrationTest.java b/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/WMQMonitorIntegrationTest.java index 853a54d850..8319ee2e4c 100644 --- a/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/WMQMonitorIntegrationTest.java +++ b/ibm-mq-metrics/src/integrationTest/java/io/opentelemetry/ibm/mq/integration/tests/WMQMonitorIntegrationTest.java @@ -20,13 +20,14 @@ import com.ibm.mq.headers.pcf.PCFMessageAgent; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; import io.opentelemetry.ibm.mq.opentelemetry.Main; import io.opentelemetry.ibm.mq.util.WmqUtil; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import io.opentelemetry.sdk.resources.Resource; import java.io.File; import java.net.URISyntaxException; import java.net.URL; @@ -50,7 +51,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.RegisterExtension; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,9 +60,6 @@ class WMQMonitorIntegrationTest { private static final Logger logger = LoggerFactory.getLogger(WMQMonitorIntegrationTest.class); - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); - private static final ExecutorService service = Executors.newFixedThreadPool( 4, /* one gets burned with our @BeforeAll message uzi, 4 is faster than 2 */ @@ -183,11 +180,13 @@ void test_monitor_with_full_config() throws Exception { String configFile = getConfigFile("conf/test-config.yml"); ConfigWrapper config = ConfigWrapper.parse(configFile); - Meter meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); - TestWMQMonitor monitor = new TestWMQMonitor(config, meter, service); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + + TestWMQMonitor monitor = new TestWMQMonitor(config, producer, service); monitor.runTest(); - List data = otelTesting.getMetrics(); + List data = producer.produce(Resource.empty()); Map metrics = new HashMap<>(); for (MetricData metricData : data) { metrics.put(metricData.getName(), metricData); @@ -244,9 +243,9 @@ void test_monitor_with_full_config() throws Exception { void test_wmqmonitor() throws Exception { String configFile = getConfigFile("conf/test-queuemgr-config.yml"); ConfigWrapper config = ConfigWrapper.parse(configFile); - Meter meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); - - TestWMQMonitor monitor = new TestWMQMonitor(config, meter, service); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + TestWMQMonitor monitor = new TestWMQMonitor(config, producer, service); monitor.runTest(); // TODO: Wait why are there no asserts here? } @@ -257,14 +256,17 @@ void test_otlphttp() throws Exception { ConfigWrapper.parse(WMQMonitorIntegrationTest.getConfigFile("conf/test-config.yml")); ScheduledExecutorService service = Executors.newScheduledThreadPool(config.getNumberOfThreads()); - Main.run(config, service, otelTesting.getOpenTelemetry()); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + + Main.run(config, service, producer); CountDownLatch latch = new CountDownLatch(1); Future ignored = service.submit(latch::countDown); Thread.sleep(5000); // TODO: This is fragile and time consuming and should be made better service.shutdown(); assertTrue(service.awaitTermination(30, TimeUnit.SECONDS)); - List data = otelTesting.getMetrics(); + List data = producer.produce(Resource.empty()); Set metricNames = new HashSet<>(); for (MetricData metricData : data) { metricNames.add(metricData.getName()); @@ -290,11 +292,12 @@ void test_bad_connection() throws Exception { String configFile = getConfigFile("conf/test-bad-config.yml"); ConfigWrapper config = ConfigWrapper.parse(configFile); - Meter meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); - TestWMQMonitor monitor = new TestWMQMonitor(config, meter, service); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + TestWMQMonitor monitor = new TestWMQMonitor(config, producer, service); monitor.runTest(); - List data = otelTesting.getMetrics(); + List data = producer.produce(Resource.empty()); assertThat(data).isNotEmpty(); assertThat(data).hasSize(2); diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/WmqMonitor.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/WmqMonitor.java index 9611604cb5..8c0a21328d 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/WmqMonitor.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/WmqMonitor.java @@ -13,11 +13,8 @@ import com.ibm.mq.MQQueueManager; import com.ibm.mq.headers.pcf.PCFMessageAgent; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.config.QueueManager; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.metricscollector.ChannelMetricsCollector; import io.opentelemetry.ibm.mq.metricscollector.InquireChannelCmdCollector; @@ -49,15 +46,15 @@ public final class WmqMonitor { private final List queueManagers; private final List> jobs = new ArrayList<>(); - private final LongCounter errorCodesCounter; - private final LongGauge heartbeatGauge; private final ExecutorService threadPool; private final MetricsConfig metricsConfig; + private final MetricProducer producer; - public WmqMonitor(ConfigWrapper config, ExecutorService threadPool, Meter meter) { + public WmqMonitor(ConfigWrapper config, ExecutorService threadPool, MetricProducer producer) { List> queueManagers = getQueueManagers(config); ObjectMapper mapper = new ObjectMapper(); + this.producer = producer; this.queueManagers = new ArrayList<>(); for (Map queueManager : queueManagers) { @@ -70,21 +67,18 @@ public WmqMonitor(ConfigWrapper config, ExecutorService threadPool, Meter meter) } this.metricsConfig = new MetricsConfig(config); - - this.heartbeatGauge = Metrics.createIbmMqHeartbeat(meter); - this.errorCodesCounter = Metrics.createIbmMqConnectionErrors(meter); this.threadPool = threadPool; - jobs.add(new QueueManagerMetricsCollector(meter)); - jobs.add(new InquireQueueManagerCmdCollector(meter)); - jobs.add(new ChannelMetricsCollector(meter)); - jobs.add(new InquireChannelCmdCollector(meter)); - jobs.add(new QueueMetricsCollector(meter, threadPool, config)); - jobs.add(new ListenerMetricsCollector(meter)); - jobs.add(new TopicMetricsCollector(meter)); - jobs.add(new ReadConfigurationEventQueueCollector(meter)); - jobs.add(new PerformanceEventQueueCollector(meter)); - jobs.add(new QueueManagerEventCollector(meter)); + jobs.add(new QueueManagerMetricsCollector(producer)); + jobs.add(new InquireQueueManagerCmdCollector(producer)); + jobs.add(new ChannelMetricsCollector(producer)); + jobs.add(new InquireChannelCmdCollector(producer)); + jobs.add(new QueueMetricsCollector(producer, threadPool, config)); + jobs.add(new ListenerMetricsCollector(producer)); + jobs.add(new TopicMetricsCollector(producer)); + jobs.add(new ReadConfigurationEventQueueCollector(producer)); + jobs.add(new PerformanceEventQueueCollector(producer)); + jobs.add(new QueueManagerEventCollector(producer)); } public void run() { @@ -115,12 +109,12 @@ public void run(QueueManager queueManager) { if (e.getCause() instanceof MQException) { MQException mqe = (MQException) e.getCause(); String errorCode = String.valueOf(mqe.getReason()); - errorCodesCounter.add( + producer.recordIbmMqConnectionErrors( 1, Attributes.of(IBM_MQ_QUEUE_MANAGER, queueManagerName, ERROR_CODE, errorCode)); } } finally { if (this.metricsConfig.isIbmMqHeartbeatEnabled()) { - heartbeatGauge.set( + producer.recordIbmMqHeartbeat( heartBeatMetricValue, Attributes.of(IBM_MQ_QUEUE_MANAGER, queueManagerName)); } cleanUp(ibmQueueManager, agent); diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricData.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricData.java new file mode 100644 index 0000000000..0536c16fc3 --- /dev/null +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricData.java @@ -0,0 +1,30 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.ibm.mq.metrics; + +import com.google.auto.value.AutoValue; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.metrics.data.Data; +import io.opentelemetry.sdk.metrics.data.MetricDataType; +import io.opentelemetry.sdk.resources.Resource; +import javax.annotation.concurrent.Immutable; + +@Immutable +@AutoValue +abstract class MetricData implements io.opentelemetry.sdk.metrics.data.MetricData { + + static MetricData createMetricData( + Resource resource, + InstrumentationScopeInfo instrumentationScopeInfo, + String name, + String description, + String unit, + MetricDataType type, + Data data) { + return new AutoValue_MetricData( + resource, instrumentationScopeInfo, name, description, unit, type, data); + } +} diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricProducer.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricProducer.java new file mode 100644 index 0000000000..fac46bb5e6 --- /dev/null +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/MetricProducer.java @@ -0,0 +1,1029 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.ibm.mq.metrics; + +import static io.opentelemetry.ibm.mq.metrics.MetricData.createMetricData; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.common.Clock; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.metrics.data.AggregationTemporality; +import io.opentelemetry.sdk.metrics.data.GaugeData; +import io.opentelemetry.sdk.metrics.data.LongPointData; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.metrics.data.MetricDataType; +import io.opentelemetry.sdk.metrics.data.SumData; +import io.opentelemetry.sdk.resources.Resource; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; + +// This file is generated using weaver. Do not edit manually. + +/** Metric definitions generated from a Weaver model. Do not edit manually. */ +public final class MetricProducer implements io.opentelemetry.sdk.metrics.export.MetricProducer { + private final Resource resource; + private final InstrumentationScopeInfo instrumentationScopeInfo; + private final AtomicReference> metricData; + + private final Map counterIbmMqQueueDepthFullEvent; + private final Map counterIbmMqQueueDepthHighEvent; + private final Map counterIbmMqQueueDepthLowEvent; + private final Map counterIbmMqUnauthorizedEvent; + private final Map counterIbmMqConnectionErrors; + + private long currentEpochNanos; + + public MetricProducer(Resource resource, InstrumentationScopeInfo info) { + this.resource = resource; + this.instrumentationScopeInfo = info; + this.metricData = new AtomicReference<>(new ArrayList<>()); + this.currentEpochNanos = Clock.getDefault().now(); + this.counterIbmMqQueueDepthFullEvent = new ConcurrentHashMap<>(); + this.counterIbmMqQueueDepthHighEvent = new ConcurrentHashMap<>(); + this.counterIbmMqQueueDepthLowEvent = new ConcurrentHashMap<>(); + this.counterIbmMqUnauthorizedEvent = new ConcurrentHashMap<>(); + this.counterIbmMqConnectionErrors = new ConcurrentHashMap<>(); + } + + public void recordIbmMqMessageRetryCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.retry.count", + "Number of message retries", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqStatus(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.status", + "Channel status", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMaxSharingConversations(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.max.sharing.conversations", + "Maximum number of conversations permitted on this channel instance.", + "{conversation}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqCurrentSharingConversations(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.current.sharing.conversations", + "Current number of conversations permitted on this channel instance.", + "{conversation}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqByteReceived(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.byte.received", + "Number of bytes received", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqByteSent(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.byte.sent", + "Number of bytes sent", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqBuffersReceived(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.buffers.received", + "Buffers received", + "{buffer}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqBuffersSent(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.buffers.sent", + "Buffers sent", + "{buffer}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMessageCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.count", + "Message count", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqOpenInputCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.open.input.count", + "Count of applications sending messages to the queue", + "{application}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqOpenOutputCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.open.output.count", + "Count of applications consuming messages from the queue", + "{application}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqHighQueueDepth(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.high.queue.depth", + "The current high queue depth", + "{percent}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqServiceInterval(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.service.interval", + "The queue service interval", + "{percent}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqQueueDepthFullEvent(long value, Attributes attributes) { + + long cumulativeValue = + this.counterIbmMqQueueDepthFullEvent.compute( + attributes, + (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.queue.depth.full.event", + "The number of full queue events", + "{event}", + MetricDataType.LONG_SUM, + SumData.createLongSumData( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, + Clock.getDefault().now(), + attributes, + cumulativeValue))))); + } + } + + public void recordIbmMqQueueDepthHighEvent(long value, Attributes attributes) { + + long cumulativeValue = + this.counterIbmMqQueueDepthHighEvent.compute( + attributes, + (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.queue.depth.high.event", + "The number of high queue events", + "{event}", + MetricDataType.LONG_SUM, + SumData.createLongSumData( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, + Clock.getDefault().now(), + attributes, + cumulativeValue))))); + } + } + + public void recordIbmMqQueueDepthLowEvent(long value, Attributes attributes) { + + long cumulativeValue = + this.counterIbmMqQueueDepthLowEvent.compute( + attributes, + (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.queue.depth.low.event", + "The number of low queue events", + "{event}", + MetricDataType.LONG_SUM, + SumData.createLongSumData( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, + Clock.getDefault().now(), + attributes, + cumulativeValue))))); + } + } + + public void recordIbmMqExpiredMessages(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.expired.messages", + "Number of expired messages", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqUncommittedMessages(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.uncommitted.messages", + "Number of uncommitted messages", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqOldestMsgAge(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.oldest.msg.age", + "Queue message oldest age", + "microseconds", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqCurrentMaxQueueFilesize(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.current.max.queue.filesize", + "Current maximum queue file size", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqCurrentQueueFilesize(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.current.queue.filesize", + "Current queue file size", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqInstancesPerClient(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.instances.per.client", + "Instances per client", + "{instance}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMessageDeqCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.deq.count", + "Message dequeue count", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMessageEnqCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.enq.count", + "Message enqueue count", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqQueueDepth(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.queue.depth", + "Current queue depth", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqServiceIntervalEvent(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.service.interval.event", + "Queue service interval event", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqReusableLogSize(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.reusable.log.size", + "The amount of space occupied, in megabytes, by log extents available to be reused.", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqManagerActiveChannels(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.manager.active.channels", + "The queue manager active maximum channels limit", + "{channel}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqRestartLogSize(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.restart.log.size", + "Size of the log data required for restart recovery in megabytes.", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMaxQueueDepth(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.max.queue.depth", + "Maximum queue depth", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqOnqtimeShortPeriod(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.onqtime.short_period", + "Amount of time, in microseconds, that a message spent on the queue, over a short period", + "microseconds", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqOnqtimeLongPeriod(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.onqtime.long_period", + "Amount of time, in microseconds, that a message spent on the queue, over a longer period", + "microseconds", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMessageReceivedCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.received.count", + "Number of messages received", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMessageSentCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.message.sent.count", + "Number of messages sent", + "{message}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqMaxInstances(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.max.instances", + "Max channel instances", + "{instance}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqConnectionCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.connection.count", + "Active connections count", + "{connection}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqManagerStatus(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.manager.status", + "Queue manager status", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqHeartbeat(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.heartbeat", + "Queue manager heartbeat", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqArchiveLogSize(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.archive.log.size", + "Queue manager archive log size", + "By", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqManagerMaxActiveChannels(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.manager.max.active.channels", + "Queue manager max active channels", + "{channel}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqManagerStatisticsInterval(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.manager.statistics.interval", + "Queue manager statistics interval", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqPublishCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.publish.count", + "Topic publication count", + "{publication}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqSubscriptionCount(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.subscription.count", + "Topic subscription count", + "{subscription}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqListenerStatus(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.listener.status", + "Listener status", + "1", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqUnauthorizedEvent(long value, Attributes attributes) { + + long cumulativeValue = + this.counterIbmMqUnauthorizedEvent.compute( + attributes, + (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.unauthorized.event", + "Number of authentication error events", + "{event}", + MetricDataType.LONG_SUM, + SumData.createLongSumData( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, + Clock.getDefault().now(), + attributes, + cumulativeValue))))); + } + } + + public void recordIbmMqManagerMaxHandles(long value, Attributes attributes) { + + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.manager.max.handles", + "Max open handles", + "{event}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData( + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + } + + public void recordIbmMqConnectionErrors(long value, Attributes attributes) { + + long cumulativeValue = + this.counterIbmMqConnectionErrors.compute( + attributes, + (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add( + createMetricData( + this.resource, + this.instrumentationScopeInfo, + "ibm.mq.connection.errors", + "Number of connection errors", + "{errors}", + MetricDataType.LONG_SUM, + SumData.createLongSumData( + /* isMonotonic= */ true, + AggregationTemporality.CUMULATIVE, + Collections.singletonList( + LongPointData.create( + this.currentEpochNanos, + Clock.getDefault().now(), + attributes, + cumulativeValue))))); + } + } + + @Override + public List produce(Resource resource) { + List collectedPoints = this.metricData.getAndSet(new ArrayList<>()); + this.currentEpochNanos = Clock.getDefault().now(); + return collectedPoints; + } +} diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/Metrics.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/Metrics.java deleted file mode 100644 index b5c5f3c871..0000000000 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metrics/Metrics.java +++ /dev/null @@ -1,441 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.ibm.mq.metrics; - -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import java.util.function.Function; - -// This file is generated using weaver. Do not edit manually. - -/** Metric definitions generated from a Weaver model. Do not edit manually. */ -public final class Metrics { - public static final Function MIBY_TO_BYTES = x -> x * 1024L * 1024L; - - private Metrics() {} - - public static LongGauge createIbmMqMessageRetryCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.retry.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Number of message retries") - .build(); - } - - public static LongGauge createIbmMqStatus(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.status") - .ofLongs() - .setUnit("1") - .setDescription("Channel status") - .build(); - } - - public static LongGauge createIbmMqMaxSharingConversations(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.max.sharing.conversations") - .ofLongs() - .setUnit("{conversation}") - .setDescription("Maximum number of conversations permitted on this channel instance.") - .build(); - } - - public static LongGauge createIbmMqCurrentSharingConversations(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.current.sharing.conversations") - .ofLongs() - .setUnit("{conversation}") - .setDescription("Current number of conversations permitted on this channel instance.") - .build(); - } - - public static LongGauge createIbmMqByteReceived(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.byte.received") - .ofLongs() - .setUnit("By") - .setDescription("Number of bytes received") - .build(); - } - - public static LongGauge createIbmMqByteSent(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.byte.sent") - .ofLongs() - .setUnit("By") - .setDescription("Number of bytes sent") - .build(); - } - - public static LongGauge createIbmMqBuffersReceived(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.buffers.received") - .ofLongs() - .setUnit("{buffer}") - .setDescription("Buffers received") - .build(); - } - - public static LongGauge createIbmMqBuffersSent(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.buffers.sent") - .ofLongs() - .setUnit("{buffer}") - .setDescription("Buffers sent") - .build(); - } - - public static LongGauge createIbmMqMessageCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Message count") - .build(); - } - - public static LongGauge createIbmMqOpenInputCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.open.input.count") - .ofLongs() - .setUnit("{application}") - .setDescription("Count of applications sending messages to the queue") - .build(); - } - - public static LongGauge createIbmMqOpenOutputCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.open.output.count") - .ofLongs() - .setUnit("{application}") - .setDescription("Count of applications consuming messages from the queue") - .build(); - } - - public static LongGauge createIbmMqHighQueueDepth(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.high.queue.depth") - .ofLongs() - .setUnit("{percent}") - .setDescription("The current high queue depth") - .build(); - } - - public static LongGauge createIbmMqServiceInterval(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.service.interval") - .ofLongs() - .setUnit("{percent}") - .setDescription("The queue service interval") - .build(); - } - - public static LongCounter createIbmMqQueueDepthFullEvent(Meter meter) { - return meter - .counterBuilder("ibm.mq.queue.depth.full.event") - .setUnit("{event}") - .setDescription("The number of full queue events") - .build(); - } - - public static LongCounter createIbmMqQueueDepthHighEvent(Meter meter) { - return meter - .counterBuilder("ibm.mq.queue.depth.high.event") - .setUnit("{event}") - .setDescription("The number of high queue events") - .build(); - } - - public static LongCounter createIbmMqQueueDepthLowEvent(Meter meter) { - return meter - .counterBuilder("ibm.mq.queue.depth.low.event") - .setUnit("{event}") - .setDescription("The number of low queue events") - .build(); - } - - public static LongGauge createIbmMqExpiredMessages(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.expired.messages") - .ofLongs() - .setUnit("{message}") - .setDescription("Number of expired messages") - .build(); - } - - public static LongGauge createIbmMqUncommittedMessages(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.uncommitted.messages") - .ofLongs() - .setUnit("{message}") - .setDescription("Number of uncommitted messages") - .build(); - } - - public static LongGauge createIbmMqOldestMsgAge(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.oldest.msg.age") - .ofLongs() - .setUnit("microseconds") - .setDescription("Queue message oldest age") - .build(); - } - - public static LongGauge createIbmMqCurrentMaxQueueFilesize(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.current.max.queue.filesize") - .ofLongs() - .setUnit("By") - .setDescription("Current maximum queue file size") - .build(); - } - - public static LongGauge createIbmMqCurrentQueueFilesize(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.current.queue.filesize") - .ofLongs() - .setUnit("By") - .setDescription("Current queue file size") - .build(); - } - - public static LongGauge createIbmMqInstancesPerClient(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.instances.per.client") - .ofLongs() - .setUnit("{instance}") - .setDescription("Instances per client") - .build(); - } - - public static LongGauge createIbmMqMessageDeqCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.deq.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Message dequeue count") - .build(); - } - - public static LongGauge createIbmMqMessageEnqCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.enq.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Message enqueue count") - .build(); - } - - public static LongGauge createIbmMqQueueDepth(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.queue.depth") - .ofLongs() - .setUnit("{message}") - .setDescription("Current queue depth") - .build(); - } - - public static LongGauge createIbmMqServiceIntervalEvent(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.service.interval.event") - .ofLongs() - .setUnit("1") - .setDescription("Queue service interval event") - .build(); - } - - public static LongGauge createIbmMqReusableLogSize(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.reusable.log.size") - .ofLongs() - .setUnit("By") - .setDescription( - "The amount of space occupied, in megabytes, by log extents available to be reused.") - .build(); - } - - public static LongGauge createIbmMqManagerActiveChannels(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.manager.active.channels") - .ofLongs() - .setUnit("{channel}") - .setDescription("The queue manager active maximum channels limit") - .build(); - } - - public static LongGauge createIbmMqRestartLogSize(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.restart.log.size") - .ofLongs() - .setUnit("By") - .setDescription("Size of the log data required for restart recovery in megabytes.") - .build(); - } - - public static LongGauge createIbmMqMaxQueueDepth(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.max.queue.depth") - .ofLongs() - .setUnit("{message}") - .setDescription("Maximum queue depth") - .build(); - } - - public static LongGauge createIbmMqOnqtimeShortPeriod(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.onqtime.short_period") - .ofLongs() - .setUnit("microseconds") - .setDescription( - "Amount of time, in microseconds, that a message spent on the queue, over a short period") - .build(); - } - - public static LongGauge createIbmMqOnqtimeLongPeriod(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.onqtime.long_period") - .ofLongs() - .setUnit("microseconds") - .setDescription( - "Amount of time, in microseconds, that a message spent on the queue, over a longer period") - .build(); - } - - public static LongGauge createIbmMqMessageReceivedCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.received.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Number of messages received") - .build(); - } - - public static LongGauge createIbmMqMessageSentCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.message.sent.count") - .ofLongs() - .setUnit("{message}") - .setDescription("Number of messages sent") - .build(); - } - - public static LongGauge createIbmMqMaxInstances(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.max.instances") - .ofLongs() - .setUnit("{instance}") - .setDescription("Max channel instances") - .build(); - } - - public static LongGauge createIbmMqConnectionCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.connection.count") - .ofLongs() - .setUnit("{connection}") - .setDescription("Active connections count") - .build(); - } - - public static LongGauge createIbmMqManagerStatus(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.manager.status") - .ofLongs() - .setUnit("1") - .setDescription("Queue manager status") - .build(); - } - - public static LongGauge createIbmMqHeartbeat(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.heartbeat") - .ofLongs() - .setUnit("1") - .setDescription("Queue manager heartbeat") - .build(); - } - - public static LongGauge createIbmMqArchiveLogSize(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.archive.log.size") - .ofLongs() - .setUnit("By") - .setDescription("Queue manager archive log size") - .build(); - } - - public static LongGauge createIbmMqManagerMaxActiveChannels(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.manager.max.active.channels") - .ofLongs() - .setUnit("{channel}") - .setDescription("Queue manager max active channels") - .build(); - } - - public static LongGauge createIbmMqManagerStatisticsInterval(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.manager.statistics.interval") - .ofLongs() - .setUnit("1") - .setDescription("Queue manager statistics interval") - .build(); - } - - public static LongGauge createIbmMqPublishCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.publish.count") - .ofLongs() - .setUnit("{publication}") - .setDescription("Topic publication count") - .build(); - } - - public static LongGauge createIbmMqSubscriptionCount(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.subscription.count") - .ofLongs() - .setUnit("{subscription}") - .setDescription("Topic subscription count") - .build(); - } - - public static LongGauge createIbmMqListenerStatus(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.listener.status") - .ofLongs() - .setUnit("1") - .setDescription("Listener status") - .build(); - } - - public static LongCounter createIbmMqUnauthorizedEvent(Meter meter) { - return meter - .counterBuilder("ibm.mq.unauthorized.event") - .setUnit("{event}") - .setDescription("Number of authentication error events") - .build(); - } - - public static LongGauge createIbmMqManagerMaxHandles(Meter meter) { - return meter - .gaugeBuilder("ibm.mq.manager.max.handles") - .ofLongs() - .setUnit("{event}") - .setDescription("Max open handles") - .build(); - } - - public static LongCounter createIbmMqConnectionErrors(Meter meter) { - return meter - .counterBuilder("ibm.mq.connection.errors") - .setUnit("{errors}") - .setDescription("Number of connection errors") - .build(); - } -} diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollector.java index 44afdb28a0..2da9c955e0 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollector.java @@ -18,9 +18,7 @@ import com.ibm.mq.headers.pcf.PCFException; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -34,29 +32,13 @@ public final class ChannelMetricsCollector implements Consumer { public static final Logger logger = LoggerFactory.getLogger(InquireChannelCmdCollector.class); - private final LongGauge maxClientsGauge; - private final LongGauge instancesPerClientGauge; - private final LongGauge messageRetryCountGauge; - private final LongGauge messageReceivedCountGauge; - private final LongGauge messageSentCountGauge; + private final MetricProducer producer; - public InquireChannelCmdCollector(Meter meter) { - this.maxClientsGauge = Metrics.createIbmMqMaxInstances(meter); - this.instancesPerClientGauge = Metrics.createIbmMqInstancesPerClient(meter); - this.messageRetryCountGauge = Metrics.createIbmMqMessageRetryCount(meter); - this.messageReceivedCountGauge = Metrics.createIbmMqMessageReceivedCount(meter); - this.messageSentCountGauge = Metrics.createIbmMqMessageSentCount(meter); + public InquireChannelCmdCollector(MetricProducer producer) { + this.producer = producer; } @Override @@ -116,12 +106,12 @@ private void updateMetrics( .build(); if (context.getMetricsConfig().isIbmMqMaxInstancesEnabled() && message.getParameter(CMQCFC.MQIACH_MAX_INSTANCES) != null) { - this.maxClientsGauge.set( + this.producer.recordIbmMqMaxInstances( message.getIntParameterValue(CMQCFC.MQIACH_MAX_INSTANCES), attributes); } if (context.getMetricsConfig().isIbmMqInstancesPerClientEnabled() && message.getParameter(CMQCFC.MQIACH_MAX_INSTS_PER_CLIENT) != null) { - this.instancesPerClientGauge.set( + this.producer.recordIbmMqInstancesPerClient( message.getIntParameterValue(CMQCFC.MQIACH_MAX_INSTS_PER_CLIENT), attributes); } if (context.getMetricsConfig().isIbmMqMessageRetryCountEnabled()) { @@ -129,21 +119,21 @@ private void updateMetrics( if (message.getParameter(CMQCFC.MQIACH_MR_COUNT) != null) { count = message.getIntParameterValue(CMQCFC.MQIACH_MR_COUNT); } - this.messageRetryCountGauge.set(count, attributes); + this.producer.recordIbmMqMessageRetryCount(count, attributes); } if (context.getMetricsConfig().isIbmMqInstancesPerClientEnabled()) { int received = 0; if (message.getParameter(CMQCFC.MQIACH_MSGS_RECEIVED) != null) { received = message.getIntParameterValue(CMQCFC.MQIACH_MSGS_RECEIVED); } - this.messageReceivedCountGauge.set(received, attributes); + this.producer.recordIbmMqMessageReceivedCount(received, attributes); } if (context.getMetricsConfig().isIbmMqMessageSentCountEnabled()) { int sent = 0; if (message.getParameter(CMQCFC.MQIACH_MSGS_SENT) != null) { sent = message.getIntParameterValue(CMQCFC.MQIACH_MSGS_SENT); } - this.messageSentCountGauge.set(sent, attributes); + this.producer.recordIbmMqMessageSentCount(sent, attributes); } } } diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/InquireQueueManagerCmdCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/InquireQueueManagerCmdCollector.java index 5cdf0c52f5..9cbfcb23e0 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/InquireQueueManagerCmdCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/InquireQueueManagerCmdCollector.java @@ -13,9 +13,7 @@ import com.ibm.mq.headers.pcf.MQCFIL; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.util.List; import java.util.function.Consumer; import org.slf4j.Logger; @@ -26,10 +24,10 @@ public final class InquireQueueManagerCmdCollector implements Consumer { private static final Logger logger = LoggerFactory.getLogger(InquireTStatusCmdCollector.class); + private final MetricProducer producer; - private final LongGauge publishCountGauge; - private final LongGauge subscriptionCountGauge; - - public InquireTStatusCmdCollector(Meter meter) { - this.publishCountGauge = Metrics.createIbmMqPublishCount(meter); - this.subscriptionCountGauge = Metrics.createIbmMqSubscriptionCount(meter); + public InquireTStatusCmdCollector(MetricProducer producer) { + this.producer = producer; } @Override @@ -123,14 +118,14 @@ private void extractMetrics( if (pcfMessage.getParameter(CMQC.MQIA_PUB_COUNT) != null) { publisherCount = pcfMessage.getIntParameterValue(CMQC.MQIA_PUB_COUNT); } - publishCountGauge.set(publisherCount, attributes); + this.producer.recordIbmMqPublishCount(publisherCount, attributes); } if (context.getMetricsConfig().isIbmMqSubscriptionCountEnabled()) { int subscriberCount = 0; if (pcfMessage.getParameter(CMQC.MQIA_SUB_COUNT) != null) { subscriberCount = pcfMessage.getIntParameterValue(CMQC.MQIA_SUB_COUNT); } - subscriptionCountGauge.set(subscriberCount, attributes); + this.producer.recordIbmMqSubscriptionCount(subscriberCount, attributes); } } } diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollector.java index a7bf3d48d9..ee3f61d9d4 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollector.java @@ -12,9 +12,7 @@ import com.ibm.mq.headers.pcf.PCFException; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.util.Arrays; import java.util.List; import java.util.Set; @@ -40,10 +38,10 @@ public final class ListenerMetricsCollector implements Consumer { private static final Logger logger = LoggerFactory.getLogger(ListenerMetricsCollector.class); - private final LongGauge listenerStatusGauge; + private final MetricProducer producer; - public ListenerMetricsCollector(Meter meter) { - this.listenerStatusGauge = Metrics.createIbmMqListenerStatus(meter); + public ListenerMetricsCollector(MetricProducer producer) { + this.producer = producer; } @Override @@ -101,7 +99,7 @@ private void updateMetrics( throws PCFException { if (context.getMetricsConfig().isIbmMqListenerStatusEnabled()) { int status = message.getIntParameterValue(CMQCFC.MQIACH_LISTENER_STATUS); - listenerStatusGauge.set( + this.producer.recordIbmMqListenerStatus( status, Attributes.of( IBM_MQ_LISTENER_NAME, diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/PerformanceEventQueueCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/PerformanceEventQueueCollector.java index db1d4a254c..c42ca4f63a 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/PerformanceEventQueueCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/PerformanceEventQueueCollector.java @@ -17,9 +17,7 @@ import com.ibm.mq.headers.pcf.PCFException; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.io.IOException; import java.util.function.Consumer; import org.slf4j.Logger; @@ -30,14 +28,10 @@ public final class PerformanceEventQueueCollector implements Consumer gauges = new HashMap<>(); private final QueueCollectorSharedState sharedState; - private final LongGauge onqtimeShort; - private final LongGauge onqtimeLong; + private final MetricProducer producer; @FunctionalInterface private interface AllowedGauge { @@ -55,93 +53,86 @@ private interface AllowedGauge { } private static AllowedGauge createAllowedGauge( - LongGauge gauge, Function allowed) { - return createAllowedGauge(gauge, allowed, Integer::longValue /*identity*/); + BiConsumer recorder, Function allowed) { + return createAllowedGauge(recorder, allowed, Integer::longValue /*identity*/); } private static AllowedGauge createAllowedGauge( - LongGauge gauge, + BiConsumer recorder, Function allowed, Function unitMangler) { return (context, val, attributes) -> { if (allowed.apply(context.getMetricsConfig())) { - gauge.set(unitMangler.apply(val), attributes); + recorder.accept(unitMangler.apply(val), attributes); } }; } - QueueCollectionBuddy(Meter meter, QueueCollectorSharedState sharedState) { + QueueCollectionBuddy(MetricProducer producer, QueueCollectorSharedState sharedState) { this.sharedState = sharedState; + this.producer = producer; gauges.put( CMQC.MQIA_CURRENT_Q_DEPTH, createAllowedGauge( - Metrics.createIbmMqQueueDepth(meter), MetricsConfig::isIbmMqQueueDepthEnabled)); + producer::recordIbmMqQueueDepth, MetricsConfig::isIbmMqQueueDepthEnabled)); gauges.put( CMQC.MQIA_MAX_Q_DEPTH, createAllowedGauge( - Metrics.createIbmMqMaxQueueDepth(meter), MetricsConfig::isIbmMqMaxQueueDepthEnabled)); + producer::recordIbmMqMaxQueueDepth, MetricsConfig::isIbmMqMaxQueueDepthEnabled)); gauges.put( CMQC.MQIA_OPEN_INPUT_COUNT, createAllowedGauge( - Metrics.createIbmMqOpenInputCount(meter), MetricsConfig::isIbmMqOpenInputCountEnabled)); + producer::recordIbmMqOpenInputCount, MetricsConfig::isIbmMqOpenInputCountEnabled)); gauges.put( CMQC.MQIA_OPEN_OUTPUT_COUNT, createAllowedGauge( - Metrics.createIbmMqOpenOutputCount(meter), - MetricsConfig::isIbmMqOpenOutputCountEnabled)); + producer::recordIbmMqOpenOutputCount, MetricsConfig::isIbmMqOpenOutputCountEnabled)); gauges.put( CMQC.MQIA_Q_SERVICE_INTERVAL, createAllowedGauge( - Metrics.createIbmMqServiceInterval(meter), - MetricsConfig::isIbmMqServiceIntervalEnabled)); + producer::recordIbmMqServiceInterval, MetricsConfig::isIbmMqServiceIntervalEnabled)); gauges.put( CMQC.MQIA_Q_SERVICE_INTERVAL_EVENT, createAllowedGauge( - Metrics.createIbmMqServiceIntervalEvent(meter), + producer::recordIbmMqServiceIntervalEvent, MetricsConfig::isIbmMqServiceIntervalEventEnabled)); gauges.put( CMQCFC.MQIACF_OLDEST_MSG_AGE, createAllowedGauge( - Metrics.createIbmMqOldestMsgAge(meter), MetricsConfig::isIbmMqOldestMsgAgeEnabled)); + producer::recordIbmMqOldestMsgAge, MetricsConfig::isIbmMqOldestMsgAgeEnabled)); gauges.put( CMQCFC.MQIACF_UNCOMMITTED_MSGS, createAllowedGauge( - Metrics.createIbmMqUncommittedMessages(meter), + producer::recordIbmMqUncommittedMessages, MetricsConfig::isIbmMqUncommittedMessagesEnabled)); gauges.put( CMQCFC.MQIACF_EXPIRY_Q_COUNT, createAllowedGauge( - Metrics.createIbmMqExpiredMessages(meter), - MetricsConfig::isIbmMqExpiredMessagesEnabled)); + producer::recordIbmMqExpiredMessages, MetricsConfig::isIbmMqExpiredMessagesEnabled)); gauges.put( CMQC.MQIA_MSG_DEQ_COUNT, createAllowedGauge( - Metrics.createIbmMqMessageDeqCount(meter), - MetricsConfig::isIbmMqMessageDeqCountEnabled)); + producer::recordIbmMqMessageDeqCount, MetricsConfig::isIbmMqMessageDeqCountEnabled)); gauges.put( CMQC.MQIA_MSG_ENQ_COUNT, createAllowedGauge( - Metrics.createIbmMqMessageEnqCount(meter), - MetricsConfig::isIbmMqMessageEnqCountEnabled)); + producer::recordIbmMqMessageEnqCount, MetricsConfig::isIbmMqMessageEnqCountEnabled)); gauges.put( CMQC.MQIA_HIGH_Q_DEPTH, createAllowedGauge( - Metrics.createIbmMqHighQueueDepth(meter), MetricsConfig::isIbmMqHighQueueDepthEnabled)); + producer::recordIbmMqHighQueueDepth, MetricsConfig::isIbmMqHighQueueDepthEnabled)); gauges.put( CMQCFC.MQIACF_CUR_Q_FILE_SIZE, createAllowedGauge( - Metrics.createIbmMqCurrentQueueFilesize(meter), + producer::recordIbmMqCurrentQueueFilesize, MetricsConfig::isIbmMqCurrentQueueFilesizeEnabled, MIBY_TO_BYTES)); gauges.put( CMQCFC.MQIACF_CUR_MAX_FILE_SIZE, createAllowedGauge( - Metrics.createIbmMqCurrentMaxQueueFilesize(meter), + producer::recordIbmMqCurrentMaxQueueFilesize, MetricsConfig::isIbmMqCurrentMaxQueueFilesizeEnabled, MIBY_TO_BYTES)); - - this.onqtimeShort = Metrics.createIbmMqOnqtimeShortPeriod(meter); - this.onqtimeLong = Metrics.createIbmMqOnqtimeLongPeriod(meter); } /** @@ -306,10 +297,10 @@ private void updateMetrics( if (pcfParam instanceof MQCFIL) { int[] metricVals = pcfMessage.getIntListParameterValue(constantValue); if (context.getMetricsConfig().isIbmMqOnqtimeShortPeriodEnabled()) { - onqtimeShort.set(metricVals[0], attributes); + this.producer.recordIbmMqOnqtimeShortPeriod(metricVals[0], attributes); } if (context.getMetricsConfig().isIbmMqOnqtimeLongPeriodEnabled()) { - onqtimeLong.set(metricVals[1], attributes); + this.producer.recordIbmMqOnqtimeLongPeriod(metricVals[1], attributes); } } } diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerEventCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerEventCollector.java index 723e433cf5..d942a7e55e 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerEventCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerEventCollector.java @@ -18,9 +18,7 @@ import com.ibm.mq.constants.MQConstants; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.io.IOException; import java.util.function.Consumer; import org.slf4j.Logger; @@ -30,10 +28,10 @@ public final class QueueManagerEventCollector implements Consumer { private static final Logger logger = LoggerFactory.getLogger(QueueManagerEventCollector.class); - private final LongCounter authorityEventCounter; + private final MetricProducer producer; - public QueueManagerEventCollector(Meter meter) { - this.authorityEventCounter = Metrics.createIbmMqUnauthorizedEvent(meter); + public QueueManagerEventCollector(MetricProducer producer) { + this.producer = producer; } private void readEvents(MetricsCollectorContext context, String queueManagerEventsQueueName) @@ -58,7 +56,7 @@ private void readEvents(MetricsCollectorContext context, String queueManagerEven if (context.getMetricsConfig().isIbmMqUnauthorizedEventEnabled()) { String username = received.getStringParameterValue(CMQCFC.MQCACF_USER_IDENTIFIER); String applicationName = received.getStringParameterValue(CMQCFC.MQCACF_APPL_NAME); - authorityEventCounter.add( + this.producer.recordIbmMqUnauthorizedEvent( 1, Attributes.of( IBM_MQ_QUEUE_MANAGER, diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollector.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollector.java index 2b3d570862..5f02d1ba69 100644 --- a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollector.java +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollector.java @@ -6,14 +6,12 @@ package io.opentelemetry.ibm.mq.metricscollector; import static io.opentelemetry.ibm.mq.metrics.IbmMqAttributes.IBM_MQ_QUEUE_MANAGER; -import static io.opentelemetry.ibm.mq.metrics.Metrics.MIBY_TO_BYTES; +import static io.opentelemetry.ibm.mq.util.MetricsUtil.MIBY_TO_BYTES; import com.ibm.mq.constants.CMQCFC; import com.ibm.mq.headers.pcf.PCFMessage; import io.opentelemetry.api.common.Attributes; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import io.opentelemetry.ibm.mq.metrics.Metrics; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import java.util.List; import java.util.function.Consumer; import org.slf4j.Logger; @@ -24,20 +22,10 @@ public final class QueueManagerMetricsCollector implements Consumer builder.setResource(Resource.empty())) - .build(); - - OpenTelemetrySdk otel = sdk.getOpenTelemetrySdk(); - - run(config, service, otel); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.create("websphere/mq")); + run(config, service, producer); } @VisibleForTesting public static void run( - ConfigWrapper config, ScheduledExecutorService service, OpenTelemetry otel) { - WmqMonitor monitor = new WmqMonitor(config, service, otel.getMeter("websphere/mq")); + ConfigWrapper config, ScheduledExecutorService service, MetricProducer producer) { + + AutoConfiguredOpenTelemetrySdk.builder() + .addMeterProviderCustomizer( + (builder, configProps) -> + builder.setResource(Resource.empty()).registerMetricProducer(producer)) + .build(); + + WmqMonitor monitor = new WmqMonitor(config, service, producer); ScheduledFuture unused = service.scheduleAtFixedRate( monitor::run, diff --git a/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/util/MetricsUtil.java b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/util/MetricsUtil.java new file mode 100644 index 0000000000..612724f3b3 --- /dev/null +++ b/ibm-mq-metrics/src/main/java/io/opentelemetry/ibm/mq/util/MetricsUtil.java @@ -0,0 +1,15 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.ibm.mq.util; + +import java.util.function.Function; + +public final class MetricsUtil { + + private MetricsUtil() {} + + public static final Function MIBY_TO_BYTES = x -> x * 1024L * 1024L; +} diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollectorTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollectorTest.java index b4ae65e887..a132e04228 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollectorTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ChannelMetricsCollectorTest.java @@ -18,12 +18,13 @@ import com.ibm.mq.headers.pcf.PCFException; import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import io.opentelemetry.sdk.resources.Resource; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -31,7 +32,6 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -41,13 +41,9 @@ @ExtendWith(MockitoExtension.class) class ChannelMetricsCollectorTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); - ChannelMetricsCollector classUnderTest; QueueManager queueManager; MetricsCollectorContext context; - Meter meter; @Mock PCFMessageAgent pcfMessageAgent; @BeforeEach @@ -55,7 +51,6 @@ void setup() throws Exception { ConfigWrapper config = ConfigWrapper.parse("src/test/resources/conf/config.yml"); ObjectMapper mapper = new ObjectMapper(); queueManager = mapper.convertValue(config.getQueueManagers().get(0), QueueManager.class); - meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); context = new MetricsCollectorContext(queueManager, pcfMessageAgent, null, new MetricsConfig(config)); } @@ -64,7 +59,9 @@ void setup() throws Exception { void testPublishMetrics() throws Exception { when(pcfMessageAgent.send(any(PCFMessage.class))) .thenReturn(createPCFResponseForInquireChannelStatusCmd()); - classUnderTest = new ChannelMetricsCollector(meter); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new ChannelMetricsCollector(producer); classUnderTest.accept(context); @@ -78,7 +75,7 @@ void testPublishMetrics() throws Exception { "ibm.mq.buffers.sent", "ibm.mq.buffers.received")); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { if (metricsList.remove(metric.getName())) { if (metric.getName().equals("ibm.mq.message.count")) { assertThat(metric.getLongGaugeData().getPoints().iterator().next().getValue()) @@ -188,31 +185,37 @@ private static PCFMessage[] createPCFResponseForInquireChannelStatusCmd() { @Test void testPublishMetrics_nullResponse() throws Exception { + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); when(pcfMessageAgent.send(any(PCFMessage.class))).thenReturn(null); - classUnderTest = new ChannelMetricsCollector(meter); + classUnderTest = new ChannelMetricsCollector(producer); classUnderTest.accept(context); - assertThat(otelTesting.getMetrics()).isEmpty(); + assertThat(producer.produce(Resource.empty())).isEmpty(); } @Test void testPublishMetrics_emptyResponse() throws Exception { + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); when(pcfMessageAgent.send(any(PCFMessage.class))).thenReturn(new PCFMessage[] {}); - classUnderTest = new ChannelMetricsCollector(meter); + classUnderTest = new ChannelMetricsCollector(producer); classUnderTest.accept(context); - assertThat(otelTesting.getMetrics()).isEmpty(); + assertThat(producer.produce(Resource.empty())).isEmpty(); } @ParameterizedTest @MethodSource("exceptionsToThrow") void testPublishMetrics_pfException(Exception exceptionToThrow) throws Exception { + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); when(pcfMessageAgent.send(any(PCFMessage.class))).thenThrow(exceptionToThrow); - classUnderTest = new ChannelMetricsCollector(meter); + classUnderTest = new ChannelMetricsCollector(producer); classUnderTest.accept(context); - List exported = otelTesting.getMetrics(); + List exported = producer.produce(Resource.empty()); assertThat(exported.get(0).getLongGaugeData().getPoints()).hasSize(1); assertThatMetric(exported.get(0), 0).hasName("ibm.mq.manager.active.channels").hasValue(0); } diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/InquireChannelCmdCollectorTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/InquireChannelCmdCollectorTest.java index b9964537f9..1aea9bc6cd 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/InquireChannelCmdCollectorTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/InquireChannelCmdCollectorTest.java @@ -14,32 +14,28 @@ import com.ibm.mq.constants.CMQXC; import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import io.opentelemetry.sdk.resources.Resource; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class InquireChannelCmdCollectorTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); - InquireChannelCmdCollector classUnderTest; MetricsCollectorContext context; - Meter meter; @Mock PCFMessageAgent pcfMessageAgent; @BeforeEach @@ -48,7 +44,6 @@ public void setup() throws Exception { ObjectMapper mapper = new ObjectMapper(); QueueManager queueManager = mapper.convertValue(config.getQueueManagers().get(0), QueueManager.class); - meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); context = new MetricsCollectorContext(queueManager, pcfMessageAgent, null, new MetricsConfig(config)); } @@ -57,7 +52,9 @@ public void setup() throws Exception { void testProcessPCFRequestAndPublishQMetricsForInquireQStatusCmd() throws Exception { when(pcfMessageAgent.send(any(PCFMessage.class))) .thenReturn(createPCFResponseForInquireChannelCmd()); - classUnderTest = new InquireChannelCmdCollector(meter); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new InquireChannelCmdCollector(producer); classUnderTest.accept(context); List metricsList = new ArrayList<>( @@ -65,7 +62,7 @@ void testProcessPCFRequestAndPublishQMetricsForInquireQStatusCmd() throws Except "ibm.mq.message.retry.count", "ibm.mq.message.received.count", "ibm.mq.message.sent.count")); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { if (metricsList.remove(metric.getName())) { if (metric.getName().equals("ibm.mq.message.retry.count")) { assertThat(metric.getLongGaugeData().getPoints().iterator().next().getValue()) diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollectorTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollectorTest.java index ea46450d38..d5c029a0b4 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollectorTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/ListenerMetricsCollectorTest.java @@ -14,25 +14,21 @@ import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; -import io.opentelemetry.sdk.metrics.data.LongPointData; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; -import java.util.HashSet; -import java.util.Set; -import java.util.stream.Collectors; +import io.opentelemetry.sdk.resources.Resource; +import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class ListenerMetricsCollectorTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); ListenerMetricsCollector classUnderTest; QueueManager queueManager; @@ -53,21 +49,20 @@ void testPublishMetrics() throws Exception { MetricsCollectorContext context = new MetricsCollectorContext(queueManager, pcfMessageAgent, null, new MetricsConfig(config)); - classUnderTest = - new ListenerMetricsCollector( - otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq")); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new ListenerMetricsCollector(producer); classUnderTest.accept(context); - MetricData metric = otelTesting.getMetrics().get(0); - assertThat(metric.getName()).isEqualTo("ibm.mq.listener.status"); - Set values = new HashSet<>(); - values.add(2L); - values.add(3L); - assertThat( - metric.getLongGaugeData().getPoints().stream() - .map(LongPointData::getValue) - .collect(Collectors.toSet())) - .isEqualTo(values); + List result = producer.produce(Resource.empty()); + assertThat(result.size()).isEqualTo(2); + assertThat(result.get(0).getName()).isEqualTo("ibm.mq.listener.status"); + assertThat(result.get(0).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(2L); + + assertThat(result.get(1).getName()).isEqualTo("ibm.mq.listener.status"); + assertThat(result.get(1).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(3L); } /* diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueCollectionBuddyTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueCollectionBuddyTest.java index 8fdf5574d8..1fae0fad1e 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueCollectionBuddyTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueCollectionBuddyTest.java @@ -15,32 +15,29 @@ import com.ibm.mq.constants.CMQCFC; import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; -import io.opentelemetry.api.metrics.Meter; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.LongPointData; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import io.opentelemetry.sdk.resources.Resource; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class QueueCollectionBuddyTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); QueueCollectionBuddy classUnderTest; QueueManager queueManager; MetricsCollectorContext collectorContext; - Meter meter; @Mock private PCFMessageAgent pcfMessageAgent; @BeforeEach @@ -48,7 +45,6 @@ void setup() throws Exception { ConfigWrapper config = ConfigWrapper.parse("src/test/resources/conf/config.yml"); ObjectMapper mapper = new ObjectMapper(); queueManager = mapper.convertValue(config.getQueueManagers().get(0), QueueManager.class); - meter = otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq"); collectorContext = new MetricsCollectorContext(queueManager, pcfMessageAgent, null, new MetricsConfig(config)); } @@ -61,8 +57,9 @@ void testProcessPcfRequestAndPublishQMetricsForInquireQStatusCmd() throws Except sharedState.putQueueType("DEV.QUEUE.1", "local-transmission"); PCFMessage request = createPCFRequestForInquireQStatusCmd(); when(pcfMessageAgent.send(request)).thenReturn(createPCFResponseForInquireQStatusCmd()); - - classUnderTest = new QueueCollectionBuddy(meter, sharedState); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new QueueCollectionBuddy(producer, sharedState); classUnderTest.processPcfRequestAndPublishQMetrics( collectorContext, request, "*", InquireQStatusCmdCollector.ATTRIBUTES); @@ -88,7 +85,7 @@ void testProcessPcfRequestAndPublishQMetricsForInquireQStatusCmd() throws Except "ibm.mq.onqtime.long_period", -1L, "ibm.mq.queue.depth", 1L)))); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { for (LongPointData d : metric.getLongGaugeData().getPoints()) { String queueName = d.getAttributes().get(MESSAGING_DESTINATION_NAME); Long expectedValue = expectedValues.get(queueName).remove(metric.getName()); @@ -105,7 +102,9 @@ void testProcessPcfRequestAndPublishQMetricsForInquireQStatusCmd() throws Except void testProcessPcfRequestAndPublishQMetricsForInquireQCmd() throws Exception { PCFMessage request = createPCFRequestForInquireQCmd(); when(pcfMessageAgent.send(request)).thenReturn(createPCFResponseForInquireQCmd()); - classUnderTest = new QueueCollectionBuddy(meter, new QueueCollectorSharedState()); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new QueueCollectionBuddy(producer, new QueueCollectorSharedState()); classUnderTest.processPcfRequestAndPublishQMetrics( collectorContext, request, "*", InquireQCmdCollector.ATTRIBUTES); @@ -127,7 +126,7 @@ void testProcessPcfRequestAndPublishQMetricsForInquireQCmd() throws Exception { "ibm.mq.open.input.count", 3L, "ibm.mq.open.output.count", 3L)))); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { for (LongPointData d : metric.getLongGaugeData().getPoints()) { String queueName = d.getAttributes().get(MESSAGING_DESTINATION_NAME); Long expectedValue = expectedValues.get(queueName).remove(metric.getName()); @@ -148,11 +147,13 @@ void testProcessPcfRequestAndPublishQMetricsForResetQStatsCmd() throws Exception sharedState.putQueueType("DEV.QUEUE.1", "local-transmission"); PCFMessage request = createPCFRequestForResetQStatsCmd(); when(pcfMessageAgent.send(request)).thenReturn(createPCFResponseForResetQStatsCmd()); - classUnderTest = new QueueCollectionBuddy(meter, sharedState); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new QueueCollectionBuddy(producer, sharedState); classUnderTest.processPcfRequestAndPublishQMetrics( collectorContext, request, "*", ResetQStatsCmdCollector.ATTRIBUTES); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { Iterator iterator = metric.getLongGaugeData().getPoints().iterator(); if (metric.getName().equals("ibm.mq.high.queue.depth")) { assertThat(iterator.next().getValue()).isEqualTo(10); diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollectorTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollectorTest.java index 3c6ef15031..1d3b2e79a1 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollectorTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/QueueManagerMetricsCollectorTest.java @@ -16,25 +16,23 @@ import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; +import io.opentelemetry.sdk.resources.Resource; import java.util.ArrayList; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class QueueManagerMetricsCollectorTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); - QueueManagerMetricsCollector classUnderTest; QueueManager queueManager; MetricsCollectorContext context; @@ -54,13 +52,13 @@ public void setup() throws Exception { void testProcessPCFRequestAndPublishQMetricsForInquireQStatusCmd() throws Exception { when(pcfMessageAgent.send(any(PCFMessage.class))) .thenReturn(createPCFResponseForInquireQMgrStatusCmd()); - classUnderTest = - new QueueManagerMetricsCollector( - otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq")); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new QueueManagerMetricsCollector(producer); classUnderTest.accept(context); List metricsList = new ArrayList<>(singletonList("ibm.mq.manager.status")); - for (MetricData metric : otelTesting.getMetrics()) { + for (MetricData metric : producer.produce(Resource.empty())) { if (metricsList.remove(metric.getName())) { assertThat(metric.getLongGaugeData().getPoints().iterator().next().getValue()).isEqualTo(2); } diff --git a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/TopicMetricsCollectorTest.java b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/TopicMetricsCollectorTest.java index 646d4ee988..8613964549 100644 --- a/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/TopicMetricsCollectorTest.java +++ b/ibm-mq-metrics/src/test/java/io/opentelemetry/ibm/mq/metricscollector/TopicMetricsCollectorTest.java @@ -15,28 +15,21 @@ import com.ibm.mq.headers.pcf.PCFMessage; import com.ibm.mq.headers.pcf.PCFMessageAgent; import io.opentelemetry.ibm.mq.config.QueueManager; +import io.opentelemetry.ibm.mq.metrics.MetricProducer; import io.opentelemetry.ibm.mq.metrics.MetricsConfig; import io.opentelemetry.ibm.mq.opentelemetry.ConfigWrapper; -import io.opentelemetry.sdk.metrics.data.LongPointData; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; import io.opentelemetry.sdk.metrics.data.MetricData; -import io.opentelemetry.sdk.testing.junit5.OpenTelemetryExtension; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; +import io.opentelemetry.sdk.resources.Resource; import java.util.List; -import java.util.Set; -import java.util.stream.Collectors; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; -import org.junit.jupiter.api.extension.RegisterExtension; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; @ExtendWith(MockitoExtension.class) class TopicMetricsCollectorTest { - @RegisterExtension - static final OpenTelemetryExtension otelTesting = OpenTelemetryExtension.create(); TopicMetricsCollector classUnderTest; QueueManager queueManager; @@ -54,42 +47,32 @@ void setup() throws Exception { void testPublishMetrics() throws Exception { MetricsCollectorContext context = new MetricsCollectorContext(queueManager, pcfMessageAgent, null, new MetricsConfig(config)); - classUnderTest = - new TopicMetricsCollector(otelTesting.getOpenTelemetry().getMeter("opentelemetry.io/mq")); + MetricProducer producer = + new MetricProducer(Resource.empty(), InstrumentationScopeInfo.empty()); + classUnderTest = new TopicMetricsCollector(producer); when(pcfMessageAgent.send(any(PCFMessage.class))) .thenReturn(createPCFResponseForInquireTopicStatusCmd()); classUnderTest.accept(context); - List metricsList = - new ArrayList<>(Arrays.asList("ibm.mq.publish.count", "ibm.mq.subscription.count")); - - for (MetricData metric : otelTesting.getMetrics()) { - if (metricsList.remove(metric.getName())) { - if (metric.getName().equals("ibm.mq.publish.count")) { - Set values = new HashSet<>(); - values.add(2L); - values.add(3L); - assertThat( - metric.getLongGaugeData().getPoints().stream() - .map(LongPointData::getValue) - .collect(Collectors.toSet())) - .isEqualTo(values); - } - if (metric.getName().equals("ibm.mq.subscription.count")) { - Set values = new HashSet<>(); - values.add(3L); - values.add(4L); - assertThat( - metric.getLongGaugeData().getPoints().stream() - .map(LongPointData::getValue) - .collect(Collectors.toSet())) - .isEqualTo(values); - } - } - } - assertThat(metricsList).isEmpty(); + List result = producer.produce(Resource.empty()); + assertThat(result.size()).isEqualTo(4); + assertThat(result.get(0).getName()).isEqualTo("ibm.mq.publish.count"); + assertThat(result.get(0).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(2L); + + assertThat(result.get(1).getName()).isEqualTo("ibm.mq.subscription.count"); + assertThat(result.get(1).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(3L); + + assertThat(result.get(2).getName()).isEqualTo("ibm.mq.publish.count"); + assertThat(result.get(2).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(3L); + + assertThat(result.get(3).getName()).isEqualTo("ibm.mq.subscription.count"); + assertThat(result.get(3).getLongGaugeData().getPoints().iterator().next().getValue()) + .isEqualTo(4L); } private static PCFMessage[] createPCFResponseForInquireTopicStatusCmd() { diff --git a/ibm-mq-metrics/templates/registry/java/MetricProducer.java.j2 b/ibm-mq-metrics/templates/registry/java/MetricProducer.java.j2 new file mode 100644 index 0000000000..44ddeda18a --- /dev/null +++ b/ibm-mq-metrics/templates/registry/java/MetricProducer.java.j2 @@ -0,0 +1,83 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ +package io.opentelemetry.ibm.mq.metrics; + +import static io.opentelemetry.ibm.mq.metrics.MetricData.createMetricData; + +import io.opentelemetry.api.common.Attributes; +import io.opentelemetry.sdk.common.Clock; +import io.opentelemetry.sdk.common.InstrumentationScopeInfo; +import io.opentelemetry.sdk.metrics.data.AggregationTemporality; +import io.opentelemetry.sdk.metrics.data.GaugeData; +import io.opentelemetry.sdk.metrics.data.LongPointData; +import io.opentelemetry.sdk.metrics.data.MetricData; +import io.opentelemetry.sdk.metrics.data.MetricDataType; +import io.opentelemetry.sdk.metrics.data.SumData; +import io.opentelemetry.sdk.resources.Resource; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicReference; + + +// This file is generated using weaver. Do not edit manually. + +/** Metric definitions generated from a Weaver model. Do not edit manually. */ +public final class MetricProducer implements io.opentelemetry.sdk.metrics.export.MetricProducer { + private final Resource resource; + private final InstrumentationScopeInfo instrumentationScopeInfo; + private final AtomicReference> metricData; + +{% for metric in ctx %}{% if metric.instrument == "counter" %}private final Map counter{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }};{% endif %}{% endfor %} + + private long currentEpochNanos; + + public MetricProducer(Resource resource, InstrumentationScopeInfo info){ + this.resource = resource; + this.instrumentationScopeInfo = info; + this.metricData = new AtomicReference<>(new ArrayList<>()); + this.currentEpochNanos = Clock.getDefault().now(); + {% for metric in ctx %}{% if metric.instrument == "counter" %}this.counter{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }} = new ConcurrentHashMap<>();{% endif %}{% endfor %} + } +{% for metric in ctx %} + + + public void record{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }}(long value, Attributes attributes) { + {% if metric.instrument == "gauge" %} + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add(createMetricData(this.resource, this.instrumentationScopeInfo, "{{ metric.metric_name }}", "{{ metric.brief }}", "{{ metric.unit }}", + MetricDataType.LONG_GAUGE, + GaugeData.createLongGaugeData(Collections.singletonList(LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, value))))); + } + {% elif metric.instrument == "counter" %} + long cumulativeValue = this.counter{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }}.compute(attributes, (k, v) -> { + if (v == null) { + return value; + } else { + return v + value; + } + }); + List currentMetrics = this.metricData.get(); + if (currentMetrics != null) { + currentMetrics.add(createMetricData(this.resource, this.instrumentationScopeInfo, "{{ metric.metric_name }}", "{{ metric.brief }}", "{{ metric.unit }}", + MetricDataType.LONG_SUM, + SumData.createLongSumData(/* isMonotonic= */true, AggregationTemporality.CUMULATIVE, Collections.singletonList(LongPointData.create( + this.currentEpochNanos, Clock.getDefault().now(), attributes, cumulativeValue))))); + } + {% endif %} + } +{% endfor %} + + @Override + public List produce(Resource resource) { + List collectedPoints = this.metricData.getAndSet(new ArrayList<>()); + this.currentEpochNanos = Clock.getDefault().now(); + return collectedPoints; + } +} diff --git a/ibm-mq-metrics/templates/registry/java/Metrics.java.j2 b/ibm-mq-metrics/templates/registry/java/Metrics.java.j2 deleted file mode 100644 index 1b0097826c..0000000000 --- a/ibm-mq-metrics/templates/registry/java/Metrics.java.j2 +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ -package io.opentelemetry.ibm.mq.metrics; - -import io.opentelemetry.api.metrics.LongCounter; -import io.opentelemetry.api.metrics.LongGauge; -import io.opentelemetry.api.metrics.Meter; -import java.util.function.Function; - -// This file is generated using weaver. Do not edit manually. - -/** Metric definitions generated from a Weaver model. Do not edit manually. */ -public final class Metrics { -public final static Function MIBY_TO_BYTES = x -> x * 1024L * 1024L; -private Metrics(){ -} -{% for metric in ctx %} - - {% if metric.instrument == "gauge" %} - public static LongGauge create{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }}(Meter meter) { - return meter - .gaugeBuilder("{{ metric.metric_name }}") - .ofLongs() - .setUnit("{{ metric.unit }}") - .setDescription("{{ metric.brief }}") - .build(); - } - {% elif metric.instrument == "counter" %} - public static LongCounter create{{ metric.metric_name.replace("_", ".")|split('.')|map('capitalize')|join }}(Meter meter) { - return meter - .counterBuilder("{{ metric.metric_name }}") - .setUnit("{{ metric.unit }}") - .setDescription("{{ metric.brief }}") - .build(); - } - {% endif %} -{% endfor %} -} diff --git a/ibm-mq-metrics/templates/registry/java/weaver.yaml b/ibm-mq-metrics/templates/registry/java/weaver.yaml index ece71c2332..a9f9c14801 100644 --- a/ibm-mq-metrics/templates/registry/java/weaver.yaml +++ b/ibm-mq-metrics/templates/registry/java/weaver.yaml @@ -1,5 +1,5 @@ templates: - - template: Metrics.java.j2 + - template: MetricProducer.java.j2 filter: '.groups | map(select(.type == "metric"))' application_mode: single - template: MetricsConfig.java.j2