Skip to content

Commit f9b19a2

Browse files
use native gauges
1 parent e9a8cd0 commit f9b19a2

File tree

7 files changed

+151
-109
lines changed

7 files changed

+151
-109
lines changed

src/main/java/com/splunk/ibm/mq/WMQMonitorTask.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import com.splunk.ibm.mq.metricscollector.InquireQueueManagerCmdCollector;
2727
import com.splunk.ibm.mq.metricscollector.JobSubmitterContext;
2828
import com.splunk.ibm.mq.metricscollector.ListenerMetricsCollector;
29-
import com.splunk.ibm.mq.metricscollector.MetricCreator;
3029
import com.splunk.ibm.mq.metricscollector.MetricsCollectorContext;
3130
import com.splunk.ibm.mq.metricscollector.MetricsPublisherJob;
3231
import com.splunk.ibm.mq.metricscollector.PerformanceEventQueueCollector;
@@ -46,7 +45,6 @@
4645
import java.util.List;
4746
import java.util.concurrent.CountDownLatch;
4847
import java.util.concurrent.ExecutorService;
49-
import java.util.function.BiFunction;
5048
import java.util.function.Function;
5149
import org.slf4j.Logger;
5250
import org.slf4j.LoggerFactory;
@@ -239,13 +237,11 @@ private void inquireQueueMetrics(QueueCollectorSharedState sharedState, PCFMessa
239237

240238
// Inquire for listener metrics
241239
private void inquireListenerMetrics(
242-
BiFunction<MetricsCollectorContext, MetricCreator, Runnable> collectorConstructor,
243-
PCFMessageAgent agent) {
240+
Function<MetricsCollectorContext, Runnable> collectorConstructor, PCFMessageAgent agent) {
244241

245242
MetricsCollectorContext context =
246243
new MetricsCollectorContext(queueManager, agent, metricWriteHelper);
247-
MetricCreator metricCreator = new MetricCreator(queueManager.getName());
248-
Runnable metricsCollector = collectorConstructor.apply(context, metricCreator);
244+
Runnable metricsCollector = collectorConstructor.apply(context);
249245
pendingJobs.add(metricsCollector);
250246
}
251247

@@ -263,10 +259,9 @@ private void inquireTopicMetrics(
263259
// Inquire configuration-specific metrics
264260
private void inquireConfigurationMetrics(MQQueueManager mqQueueManager, PCFMessageAgent agent) {
265261

266-
MetricCreator metricCreator = new MetricCreator(queueManager.getName());
267262
ReadConfigurationEventQueueCollector collector =
268263
new ReadConfigurationEventQueueCollector(
269-
agent, mqQueueManager, queueManager, metricWriteHelper, metricCreator);
264+
agent, mqQueueManager, queueManager, metricWriteHelper);
270265
pendingJobs.add(collector);
271266
}
272267

src/main/java/com/splunk/ibm/mq/metricscollector/InquireTStatusCmdCollector.java

Lines changed: 34 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616
package com.splunk.ibm.mq.metricscollector;
1717

18-
import com.google.common.collect.Lists;
1918
import com.ibm.mq.constants.CMQC;
2019
import com.ibm.mq.constants.CMQCFC;
2120
import com.ibm.mq.headers.MQDataException;
2221
import com.ibm.mq.headers.pcf.PCFException;
2322
import com.ibm.mq.headers.pcf.PCFMessage;
2423
import io.opentelemetry.api.common.AttributeKey;
2524
import io.opentelemetry.api.common.Attributes;
25+
import io.opentelemetry.api.metrics.LongGauge;
2626
import java.io.IOException;
2727
import java.util.List;
2828
import java.util.Set;
@@ -32,14 +32,28 @@
3232
final class InquireTStatusCmdCollector implements Runnable {
3333

3434
private static final Logger logger = LoggerFactory.getLogger(InquireTStatusCmdCollector.class);
35-
private final MetricCreator metricCreator;
3635

3736
static final String COMMAND = "MQCMD_INQUIRE_TOPIC_STATUS";
3837
private final MetricsCollectorContext context;
38+
private final LongGauge publishCountGauge;
39+
private final LongGauge subscriptionCountGauge;
3940

40-
public InquireTStatusCmdCollector(MetricsCollectorContext context, MetricCreator metricCreator) {
41+
public InquireTStatusCmdCollector(MetricsCollectorContext context) {
4142
this.context = context;
42-
this.metricCreator = metricCreator;
43+
this.publishCountGauge =
44+
context
45+
.getMetricWriteHelper()
46+
.getMeter()
47+
.gaugeBuilder("mq.publish.count")
48+
.ofLongs()
49+
.build();
50+
this.subscriptionCountGauge =
51+
context
52+
.getMetricWriteHelper()
53+
.getMeter()
54+
.gaugeBuilder("mq.subscription.count")
55+
.ofLongs()
56+
.build();
4357
}
4458

4559
@Override
@@ -114,38 +128,36 @@ private void processPCFRequestAndPublishQMetrics(
114128
for (PCFMessage message : messages) {
115129
String topicName = MessageBuddy.topicName(message);
116130
logger.debug("Pulling out metrics for topic name {} for command {}", topicName, command);
117-
List<Metric> responseMetrics = extractMetrics(command, message, topicName);
118-
context.transformAndPrintMetrics(responseMetrics);
131+
extractMetrics(message, topicName);
119132
}
120133
}
121134

122-
private List<Metric> extractMetrics(String command, PCFMessage pcfMessage, String topicString)
123-
throws PCFException {
124-
List<Metric> responseMetrics = Lists.newArrayList();
135+
private void extractMetrics(PCFMessage pcfMessage, String topicString) throws PCFException {
125136
{
126137
int count = 0;
127138
if (pcfMessage.getParameter(CMQC.MQIA_PUB_COUNT) != null) {
128139
count = pcfMessage.getIntParameterValue(CMQC.MQIA_PUB_COUNT);
129140
}
130-
Metric metric =
131-
metricCreator.createMetric(
132-
"mq.publish.count",
133-
count,
134-
Attributes.of(AttributeKey.stringKey("topic.name"), topicString));
135-
responseMetrics.add(metric);
141+
publishCountGauge.set(
142+
count,
143+
Attributes.of(
144+
AttributeKey.stringKey("topic.name"),
145+
topicString,
146+
AttributeKey.stringKey("queue.manager"),
147+
context.getQueueManagerName()));
136148
}
137149
{
138150
int count = 0;
139151
if (pcfMessage.getParameter(CMQC.MQIA_SUB_COUNT) != null) {
140152
count = pcfMessage.getIntParameterValue(CMQC.MQIA_SUB_COUNT);
141153
}
142-
Metric metric =
143-
metricCreator.createMetric(
144-
"mq.subscription.count",
145-
count,
146-
Attributes.of(AttributeKey.stringKey("topic.name"), topicString));
147-
responseMetrics.add(metric);
154+
subscriptionCountGauge.set(
155+
count,
156+
Attributes.of(
157+
AttributeKey.stringKey("topic.name"),
158+
topicString,
159+
AttributeKey.stringKey("queue.manager"),
160+
context.getQueueManagerName()));
148161
}
149-
return responseMetrics;
150162
}
151163
}

src/main/java/com/splunk/ibm/mq/metricscollector/ListenerMetricsCollector.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,15 @@
1515
*/
1616
package com.splunk.ibm.mq.metricscollector;
1717

18-
import com.google.common.collect.Lists;
1918
import com.ibm.mq.constants.CMQCFC;
2019
import com.ibm.mq.headers.pcf.PCFException;
2120
import com.ibm.mq.headers.pcf.PCFMessage;
2221
import io.opentelemetry.api.common.AttributeKey;
2322
import io.opentelemetry.api.common.Attributes;
23+
import io.opentelemetry.api.metrics.LongGauge;
2424
import java.util.Arrays;
2525
import java.util.List;
2626
import java.util.Set;
27-
import org.jetbrains.annotations.NotNull;
2827
import org.slf4j.Logger;
2928
import org.slf4j.LoggerFactory;
3029

@@ -46,12 +45,18 @@
4645
public final class ListenerMetricsCollector implements Runnable {
4746

4847
private static final Logger logger = LoggerFactory.getLogger(ListenerMetricsCollector.class);
49-
private final MetricCreator metricCreator;
5048
private final MetricsCollectorContext context;
49+
private final LongGauge listenerStatusGauge;
5150

52-
public ListenerMetricsCollector(MetricsCollectorContext context, MetricCreator metricCreator) {
53-
this.metricCreator = metricCreator;
51+
public ListenerMetricsCollector(MetricsCollectorContext context) {
5452
this.context = context;
53+
this.listenerStatusGauge =
54+
context
55+
.getMetricWriteHelper()
56+
.getMeter()
57+
.gaugeBuilder("mq.listener.status")
58+
.ofLongs()
59+
.build();
5560
}
5661

5762
@Override
@@ -93,8 +98,7 @@ public void run() {
9398
for (PCFMessage message : messages) {
9499
String listenerName = MessageBuddy.listenerName(message);
95100
logger.debug("Pulling out metrics for listener name {}", listenerName);
96-
List<Metric> responseMetrics = getMetrics(message, listenerName);
97-
context.transformAndPrintMetrics(responseMetrics);
101+
updateMetrics(message, listenerName);
98102
}
99103
} catch (Exception e) {
100104
logger.error(
@@ -105,18 +109,16 @@ public void run() {
105109
logger.debug("Time taken to publish metrics for all listener is {} milliseconds", exitTime);
106110
}
107111

108-
private @NotNull List<Metric> getMetrics(PCFMessage message, String listenerName)
109-
throws PCFException {
110-
List<Metric> responseMetrics = Lists.newArrayList();
112+
private void updateMetrics(PCFMessage message, String listenerName) throws PCFException {
111113
{
112-
int interval = message.getIntParameterValue(CMQCFC.MQIACH_LISTENER_STATUS);
113-
Metric metric =
114-
metricCreator.createMetric(
115-
"mq.listener.status",
116-
interval,
117-
Attributes.of(AttributeKey.stringKey("listener.name"), listenerName));
118-
responseMetrics.add(metric);
114+
int status = message.getIntParameterValue(CMQCFC.MQIACH_LISTENER_STATUS);
115+
listenerStatusGauge.set(
116+
status,
117+
Attributes.of(
118+
AttributeKey.stringKey("listener.name"),
119+
listenerName,
120+
AttributeKey.stringKey("queue.manager"),
121+
context.getQueueManagerName()));
119122
}
120-
return responseMetrics;
121123
}
122124
}

src/main/java/com/splunk/ibm/mq/metricscollector/ReadConfigurationEventQueueCollector.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package com.splunk.ibm.mq.metricscollector;
1717

18-
import com.google.common.collect.Lists;
1918
import com.ibm.mq.MQException;
2019
import com.ibm.mq.MQGetMessageOptions;
2120
import com.ibm.mq.MQMessage;
@@ -28,35 +27,34 @@
2827
import com.ibm.mq.headers.pcf.PCFMessageAgent;
2928
import com.splunk.ibm.mq.config.QueueManager;
3029
import com.splunk.ibm.mq.opentelemetry.OpenTelemetryMetricWriteHelper;
30+
import io.opentelemetry.api.common.AttributeKey;
3131
import io.opentelemetry.api.common.Attributes;
32+
import io.opentelemetry.api.metrics.LongGauge;
3233
import java.io.IOException;
33-
import java.util.List;
3434
import org.slf4j.Logger;
3535
import org.slf4j.LoggerFactory;
3636

3737
public final class ReadConfigurationEventQueueCollector implements Runnable {
3838

3939
private static final Logger logger =
4040
LoggerFactory.getLogger(ReadConfigurationEventQueueCollector.class);
41-
private final OpenTelemetryMetricWriteHelper metricWriteHelper;
4241
private final QueueManager queueManager;
4342
private final PCFMessageAgent agent;
4443
private final MQQueueManager mqQueueManager;
4544
private final long bootTime;
46-
private final MetricCreator metricCreator;
45+
private final LongGauge maxHandlesGauge;
4746

4847
public ReadConfigurationEventQueueCollector(
4948
PCFMessageAgent agent,
5049
MQQueueManager mqQueueManager,
5150
QueueManager queueManager,
52-
OpenTelemetryMetricWriteHelper metricWriteHelper,
53-
MetricCreator metricCreator) {
51+
OpenTelemetryMetricWriteHelper metricWriteHelper) {
5452
this.agent = agent;
5553
this.mqQueueManager = mqQueueManager;
5654
this.queueManager = queueManager;
57-
this.metricWriteHelper = metricWriteHelper;
5855
this.bootTime = System.currentTimeMillis();
59-
this.metricCreator = metricCreator;
56+
this.maxHandlesGauge =
57+
metricWriteHelper.getMeter().gaugeBuilder("mq.manager.max.handles").ofLongs().build();
6058
}
6159

6260
private PCFMessage findLastUpdate(long entryTime, String configurationQueueName)
@@ -141,14 +139,10 @@ public void run() {
141139
}
142140

143141
if (candidate != null) {
144-
List<Metric> metrics = Lists.newArrayList();
145-
{
146-
int maxHandles = candidate.getIntParameterValue(CMQC.MQIA_MAX_HANDLES);
147-
Metric metric =
148-
metricCreator.createMetric("mq.manager.max.handles", maxHandles, Attributes.empty());
149-
metrics.add(metric);
150-
}
151-
this.metricWriteHelper.transformAndPrintMetrics(metrics);
142+
int maxHandles = candidate.getIntParameterValue(CMQC.MQIA_MAX_HANDLES);
143+
maxHandlesGauge.set(
144+
maxHandles,
145+
Attributes.of(AttributeKey.stringKey("queue.manager"), this.queueManager.getName()));
152146
}
153147

154148
} catch (Exception e) {

src/main/java/com/splunk/ibm/mq/metricscollector/TopicMetricsCollector.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,8 @@ public void run() {
3737

3838
// to query the current status of topics, which is essential for monitoring and managing the
3939
// publish/subscribe environment in IBM MQ.
40-
MetricCreator metricCreator = context.newMetricCreator();
4140
MetricsCollectorContext collectorContext = context.newCollectorContext();
42-
InquireTStatusCmdCollector metricsPublisher =
43-
new InquireTStatusCmdCollector(collectorContext, metricCreator);
41+
InquireTStatusCmdCollector metricsPublisher = new InquireTStatusCmdCollector(collectorContext);
4442
publishers.add(metricsPublisher);
4543
CountDownLatch latch = new CountDownLatch(publishers.size());
4644
for (Runnable publisher : publishers) {

0 commit comments

Comments
 (0)