1
0
mirror of https://gitee.com/mirrors/Spring-Cloud-Alibaba.git synced 2021-06-26 13:25:11 +08:00

make metrics-core as provided dependency and refactor for 1.x branch

This commit is contained in:
fangjian0423 2018-11-29 11:17:08 +08:00
parent 71f4295170
commit 2c7ab714fb
11 changed files with 309 additions and 281 deletions

View File

@ -25,7 +25,6 @@
<alicloud.context.version>1.0.0</alicloud.context.version> <alicloud.context.version>1.0.0</alicloud.context.version>
<aliyun.sdk.edas.version>2.16.0</aliyun.sdk.edas.version> <aliyun.sdk.edas.version>2.16.0</aliyun.sdk.edas.version>
<rocketmq.version>4.3.1</rocketmq.version> <rocketmq.version>4.3.1</rocketmq.version>
<metrics.core>3.2.6</metrics.core>
</properties> </properties>
<dependencyManagement> <dependencyManagement>
@ -221,13 +220,6 @@
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<!-- Third dependencies -->
<dependency>
<groupId>io.dropwizard.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>${metrics.core}</version>
</dependency>
<!-- Testing Dependencies --> <!-- Testing Dependencies -->

View File

@ -21,13 +21,15 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.dropwizard.metrics</groupId> <groupId>org.apache.rocketmq</groupId>
<artifactId>metrics-core</artifactId> <artifactId>rocketmq-client</artifactId>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.rocketmq</groupId> <groupId>io.dropwizard.metrics</groupId>
<artifactId>rocketmq-client</artifactId> <artifactId>metrics-core</artifactId>
<scope>provided</scope>
<optional>true</optional>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -42,14 +42,14 @@ import org.springframework.messaging.MessageHandler;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
public class RocketMQMessageChannelBinder extends public class RocketMQMessageChannelBinder extends
AbstractMessageChannelBinder<ExtendedConsumerProperties<RocketMQConsumerProperties>, AbstractMessageChannelBinder<ExtendedConsumerProperties<RocketMQConsumerProperties>, ExtendedProducerProperties<RocketMQProducerProperties>, RocketMQTopicProvisioner>
ExtendedProducerProperties<RocketMQProducerProperties>, RocketMQTopicProvisioner> implements
implements ExtendedPropertiesBinder<MessageChannel, RocketMQConsumerProperties, RocketMQProducerProperties> { ExtendedPropertiesBinder<MessageChannel, RocketMQConsumerProperties, RocketMQProducerProperties> {
private static final Logger logger = LoggerFactory.getLogger(RocketMQMessageChannelBinder.class); private static final Logger logger = LoggerFactory
.getLogger(RocketMQMessageChannelBinder.class);
private final RocketMQExtendedBindingProperties extendedBindingProperties; private final RocketMQExtendedBindingProperties extendedBindingProperties;
private final RocketMQTopicProvisioner rocketTopicProvisioner;
private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties; private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties;
private final InstrumentationManager instrumentationManager; private final InstrumentationManager instrumentationManager;
private final ConsumersManager consumersManager; private final ConsumersManager consumersManager;
@ -62,44 +62,50 @@ public class RocketMQMessageChannelBinder extends
super(true, null, provisioningProvider); super(true, null, provisioningProvider);
this.consumersManager = consumersManager; this.consumersManager = consumersManager;
this.extendedBindingProperties = extendedBindingProperties; this.extendedBindingProperties = extendedBindingProperties;
this.rocketTopicProvisioner = provisioningProvider;
this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties; this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties;
this.instrumentationManager = instrumentationManager; this.instrumentationManager = instrumentationManager;
} }
@Override @Override
protected MessageHandler createProducerMessageHandler(ProducerDestination destination, protected MessageHandler createProducerMessageHandler(ProducerDestination destination,
ExtendedProducerProperties<RocketMQProducerProperties> ExtendedProducerProperties<RocketMQProducerProperties> producerProperties,
producerProperties,
MessageChannel errorChannel) throws Exception { MessageChannel errorChannel) throws Exception {
if (producerProperties.getExtension().getEnabled()) { if (producerProperties.getExtension().getEnabled()) {
return new RocketMQMessageHandler(destination.getName(), producerProperties.getExtension(), return new RocketMQMessageHandler(destination.getName(),
producerProperties.getExtension(),
rocketBinderConfigurationProperties, instrumentationManager); rocketBinderConfigurationProperties, instrumentationManager);
} else { }
throw new RuntimeException( else {
"Binding for channel " + destination.getName() + "has been disabled, message can't be delivered"); throw new RuntimeException("Binding for channel " + destination.getName()
+ " has been disabled, message can't be delivered");
} }
} }
@Override @Override
protected MessageProducer createConsumerEndpoint(ConsumerDestination destination, String group, protected MessageProducer createConsumerEndpoint(ConsumerDestination destination,
ExtendedConsumerProperties<RocketMQConsumerProperties> String group,
consumerProperties) ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties)
throws Exception { throws Exception {
if (group == null || "".equals(group)) { if (group == null || "".equals(group)) {
throw new RuntimeException("'group' must be configured for channel + " + destination.getName()); throw new RuntimeException(
"'group' must be configured for channel + " + destination.getName());
} }
RocketMQInboundChannelAdapter rocketInboundChannelAdapter = new RocketMQInboundChannelAdapter(consumersManager, RocketMQInboundChannelAdapter rocketInboundChannelAdapter = new RocketMQInboundChannelAdapter(
consumerProperties, destination.getName(), group, instrumentationManager); consumersManager, consumerProperties, destination.getName(), group,
instrumentationManager);
ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination, group, ErrorInfrastructure errorInfrastructure = registerErrorInfrastructure(destination,
consumerProperties); group, consumerProperties);
if (consumerProperties.getMaxAttempts() > 1) { if (consumerProperties.getMaxAttempts() > 1) {
rocketInboundChannelAdapter.setRetryTemplate(buildRetryTemplate(consumerProperties)); rocketInboundChannelAdapter
rocketInboundChannelAdapter.setRecoveryCallback(errorInfrastructure.getRecoverer()); .setRetryTemplate(buildRetryTemplate(consumerProperties));
} else { rocketInboundChannelAdapter
rocketInboundChannelAdapter.setErrorChannel(errorInfrastructure.getErrorChannel()); .setRecoveryCallback(errorInfrastructure.getRecoverer());
}
else {
rocketInboundChannelAdapter
.setErrorChannel(errorInfrastructure.getErrorChannel());
} }
return rocketInboundChannelAdapter; return rocketInboundChannelAdapter;

View File

@ -20,11 +20,10 @@ import static org.springframework.cloud.stream.binder.rocketmq.RocketMQBinderCon
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.AbstractEndpoint; import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
import org.springframework.cloud.stream.binder.rocketmq.metrics.InstrumentationManager;
import com.codahale.metrics.MetricRegistry;
/** /**
* @author Timur Valiev * @author Timur Valiev
@ -32,8 +31,8 @@ import com.codahale.metrics.MetricRegistry;
*/ */
public class RocketMQBinderEndpoint extends AbstractEndpoint<Map<String, Object>> { public class RocketMQBinderEndpoint extends AbstractEndpoint<Map<String, Object>> {
private MetricRegistry metricRegistry = new MetricRegistry(); @Autowired(required = false)
private Map<String, Object> runtime = new ConcurrentHashMap<>(); private InstrumentationManager instrumentationManager;
public RocketMQBinderEndpoint() { public RocketMQBinderEndpoint() {
super(ENDPOINT_ID); super(ENDPOINT_ID);
@ -42,17 +41,15 @@ public class RocketMQBinderEndpoint extends AbstractEndpoint<Map<String, Object>
@Override @Override
public Map<String, Object> invoke() { public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<>(); Map<String, Object> result = new HashMap<>();
result.put("metrics", metricRegistry().getMetrics()); if (instrumentationManager != null) {
result.put("runtime", runtime()); result.put("metrics", instrumentationManager.getMetricRegistry());
result.put("runtime", instrumentationManager.getRuntime());
}
else {
result.put("warning",
"please add metrics-core dependency, we use it for metrics");
}
return result; return result;
} }
public MetricRegistry metricRegistry() {
return metricRegistry;
}
public Map<String, Object> runtime() {
return runtime;
}
} }

View File

@ -16,6 +16,7 @@
package org.springframework.cloud.stream.binder.rocketmq.actuator; package org.springframework.cloud.stream.binder.rocketmq.actuator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.AbstractHealthIndicator; import org.springframework.boot.actuate.health.AbstractHealthIndicator;
import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.Health;
import org.springframework.cloud.stream.binder.rocketmq.metrics.Instrumentation; import org.springframework.cloud.stream.binder.rocketmq.metrics.Instrumentation;
@ -27,15 +28,13 @@ import org.springframework.cloud.stream.binder.rocketmq.metrics.InstrumentationM
*/ */
public class RocketMQBinderHealthIndicator extends AbstractHealthIndicator { public class RocketMQBinderHealthIndicator extends AbstractHealthIndicator {
private final InstrumentationManager instrumentationManager; @Autowired(required = false)
private InstrumentationManager instrumentationManager;
public RocketMQBinderHealthIndicator(InstrumentationManager instrumentationManager) {
this.instrumentationManager = instrumentationManager;
}
@Override @Override
protected void doHealthCheck(Health.Builder builder) throws Exception { protected void doHealthCheck(Health.Builder builder) throws Exception {
int upCount = 0, outOfServiceCount = 0; int upCount = 0, outOfServiceCount = 0;
if (instrumentationManager != null) {
for (Instrumentation instrumentation : instrumentationManager for (Instrumentation instrumentation : instrumentationManager
.getHealthInstrumentations()) { .getHealthInstrumentations()) {
if (instrumentation.isUp()) { if (instrumentation.isUp()) {
@ -49,8 +48,8 @@ public class RocketMQBinderHealthIndicator extends AbstractHealthIndicator {
builder.up(); builder.up();
return; return;
} }
else if (outOfServiceCount == instrumentationManager.getHealthInstrumentations() else if (outOfServiceCount == instrumentationManager
.size()) { .getHealthInstrumentations().size()) {
builder.outOfService(); builder.outOfService();
return; return;
} }
@ -63,4 +62,11 @@ public class RocketMQBinderHealthIndicator extends AbstractHealthIndicator {
} }
} }
} }
else {
builder.down();
builder.withDetail("warning",
"please add metrics-core dependency, we use it for metrics");
}
}
} }

View File

@ -33,19 +33,25 @@ import org.springframework.context.annotation.Configuration;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
@Configuration @Configuration
@EnableConfigurationProperties({RocketMQBinderConfigurationProperties.class, RocketMQExtendedBindingProperties.class}) @EnableConfigurationProperties({ RocketMQBinderConfigurationProperties.class,
RocketMQExtendedBindingProperties.class })
public class RocketMQBinderAutoConfiguration { public class RocketMQBinderAutoConfiguration {
private final RocketMQExtendedBindingProperties extendedBindingProperties; private final RocketMQExtendedBindingProperties extendedBindingProperties;
private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties; private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties;
@Autowired(required = false)
private InstrumentationManager instrumentationManager;
@Autowired @Autowired
public RocketMQBinderAutoConfiguration(RocketMQExtendedBindingProperties extendedBindingProperties, public RocketMQBinderAutoConfiguration(
RocketMQExtendedBindingProperties extendedBindingProperties,
RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties) { RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties) {
this.extendedBindingProperties = extendedBindingProperties; this.extendedBindingProperties = extendedBindingProperties;
this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties; this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties;
System.setProperty(ClientLogger.CLIENT_LOG_LEVEL, this.rocketBinderConfigurationProperties.getLogLevel()); System.setProperty(ClientLogger.CLIENT_LOG_LEVEL,
this.rocketBinderConfigurationProperties.getLogLevel());
} }
@Bean @Bean
@ -54,17 +60,19 @@ public class RocketMQBinderAutoConfiguration {
} }
@Bean @Bean
public RocketMQMessageChannelBinder rocketMessageChannelBinder(RocketMQTopicProvisioner provisioningProvider, public RocketMQMessageChannelBinder rocketMessageChannelBinder(
InstrumentationManager instrumentationManager, RocketMQTopicProvisioner provisioningProvider,
ConsumersManager consumersManager) { ConsumersManager consumersManager) {
RocketMQMessageChannelBinder binder = new RocketMQMessageChannelBinder(consumersManager, extendedBindingProperties, RocketMQMessageChannelBinder binder = new RocketMQMessageChannelBinder(
provisioningProvider, rocketBinderConfigurationProperties, instrumentationManager); consumersManager, extendedBindingProperties, provisioningProvider,
rocketBinderConfigurationProperties, instrumentationManager);
return binder; return binder;
} }
@Bean @Bean
public ConsumersManager consumersManager(InstrumentationManager instrumentationManager) { public ConsumersManager consumersManager() {
return new ConsumersManager(instrumentationManager, rocketBinderConfigurationProperties); return new ConsumersManager(instrumentationManager,
rocketBinderConfigurationProperties);
} }
} }

View File

@ -17,7 +17,9 @@
package org.springframework.cloud.stream.binder.rocketmq.config; package org.springframework.cloud.stream.binder.rocketmq.config;
import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.autoconfigure.AutoConfigureAfter; import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.cloud.stream.binder.rocketmq.actuator.RocketMQBinderEndpoint; import org.springframework.cloud.stream.binder.rocketmq.actuator.RocketMQBinderEndpoint;
import org.springframework.cloud.stream.binder.rocketmq.actuator.RocketMQBinderHealthIndicator; import org.springframework.cloud.stream.binder.rocketmq.actuator.RocketMQBinderHealthIndicator;
import org.springframework.cloud.stream.binder.rocketmq.metrics.InstrumentationManager; import org.springframework.cloud.stream.binder.rocketmq.metrics.InstrumentationManager;
@ -29,6 +31,7 @@ import org.springframework.context.annotation.Configuration;
*/ */
@Configuration @Configuration
@AutoConfigureAfter(EndpointAutoConfiguration.class) @AutoConfigureAfter(EndpointAutoConfiguration.class)
@ConditionalOnClass(Endpoint.class)
public class RocketMQBinderEndpointAutoConfiguration { public class RocketMQBinderEndpointAutoConfiguration {
@Bean @Bean
@ -37,16 +40,14 @@ public class RocketMQBinderEndpointAutoConfiguration {
} }
@Bean @Bean
public RocketMQBinderHealthIndicator rocketBinderHealthIndicator( public RocketMQBinderHealthIndicator rocketBinderHealthIndicator() {
InstrumentationManager instrumentationManager) { return new RocketMQBinderHealthIndicator();
return new RocketMQBinderHealthIndicator(instrumentationManager);
} }
@Bean @Bean
public InstrumentationManager instrumentationManager( @ConditionalOnClass(name = "com.codahale.metrics.Counter")
RocketMQBinderEndpoint rocketBinderEndpoint) { public InstrumentationManager instrumentationManager() {
return new InstrumentationManager(rocketBinderEndpoint.metricRegistry(), return new InstrumentationManager();
rocketBinderEndpoint.runtime());
} }
} }

