Commit 1700cf77077937d5f66e793ff4ddded5e7fe4629

Authored by vzikratyi
Committed by Andrew Shvayka
1 parent 4d7e5add

Added producer stats to 'transport' services

Showing 22 changed files with 228 additions and 111 deletions
... ... @@ -298,18 +298,6 @@
298 298 <groupId>com.github.ua-parser</groupId>
299 299 <artifactId>uap-java</artifactId>
300 300 </dependency>
301   - <dependency>
302   - <groupId>org.springframework.boot</groupId>
303   - <artifactId>spring-boot-starter-actuator</artifactId>
304   - </dependency>
305   - <dependency>
306   - <groupId>io.micrometer</groupId>
307   - <artifactId>micrometer-core</artifactId>
308   - </dependency>
309   - <dependency>
310   - <groupId>io.micrometer</groupId>
311   - <artifactId>micrometer-registry-prometheus</artifactId>
312   - </dependency>
313 301 </dependencies>
314 302
315 303 <build>
... ...
... ... @@ -31,6 +31,7 @@ import org.thingsboard.server.queue.TbQueueConsumer;
31 31 import org.thingsboard.server.queue.common.TbProtoQueueMsg;
32 32 import org.thingsboard.server.queue.discovery.PartitionChangeEvent;
33 33 import org.thingsboard.server.queue.provider.TbCoreQueueFactory;
  34 +import org.thingsboard.server.common.msg.stats.StatsFactory;
34 35 import org.thingsboard.server.queue.util.TbCoreComponent;
35 36 import org.thingsboard.server.service.encoding.DataDecodingEncodingService;
36 37 import org.thingsboard.server.service.queue.processing.AbstractConsumerService;
... ... @@ -38,7 +39,6 @@ import org.thingsboard.server.service.rpc.FromDeviceRpcResponse;
38 39 import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService;
39 40 import org.thingsboard.server.service.rpc.ToDeviceRpcRequestActorMsg;
40 41 import org.thingsboard.server.service.state.DeviceStateService;
41   -import org.thingsboard.server.service.stats.StatsCounterFactory;
42 42 import org.thingsboard.server.service.subscription.SubscriptionManagerService;
43 43 import org.thingsboard.server.service.subscription.TbLocalSubscriptionService;
44 44 import org.thingsboard.server.service.subscription.TbSubscriptionUtils;
... ... @@ -78,14 +78,14 @@ public class DefaultTbCoreConsumerService extends AbstractConsumerService<ToCore
78 78 public DefaultTbCoreConsumerService(TbCoreQueueFactory tbCoreQueueFactory, ActorSystemContext actorContext,
79 79 DeviceStateService stateService, TbLocalSubscriptionService localSubscriptionService,
80 80 SubscriptionManagerService subscriptionManagerService, DataDecodingEncodingService encodingService,
81   - TbCoreDeviceRpcService tbCoreDeviceRpcService, StatsCounterFactory counterFactory) {
  81 + TbCoreDeviceRpcService tbCoreDeviceRpcService, StatsFactory statsFactory) {
82 82 super(actorContext, encodingService, tbCoreQueueFactory.createToCoreNotificationsMsgConsumer());
83 83 this.mainConsumer = tbCoreQueueFactory.createToCoreMsgConsumer();
84 84 this.stateService = stateService;
85 85 this.localSubscriptionService = localSubscriptionService;
86 86 this.subscriptionManagerService = subscriptionManagerService;
87 87 this.tbCoreDeviceRpcService = tbCoreDeviceRpcService;
88   - this.stats = new TbCoreConsumerStats(counterFactory);
  88 + this.stats = new TbCoreConsumerStats(statsFactory);
89 89 }
90 90
91 91 @PostConstruct
... ...
... ... @@ -42,6 +42,7 @@ import org.thingsboard.server.queue.discovery.PartitionChangeEvent;
42 42 import org.thingsboard.server.queue.provider.TbRuleEngineQueueFactory;
43 43 import org.thingsboard.server.queue.settings.TbQueueRuleEngineSettings;
44 44 import org.thingsboard.server.queue.settings.TbRuleEngineQueueConfiguration;
  45 +import org.thingsboard.server.common.msg.stats.StatsFactory;
45 46 import org.thingsboard.server.queue.util.TbRuleEngineComponent;
46 47 import org.thingsboard.server.service.encoding.DataDecodingEncodingService;
47 48 import org.thingsboard.server.service.queue.processing.AbstractConsumerService;
... ... @@ -54,7 +55,6 @@ import org.thingsboard.server.service.queue.processing.TbRuleEngineSubmitStrateg
54 55 import org.thingsboard.server.service.rpc.FromDeviceRpcResponse;
55 56 import org.thingsboard.server.service.rpc.TbRuleEngineDeviceRpcService;
56 57 import org.thingsboard.server.service.stats.RuleEngineStatisticsService;
57   -import org.thingsboard.server.service.stats.StatsCounterFactory;
58 58
59 59 import javax.annotation.PostConstruct;
60 60 import javax.annotation.PreDestroy;
... ... @@ -83,7 +83,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
83 83 @Value("${queue.rule-engine.stats.enabled:true}")
84 84 private boolean statsEnabled;
85 85
86   - private final StatsCounterFactory counterFactory;
  86 + private final StatsFactory statsFactory;
