1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans.stats.collectors;
12
13 import jakarta.inject.Inject;
14
15 import javax.naming.NamingException;
16
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19 import org.springframework.beans.factory.annotation.Value;
20
21 import psiprobe.beans.ContainerWrapperBean;
22 import psiprobe.model.ApplicationResource;
23 import psiprobe.model.DataSourceInfo;
24 import psiprobe.tools.TimeExpression;
25
26
27
28
29 public class DatasourceStatsCollectorBean extends AbstractStatsCollectorBean {
30
31
32 private static final String PREFIX_ESTABLISHED = "ds.est.";
33
34
35 private static final String PREFIX_BUSY = "ds.busy.";
36
37
38 private static final Logger logger = LoggerFactory.getLogger(DatasourceStatsCollectorBean.class);
39
40
41 @Inject
42 private ContainerWrapperBean containerWrapper;
43
44
45
46
47
48
49 public ContainerWrapperBean getContainerWrapper() {
50 return containerWrapper;
51 }
52
53
54
55
56
57
58 public void setContainerWrapper(ContainerWrapperBean containerWrapper) {
59 this.containerWrapper = containerWrapper;
60 }
61
62 @Override
63 public void collect() throws NamingException, InterruptedException {
64 long currentTime = System.currentTimeMillis();
65 if (containerWrapper == null) {
66 logger.error("Cannot collect data source stats. Container wrapper is not set.");
67 } else {
68 for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
69 String appName = ds.getApplicationName();
70 String name = (appName == null ? "" : appName) + '/' + ds.getName();
71 DataSourceInfo dsi = ds.getDataSourceInfo();
72 int numEstablished = dsi.getEstablishedConnections();
73 int numBusy = dsi.getBusyConnections();
74 logger.trace("Collecting stats for datasource: {}", name);
75 buildAbsoluteStats(PREFIX_ESTABLISHED + name, numEstablished, currentTime);
76 buildAbsoluteStats(PREFIX_BUSY + name, numBusy, currentTime);
77 }
78 logger.debug("datasource stats collected in {}ms", System.currentTimeMillis() - currentTime);
79 }
80 }
81
82
83
84
85
86
87 public void reset() throws NamingException {
88 if (containerWrapper == null) {
89 logger.error("Cannot reset application stats. Container wrapper is not set.");
90 } else {
91 for (ApplicationResource ds : getContainerWrapper().getDataSources()) {
92 reset(ds.getName());
93 }
94 }
95 }
96
97
98
99
100
101
102 public void reset(String name) {
103 resetStats(PREFIX_ESTABLISHED + name);
104 resetStats(PREFIX_BUSY + name);
105 }
106
107
108
109
110
111
112
113 public void setMaxSeries(
114 @Value("${psiprobe.beans.stats.collectors.datasource.period}") long period,
115 @Value("${psiprobe.beans.stats.collectors.datasource.span}") long span) {
116 super.setMaxSeries((int) TimeExpression.dataPoints(period, span));
117 }
118
119 }