diff --git a/apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java b/apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java index 0582108bf4e8251923a86d01545e6a637dbbb440..1c6fac89b18885526bde1b805ba7dd5c3270a550 100644 --- a/apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java +++ b/apm-network/src/main/java/org/skywalking/apm/network/trace/component/ComponentsDefine.java @@ -67,6 +67,8 @@ public class ComponentsDefine { public static final OfficialComponent SHARDING_JDBC = new OfficialComponent(21, "ShardingJDBC"); + public static final OfficialComponent POSTGRESQL = new OfficialComponent(22, "PostgreSQL"); + private static ComponentsDefine instance = new ComponentsDefine(); private String[] components; @@ -76,7 +78,7 @@ public class ComponentsDefine { } public ComponentsDefine() { - components = new String[22]; + components = new String[23]; addComponent(TOMCAT); addComponent(HTTPCLIENT); addComponent(DUBBO); @@ -98,6 +100,7 @@ public class ComponentsDefine { addComponent(JETTY_SERVER); addComponent(MEMCACHED); addComponent(SHARDING_JDBC); + addComponent(POSTGRESQL); } private void addComponent(OfficialComponent component) { diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/pom.xml b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/pom.xml index f3bdd0c048a42215c647e538aebc24bd1284e560..c271e7fa74fa42fbc3efd24e2e45d66ed311849a 100755 --- a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/pom.xml +++ b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/pom.xml @@ -60,6 +60,12 @@ [2.0.14,6.0.6] provided + + org.postgresql + postgresql + 42.0.0 + provided + diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/PostgreSQLURLParser.java b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/PostgreSQLURLParser.java new file mode 100644 index 0000000000000000000000000000000000000000..3ce8604232e614209504fac3566905d5b1fadb66 --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/PostgreSQLURLParser.java @@ -0,0 +1,85 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.connectionurl.parser; + +import org.skywalking.apm.network.trace.component.ComponentsDefine; +import org.skywalking.apm.plugin.jdbc.ConnectionInfo; + +/** + * {@link PostgreSQLURLParser} parse connection url of mysql. + *

+ * The {@link ConnectionInfo#host} be set the string between charset "//" and the first + * charset "/" after the charset "//", and {@link ConnectionInfo#databaseName} be set the + * string between the last index of "/" and the first charset "?". but one more thing, the + * {@link ConnectionInfo#hosts} be set if the host container multiple host. + * + * @author zhangxin + */ +public class PostgreSQLURLParser extends AbstractURLParser { + + private static final int DEFAULT_PORT = 5432; + private static final String DB_TYPE = "PostgreSQL"; + + public PostgreSQLURLParser(String url) { + super(url); + } + + @Override + protected int[] fetchDatabaseHostsIndexRange() { + int hostLabelStartIndex = url.indexOf("//"); + int hostLabelEndIndex = url.indexOf("/", hostLabelStartIndex + 2); + return new int[] {hostLabelStartIndex + 2, hostLabelEndIndex}; + } + + @Override + protected int[] fetchDatabaseNameIndexRange() { + int databaseStartTag = url.lastIndexOf("/"); + int databaseEndTag = url.indexOf("?", databaseStartTag); + if (databaseEndTag == -1) { + databaseEndTag = url.length(); + } + return new int[] {databaseStartTag + 1, databaseEndTag}; + } + + @Override + public ConnectionInfo parse() { + int[] hostRangeIndex = fetchDatabaseHostsIndexRange(); + String hosts = url.substring(hostRangeIndex[0], hostRangeIndex[1]); + String[] hostSegment = hosts.split(","); + if (hostSegment.length > 1) { + StringBuilder sb = new StringBuilder(); + for (String host : hostSegment) { + if (host.split(":").length == 1) { + sb.append(host + ":" + DEFAULT_PORT + ","); + } else { + sb.append(host + ","); + } + } + return new ConnectionInfo(ComponentsDefine.POSTGRESQL, DB_TYPE, sb.toString(), fetchDatabaseNameFromURL()); + } else { + String[] hostAndPort = hostSegment[0].split(":"); + if (hostAndPort.length != 1) { + return new ConnectionInfo(ComponentsDefine.POSTGRESQL, DB_TYPE, hostAndPort[0], Integer.valueOf(hostAndPort[1]), fetchDatabaseNameFromURL()); + } else { + return new ConnectionInfo(ComponentsDefine.POSTGRESQL, DB_TYPE, hostAndPort[0], DEFAULT_PORT, fetchDatabaseNameFromURL()); + } + } + } + +} diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java index ff52f80ec575d4e82150e963e44ee8bd04a8194a..ffc5ac051028368aa2f5056652c4a9348c25e5bc 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java +++ b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java @@ -21,8 +21,8 @@ package org.skywalking.apm.plugin.jdbc.connectionurl.parser; import org.skywalking.apm.plugin.jdbc.ConnectionInfo; /** - * {@link URLParser#parser(String)} support parse the connection url, such as Mysql, Oracle, H2 Database. - * But there are some url cannot be parsed, such as Oracle connection url with multiple host. + * {@link URLParser#parser(String)} support parse the connection url, such as Mysql, Oracle, H2 Database. But there are + * some url cannot be parsed, such as Oracle connection url with multiple host. * * @author zhangxin */ @@ -31,6 +31,7 @@ public class URLParser { private static final String MYSQL_JDBC_URL_PREFIX = "jdbc:mysql"; private static final String ORACLE_JDBC_URL_PREFIX = "jdbc:oracle"; private static final String H2_JDBC_URL_PREFIX = "jdbc:h2"; + private static final String POSTGRESQL_JDBC_URL_PREFIX = "jdbc:postgresql"; public static ConnectionInfo parser(String url) { ConnectionURLParser parser = null; @@ -40,6 +41,8 @@ public class URLParser { parser = new OracleURLParser(url); } else if (url.startsWith(H2_JDBC_URL_PREFIX)) { parser = new H2URLParser(url); + } else if (url.startsWith(POSTGRESQL_JDBC_URL_PREFIX)) { + parser = new PostgreSQLURLParser(url); } return parser.parse(); } diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/define/PostgreSQLInstrumentation.java b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/define/PostgreSQLInstrumentation.java new file mode 100644 index 0000000000000000000000000000000000000000..99e249a64f416c008535662638da94af57de125c --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/java/org/skywalking/apm/plugin/jdbc/define/PostgreSQLInstrumentation.java @@ -0,0 +1,35 @@ +/* + * Copyright 2017, OpenSkywalking Organization All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Project repository: https://github.com/OpenSkywalking/skywalking + */ + +package org.skywalking.apm.plugin.jdbc.define; + +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; + +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; + +/** + * {@link PostgreSQLInstrumentation} presents that skywalking intercepts {@link org.postgresql.Driver}. + * + * @author zhangxin + */ +public class PostgreSQLInstrumentation extends AbstractDatabaseInstrumentation { + @Override + protected ClassMatch enhanceClass() { + return byName("org.postgresql.Driver"); + } +} diff --git a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/resources/skywalking-plugin.def b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/resources/skywalking-plugin.def index c946b2924ce5670b02a3d72778a9edb636d57501..a28a6c03dab0588174ea364ba15413abdd791517 100644 --- a/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/resources/skywalking-plugin.def +++ b/apm-sniffer/apm-sdk-plugin/jdbc-plugin/src/main/resources/skywalking-plugin.def @@ -1,3 +1,4 @@ jdbc=org.skywalking.apm.plugin.jdbc.define.H2Instrumentation jdbc=org.skywalking.apm.plugin.jdbc.define.MysqlInstrumentation -jdbc=org.skywalking.apm.plugin.jdbc.define.OracleInstrumentation \ No newline at end of file +jdbc=org.skywalking.apm.plugin.jdbc.define.OracleInstrumentation +jdbc=org.skywalking.apm.plugin.jdbc.define.PostgreSQLInstrumentation