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 @Override
45 public void collect() throws NamingException, InterruptedException {
46 long currentTime = System.currentTimeMillis();
47 if (containerWrapper == null) {
48 logger.error("Cannot collect data source stats. Container wrapper is not set.");
49 } else {
50 for (ApplicationResource ds : containerWrapper.getDataSources()) {
51 String appName = ds.getApplicationName();
52 String name = (appName == null ? "" : appName) + '/' + ds.getName();
53 DataSourceInfo dsi = ds.getDataSourceInfo();
54 int numEstablished = dsi.getEstablishedConnections();
55 int numBusy = dsi.getBusyConnections();
56 logger.trace("Collecting stats for datasource: {}", name);
57 buildAbsoluteStats(PREFIX_ESTABLISHED + name, numEstablished, currentTime);
58 buildAbsoluteStats(PREFIX_BUSY + name, numBusy, currentTime);
59 }
60 logger.debug("datasource stats collected in {}ms", System.currentTimeMillis() - currentTime);
61 }
62 }
63
64
65
66
67
68
69 public void reset() throws NamingException {
70 if (containerWrapper == null) {
71 logger.error("Cannot reset application stats. Container wrapper is not set.");
72 } else {
73 for (ApplicationResource ds : containerWrapper.getDataSources()) {
74 reset(ds.getName());
75 }
76 }
77 }
78
79
80
81
82
83
84 public void reset(String name) {
85 resetStats(PREFIX_ESTABLISHED + name);
86 resetStats(PREFIX_BUSY + name);
87 }
88
89
90
91
92
93
94
95 public void setMaxSeries(
96 @Value("${psiprobe.beans.stats.collectors.datasource.period}") String period,
97 @Value("${psiprobe.beans.stats.collectors.datasource.span}") String span) {
98 super.setMaxSeries((int) TimeExpression.dataPoints(period, span));
99 }
100
101 }