Skip to content

Commit

Permalink
Simplify instance metric
Browse files Browse the repository at this point in the history
  • Loading branch information
obenkenobi committed May 21, 2024
1 parent ef102b5 commit 3d59d61
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1994,7 +1994,7 @@ public void testMessagingAPI() throws Exception {
server.start();
runTestMessagingAPI();
String messageBrokerMetric = "MessageBroker/JMS/Queue/Consume/Temp";
String endpointMetric = "MessageBroker/instance/JMS/unknown/unknown/Consume/Queue/Temp";
String endpointMetric = "MessageBroker/instance/unknown/unknown/Queue/Temp";
Assert.assertTrue("The following metric should exist: " + messageBrokerMetric, apiTestHelper.tranStats.getScopedStats().getStatsMap().containsKey(messageBrokerMetric));
Assert.assertTrue("The following metric should exist: " + endpointMetric, apiTestHelper.tranStats.getUnscopedStats().getStatsMap().containsKey(endpointMetric));
} catch (IOException e) {
Expand All @@ -2017,7 +2017,7 @@ public void testMessagingAPIWithHostAndPort() throws Exception {
server.start();
runTestMessagingAPIWithHostAndPort();
String messageBrokerMetric = "MessageBroker/JMS/Queue/Consume/Temp";
String endpointMetric = String.format("MessageBroker/instance/JMS/%s/8088/Consume/Queue/Temp", HOSTNAME);
String endpointMetric = String.format("MessageBroker/instance/%s/8088/Queue/Temp", HOSTNAME);
Assert.assertTrue("The following metric should exist: " + messageBrokerMetric, apiTestHelper.tranStats.getScopedStats().getStatsMap().containsKey(messageBrokerMetric));
Assert.assertTrue("The following metric should exist: " + endpointMetric, apiTestHelper.tranStats.getUnscopedStats().getStatsMap().containsKey(endpointMetric));
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;

import javax.jms.JMSException;

Expand All @@ -18,6 +17,11 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@RunWith(InstrumentationTestRunner.class)
@InstrumentationTestConfig(includePrefixes = { "org.apache" })
Expand All @@ -28,14 +32,14 @@ public class ActiveMQMessageTest {

@Before
public void setUp() {
ActiveMQConnection connection = Mockito.mock(ActiveMQConnection.class);
transport = Mockito.mock(Transport.class);
ActiveMQConnection connection = mock(ActiveMQConnection.class);
transport = mock(Transport.class);

Mockito.when(connection.getTransport()).thenReturn(transport);
when(connection.getTransport()).thenReturn(transport);

ActiveMQMessage message = new ActiveMQMessage();
message.setConnection(connection);
activeMQMessage = Mockito.spy(message);
activeMQMessage = spy(message);
}

@Test
Expand Down Expand Up @@ -87,12 +91,12 @@ public void fromMessageGetHostAndPortWithLocalHost() throws Exception {
}

private void setStubs(String transportString) {
Mockito.when(transport.toString()).thenReturn(transportString);
when(transport.toString()).thenReturn(transportString);
}

private void assertMessage(String expectedHost, Integer expectedPort, ActiveMQMessage message, Integer timesGetConnectionCalled) throws JMSException {
BrokerInstance brokerInstance = (BrokerInstance)message.getObjectProperty(NR_JMS_HOST_AND_PORT_PROPERTY);
Mockito.verify(message, Mockito.times(timesGetConnectionCalled)).getConnection();
verify(message, times(timesGetConnectionCalled)).getConnection();
assertNotNull("Failed to retrieve brokerInstance from ActiveMQ message", brokerInstance);
assertEquals("Expected host did not match", expectedHost, brokerInstance.getHostName());
assertEquals("Expected port did not match", expectedPort, brokerInstance.getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,6 @@ public class MetricNames {
public static final String MESSAGE_BROKER_CONSUME_NAMED = "MessageBroker/{0}/{1}/Consume/Named/{2}";
public static final String MESSAGE_BROKER_CONSUME_TEMP = "MessageBroker/{0}/{1}/Consume/Temp";

public static final String MESSAGE_BROKER_INSTANCE = "MessageBroker/Instance/{0}/{1}";

// API tracking supportability metrics that include API source. e.g. Supportability/API/Ignore/{source}
// tokens
public static final String SUPPORTABILITY_API_TOKEN = "Token";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,43 @@ public class MessageMetrics {
public static final String MESSAGE_BROKER_INSTANCE = METRIC_NAMESPACE + "/instance/";

public static final String UNKNOWN = "unknown";

public static final String PRODUCE = "Produce";
public static final String CONSUME = "Consume";
public static String HOSTNAME = Hostname.getHostname(ServiceFactory.getConfigService().getDefaultAgentConfig());

public static boolean isAnyEndpointParamsKnown(String host, Integer port) {
return !(isParamUnknown(host) && isParamUnknown(port));
}
public static void collectMessageProducerRollupMetrics(TracedMethod method, String library, String host, Integer port,
public static void collectMessageProducerRollupMetrics(TracedMethod method, String host, Integer port,
DestinationType destinationType, String destinationName) {
reportInstanceIfEnabled(method, library, PRODUCE, host, port, destinationType, destinationName);
reportInstanceIfEnabled(method, host, port, destinationType, destinationName);
}

public static void collectMessageConsumerRollupMetrics(TracedMethod method, String library, String host, Integer port,
public static void collectMessageConsumerRollupMetrics(TracedMethod method, String host, Integer port,
DestinationType destinationType, String destinationName) {
reportInstanceIfEnabled(method, library, CONSUME, host, port, destinationType, destinationName);
reportInstanceIfEnabled(method, host, port, destinationType, destinationName);
}

public static void reportInstanceIfEnabled(TracedMethod method, String library, String operation, String host, Integer port,
public static void reportInstanceIfEnabled(TracedMethod method, String host, Integer port,
DestinationType destinationType, String destinationName) {
MessageBrokerConfig messageBrokerConfig = ServiceFactory.getConfigService().getDefaultAgentConfig().getMessageBrokerConfig();
if (messageBrokerConfig.isInstanceReportingEnabled()) {
String metric = buildInstanceMetric(library, host, port, operation, destinationType, destinationName);
String metric = buildInstanceMetric(host, port, destinationType, destinationName);
method.addRollupMetricName(metric);
}
}

public static String buildInstanceMetric(String library, String host, Integer port, String operation,
public static String buildInstanceMetric(String host, Integer port,
DestinationType destinationType, String destinationName) {
String instance = buildInstanceIdentifier(library, host, port, operation, destinationType, destinationName);
String instance = buildInstanceIdentifier(host, port, destinationType, destinationName);
return MESSAGE_BROKER_INSTANCE + instance;
}

public static String buildInstanceIdentifier(String library, String host, Integer port, String operation,
public static String buildInstanceIdentifier(String host, Integer port,
DestinationType destinationType, String destinationName) {
String libraryName = replaceLibrary(library);
String hostname = replaceLocalhost(host);
String portName = replacePort(port);
String parsedDestinationName = replaceDestinationName(destinationType, destinationName);

return libraryName + SLASH + hostname + SLASH + portName + SLASH + operation + SLASH + destinationType.getTypeName() + SLASH + parsedDestinationName;
return hostname + SLASH + portName + SLASH + destinationType.getTypeName() + SLASH + parsedDestinationName;
}

public static String replaceDestinationName(DestinationType destinationType, String destinationName) {
Expand All @@ -71,13 +67,6 @@ public static String replaceDestinationName(DestinationType destinationType, Str
return "Named" + SLASH + destinationName;
}

public static String replaceLibrary(String library) {
if (isParamUnknown(library)) {
return UNKNOWN;
}
return library;
}

public static String replaceLocalhost(String host) {
if (isParamUnknown(host)) {
return UNKNOWN;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import com.newrelic.agent.bridge.external.ExternalMetrics;
import com.newrelic.agent.config.AgentConfigImpl;
import com.newrelic.agent.config.DatastoreConfig;
import com.newrelic.agent.config.MessageBrokerConfig;
import com.newrelic.agent.config.TransactionTracerConfig;
import com.newrelic.agent.database.DatastoreMetrics;
import com.newrelic.agent.database.SqlObfuscator;
Expand Down Expand Up @@ -827,7 +826,7 @@ private void recordMessageBrokerMetrics(MessageProduceParameters messageProduceP
String host = messageProduceParameters.getHost();
Integer port = messageProduceParameters.getPort();

MessageMetrics.collectMessageProducerRollupMetrics(this, library, host, port, destinationType, messageProduceParameters.getDestinationName());
MessageMetrics.collectMessageProducerRollupMetrics(this, host, port, destinationType, messageProduceParameters.getDestinationName());
}

private void recordMessageBrokerMetrics(MessageConsumeParameters messageConsumeParameters) {
Expand Down Expand Up @@ -856,7 +855,7 @@ private void recordMessageBrokerMetrics(MessageConsumeParameters messageConsumeP
String host = messageConsumeParameters.getHost();
Integer port = messageConsumeParameters.getPort();

MessageMetrics.collectMessageConsumerRollupMetrics(this, library, host, port, destinationType, messageConsumeParameters.getDestinationName());
MessageMetrics.collectMessageConsumerRollupMetrics(this, host, port, destinationType, messageConsumeParameters.getDestinationName());
}

private <T> void recordSlowQueryData(SlowQueryDatastoreParameters<T> slowQueryDatastoreParameters) {
Expand Down
Loading

0 comments on commit 3d59d61

Please sign in to comment.