From a12b1a9872073b6557f90a18937b350a8f567f2d Mon Sep 17 00:00:00 2001 From: ani-sha Date: Fri, 24 Nov 2023 12:56:34 +0530 Subject: [PATCH] DBZ-6764 Connector metrics implementation for REST extension --- .../resources/META-INF/mongodb-attributes.txt | 3 + .../resources/META-INF/mysql-attributes.txt | 3 + .../resources/META-INF/oracle-attributes.txt | 3 + .../META-INF/postgres-attributes.txt | 3 + .../META-INF/sqlserver-attributes.txt | 3 + debezium-core/pom.xml | 8 +++ .../rest/jolokia/JolokiaAttributes.java | 42 +++++++++++++ .../debezium/rest/jolokia/JolokiaClient.java | 60 +++++++++++++++++++ pom.xml | 3 + 9 files changed, 128 insertions(+) create mode 100644 debezium-connector-mongodb/src/main/resources/META-INF/mongodb-attributes.txt create mode 100644 debezium-connector-mysql/src/main/resources/META-INF/mysql-attributes.txt create mode 100644 debezium-connector-oracle/src/main/resources/META-INF/oracle-attributes.txt create mode 100644 debezium-connector-postgres/src/main/resources/META-INF/postgres-attributes.txt create mode 100644 debezium-connector-sqlserver/src/main/resources/META-INF/sqlserver-attributes.txt create mode 100644 debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaAttributes.java create mode 100644 debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaClient.java diff --git a/debezium-connector-mongodb/src/main/resources/META-INF/mongodb-attributes.txt b/debezium-connector-mongodb/src/main/resources/META-INF/mongodb-attributes.txt new file mode 100644 index 000000000..a9f39e8f7 --- /dev/null +++ b/debezium-connector-mongodb/src/main/resources/META-INF/mongodb-attributes.txt @@ -0,0 +1,3 @@ +Connected +MilliSecondsSinceLastEvent +TotalNumberOfEventsSeen \ No newline at end of file diff --git a/debezium-connector-mysql/src/main/resources/META-INF/mysql-attributes.txt b/debezium-connector-mysql/src/main/resources/META-INF/mysql-attributes.txt new file mode 100644 index 000000000..a9f39e8f7 --- /dev/null +++ b/debezium-connector-mysql/src/main/resources/META-INF/mysql-attributes.txt @@ -0,0 +1,3 @@ +Connected +MilliSecondsSinceLastEvent +TotalNumberOfEventsSeen \ No newline at end of file diff --git a/debezium-connector-oracle/src/main/resources/META-INF/oracle-attributes.txt b/debezium-connector-oracle/src/main/resources/META-INF/oracle-attributes.txt new file mode 100644 index 000000000..a9f39e8f7 --- /dev/null +++ b/debezium-connector-oracle/src/main/resources/META-INF/oracle-attributes.txt @@ -0,0 +1,3 @@ +Connected +MilliSecondsSinceLastEvent +TotalNumberOfEventsSeen \ No newline at end of file diff --git a/debezium-connector-postgres/src/main/resources/META-INF/postgres-attributes.txt b/debezium-connector-postgres/src/main/resources/META-INF/postgres-attributes.txt new file mode 100644 index 000000000..a9f39e8f7 --- /dev/null +++ b/debezium-connector-postgres/src/main/resources/META-INF/postgres-attributes.txt @@ -0,0 +1,3 @@ +Connected +MilliSecondsSinceLastEvent +TotalNumberOfEventsSeen \ No newline at end of file diff --git a/debezium-connector-sqlserver/src/main/resources/META-INF/sqlserver-attributes.txt b/debezium-connector-sqlserver/src/main/resources/META-INF/sqlserver-attributes.txt new file mode 100644 index 000000000..a9f39e8f7 --- /dev/null +++ b/debezium-connector-sqlserver/src/main/resources/META-INF/sqlserver-attributes.txt @@ -0,0 +1,3 @@ +Connected +MilliSecondsSinceLastEvent +TotalNumberOfEventsSeen \ No newline at end of file diff --git a/debezium-core/pom.xml b/debezium-core/pom.xml index 4e69197ba..e868a00d5 100644 --- a/debezium-core/pom.xml +++ b/debezium-core/pom.xml @@ -48,6 +48,14 @@ jackson-datatype-jsr310 + + + + org.jolokia + jolokia-client-java + ${version.jolokia.client} + + io.opentelemetry diff --git a/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaAttributes.java b/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaAttributes.java new file mode 100644 index 000000000..2cb5d6500 --- /dev/null +++ b/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaAttributes.java @@ -0,0 +1,42 @@ +/* + * Copyright Debezium Authors. + * + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.debezium.rest.jolokia; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class JolokiaAttributes { + private List attributeNames = new ArrayList<>(); + + public JolokiaAttributes(String attributesFilePath) { + try (InputStream stream = getClass().getClassLoader().getResourceAsStream(attributesFilePath)) { + if (stream != null) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))) { + String attribute; + while ((attribute = reader.readLine()) != null) { + attributeNames.add(attribute); + } + } + } + else { + attributeNames = Collections.emptyList(); + } + } + catch (IOException e) { + throw new RuntimeException(e); + } + } + + public List getAttributeNames() { + return attributeNames; + } +} diff --git a/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaClient.java b/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaClient.java new file mode 100644 index 000000000..0a23714d4 --- /dev/null +++ b/debezium-core/src/main/java/io/debezium/rest/jolokia/JolokiaClient.java @@ -0,0 +1,60 @@ +/* + * Copyright Debezium Authors. + * + * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0 + */ +package io.debezium.rest.jolokia; + +import java.util.ArrayList; +import java.util.List; + +import javax.management.MalformedObjectNameException; + +import org.jolokia.client.BasicAuthenticator; +import org.jolokia.client.J4pClient; +import org.jolokia.client.exception.J4pException; +import org.jolokia.client.request.J4pReadRequest; +import org.jolokia.client.request.J4pReadResponse; +import org.json.simple.JSONObject; + +public class JolokiaClient { + public static final Integer DEFAULT_JOLOKIA_PORT = 8778; + public static final String DEFAULT_JOLOKIA_URL = "http://localhost:" + DEFAULT_JOLOKIA_PORT + "/jolokia"; + + public List getConnectorMetrics(String type, String server, Integer taskMax, List attributes) { + List metrics = new ArrayList<>(); + try { + J4pClient client = createJolokiaClient(); + String baseMbeanName = String.format("debezium.%s:type=connector-metrics,context=streaming,server=%s", type, server); + + if ("sql_server".equals(type) || "mongodb".equals(type)) { + for (int task = 0; task < Math.max(1, taskMax); task++) { + String mbeanName = String.format("%s,task=%s", baseMbeanName, task); + addMetrics(client, metrics, mbeanName, attributes); + } + } + else { + addMetrics(client, metrics, baseMbeanName, attributes); + } + } + catch (J4pException | MalformedObjectNameException e) { + throw new RuntimeException(e); + } + return metrics; + } + + private void addMetrics(J4pClient client, List metrics, String mbeanName, List attributes) throws J4pException, MalformedObjectNameException { + for (String attribute : attributes) { + J4pReadRequest request = new J4pReadRequest(mbeanName, attribute); + J4pReadResponse response = client.execute(request); + metrics.add(response.asJSONObject()); + } + } + + private J4pClient createJolokiaClient() { + return J4pClient.url(JolokiaClient.DEFAULT_JOLOKIA_URL) + .authenticator(new BasicAuthenticator().preemptive()) + .connectionTimeout(3000) + .build(); + } +} diff --git a/pom.xml b/pom.xml index 1df823787..5f5ea946d 100644 --- a/pom.xml +++ b/pom.xml @@ -155,6 +155,9 @@ container-registry.oracle.com/mysql/community-server + + + 1.7.2