View File

@ -40,11 +40,11 @@ public class ConsumersManager {
private final Logger logger = LoggerFactory.getLogger(this.getClass()); private final Logger logger = LoggerFactory.getLogger(this.getClass());
private InstrumentationManager instrumentationManager;
private final Map<String, DefaultMQPushConsumer> consumerGroups = new HashMap<>(); private final Map<String, DefaultMQPushConsumer> consumerGroups = new HashMap<>();
private final Map<String, Boolean> started = new HashMap<>(); private final Map<String, Boolean> started = new HashMap<>();
private final Map<Map.Entry<String, String>, ExtendedConsumerProperties<RocketMQConsumerProperties>> propertiesMap private final Map<Map.Entry<String, String>, ExtendedConsumerProperties<RocketMQConsumerProperties>> propertiesMap = new HashMap<>();
= new HashMap<>();
private final InstrumentationManager instrumentationManager;
private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties; private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties;
public ConsumersManager(InstrumentationManager instrumentationManager, public ConsumersManager(InstrumentationManager instrumentationManager,
@ -53,11 +53,16 @@ public class ConsumersManager {
this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties; this.rocketBinderConfigurationProperties = rocketBinderConfigurationProperties;
} }
public synchronized DefaultMQPushConsumer getOrCreateConsumer(String group, String topic, public synchronized DefaultMQPushConsumer getOrCreateConsumer(String group,
String topic,
ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties) { ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties) {
propertiesMap.put(new AbstractMap.SimpleEntry<>(group, topic), consumerProperties); propertiesMap.put(new AbstractMap.SimpleEntry<>(group, topic),
ConsumerGroupInstrumentation instrumentation = instrumentationManager.getConsumerGroupInstrumentation(group); consumerProperties);
if (instrumentationManager != null) {
ConsumerGroupInstrumentation instrumentation = instrumentationManager
.getConsumerGroupInstrumentation(group);
instrumentationManager.addHealthInstrumentation(instrumentation); instrumentationManager.addHealthInstrumentation(instrumentation);
}
if (consumerGroups.containsKey(group)) { if (consumerGroups.containsKey(group)) {
return consumerGroups.get(group); return consumerGroups.get(group);
@ -101,16 +106,25 @@ public class ConsumersManager {
if (started.get(group)) { if (started.get(group)) {
return; return;
} }
ConsumerGroupInstrumentation groupInstrumentation = instrumentationManager.getConsumerGroupInstrumentation( ConsumerGroupInstrumentation groupInstrumentation = null;
group); if (instrumentationManager != null) {
groupInstrumentation = instrumentationManager
.getConsumerGroupInstrumentation(group);
instrumentationManager.addHealthInstrumentation(groupInstrumentation); instrumentationManager.addHealthInstrumentation(groupInstrumentation);
}
try { try {
consumerGroups.get(group).start(); consumerGroups.get(group).start();
started.put(group, true); started.put(group, true);
if (groupInstrumentation != null) {
groupInstrumentation.markStartedSuccessfully(); groupInstrumentation.markStartedSuccessfully();
} catch (MQClientException e) { }
}
catch (MQClientException e) {
if (groupInstrumentation != null) {
groupInstrumentation.markStartFailed(e); groupInstrumentation.markStartFailed(e);
logger.error("RocketMQ Consumer hasn't been started. Caused by " + e.getErrorMessage(), e); }
logger.error("RocketMQ Consumer hasn't been started. Caused by "
+ e.getErrorMessage(), e);
throw e; throw e;
} }
} }
@ -119,4 +133,3 @@ public class ConsumersManager {
return consumerGroups.keySet(); return consumerGroups.keySet();
} }
} }

View File

@ -62,14 +62,14 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
private ConsumerInstrumentation consumerInstrumentation; private ConsumerInstrumentation consumerInstrumentation;
private InstrumentationManager instrumentationManager;
private final ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties; private final ExtendedConsumerProperties<RocketMQConsumerProperties> consumerProperties;
private final String destination; private final String destination;
private final String group; private final String group;
private final InstrumentationManager instrumentationManager;
private final ConsumersManager consumersManager; private final ConsumersManager consumersManager;
private RetryTemplate retryTemplate; private RetryTemplate retryTemplate;
@ -89,21 +89,20 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
@Override @Override
protected void doStart() { protected void doStart() {
if (!consumerProperties.getExtension().getEnabled()) { if (consumerProperties == null
|| !consumerProperties.getExtension().getEnabled()) {
return; return;
} }
String tags = consumerProperties == null ? null String tags = consumerProperties.getExtension().getTags();
: consumerProperties.getExtension().getTags(); Boolean isOrderly = consumerProperties.getExtension().getOrderly();
Boolean isOrderly = consumerProperties == null ? false
: consumerProperties.getExtension().getOrderly();
DefaultMQPushConsumer consumer = consumersManager.getOrCreateConsumer(group, DefaultMQPushConsumer consumer = consumersManager.getOrCreateConsumer(group,
destination, consumerProperties); destination, consumerProperties);
final CloudStreamMessageListener listener = isOrderly final CloudStreamMessageListener listener = isOrderly
? new CloudStreamMessageListenerOrderly(instrumentationManager) ? new CloudStreamMessageListenerOrderly()
: new CloudStreamMessageListenerConcurrently(instrumentationManager); : new CloudStreamMessageListenerConcurrently();
if (retryTemplate != null) { if (retryTemplate != null) {
retryTemplate.registerListener(listener); retryTemplate.registerListener(listener);
@ -116,9 +115,11 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
} }
} }
if (instrumentationManager != null) {
consumerInstrumentation = instrumentationManager consumerInstrumentation = instrumentationManager
.getConsumerInstrumentation(destination); .getConsumerInstrumentation(destination);
instrumentationManager.addHealthInstrumentation(consumerInstrumentation); instrumentationManager.addHealthInstrumentation(consumerInstrumentation);
}
try { try {
if (!StringUtils.isEmpty(consumerProperties.getExtension().getSql())) { if (!StringUtils.isEmpty(consumerProperties.getExtension().getSql())) {
@ -129,10 +130,14 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
consumer.subscribe(destination, consumer.subscribe(destination,
org.apache.commons.lang3.StringUtils.join(tagsSet, " || ")); org.apache.commons.lang3.StringUtils.join(tagsSet, " || "));
} }
if (consumerInstrumentation != null) {
consumerInstrumentation.markStartedSuccessfully(); consumerInstrumentation.markStartedSuccessfully();
} }
}
catch (MQClientException e) { catch (MQClientException e) {
if (consumerInstrumentation != null) {
consumerInstrumentation.markStartFailed(e); consumerInstrumentation.markStartFailed(e);
}
logger.error("RocketMQ Consumer hasn't been subscribed. Caused by " logger.error("RocketMQ Consumer hasn't been subscribed. Caused by "
+ e.getErrorMessage(), e); + e.getErrorMessage(), e);
throw new RuntimeException("RocketMQ Consumer hasn't been subscribed.", e); throw new RuntimeException("RocketMQ Consumer hasn't been subscribed.", e);
@ -166,19 +171,12 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
protected class CloudStreamMessageListener implements MessageListener, RetryListener { protected class CloudStreamMessageListener implements MessageListener, RetryListener {
private final InstrumentationManager instrumentationManager;
CloudStreamMessageListener(InstrumentationManager instrumentationManager) {
this.instrumentationManager = instrumentationManager;
}
Acknowledgement consumeMessage(final List<MessageExt> msgs) { Acknowledgement consumeMessage(final List<MessageExt> msgs) {
boolean enableRetry = RocketMQInboundChannelAdapter.this.retryTemplate != null; boolean enableRetry = RocketMQInboundChannelAdapter.this.retryTemplate != null;
try { try {
if (enableRetry) { if (enableRetry) {
return RocketMQInboundChannelAdapter.this.retryTemplate.execute( return RocketMQInboundChannelAdapter.this.retryTemplate
// (RetryCallback<Acknowledgement, Exception>) .execute(new RetryCallback<Acknowledgement, Exception>() {
new RetryCallback<Acknowledgement, Exception>() {
@Override @Override
public Acknowledgement doWithRetry(RetryContext context) public Acknowledgement doWithRetry(RetryContext context)
throws Exception { throws Exception {
@ -203,10 +201,12 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
} }
else { else {
Acknowledgement result = doSendMsgs(msgs, null); Acknowledgement result = doSendMsgs(msgs, null);
instrumentationManager if (RocketMQInboundChannelAdapter.this.instrumentationManager != null) {
RocketMQInboundChannelAdapter.this.instrumentationManager
.getConsumerInstrumentation( .getConsumerInstrumentation(
RocketMQInboundChannelAdapter.this.destination) RocketMQInboundChannelAdapter.this.destination)
.markConsumed(); .markConsumed();
}
return result; return result;
} }
} }
@ -214,10 +214,12 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
logger.error( logger.error(
"RocketMQ Message hasn't been processed successfully. Caused by ", "RocketMQ Message hasn't been processed successfully. Caused by ",
e); e);
instrumentationManager if (RocketMQInboundChannelAdapter.this.instrumentationManager != null) {
RocketMQInboundChannelAdapter.this.instrumentationManager
.getConsumerInstrumentation( .getConsumerInstrumentation(
RocketMQInboundChannelAdapter.this.destination) RocketMQInboundChannelAdapter.this.destination)
.markConsumedFailure(); .markConsumedFailure();
}
throw new RuntimeException( throw new RuntimeException(
"RocketMQ Message hasn't been processed successfully. Caused by ", "RocketMQ Message hasn't been processed successfully. Caused by ",
e); e);
@ -254,14 +256,17 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
@Override @Override
public <T, E extends Throwable> void close(RetryContext context, public <T, E extends Throwable> void close(RetryContext context,
RetryCallback<T, E> callback, Throwable throwable) { RetryCallback<T, E> callback, Throwable throwable) {
if (RocketMQInboundChannelAdapter.this.instrumentationManager == null) {
return;
}
if (throwable != null) { if (throwable != null) {
instrumentationManager RocketMQInboundChannelAdapter.this.instrumentationManager
.getConsumerInstrumentation( .getConsumerInstrumentation(
RocketMQInboundChannelAdapter.this.destination) RocketMQInboundChannelAdapter.this.destination)
.markConsumedFailure(); .markConsumedFailure();
} }
else { else {
instrumentationManager RocketMQInboundChannelAdapter.this.instrumentationManager
.getConsumerInstrumentation( .getConsumerInstrumentation(
RocketMQInboundChannelAdapter.this.destination) RocketMQInboundChannelAdapter.this.destination)
.markConsumed(); .markConsumed();
@ -277,11 +282,6 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
protected class CloudStreamMessageListenerConcurrently protected class CloudStreamMessageListenerConcurrently
extends CloudStreamMessageListener implements MessageListenerConcurrently { extends CloudStreamMessageListener implements MessageListenerConcurrently {
public CloudStreamMessageListenerConcurrently(
InstrumentationManager instrumentationManager) {
super(instrumentationManager);
}
@Override @Override
public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs, public ConsumeConcurrentlyStatus consumeMessage(final List<MessageExt> msgs,
ConsumeConcurrentlyContext context) { ConsumeConcurrentlyContext context) {
@ -295,11 +295,6 @@ public class RocketMQInboundChannelAdapter extends MessageProducerSupport {
protected class CloudStreamMessageListenerOrderly extends CloudStreamMessageListener protected class CloudStreamMessageListenerOrderly extends CloudStreamMessageListener
implements MessageListenerOrderly { implements MessageListenerOrderly {
public CloudStreamMessageListenerOrderly(
InstrumentationManager instrumentationManager) {
super(instrumentationManager);
}
@Override @Override
public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs,
ConsumeOrderlyContext context) { ConsumeOrderlyContext context) {

View File

@ -45,14 +45,14 @@ public class RocketMQMessageHandler extends AbstractMessageHandler implements Li
private ProducerInstrumentation producerInstrumentation; private ProducerInstrumentation producerInstrumentation;
private InstrumentationManager instrumentationManager;
private final RocketMQProducerProperties producerProperties; private final RocketMQProducerProperties producerProperties;
private final String destination; private final String destination;
private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties; private final RocketMQBinderConfigurationProperties rocketBinderConfigurationProperties;
private final InstrumentationManager instrumentationManager;
protected volatile boolean running = false; protected volatile boolean running = false;
public RocketMQMessageHandler(String destination, public RocketMQMessageHandler(String destination,
@ -69,9 +69,11 @@ public class RocketMQMessageHandler extends AbstractMessageHandler implements Li
public void start() { public void start() {
producer = new DefaultMQProducer(destination); producer = new DefaultMQProducer(destination);
if (instrumentationManager != null) {
producerInstrumentation = instrumentationManager producerInstrumentation = instrumentationManager
.getProducerInstrumentation(destination); .getProducerInstrumentation(destination);
instrumentationManager.addHealthInstrumentation(producerInstrumentation); instrumentationManager.addHealthInstrumentation(producerInstrumentation);
}
producer.setNamesrvAddr(rocketBinderConfigurationProperties.getNamesrvAddr()); producer.setNamesrvAddr(rocketBinderConfigurationProperties.getNamesrvAddr());
@ -81,10 +83,14 @@ public class RocketMQMessageHandler extends AbstractMessageHandler implements Li
try { try {
producer.start(); producer.start();
if (producerInstrumentation != null) {
producerInstrumentation.markStartedSuccessfully(); producerInstrumentation.markStartedSuccessfully();
} }
}
catch (MQClientException e) { catch (MQClientException e) {
if (producerInstrumentation != null) {
producerInstrumentation.markStartFailed(e); producerInstrumentation.markStartFailed(e);
}
logger.error( logger.error(
"RocketMQ Message hasn't been sent. Caused by " + e.getMessage()); "RocketMQ Message hasn't been sent. Caused by " + e.getMessage());
throw new MessagingException(e.getMessage(), e); throw new MessagingException(e.getMessage(), e);
@ -142,14 +148,18 @@ public class RocketMQMessageHandler extends AbstractMessageHandler implements Li
RocketMQMessageHeaderAccessor.putSendResult((MutableMessage) message, RocketMQMessageHeaderAccessor.putSendResult((MutableMessage) message,
sendRes); sendRes);
} }
if (instrumentationManager != null) {
instrumentationManager.getRuntime().put( instrumentationManager.getRuntime().put(
RocketMQBinderConstants.LASTSEND_TIMESTAMP, RocketMQBinderConstants.LASTSEND_TIMESTAMP,
System.currentTimeMillis()); System.currentTimeMillis());
producerInstrumentation.markSent(); producerInstrumentation.markSent();
} }
}
catch (MQClientException | RemotingException | MQBrokerException catch (MQClientException | RemotingException | MQBrokerException
| InterruptedException | UnsupportedOperationException e) { | InterruptedException | UnsupportedOperationException e) {
if (producerInstrumentation != null) {
producerInstrumentation.markSentFailure(); producerInstrumentation.markSentFailure();
}
logger.error( logger.error(
"RocketMQ Message hasn't been sent. Caused by " + e.getMessage()); "RocketMQ Message hasn't been sent. Caused by " + e.getMessage());
throw new MessagingException(e.getMessage(), e); throw new MessagingException(e.getMessage(), e);

View File

@ -28,20 +28,14 @@ import com.codahale.metrics.MetricRegistry;
* @author <a href="mailto:fangjian0423@gmail.com">Jim</a> * @author <a href="mailto:fangjian0423@gmail.com">Jim</a>
*/ */
public class InstrumentationManager { public class InstrumentationManager {
private final MetricRegistry metricRegistry; private final MetricRegistry metricRegistry = new MetricRegistry();
private final Map<String, Object> runtime; private final Map<String, Object> runtime = new HashMap<>();
private final Map<String, ProducerInstrumentation> producerInstrumentations = new HashMap<>(); private final Map<String, ProducerInstrumentation> producerInstrumentations = new HashMap<>();
private final Map<String, ConsumerInstrumentation> consumeInstrumentations = new HashMap<>(); private final Map<String, ConsumerInstrumentation> consumeInstrumentations = new HashMap<>();
private final Map<String, ConsumerGroupInstrumentation> consumerGroupsInstrumentations = new HashMap<>(); private final Map<String, ConsumerGroupInstrumentation> consumerGroupsInstrumentations = new HashMap<>();
private final Map<String, Instrumentation> healthInstrumentations = new HashMap<>(); private final Map<String, Instrumentation> healthInstrumentations = new HashMap<>();
public InstrumentationManager(MetricRegistry metricRegistry,
Map<String, Object> runtime) {
this.metricRegistry = metricRegistry;
this.runtime = runtime;
}
public ProducerInstrumentation getProducerInstrumentation(String destination) { public ProducerInstrumentation getProducerInstrumentation(String destination) {
String key = "scs-rocketmq.producer." + destination; String key = "scs-rocketmq.producer." + destination;
ProducerInstrumentation producerInstrumentation = producerInstrumentations ProducerInstrumentation producerInstrumentation = producerInstrumentations
@ -86,4 +80,8 @@ public class InstrumentationManager {
public Map<String, Object> getRuntime() { public Map<String, Object> getRuntime() {
return runtime; return runtime;
} }
public MetricRegistry getMetricRegistry() {
return metricRegistry;
}
} }