-
Notifications
You must be signed in to change notification settings - Fork 4
/
P6Factory.java
126 lines (115 loc) · 3.59 KB
/
P6Factory.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package com.xqbase.metric.util;
import java.sql.SQLException;
import java.sql.SQLIntegrityConstraintViolationException;
import com.p6spy.engine.common.PreparedStatementInformation;
import com.p6spy.engine.common.StatementInformation;
import com.p6spy.engine.event.JdbcEventListener;
import com.p6spy.engine.event.SimpleJdbcEventListener;
import com.p6spy.engine.spy.P6SpyFactory;
import com.xqbase.metric.common.Metric;
import com.xqbase.util.Log;
public class P6Factory extends P6SpyFactory {
private static final int SLOW_THRESHOLD = 1000;
private static String removeQuote(String s) {
int len = s.length();
return len > 2 && s.charAt(0) == '`' && s.charAt(len - 1) == '`' ?
s.substring(1, len - 1) : s;
}
static void log(String sql_, int elapsed, int rowCount, SQLException e) {
// Handle Error
if (e != null) {
if (e instanceof SQLIntegrityConstraintViolationException) {
// Log.d("Update Failure: " + sql_ + ", " + elapsed + "ms, " + e);
} else {
Log.w("Execute Failure: " + sql_ + ", " + elapsed + "ms, " + e);
}
return;
}
// Parse Command
String sql = sql_.trim().replaceAll("\\s+", " ");
if (sql.isEmpty()) {
return;
}
int space = sql.indexOf(' ');
if (space < 0) {
Log.i("DDL or DCL: " + sql);
return;
}
if (elapsed > SLOW_THRESHOLD) {
Log.w("Slow SQL (" + elapsed + "ms): " + sql);
}
String cmd = sql.substring(0, space).toUpperCase();
switch (cmd) {
case "SELECT":
case "DELETE":
int from = sql.toUpperCase().indexOf(" FROM ", space - 1);
if (from < 0) {
Log.w("Invalid " + cmd + ": " + sql);
return;
}
space = from + 6;
break;
case "INSERT":
case "UPDATE":
case "REPLACE":
int into = sql.toUpperCase().indexOf(" INTO ", space - 1);
space = into < 0 ? space + 1 : into + 6;
break;
default:
Log.i("DDL or DCL: " + sql);
return;
}
// Parse Schema and Table
String table;
int comma = sql.indexOf(',', space);
int space2 = sql.indexOf(' ', space);
if (comma < 0) {
if (space2 < 0) {
table = sql.substring(space);
} else {
table = sql.substring(space, space2);
}
} else if (space2 < 0) {
table = sql.substring(space, comma);
} else {
table = sql.substring(space, Math.min(comma, space2));
}
table = table.toLowerCase();
String schema = "_";
int dot = table.indexOf('.');
if (dot > 0) {
schema = removeQuote(table.substring(0, dot));
table = table.substring(dot + 1);
}
table = removeQuote(table);
Metric.put("metric.sql.elapsed", elapsed,
"schema", schema, "table", table, "command", cmd);
if (!cmd.equals("SELECT")) {
Metric.put("metric.sql.rowcount", rowCount,
"schema", schema, "table", table, "command", cmd);
}
}
static int MILLIS(long timeElapsedNanos) {
return (int) (timeElapsedNanos / 1_000_000);
}
@Override
public JdbcEventListener getJdbcEventListener() {
return new SimpleJdbcEventListener() {
@Override
public void onAfterExecuteUpdate(PreparedStatementInformation psi,
long timeElapsedNanos, int rowCount, SQLException e) {
log(psi.getSqlWithValues(), MILLIS(timeElapsedNanos), rowCount, e);
}
@Override
public void onAfterExecuteUpdate(StatementInformation si,
long timeElapsedNanos, String sql, int rowCount, SQLException e) {
log(sql, MILLIS(timeElapsedNanos), rowCount, e);
}
@Override
public void onAfterAnyExecute(StatementInformation si,
long timeElapsedNanos, SQLException e) {
log(si.getSqlWithValues(), MILLIS(timeElapsedNanos), 0, e);
}
};
}
}