Skip to content

move Metric inside the project to prepare for moving to otel #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static com.ibm.mq.constants.CMQC.MQRC_SELECTOR_ERROR;
import static com.ibm.mq.constants.CMQCFC.MQRCCF_CHL_STATUS_NOT_FOUND;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.constants.MQConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.constants.MQConstants;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.constants.CMQCFC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.headers.pcf.PCFException;
Expand Down
152 changes: 152 additions & 0 deletions src/main/java/com/splunk/ibm/mq/metricscollector/Metric.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.DefaultMetricProperties;
import com.appdynamics.extensions.metrics.MetricProperties;
import com.appdynamics.extensions.metrics.MetricPropertiesBuilder;
import com.appdynamics.extensions.util.AssertUtils;
import com.appdynamics.extensions.util.MetricPathUtils;
import java.util.Map;

public class Metric {
private String metricName;
private String metricValue;
private String metricPath;
private MetricProperties metricProperties;

public Metric(String metricName, String metricValue, String metricPath) {
AssertUtils.assertNotNull(metricName, "Metric name cannot be null");
AssertUtils.assertNotNull(metricValue, "Metric value cannot be null");
AssertUtils.assertNotNull(metricPath, "Metric path cannot be null");
this.metricName = metricName;
this.metricValue = metricValue;
this.metricPath = metricPath;
this.metricProperties = new DefaultMetricProperties(metricName);
}

public Metric(
String metricName,
String metricValue,
String metricPath,
String aggregationType,
String timeRollUpType,
String clusterRollUpType) {
this(metricName, metricValue, metricPath);
this.metricProperties.setAggregationType(aggregationType);
this.metricProperties.setTimeRollUpType(timeRollUpType);
this.metricProperties.setClusterRollUpType(clusterRollUpType);
}

public Metric(
String metricName, String metricValue, String metricPath, Map<String, ?> metricProperties) {
this(metricName, metricValue, metricPath);
AssertUtils.assertNotNull(metricProperties, "Metric Properties cannot be null");
this.metricProperties = buildMetricProperties(metricProperties);
}

/**
* Constructor for building the metric path with replacements, with metric properties. Use this
* constructor when you want to apply metric replacements and build the metric path.
*
* @param metricName Name of the metric
* @param metricValue Value of the metric
* @param metricProperties Map of metric properties
* @param metricPrefix Metric Path prefix
* @param metricPathTokens All tokens in metric path
*/
public Metric(
String metricName,
String metricValue,
Map<String, ?> metricProperties,
String metricPrefix,
String... metricPathTokens) {
this(
MetricPathUtils.getReplacedString(metricName),
metricValue,
MetricPathUtils.buildMetricPath(metricPrefix, metricPathTokens),
metricProperties);
}

/**
* Constructor for building the metric path with replacements. Use this constructor when you want
* to apply metric replacements and build the metric path.
*
* @param metricName Name of the metric
* @param metricValue Value of the metric
* @param metricPrefix Metric Path prefix
* @param metricPathTokens All tokens in metric path
*/
public Metric(
String metricName, String metricValue, String metricPrefix, String... metricPathTokens) {
this(
MetricPathUtils.getReplacedString(metricName),
metricValue,
MetricPathUtils.buildMetricPath(metricPrefix, metricPathTokens));
}

private MetricProperties buildMetricProperties(Map<String, ?> metricProperties) {
MetricPropertiesBuilder metricPropertiesBuilder =
new MetricPropertiesBuilder(metricProperties, metricName);
return metricPropertiesBuilder.buildMetricProperties();
}

public String getMetricName() {
return metricName;
}

public String getMetricValue() {
return metricValue;
}

public void setMetricValue(String metricValue) {
this.metricValue = metricValue;
}

public String getMetricPath() {
return metricPath;
}

public void setMetricPath(String metricPath) {
this.metricPath = metricPath;
}

public MetricProperties getMetricProperties() {
return metricProperties;
}

public String getAggregationType() {
return metricProperties.getAggregationType();
}

public String getTimeRollUpType() {
return metricProperties.getTimeRollUpType();
}

public String getClusterRollUpType() {
return metricProperties.getClusterRollUpType();
}

public String toString() {
return String.format(
"[%s/%s/%s] [%s]=[%s]]",
metricProperties.getAggregationType(),
metricProperties.getTimeRollUpType(),
metricProperties.getClusterRollUpType(),
getMetricPath(),
getMetricValue());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.splunk.ibm.mq.config.QueueManager;
import com.splunk.ibm.mq.config.WMQMetricOverride;
import javax.annotation.Nullable;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;

import com.appdynamics.extensions.metrics.Metric;
import com.ibm.mq.headers.MQDataException;
import com.ibm.mq.headers.pcf.PCFException;
import com.ibm.mq.headers.pcf.PCFMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQC;
import com.ibm.mq.headers.MQDataException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.constants.CMQCFC;
import com.ibm.mq.headers.pcf.PCFMessage;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.metrics.Metric;
import com.google.common.collect.Lists;
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
Expand Down
139 changes: 139 additions & 0 deletions src/main/java/com/splunk/ibm/mq/metricscollector/Transformer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
* Copyright Splunk Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.splunk.ibm.mq.metricscollector;

import com.appdynamics.extensions.logging.ExtensionsLoggerFactory;
import com.appdynamics.extensions.metrics.DeltaMetricsCalculator;
import com.appdynamics.extensions.util.AssertUtils;
import com.appdynamics.extensions.util.NumberUtils;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Transformer {
private static Logger logger = LoggerFactory.getLogger(Transformer.class);
private List<Metric> metricList;
private DeltaTranform deltaTranform = new DeltaTranform();
private MultiplierTransform multiplierTransform = new MultiplierTransform();
private ConvertTransform convertTransform = new ConvertTransform();
private AliasTransform aliasTransform = new AliasTransform();

public Transformer(List<Metric> metricList) {
AssertUtils.assertNotNull(metricList, "Metrics List cannot be null");
this.metricList = metricList;
}

public void transform() {
for (Metric metric : metricList) {
applyTransforms(metric);
}
}

private void applyTransforms(Metric metric) {
aliasTransform.applyAlias(metric);
if (metric.getMetricValue() != null) {
convertTransform.convert(metric);
}
if (metric.getMetricValue() != null) {
deltaTranform.applyDelta(metric);
}
if (metric.getMetricValue() != null) {
multiplierTransform.multiply(metric);
}
if (metric.getMetricValue() != null) {
try {
BigDecimal value = new BigDecimal(metric.getMetricValue());
metric.setMetricValue(value.setScale(0, RoundingMode.HALF_UP).toBigInteger().toString());
} catch (NumberFormatException e) {
logger.debug(e.toString());
}
}
}
}

class AliasTransform {
Splitter PIPE_SPLITTER = Splitter.on('|').trimResults();

void applyAlias(Metric metric) {
String metricName = metric.getMetricName();
String alias = metric.getMetricProperties().getAlias();
String metricPath = metric.getMetricPath();
List<String> splitList = new ArrayList<>(PIPE_SPLITTER.splitToList(metricPath));
if (splitList.size() > 0) {
String metricNameFromSplit = splitList.get(splitList.size() - 1);
if (metricNameFromSplit.equals(metricName)) {
splitList.remove(splitList.size() - 1);
splitList.add(alias);
metric.setMetricPath(Joiner.on("|").join(splitList));
}
}
}
}

class ConvertTransform {
private static final org.slf4j.Logger logger =
ExtensionsLoggerFactory.getLogger(ConvertTransform.class);

void convert(Metric metric) {
Map<Object, Object> convertMap = metric.getMetricProperties().getConversionValues();
String metricValue = metric.getMetricValue();
if (convertMap != null && !convertMap.isEmpty() && convertMap.containsKey(metricValue)) {
metric.setMetricValue(convertMap.get(metricValue).toString());
logger.debug(
"Applied conversion on {} and replaced value {} with {}",
metric.getMetricPath(),
metricValue,
metric.getMetricValue());
}
}
}

class DeltaTranform {
private static final org.slf4j.Logger logger =
ExtensionsLoggerFactory.getLogger(DeltaTranform.class);
private static DeltaMetricsCalculator deltaCalculator = new DeltaMetricsCalculator(10);

void applyDelta(Metric metric) {
String metricValue = metric.getMetricValue();
if (NumberUtils.isNumber(metricValue) && metric.getMetricProperties().getDelta() == true) {
BigDecimal deltaValue =
deltaCalculator.calculateDelta(metric.getMetricPath(), new BigDecimal(metricValue));
if (deltaValue != null) {
metric.setMetricValue(deltaValue.toString());
} else {
metric.setMetricValue(null);
}
}
}
}

class MultiplierTransform {

void multiply(Metric metric) {
String metricValue = metric.getMetricValue();
if (NumberUtils.isNumber(metricValue)) {
BigDecimal metricValueBigD = new BigDecimal(metricValue);
metric.setMetricValue(
(metricValueBigD.multiply(metric.getMetricProperties().getMultiplier())).toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
*/
package com.splunk.ibm.mq.opentelemetry;

import com.appdynamics.extensions.metrics.Metric;
import com.appdynamics.extensions.util.AssertUtils;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.splunk.ibm.mq.metricscollector.Metric;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.sdk.common.InstrumentationScopeInfo;
Expand Down Expand Up @@ -283,7 +282,9 @@ public String toString() {
public MQOtelTranslator() {}

public Collection<MetricData> translate(List<Metric> metricList) {
AssertUtils.assertNotNull(metricList, "Metrics List cannot be null");
if (metricList == null || metricList.isEmpty()) {
throw new IllegalArgumentException("Metrics List cannot be null");
}
Resource res = Resource.empty();
InstrumentationScopeInfo scopeInfo = InstrumentationScopeInfo.create("websphere/mq");
List<MetricData> metrics = new ArrayList<>();
Expand Down
Loading