Skip to content

Commit 7558d73

Browse files
committed
[Feature] Add URLParser for dameng(DM)
1 parent 529d6d7 commit 7558d73

5 files changed

Lines changed: 169 additions & 0 deletions

File tree

apm-protocol/apm-network/src/main/java/org/apache/skywalking/apm/network/trace/component/ComponentsDefine.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,4 +262,7 @@ public class ComponentsDefine {
262262
public static final OfficialComponent CAFFEINE = new OfficialComponent(160, "Caffeine");
263263

264264
public static final OfficialComponent THREAD_PER_TASK_EXECUTOR = new OfficialComponent(161, "ThreadPerTask-executor");
265+
266+
public static final OfficialComponent DMDB_JDBC_DRIVER = new OfficialComponent(162, "Dmdb-jdbc-driver");
267+
265268
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.plugin.jdbc.connectionurl.parser;
20+
21+
import org.apache.skywalking.apm.network.trace.component.ComponentsDefine;
22+
import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo;
23+
24+
public class DMURLParser extends AbstractURLParser {
25+
private static final int DEFAULT_PORT = 5236;
26+
private static final String DB_TYPE = "DM";
27+
private static final String URL_PARAMS_HOST_KEY = "host";
28+
private static final String URL_PARAMS_PORT_KEY = "port";
29+
private static final String URL_PARAMS_SCHEMA_KEY = "schema";
30+
31+
public DMURLParser(String url) {
32+
super(url);
33+
}
34+
35+
@Override
36+
protected URLLocation fetchDatabaseHostsIndexRange() {
37+
int hostLabelStartIndex = url.indexOf("//");
38+
if (hostLabelStartIndex == -1) {
39+
return new URLLocation(0, 0); // 格式错误
40+
}
41+
int hostLabelEndIndex = url.indexOf("?", hostLabelStartIndex + 2);
42+
if (hostLabelEndIndex == -1) {
43+
hostLabelEndIndex = url.length();
44+
}
45+
return new URLLocation(hostLabelStartIndex + 2, hostLabelEndIndex);
46+
}
47+
48+
@Override
49+
protected URLLocation fetchDatabaseNameIndexRange() {
50+
return new URLLocation(0, 0);
51+
}
52+
53+
@Override
54+
public ConnectionInfo parse() {
55+
URLLocation location = fetchDatabaseHostsIndexRange();
56+
String hostPortSegment = "";
57+
if (location.endIndex() > location.startIndex()) {
58+
hostPortSegment = url.substring(location.startIndex(), location.endIndex());
59+
}
60+
61+
String host = "";
62+
String port = "";
63+
64+
if (!hostPortSegment.isEmpty()) {
65+
String[] parts = hostPortSegment.split(":");
66+
if (parts.length >= 1) {
67+
host = parts[0];
68+
}
69+
if (parts.length == 2) {
70+
port = parts[1];
71+
}
72+
}
73+
74+
// 如果 host 或 port 没有从路径中获取到,则尝试从参数中获取
75+
if (host.isEmpty()) {
76+
host = fetchFromUrlParams(URL_PARAMS_HOST_KEY);
77+
}
78+
if (port == null || port.isEmpty()) {
79+
port = fetchFromUrlParams(URL_PARAMS_PORT_KEY);
80+
}
81+
82+
if (port == null || port.isEmpty()) {
83+
port = String.valueOf(DEFAULT_PORT);
84+
}
85+
86+
String schema = fetchFromUrlParams(URL_PARAMS_SCHEMA_KEY);
87+
88+
return new ConnectionInfo(
89+
ComponentsDefine.DMDB_JDBC_DRIVER,
90+
DB_TYPE,
91+
host,
92+
Integer.parseInt(port),
93+
schema == null ? "" : schema
94+
);
95+
}
96+
97+
/**
98+
* 从 URL 参数中提取指定 key 的值
99+
*/
100+
private String fetchFromUrlParams(String key) {
101+
int paramIndex = url.indexOf("?");
102+
if (paramIndex == -1) {
103+
return null;
104+
}
105+
String[] params = url.substring(paramIndex + 1).split("&");
106+
for (String pair : params) {
107+
if (pair.startsWith(key + "=")) {
108+
String[] segments = pair.split("=", 2);
109+
if (segments.length == 2) {
110+
return segments[1];
111+
}
112+
}
113+
}
114+
return null;
115+
}
116+
}

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/main/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParser.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public class URLParser {
4141
private static final String DB2_JDBC_URL_PREFIIX = "jdbc:db2:";
4242
private static final String SYBASE_JDBC_URL_PREFIX = "jdbc:sybase:tds:";
4343
private static final String OCEANBASE_JDBC_URL_PREFIX = "jdbc:oceanbase:";
44+
private static final String DM_JDBC_URL_PREFIX = "jdbc:dm:";
4445

4546
public static ConnectionInfo parser(String url) {
4647
ConnectionURLParser parser = null;
@@ -75,7 +76,10 @@ public static ConnectionInfo parser(String url) {
7576
parser = new SybaseURLParser(url);
7677
} else if (lowerCaseUrl.startsWith(OCEANBASE_JDBC_URL_PREFIX)) {
7778
parser = new OceanBaseURLParser(url);
79+
} else if (lowerCaseUrl.startsWith(DM_JDBC_URL_PREFIX)) {
80+
parser = new DMURLParser(url);
7881
}
82+
7983
return parser.parse();
8084
}
8185
}

apm-sniffer/apm-sdk-plugin/jdbc-commons/src/test/java/org/apache/skywalking/apm/plugin/jdbc/connectionurl/parser/URLParserTest.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,4 +415,49 @@ public void testParseSybaseJDBCURL() {
415415
assertThat(connectionInfo.getDatabaseName(), is("mydb"));
416416
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5000"));
417417
}
418+
419+
@Test
420+
public void testParseDMJDBCURLWithNamedParams()
421+
{
422+
ConnectionInfo connectionInfo = URLParser.parser("jdbc:dm://?host=localhost&port=5236");
423+
assertThat(connectionInfo.getDBType(), is("DM"));
424+
assertThat(connectionInfo.getDatabaseName(), is(""));
425+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5236"));
426+
}
427+
428+
@Test
429+
public void testParseDMJDBCURLWithNamedParamsAndScheme()
430+
{
431+
ConnectionInfo connectionInfo = URLParser.parser("jdbc:dm://?host=localhost&port=5236&schema=dm");
432+
assertThat(connectionInfo.getDBType(), is("DM"));
433+
assertThat(connectionInfo.getDatabaseName(), is("dm"));
434+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5236"));
435+
}
436+
437+
@Test
438+
public void testParseDMJDBCURLWithoutHost()
439+
{
440+
ConnectionInfo connectionInfo = URLParser.parser("jdbc:dm://localhost");
441+
assertThat(connectionInfo.getDBType(), is("DM"));
442+
assertThat(connectionInfo.getDatabaseName(), is(""));
443+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5236"));
444+
}
445+
446+
@Test
447+
public void testParseDMJDBCURLWithoutHostAndScheme()
448+
{
449+
ConnectionInfo connectionInfo = URLParser.parser("jdbc:dm://localhost?schema=dm");
450+
assertThat(connectionInfo.getDBType(), is("DM"));
451+
assertThat(connectionInfo.getDatabaseName(), is("dm"));
452+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5236"));
453+
}
454+
455+
@Test
456+
public void testParseDMJDBCURLWithoutHostPort()
457+
{
458+
ConnectionInfo connectionInfo = URLParser.parser("jdbc:dm://localhost:5237?schema=dm");
459+
assertThat(connectionInfo.getDBType(), is("DM"));
460+
assertThat(connectionInfo.getDatabaseName(), is("dm"));
461+
assertThat(connectionInfo.getDatabasePeer(), is("localhost:5237"));
462+
}
418463
}

docs/en/setup/service-agent/java-agent/Supported-list.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ The meter plugin provides the advanced metrics collections, which are not a part
185185
* [DB2](https://www.ibm.com/products/db2/database)
186186
* Sybase
187187
* [OceanBase](https://www.oceanbase.com/)
188+
* [DaMeng](https://www.dameng.com/)
188189
* Supported Connection Pool Frameworks
189190
* [Apache Commons DBCP](https://github.com/apache/commons-dbcp) 2.x
190191
* [Alibaba Druid](https://github.com/alibaba/druid) 1.x

0 commit comments

Comments
 (0)