Skip to content

Commit 57e4552

Browse files
committed
Merge pull request 'Make StatsCollector work for more JDBC drivers' (#15) from feature/23653 into develop
2 parents f16b4c1 + f4f86fb commit 57e4552

File tree

4 files changed

+103
-72
lines changed

4 files changed

+103
-72
lines changed

pom.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,20 @@
227227
<artifactId>monetdb-jdbc</artifactId>
228228
<version>2.28</version>
229229
</dependency>
230+
<dependency>
231+
<groupId>com.microsoft.sqlserver</groupId>
232+
<artifactId>mssql-jdbc</artifactId>
233+
</dependency>
234+
<dependency>
235+
<groupId>org.postgresql</groupId>
236+
<artifactId>postgresql</artifactId>
237+
</dependency>
238+
<dependency>
239+
<groupId>mysql</groupId>
240+
<artifactId>mysql-connector-java</artifactId>
241+
</dependency>
242+
243+
230244

231245
<!-- Kubernetes -->
232246
<dependency>

src/main/java/eu/openanalytics/containerproxy/stat/StatCollectorRegistry.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import eu.openanalytics.containerproxy.service.EventService;
3535
import eu.openanalytics.containerproxy.service.EventService.Event;
3636
import eu.openanalytics.containerproxy.stat.impl.InfluxDBCollector;
37-
import eu.openanalytics.containerproxy.stat.impl.MonetDBCollector;
37+
import eu.openanalytics.containerproxy.stat.impl.JDBCCollector;
3838

3939
@Service
4040
public class StatCollectorRegistry implements Consumer<Event> {
@@ -76,8 +76,8 @@ private IStatCollector findCollector(String baseURL) {
7676
if (baseURL == null || baseURL.isEmpty()) return null;
7777
if (baseURL.toLowerCase().contains("/write?db=")) {
7878
return new InfluxDBCollector();
79-
} else if (baseURL.toLowerCase().startsWith("jdbc:monetdb")) {
80-
return new MonetDBCollector();
79+
} else if (baseURL.toLowerCase().startsWith("jdbc")) {
80+
return new JDBCCollector();
8181
}
8282
return null;
8383
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* ContainerProxy
3+
*
4+
* Copyright (C) 2016-2020 Open Analytics
5+
*
6+
* ===========================================================================
7+
*
8+
* This program is free software: you can redistribute it and/or modify
9+
* it under the terms of the Apache License as published by
10+
* The Apache Software Foundation, either version 2 of the License, or
11+
* (at your option) any later version.
12+
*
13+
* This program is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* Apache License for more details.
17+
*
18+
* You should have received a copy of the Apache License
19+
* along with this program. If not, see <http://www.apache.org/licenses/>
20+
*/
21+
package eu.openanalytics.containerproxy.stat.impl;
22+
23+
import java.io.IOException;
24+
import java.sql.Connection;
25+
import java.sql.DriverManager;
26+
import java.sql.PreparedStatement;
27+
import java.sql.SQLException;
28+
import java.sql.Timestamp;
29+
30+
import org.springframework.core.env.Environment;
31+
32+
import eu.openanalytics.containerproxy.service.EventService.Event;
33+
import eu.openanalytics.containerproxy.stat.IStatCollector;
34+
35+
/**
36+
*
37+
* # MonetDB, Postgresql, MySQL/MariaDB usage-stats-url:
38+
* jdbc:monetdb://localhost:50000/usage_stats usage-stats-url:
39+
* jdbc:postgresql://localhost/postgres usage-stats-url:
40+
* jdbc:mysql://localhost/shinyproxy
41+
*
42+
* Assumed table layout:
43+
*
44+
* create table event( event_time timestamp, username varchar(128), type
45+
* varchar(128), data text );
46+
*
47+
*
48+
* # MS SQL Server usage-stats-url:
49+
* jdbc:sqlserver://localhost;databaseName=shinyproxy
50+
*
51+
* Assumed table layout:
52+
*
53+
* create table event( event_time datetime, username varchar(128), type
54+
* varchar(128), data text );
55+
*
56+
*/
57+
public class JDBCCollector implements IStatCollector {
58+
59+
private Connection conn;
60+
61+
@Override
62+
public void accept(Event event, Environment env) throws IOException {
63+
synchronized (this) {
64+
String baseURL = env.getProperty("proxy.usage-stats-url");
65+
String username = env.getProperty("proxy.usage-stats-username", "monetdb");
66+
String password = env.getProperty("proxy.usage-stats-password", "monetdb");
67+
try {
68+
if (conn == null || conn.isClosed()) {
69+
conn = DriverManager.getConnection(baseURL, username, password);
70+
}
71+
} catch (SQLException e) {
72+
throw new IOException("Failed to connect to " + baseURL, e);
73+
}
74+
}
75+
String sql = "INSERT INTO event(event_time, username, type, data) VALUES (?,?,?,?)";
76+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
77+
stmt.setTimestamp(1, new Timestamp(System.currentTimeMillis()));
78+
stmt.setString(2, event.user);
79+
stmt.setString(3, event.type);
80+
stmt.setString(4, event.data);
81+
stmt.executeUpdate();
82+
} catch (SQLException e) {
83+
throw new IOException("Exception while loggin stats", e);
84+
}
85+
}
86+
}

src/main/java/eu/openanalytics/containerproxy/stat/impl/MonetDBCollector.java

Lines changed: 0 additions & 69 deletions
This file was deleted.

0 commit comments

Comments
 (0)