87 87 private final TbRuleEngineSubmitStrategyFactory submitStrategyFactory;
88 88 private final TbRuleEngineProcessingStrategyFactory processingStrategyFactory;
89 89 private final TbRuleEngineQueueFactory tbRuleEngineQueueFactory;
... ... @@ -101,7 +101,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
101 101 TbRuleEngineQueueFactory tbRuleEngineQueueFactory, RuleEngineStatisticsService statisticsService,
102 102 ActorSystemContext actorContext, DataDecodingEncodingService encodingService,
103 103 TbRuleEngineDeviceRpcService tbDeviceRpcService,
104   - StatsCounterFactory counterFactory) {
  104 + StatsFactory statsFactory) {
105 105 super(actorContext, encodingService, tbRuleEngineQueueFactory.createToRuleEngineNotificationsMsgConsumer());
106 106 this.statisticsService = statisticsService;
107 107 this.ruleEngineSettings = ruleEngineSettings;
... ... @@ -109,7 +109,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
109 109 this.submitStrategyFactory = submitStrategyFactory;
110 110 this.processingStrategyFactory = processingStrategyFactory;
111 111 this.tbDeviceRpcService = tbDeviceRpcService;
112   - this.counterFactory = counterFactory;
  112 + this.statsFactory = statsFactory;
113 113 }
114 114
115 115 @PostConstruct
... ... @@ -118,7 +118,7 @@ public class DefaultTbRuleEngineConsumerService extends AbstractConsumerService<
118 118 for (TbRuleEngineQueueConfiguration configuration : ruleEngineSettings.getQueues()) {
119 119 consumerConfigurations.putIfAbsent(configuration.getName(), configuration);
120 120 consumers.computeIfAbsent(configuration.getName(), queueName -> tbRuleEngineQueueFactory.createToRuleEngineMsgConsumer(configuration));
121   - consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), counterFactory));
  121 + consumerStats.put(configuration.getName(), new TbRuleEngineConsumerStats(configuration.getName(), statsFactory));
122 122 }
123 123 submitExecutor = Executors.newSingleThreadExecutor();
124 124 }
... ...
... ... @@ -17,12 +17,11 @@ package org.thingsboard.server.service.queue;
17 17
18 18 import lombok.extern.slf4j.Slf4j;
19 19 import org.thingsboard.server.gen.transport.TransportProtos;
20   -import org.thingsboard.server.service.stats.StatsCounter;
21   -import org.thingsboard.server.service.stats.StatsCounterFactory;
22   -import org.thingsboard.server.service.stats.StatsType;
  20 +import org.thingsboard.server.common.msg.stats.StatsCounter;
  21 +import org.thingsboard.server.common.msg.stats.StatsFactory;
  22 +import org.thingsboard.server.common.msg.stats.StatsType;
23 23
24 24 import java.util.*;
25   -import java.util.concurrent.atomic.AtomicInteger;
26 25
27 26 @Slf4j
28 27 public class TbCoreConsumerStats {
... ... @@ -53,20 +52,20 @@ public class TbCoreConsumerStats {
53 52
54 53 private final List<StatsCounter> counters = new ArrayList<>();
55 54
56   - public TbCoreConsumerStats(StatsCounterFactory counterFactory) {
  55 + public TbCoreConsumerStats(StatsFactory statsFactory) {
57 56 String statsKey = StatsType.CORE.getName();
58 57
59   - this.totalCounter = counterFactory.createStatsCounter(statsKey, TOTAL_MSGS);
60   - this.sessionEventCounter = counterFactory.createStatsCounter(statsKey, SESSION_EVENTS);
61   - this.getAttributesCounter = counterFactory.createStatsCounter(statsKey, GET_ATTRIBUTE);
62   - this.subscribeToAttributesCounter = counterFactory.createStatsCounter(statsKey, ATTRIBUTE_SUBSCRIBES);
63   - this.subscribeToRPCCounter = counterFactory.createStatsCounter(statsKey, RPC_SUBSCRIBES);
64   - this.toDeviceRPCCallResponseCounter = counterFactory.createStatsCounter(statsKey, TO_DEVICE_RPC_CALL_RESPONSES);
65   - this.subscriptionInfoCounter = counterFactory.createStatsCounter(statsKey, SUBSCRIPTION_INFO);
66   - this.claimDeviceCounter = counterFactory.createStatsCounter(statsKey, DEVICE_CLAIMS);
67   - this.deviceStateCounter = counterFactory.createStatsCounter(statsKey, DEVICE_STATES);
68   - this.subscriptionMsgCounter = counterFactory.createStatsCounter(statsKey, SUBSCRIPTION_MSGS);
69   - this.toCoreNotificationsCounter = counterFactory.createStatsCounter(statsKey, TO_CORE_NOTIFICATIONS);
  58 + this.totalCounter = statsFactory.createStatsCounter(statsKey, TOTAL_MSGS);
  59 + this.sessionEventCounter = statsFactory.createStatsCounter(statsKey, SESSION_EVENTS);
  60 + this.getAttributesCounter = statsFactory.createStatsCounter(statsKey, GET_ATTRIBUTE);
  61 + this.subscribeToAttributesCounter = statsFactory.createStatsCounter(statsKey, ATTRIBUTE_SUBSCRIBES);
  62 + this.subscribeToRPCCounter = statsFactory.createStatsCounter(statsKey, RPC_SUBSCRIBES);
  63 + this.toDeviceRPCCallResponseCounter = statsFactory.createStatsCounter(statsKey, TO_DEVICE_RPC_CALL_RESPONSES);
  64 + this.subscriptionInfoCounter = statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_INFO);
  65 + this.claimDeviceCounter = statsFactory.createStatsCounter(statsKey, DEVICE_CLAIMS);
  66 + this.deviceStateCounter = statsFactory.createStatsCounter(statsKey, DEVICE_STATES);
  67 + this.subscriptionMsgCounter = statsFactory.createStatsCounter(statsKey, SUBSCRIPTION_MSGS);
  68 + this.toCoreNotificationsCounter = statsFactory.createStatsCounter(statsKey, TO_CORE_NOTIFICATIONS);
70 69
71 70
72 71 counters.add(totalCounter);
... ...
... ... @@ -15,21 +15,19 @@
15 15 */
16 16 package org.thingsboard.server.service.queue;
17 17
18   -import lombok.Data;
19 18 import lombok.extern.slf4j.Slf4j;
20 19 import org.thingsboard.server.common.data.id.TenantId;
21 20 import org.thingsboard.server.common.msg.queue.RuleEngineException;
22 21 import org.thingsboard.server.gen.transport.TransportProtos.ToRuleEngineMsg;
23 22 import org.thingsboard.server.queue.common.TbProtoQueueMsg;
  23 +import org.thingsboard.server.common.msg.stats.StatsFactory;
24 24 import org.thingsboard.server.service.queue.processing.TbRuleEngineProcessingResult;
25   -import org.thingsboard.server.service.stats.StatsCounter;
26   -import org.thingsboard.server.service.stats.StatsCounterFactory;
27   -import org.thingsboard.server.service.stats.StatsType;
  25 +import org.thingsboard.server.common.msg.stats.StatsCounter;
  26 +import org.thingsboard.server.common.msg.stats.StatsType;
28 27
29 28 import java.util.*;
30 29 import java.util.concurrent.ConcurrentHashMap;
31 30 import java.util.concurrent.ConcurrentMap;
32   -import java.util.concurrent.atomic.AtomicInteger;
33 31
34 32 @Slf4j
35 33 public class TbRuleEngineConsumerStats {
... ... @@ -60,18 +58,18 @@ public class TbRuleEngineConsumerStats {
60 58
61 59 private final String queueName;
62 60
63   - public TbRuleEngineConsumerStats(String queueName, StatsCounterFactory counterFactory) {
  61 + public TbRuleEngineConsumerStats(String queueName, StatsFactory statsFactory) {
64 62 this.queueName = queueName;
65 63
66 64 String statsKey = StatsType.RULE_ENGINE.getName() + "." + queueName;
67   - this.totalMsgCounter = counterFactory.createStatsCounter(statsKey, TOTAL_MSGS);
68   - this.successMsgCounter = counterFactory.createStatsCounter(statsKey, SUCCESSFUL_MSGS);
69   - this.timeoutMsgCounter = counterFactory.createStatsCounter(statsKey, TIMEOUT_MSGS);
70   - this.failedMsgCounter = counterFactory.createStatsCounter(statsKey, FAILED_MSGS);
71   - this.tmpTimeoutMsgCounter = counterFactory.createStatsCounter(statsKey, TMP_TIMEOUT);
72   - this.tmpFailedMsgCounter = counterFactory.createStatsCounter(statsKey, TMP_FAILED);
73   - this.successIterationsCounter = counterFactory.createStatsCounter(statsKey, SUCCESSFUL_ITERATIONS);
74   - this.failedIterationsCounter = counterFactory.createStatsCounter(statsKey, FAILED_ITERATIONS);
  65 + this.totalMsgCounter = statsFactory.createStatsCounter(statsKey, TOTAL_MSGS);
  66 + this.successMsgCounter = statsFactory.createStatsCounter(statsKey, SUCCESSFUL_MSGS);
  67 + this.timeoutMsgCounter = statsFactory.createStatsCounter(statsKey, TIMEOUT_MSGS);
  68 + this.failedMsgCounter = statsFactory.createStatsCounter(statsKey, FAILED_MSGS);
  69 + this.tmpTimeoutMsgCounter = statsFactory.createStatsCounter(statsKey, TMP_TIMEOUT);
  70 + this.tmpFailedMsgCounter = statsFactory.createStatsCounter(statsKey, TMP_FAILED);
  71 + this.successIterationsCounter = statsFactory.createStatsCounter(statsKey, SUCCESSFUL_ITERATIONS);
  72 + this.failedIterationsCounter = statsFactory.createStatsCounter(statsKey, FAILED_ITERATIONS);
75 73
76 74 counters.add(totalMsgCounter);
77 75 counters.add(successMsgCounter);
... ...
... ... @@ -18,6 +18,9 @@ package org.thingsboard.server.service.stats;
18 18 import org.springframework.beans.factory.annotation.Autowired;
19 19 import org.springframework.stereotype.Service;
20 20 import org.thingsboard.server.actors.JsInvokeStats;
  21 +import org.thingsboard.server.common.msg.stats.StatsCounter;
  22 +import org.thingsboard.server.common.msg.stats.StatsFactory;
  23 +import org.thingsboard.server.common.msg.stats.StatsType;
21 24
22 25 import javax.annotation.PostConstruct;
23 26
... ... @@ -32,14 +35,14 @@ public class DefaultJsInvokeStats implements JsInvokeStats {
32 35 private StatsCounter failuresCounter;
33 36
34 37 @Autowired
35   - private StatsCounterFactory counterFactory;
  38 + private StatsFactory statsFactory;
36 39
37 40 @PostConstruct
38 41 public void init() {
39 42 String key = StatsType.JS_INVOKE.getName();
40   - this.requestsCounter = counterFactory.createStatsCounter(key, REQUESTS);
41   - this.responsesCounter = counterFactory.createStatsCounter(key, RESPONSES);
42   - this.failuresCounter = counterFactory.createStatsCounter(key, FAILURES);
  43 + this.requestsCounter = statsFactory.createStatsCounter(key, REQUESTS);
  44 + this.responsesCounter = statsFactory.createStatsCounter(key, RESPONSES);
  45 + this.failuresCounter = statsFactory.createStatsCounter(key, FAILURES);
43 46 }
44 47
45 48 @Override
... ...
... ... @@ -20,6 +20,9 @@ import org.springframework.beans.factory.annotation.Value;
20 20 import org.springframework.boot.context.event.ApplicationReadyEvent;
21 21 import org.springframework.context.event.EventListener;
22 22 import org.springframework.stereotype.Service;
  23 +import org.thingsboard.server.common.msg.stats.MessagesStats;
  24 +import org.thingsboard.server.common.msg.stats.StatsFactory;
  25 +import org.thingsboard.server.common.msg.stats.StatsType;
23 26 import org.thingsboard.server.queue.TbQueueConsumer;
24 27 import org.thingsboard.server.queue.TbQueueProducer;
25 28 import org.thingsboard.server.queue.TbQueueResponseTemplate;
... ... @@ -29,10 +32,6 @@ import org.thingsboard.server.gen.transport.TransportProtos.TransportApiRequestM
29 32 import org.thingsboard.server.gen.transport.TransportProtos.TransportApiResponseMsg;
30 33 import org.thingsboard.server.queue.provider.TbCoreQueueFactory;
31 34 import org.thingsboard.server.queue.util.TbCoreComponent;
32   -import org.thingsboard.server.service.stats.DefaultQueueStats;
33   -import org.thingsboard.server.service.stats.StatsCounter;
34   -import org.thingsboard.server.service.stats.StatsCounterFactory;
35   -import org.thingsboard.server.service.stats.StatsType;
36 35
37 36 import javax.annotation.PostConstruct;
38 37 import javax.annotation.PreDestroy;
... ... @@ -45,13 +44,9 @@ import java.util.concurrent.*;
45 44 @Service
46 45 @TbCoreComponent
47 46 public class TbCoreTransportApiService {
48   - private static final String TOTAL_MSGS = "totalMsgs";
49   - private static final String SUCCESSFUL_MSGS = "successfulMsgs";
50   - private static final String FAILED_MSGS = "failedMsgs";
51   -
52 47 private final TbCoreQueueFactory tbCoreQueueFactory;
53 48 private final TransportApiService transportApiService;
54   - private final StatsCounterFactory counterFactory;
  49 + private final StatsFactory statsFactory;
55 50
56 51 @Value("${queue.transport_api.max_pending_requests:10000}")
57 52 private int maxPendingRequests;
... ... @@ -66,10 +61,10 @@ public class TbCoreTransportApiService {
66 61 private TbQueueResponseTemplate<TbProtoQueueMsg<TransportApiRequestMsg>,
67 62 TbProtoQueueMsg<TransportApiResponseMsg>> transportApiTemplate;
68 63
69   - public TbCoreTransportApiService(TbCoreQueueFactory tbCoreQueueFactory, TransportApiService transportApiService, StatsCounterFactory counterFactory) {
  64 + public TbCoreTransportApiService(TbCoreQueueFactory tbCoreQueueFactory, TransportApiService transportApiService, StatsFactory statsFactory) {
70 65 this.tbCoreQueueFactory = tbCoreQueueFactory;
71 66 this.transportApiService = transportApiService;
72   - this.counterFactory = counterFactory;
  67 + this.statsFactory = statsFactory;
73 68 }
74 69
75 70 @PostConstruct
... ... @@ -79,10 +74,7 @@ public class TbCoreTransportApiService {
79 74 TbQueueConsumer<TbProtoQueueMsg<TransportApiRequestMsg>> consumer = tbCoreQueueFactory.createTransportApiRequestConsumer();
80 75
81 76 String key = StatsType.TRANSPORT.getName();
82   - StatsCounter totalCounter = counterFactory.createStatsCounter(key, TOTAL_MSGS);
83   - StatsCounter successfulCounter = counterFactory.createStatsCounter(key, SUCCESSFUL_MSGS);
84   - StatsCounter failedCounter = counterFactory.createStatsCounter(key, FAILED_MSGS);
85   - DefaultQueueStats queueStats = new DefaultQueueStats(totalCounter, successfulCounter, failedCounter);
  77 + MessagesStats queueStats = statsFactory.createMessagesStats(key);
86 78
87 79 DefaultTbQueueResponseTemplate.DefaultTbQueueResponseTemplateBuilder
88 80 <TbProtoQueueMsg<TransportApiRequestMsg>, TbProtoQueueMsg<TransportApiResponseMsg>> builder = DefaultTbQueueResponseTemplate.builder();
... ...
... ... @@ -69,6 +69,21 @@
69 69 <artifactId>protobuf-java</artifactId>
70 70 <scope>provided</scope>
71 71 </dependency>
  72 +
  73 + <!--TODO is this a good idea to put actuator here?-->
  74 + <dependency>
  75 + <groupId>org.springframework.boot</groupId>
  76 + <artifactId>spring-boot-starter-actuator</artifactId>
  77 + </dependency>
  78 + <dependency>
  79 + <groupId>io.micrometer</groupId>
  80 + <artifactId>micrometer-core</artifactId>
  81 + </dependency>
  82 + <dependency>
  83 + <groupId>io.micrometer</groupId>
  84 + <artifactId>micrometer-registry-prometheus</artifactId>
  85 + </dependency>
  86 +
72 87 <dependency>
73 88 <groupId>junit</groupId>
74 89 <artifactId>junit</artifactId>
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/DefaultMessagesStats.java renamed from application/src/main/java/org/thingsboard/server/service/stats/DefaultQueueStats.java
... ... @@ -13,16 +13,14 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.stats;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18   -import org.thingsboard.server.queue.stats.QueueStats;
19   -
20   -public class DefaultQueueStats implements QueueStats {
  18 +public class DefaultMessagesStats implements MessagesStats {
21 19 private final StatsCounter totalCounter;
22 20 private final StatsCounter successfulCounter;
23 21 private final StatsCounter failedCounter;
24 22
25   - public DefaultQueueStats(StatsCounter totalCounter, StatsCounter successfulCounter, StatsCounter failedCounter) {
  23 + public DefaultMessagesStats(StatsCounter totalCounter, StatsCounter successfulCounter, StatsCounter failedCounter) {
26 24 this.totalCounter = totalCounter;
27 25 this.successfulCounter = successfulCounter;
28 26 this.failedCounter = failedCounter;
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/DefaultStatsFactory.java renamed from application/src/main/java/org/thingsboard/server/service/stats/StatsCounterFactory.java
... ... @@ -13,19 +13,22 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.stats;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18 18 import io.micrometer.core.instrument.Counter;
19 19 import io.micrometer.core.instrument.MeterRegistry;
20 20 import org.springframework.beans.factory.annotation.Autowired;
21 21 import org.springframework.beans.factory.annotation.Value;
22 22 import org.springframework.stereotype.Service;
23   -import org.thingsboard.server.service.metrics.StubCounter;
24 23
25 24 import java.util.concurrent.atomic.AtomicInteger;
26 25
27 26 @Service
28   -public class StatsCounterFactory {
  27 +public class DefaultStatsFactory implements StatsFactory {
  28 + private static final String TOTAL_MSGS = "totalMsgs";
  29 + private static final String SUCCESSFUL_MSGS = "successfulMsgs";
  30 + private static final String FAILED_MSGS = "failedMsgs";
  31 +
29 32 private static final String STATS_NAME_TAG = "statsName";
30 33
31 34 private static final Counter STUB_COUNTER = new StubCounter();
... ... @@ -33,9 +36,10 @@ public class StatsCounterFactory {
33 36 @Autowired
34 37 private MeterRegistry meterRegistry;
35 38
36   - @Value("${metrics.enabled}")
  39 + @Value("${metrics.enabled:false}")
37 40 private Boolean metricsEnabled;
38 41
  42 + @Override
39 43 public StatsCounter createStatsCounter(String key, String statsName) {
40 44 return new StatsCounter(
41 45 new AtomicInteger(0),
... ... @@ -45,4 +49,27 @@ public class StatsCounterFactory {
45 49 statsName
46 50 );
47 51 }
  52 +
  53 + @Override
  54 + public MessagesStats createMessagesStats(String key) {
  55 + StatsCounter totalCounter = createStatsCounter(key, TOTAL_MSGS);
  56 + StatsCounter successfulCounter = createStatsCounter(key, SUCCESSFUL_MSGS);
  57 + StatsCounter failedCounter = createStatsCounter(key, FAILED_MSGS);
  58 + return new DefaultMessagesStats(totalCounter, successfulCounter, failedCounter);
  59 + }
  60 +
  61 + private static class StubCounter implements Counter {
  62 + @Override
  63 + public void increment(double amount) {}
  64 +
  65 + @Override
  66 + public double count() {
  67 + return 0;
  68 + }
  69 +
  70 + @Override
  71 + public Id getId() {
  72 + return null;
  73 + }
  74 + }
48 75 }
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/MessagesStats.java renamed from common/queue/src/main/java/org/thingsboard/server/queue/stats/QueueStats.java
... ... @@ -13,9 +13,9 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.queue.stats;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18   -public interface QueueStats {
  18 +public interface MessagesStats {
19 19 default void incrementTotal() {
20 20 incrementTotal(1);
21 21 }
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/StatsCounter.java renamed from application/src/main/java/org/thingsboard/server/service/stats/StatsCounter.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.stats;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18 18 import io.micrometer.core.instrument.Counter;
19 19
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/StatsFactory.java renamed from application/src/main/java/org/thingsboard/server/service/metrics/StubCounter.java
... ... @@ -13,21 +13,10 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.metrics;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18   -import io.micrometer.core.instrument.Counter;
  18 +public interface StatsFactory {
  19 + StatsCounter createStatsCounter(String key, String statsName);
19 20
20   -public class StubCounter implements Counter {
21   - @Override
22   - public void increment(double amount) {}
23   -
24   - @Override
25   - public double count() {
26   - return 0;
27   - }
28   -
29   - @Override
30   - public Id getId() {
31   - return null;
32   - }
  21 + MessagesStats createMessagesStats(String key);
33 22 }
... ...
common/message/src/main/java/org/thingsboard/server/common/msg/stats/StatsType.java renamed from application/src/main/java/org/thingsboard/server/service/stats/StatsType.java
... ... @@ -13,7 +13,7 @@
13 13 * See the License for the specific language governing permissions and
14 14 * limitations under the License.
15 15 */
16   -package org.thingsboard.server.service.stats;
  16 +package org.thingsboard.server.common.msg.stats;
17 17
18 18 public enum StatsType {
19 19 RULE_ENGINE("ruleEngine"), CORE("core"), TRANSPORT("transport"), JS_INVOKE("jsInvoke");
... ...
... ... @@ -112,6 +112,7 @@
112 112 <groupId>org.apache.curator</groupId>
113 113 <artifactId>curator-recipes</artifactId>
114 114 </dependency>
  115 +
115 116 <dependency>
116 117 <groupId>junit</groupId>
117 118 <artifactId>junit</artifactId>
... ...
... ... @@ -16,6 +16,7 @@
16 16 package org.thingsboard.server.queue;
17 17
18 18 import com.google.common.util.concurrent.ListenableFuture;
  19 +import org.thingsboard.server.common.msg.stats.MessagesStats;
19 20
20 21 public interface TbQueueRequestTemplate<Request extends TbQueueMsg, Response extends TbQueueMsg> {
21 22
... ... @@ -25,4 +26,5 @@ public interface TbQueueRequestTemplate<Request extends TbQueueMsg, Response ext
25 26
26 27 void stop();
27 28
  29 + void setMessagesStats(MessagesStats messagesStats);
28 30 }
... ...
... ... @@ -28,6 +28,7 @@ import org.thingsboard.server.queue.TbQueueMsg;
28 28 import org.thingsboard.server.queue.TbQueueMsgMetadata;
29 29 import org.thingsboard.server.queue.TbQueueProducer;
30 30 import org.thingsboard.server.queue.TbQueueRequestTemplate;
  31 +import org.thingsboard.server.common.msg.stats.MessagesStats;
31 32
32 33 import java.util.List;
33 34 import java.util.UUID;
... ... @@ -54,6 +55,8 @@ public class DefaultTbQueueRequestTemplate<Request extends TbQueueMsg, Response
54 55 private volatile long tickSize = 0L;
55 56 private volatile boolean stopped = false;
56 57
  58 + private MessagesStats messagesStats;
  59 +
57 60 @Builder
58 61 public DefaultTbQueueRequestTemplate(TbQueueAdmin queueAdmin,
59 62 TbQueueProducer<Request> requestTemplate,
... ... @@ -154,6 +157,11 @@ public class DefaultTbQueueRequestTemplate<Request extends TbQueueMsg, Response
154 157 }
155 158
156 159 @Override
  160 + public void setMessagesStats(MessagesStats messagesStats) {
  161 + this.messagesStats = messagesStats;
  162 + }
  163 +
  164 + @Override
157 165 public ListenableFuture<Response> send(Request request) {
158 166 if (tickSize > maxPendingRequests) {
159 167 return Futures.immediateFailedFuture(new RuntimeException("Pending request map is full!"));
... ... @@ -166,14 +174,17 @@ public class DefaultTbQueueRequestTemplate<Request extends TbQueueMsg, Response
166 174 ResponseMetaData<Response> responseMetaData = new ResponseMetaData<>(tickTs + maxRequestTimeout, future);
167 175 pendingRequests.putIfAbsent(requestId, responseMetaData);
168 176 log.trace("[{}] Sending request, key [{}], expTime [{}]", requestId, request.getKey(), responseMetaData.expTime);
  177 + if (messagesStats != null) messagesStats.incrementTotal();
169 178 requestTemplate.send(TopicPartitionInfo.builder().topic(requestTemplate.getDefaultTopic()).build(), request, new TbQueueCallback() {
170 179 @Override
171 180 public void onSuccess(TbQueueMsgMetadata metadata) {
  181 + if (messagesStats != null) messagesStats.incrementSuccessful();
172 182 log.trace("[{}] Request sent: {}", requestId, metadata);
173 183 }
174 184
175 185 @Override
176 186 public void onFailure(Throwable t) {
  187 + if (messagesStats != null) messagesStats.incrementFailed();
177 188 pendingRequests.remove(requestId);
178 189 future.setException(t);
179 190 }
... ...
... ... @@ -23,7 +23,7 @@ import org.thingsboard.server.queue.TbQueueHandler;
23 23 import org.thingsboard.server.queue.TbQueueMsg;
24 24 import org.thingsboard.server.queue.TbQueueProducer;
25 25 import org.thingsboard.server.queue.TbQueueResponseTemplate;
26   -import org.thingsboard.server.queue.stats.QueueStats;
  26 +import org.thingsboard.server.common.msg.stats.MessagesStats;
27 27
28 28 import java.util.List;
29 29 import java.util.UUID;
... ... @@ -45,7 +45,7 @@ public class DefaultTbQueueResponseTemplate<Request extends TbQueueMsg, Response
45 45 private final ExecutorService loopExecutor;
46 46 private final ScheduledExecutorService timeoutExecutor;
47 47 private final ExecutorService callbackExecutor;
48   - private final QueueStats stats;
  48 + private final MessagesStats stats;
49 49 private final int maxPendingRequests;
50 50 private final long requestTimeout;
51 51
... ... @@ -61,7 +61,7 @@ public class DefaultTbQueueResponseTemplate<Request extends TbQueueMsg, Response
61 61 long requestTimeout,
62 62 int maxPendingRequests,
63 63 ExecutorService executor,
64   - QueueStats stats) {
  64 + MessagesStats stats) {
65 65 this.requestTemplate = requestTemplate;
66 66 this.responseTemplate = responseTemplate;
67 67 this.pendingRequests = new ConcurrentHashMap<>();
... ...
... ... @@ -55,6 +55,9 @@ import org.thingsboard.server.queue.discovery.PartitionService;
55 55 import org.thingsboard.server.queue.discovery.TbServiceInfoProvider;
56 56 import org.thingsboard.server.queue.provider.TbQueueProducerProvider;
57 57 import org.thingsboard.server.queue.provider.TbTransportQueueFactory;
  58 +import org.thingsboard.server.common.msg.stats.MessagesStats;
  59 +import org.thingsboard.server.common.msg.stats.StatsFactory;
  60 +import org.thingsboard.server.common.msg.stats.StatsType;
58 61
59 62 import javax.annotation.PostConstruct;
60 63 import javax.annotation.PreDestroy;
... ... @@ -101,12 +104,17 @@ public class DefaultTransportService implements TransportService {
101 104 private final TbQueueProducerProvider producerProvider;
102 105 private final PartitionService partitionService;
103 106 private final TbServiceInfoProvider serviceInfoProvider;
  107 + private final StatsFactory statsFactory;
104 108
105 109 protected TbQueueRequestTemplate<TbProtoQueueMsg<TransportApiRequestMsg>, TbProtoQueueMsg<TransportApiResponseMsg>> transportApiRequestTemplate;
106 110 protected TbQueueProducer<TbProtoQueueMsg<ToRuleEngineMsg>> ruleEngineMsgProducer;
107 111 protected TbQueueProducer<TbProtoQueueMsg<ToCoreMsg>> tbCoreMsgProducer;
108 112 protected TbQueueConsumer<TbProtoQueueMsg<ToTransportMsg>> transportNotificationsConsumer;
109 113
  114 + protected MessagesStats ruleEngineProducerStats;
  115 + protected MessagesStats tbCoreProducerStats;
  116 + protected MessagesStats transportApiStats;
  117 +
110 118 protected ScheduledExecutorService schedulerExecutor;
111 119 protected ExecutorService transportCallbackExecutor;
112 120
... ... @@ -119,11 +127,12 @@ public class DefaultTransportService implements TransportService {
119 127 private ExecutorService mainConsumerExecutor = Executors.newSingleThreadExecutor(ThingsBoardThreadFactory.forName("transport-consumer"));
120 128 private volatile boolean stopped = false;
121 129
122   - public DefaultTransportService(TbServiceInfoProvider serviceInfoProvider, TbTransportQueueFactory queueProvider, TbQueueProducerProvider producerProvider, PartitionService partitionService) {
  130 + public DefaultTransportService(TbServiceInfoProvider serviceInfoProvider, TbTransportQueueFactory queueProvider, TbQueueProducerProvider producerProvider, PartitionService partitionService, StatsFactory statsFactory) {
123 131 this.serviceInfoProvider = serviceInfoProvider;
124 132 this.queueProvider = queueProvider;
125 133 this.producerProvider = producerProvider;
126 134 this.partitionService = partitionService;
  135 + this.statsFactory = statsFactory;
127 136 }
128 137
129 138 @PostConstruct
... ... @@ -133,10 +142,14 @@ public class DefaultTransportService implements TransportService {
133 142 new TbRateLimits(perTenantLimitsConf);
134 143 new TbRateLimits(perDevicesLimitsConf);
135 144 }
  145 + this.ruleEngineProducerStats = statsFactory.createMessagesStats(StatsType.RULE_ENGINE.getName() + ".producer");
  146 + this.tbCoreProducerStats = statsFactory.createMessagesStats(StatsType.CORE.getName() + ".producer");
  147 + this.transportApiStats = statsFactory.createMessagesStats(StatsType.TRANSPORT.getName() + ".producer");
136 148 this.schedulerExecutor = Executors.newSingleThreadScheduledExecutor(ThingsBoardThreadFactory.forName("transport-scheduler"));
137 149 this.transportCallbackExecutor = Executors.newWorkStealingPool(20);
138 150 this.schedulerExecutor.scheduleAtFixedRate(this::checkInactivityAndReportActivity, new Random().nextInt((int) sessionReportTimeout), sessionReportTimeout, TimeUnit.MILLISECONDS);
139 151 transportApiRequestTemplate = queueProvider.createTransportApiRequestTemplate();
  152 + transportApiRequestTemplate.setMessagesStats(transportApiStats);
140 153 ruleEngineMsgProducer = producerProvider.getRuleEngineMsgProducer();
141 154 tbCoreMsgProducer = producerProvider.getTbCoreMsgProducer();
142 155 transportNotificationsConsumer = queueProvider.createTransportNotificationsConsumer();
... ... @@ -557,10 +570,14 @@ public class DefaultTransportService implements TransportService {
557 570 if (log.isTraceEnabled()) {
558 571 log.trace("[{}][{}] Pushing to topic {} message {}", getTenantId(sessionInfo), getDeviceId(sessionInfo), tpi.getFullTopicName(), toDeviceActorMsg);
559 572 }
  573 + TransportTbQueueCallback transportTbQueueCallback = callback != null ?
  574 + new TransportTbQueueCallback(callback) : null;
  575 + tbCoreProducerStats.incrementTotal();
  576 + StatsCallback wrappedCallback = new StatsCallback(transportTbQueueCallback, tbCoreProducerStats);
560 577 tbCoreMsgProducer.send(tpi,
561 578 new TbProtoQueueMsg<>(getRoutingKey(sessionInfo),
562   - ToCoreMsg.newBuilder().setToDeviceActorMsg(toDeviceActorMsg).build()), callback != null ?
563   - new TransportTbQueueCallback(callback) : null);
  579 + ToCoreMsg.newBuilder().setToDeviceActorMsg(toDeviceActorMsg).build()),
  580 + wrappedCallback);
564 581 }
565 582
566 583 protected void sendToRuleEngine(TenantId tenantId, TbMsg tbMsg, TbQueueCallback callback) {
... ... @@ -571,7 +588,9 @@ public class DefaultTransportService implements TransportService {
571 588 ToRuleEngineMsg msg = ToRuleEngineMsg.newBuilder().setTbMsg(TbMsg.toByteString(tbMsg))
572 589 .setTenantIdMSB(tenantId.getId().getMostSignificantBits())
573 590 .setTenantIdLSB(tenantId.getId().getLeastSignificantBits()).build();
574   - ruleEngineMsgProducer.send(tpi, new TbProtoQueueMsg<>(tbMsg.getId(), msg), callback);
  591 + ruleEngineProducerStats.incrementTotal();
  592 + StatsCallback wrappedCallback = new StatsCallback(callback, ruleEngineProducerStats);
  593 + ruleEngineMsgProducer.send(tpi, new TbProtoQueueMsg<>(tbMsg.getId(), msg), wrappedCallback);
575 594 }
576 595
577 596 private class TransportTbQueueCallback implements TbQueueCallback {
... ... @@ -592,6 +611,30 @@ public class DefaultTransportService implements TransportService {
592 611 }
593 612 }
594 613
  614 + private class StatsCallback implements TbQueueCallback {
  615 + private final TbQueueCallback callback;
  616 + private final MessagesStats stats;
  617 +
  618 + private StatsCallback(TbQueueCallback callback, MessagesStats stats) {
  619 + this.callback = callback;
  620 + this.stats = stats;
  621 + }
  622 +
  623 + @Override
  624 + public void onSuccess(TbQueueMsgMetadata metadata) {
  625 + stats.incrementSuccessful();
  626 + if (callback != null)
  627 + callback.onSuccess(metadata);
  628 + }
  629 +
  630 + @Override
  631 + public void onFailure(Throwable t) {
  632 + stats.incrementFailed();
  633 + if (callback != null)
  634 + callback.onFailure(t);
  635 + }
  636 + }
  637 +
595 638 private class MsgPackCallback implements TbQueueCallback {
596 639 private final AtomicInteger msgCount;
597 640 private final TransportServiceCallback<Void> callback;
... ...
... ... @@ -14,8 +14,16 @@
14 14 # limitations under the License.
15 15 #
16 16
17   -spring.main.web-environment: false
18   -spring.main.web-application-type: none
  17 +# If you enabled process metrics you should also enable 'web-environment'.
  18 +spring.main.web-environment: "${WEB_APPLICATION_ENABLE:false}"
  19 +# If you enabled process metrics you should set 'web-application-type' to 'servlet' value.
  20 +spring.main.web-application-type: "${WEB_APPLICATION_TYPE:none}"
  21 +
  22 +server:
  23 + # Server bind address (has no effect if web-environment is disabled).
  24 + address: "${HTTP_BIND_ADDRESS:0.0.0.0}"
  25 + # Server bind port (has no effect if web-environment is disabled).
  26 + port: "${HTTP_BIND_PORT:8083}"
19 27
20 28 # Zookeeper connection parameters. Used for service discovery.
21 29 zk:
... ... @@ -211,4 +219,16 @@ service:
211 219 type: "${TB_SERVICE_TYPE:tb-transport}"
212 220 # Unique id for this service (autogenerated if empty)
213 221 id: "${TB_SERVICE_ID:}"
214   - tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
\ No newline at end of file
  222 + tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
  223 +
  224 +
  225 +metrics:
  226 + # Enable/disable actuator metrics.
  227 + enabled: "${METRICS_ENABLED:false}"
  228 +
  229 +management:
  230 + endpoints:
  231 + web:
  232 + exposure:
  233 + # Expose metrics endpoint (use value 'prometheus' to enable prometheus metrics).
  234 + include: '${METRICS_ENDPOINTS_EXPOSE:info}'
\ No newline at end of file
... ...
... ... @@ -212,4 +212,16 @@ service:
212 212 type: "${TB_SERVICE_TYPE:tb-transport}"
213 213 # Unique id for this service (autogenerated if empty)
214 214 id: "${TB_SERVICE_ID:}"
215   - tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
\ No newline at end of file
  215 + tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
  216 +
  217 +
  218 +metrics:
  219 + # Enable/disable actuator metrics.
  220 + enabled: "${METRICS_ENABLED:false}"
  221 +
  222 +management:
  223 + endpoints:
  224 + web:
  225 + exposure:
  226 + # Expose metrics endpoint (use value 'prometheus' to enable prometheus metrics).
  227 + include: '${METRICS_ENDPOINTS_EXPOSE:info}'
\ No newline at end of file
... ...
... ... @@ -14,8 +14,16 @@
14 14 # limitations under the License.
15 15 #
16 16
17   -spring.main.web-environment: false
18   -spring.main.web-application-type: none
  17 +# If you enabled process metrics you should also enable 'web-environment'.
  18 +spring.main.web-environment: "${WEB_APPLICATION_ENABLE:false}"
  19 +# If you enabled process metrics you should set 'web-application-type' to 'servlet' value.
  20 +spring.main.web-application-type: "${WEB_APPLICATION_TYPE:none}"
  21 +
  22 +server:
  23 + # Server bind address (has no effect if web-environment is disabled).
  24 + address: "${HTTP_BIND_ADDRESS:0.0.0.0}"
  25 + # Server bind port (has no effect if web-environment is disabled).
  26 + port: "${HTTP_BIND_PORT:8083}"
19 27
20 28 # Zookeeper connection parameters. Used for service discovery.
21 29 zk:
... ... @@ -232,4 +240,15 @@ service:
232 240 type: "${TB_SERVICE_TYPE:tb-transport}"
233 241 # Unique id for this service (autogenerated if empty)
234 242 id: "${TB_SERVICE_ID:}"
235   - tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
\ No newline at end of file
  243 + tenant_id: "${TB_SERVICE_TENANT_ID:}" # empty or specific tenant id.
  244 +
  245 +metrics:
  246 + # Enable/disable actuator metrics.
  247 + enabled: "${METRICS_ENABLED:false}"
  248 +
  249 +management:
  250 + endpoints:
  251 + web:
  252 + exposure:
  253 + # Expose metrics endpoint (use value 'prometheus' to enable prometheus metrics).
  254 + include: '${METRICS_ENDPOINTS_EXPOSE:info}'
\ No newline at end of file
... ...