You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@eventmesh.apache.org by "willimpo (via GitHub)" <gi...@apache.org> on 2023/10/17 11:17:40 UTC

[PR] [ISSUE #4415] Add Promethus source connector (eventmesh)

willimpo opened a new pull request, #4492:
URL: https://github.com/apache/eventmesh/pull/4492

   <!--
   ### Contribution Checklist
   
     - Name the pull request in the form "[ISSUE #XXXX] Title of the pull request", 
       where *XXXX* should be replaced by the actual issue number.
       Skip *[ISSUE #XXXX]* if there is no associated github issue for this pull request.
   
     - Fill out the template below to describe the changes contributed by the pull request. 
       That will give reviewers the context they need to do the review.
     
     - Each pull request should address only one issue. 
       Please do not mix up code from multiple issues.
     
     - Each commit in the pull request should have a meaningful commit message.
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, 
       leaving only the filled out template below.
   
   (The sections below can be removed for hotfixes of typos)
   -->
   
   <!--
   (If this PR fixes a GitHub issue, please add `Fixes #<XXX>` or `Closes #<XXX>`.)
   -->
   
   Fixes #<4415>.
   
   ### Motivation
   
   *Add Promethus source connector*
   
   
   ### Modifications
   
   *Add new source connector module for prometheus*
   
   
   
   ### Documentation
   
   - Does this pull request introduce a new feature? (yes / no)
   - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   - If a feature is not applicable for documentation, explain why?
   - If a feature is not documented yet in this PR, please create a followup issue for adding the documentation
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org


Re: [PR] [ISSUE #4415] Add Promethus source connector (eventmesh)

Posted by "willimpo (via GitHub)" <gi...@apache.org>.
willimpo commented on PR #4492:
URL: https://github.com/apache/eventmesh/pull/4492#issuecomment-1766221326

   @xwm1992 @Alonexc please make a review, thanks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: issues-help@eventmesh.apache.org


Re: [PR] [ISSUE #4415] Add Promethus source connector (eventmesh)

Posted by "willimpo (via GitHub)" <gi...@apache.org>.
willimpo commented on code in PR #4492:
URL: https://github.com/apache/eventmesh/pull/4492#discussion_r1362101300


##########
eventmesh-connectors/eventmesh-connector-prometheus/src/main/java/org/apache/eventmesh/connector/prometheus/source/connector/PrometheusSourceConnector.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.eventmesh.connector.prometheus.source.connector;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import java.nio.charset.StandardCharsets;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.eventmesh.connector.prometheus.model.QueryPrometheusReq;
+import org.apache.eventmesh.connector.prometheus.model.QueryPrometheusRsp;
+import org.apache.eventmesh.connector.prometheus.source.config.PrometheusSourceConfig;
+import org.apache.eventmesh.openconnect.api.config.Config;
+import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
+import org.apache.eventmesh.openconnect.api.connector.SourceConnectorContext;
+import org.apache.eventmesh.openconnect.api.source.Source;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordOffset;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordPartition;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.storage.OffsetStorageReader;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+
+@Slf4j
+public class PrometheusSourceConnector implements Source {
+
+    private static final String INSTANCE_ID = "instanceId";
+
+    private OffsetStorageReader offsetStorageReader;
+
+    private PrometheusSourceConfig sourceConfig;
+
+    private CloseableHttpClient httpClient;
+
+    private QueryPrometheusReq queryPrometheusReq;
+
+    private Long curStartTime;
+
+    private Long initTime;
+
+    private Integer interval;
+
+    private String url;
+
+    @Override
+    public Class<? extends Config> configClass() {
+        return PrometheusSourceConfig.class;
+    }
+
+    @Override
+    public void init(Config config) throws Exception {
+        this.sourceConfig = (PrometheusSourceConfig) config;
+        doInit();
+    }
+
+    @Override
+    public void init(ConnectorContext connectorContext) throws Exception {
+        SourceConnectorContext sourceConnectorContext = (SourceConnectorContext) connectorContext;
+        this.sourceConfig = (PrometheusSourceConfig) sourceConnectorContext.getSourceConfig();
+        this.offsetStorageReader = sourceConnectorContext.getOffsetStorageReader();
+
+        doInit();
+    }
+
+    private void doInit() {
+        queryPrometheusReq = new QueryPrometheusReq();
+        queryPrometheusReq.setQuery(sourceConfig.getConnectorConfig().getQuery());
+        queryPrometheusReq.setStep(sourceConfig.getConnectorConfig().getStep());
+
+        interval = sourceConfig.getConnectorConfig().getInterval();
+        initTime = sourceConfig.getConnectorConfig().getInitTime();
+
+        url = MessageFormat.format("{0}/{1}", sourceConfig.getConnectorConfig().getAddress(), sourceConfig.getConnectorConfig().getApi());
+
+        httpClient = HttpClientBuilder.create().build();
+    }
+
+    @Override
+    public void start() throws Exception {
+        log.info("prometheus source connector start.");
+
+        Map<String, String> partitionMap = Map.of(INSTANCE_ID, sourceConfig.getConnectorConfig().getConnectorId());
+        RecordPartition recordPartition = new RecordPartition(partitionMap);
+        RecordOffset recordOffset = offsetStorageReader.readOffset(recordPartition);
+        if (recordOffset != null) {
+            Long pollOffset = (Long) recordOffset.getOffset().get("queueOffset");
+            if (pollOffset != null) {
+                // use offset
+                curStartTime = pollOffset;
+            } else if (initTime != null) {
+                // use preset time
+                curStartTime = initTime;
+            } else {
+                // use real time
+                curStartTime = System.currentTimeMillis() / 1000;
+            }
+        }
+    }
+
+    @Override
+    public void commit(ConnectRecord record) {
+        curStartTime += interval;

Review Comment:
   yes, I should save end_time in offsetStorage for each time. thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: issues-help@eventmesh.apache.org


Re: [PR] [ISSUE #4415] Add Promethus source connector (eventmesh)

Posted by "pandaapo (via GitHub)" <gi...@apache.org>.
pandaapo commented on code in PR #4492:
URL: https://github.com/apache/eventmesh/pull/4492#discussion_r1361983997


##########
eventmesh-connectors/eventmesh-connector-prometheus/src/main/java/org/apache/eventmesh/connector/prometheus/source/connector/PrometheusSourceConnector.java:
##########
@@ -0,0 +1,188 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+
+package org.apache.eventmesh.connector.prometheus.source.connector;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.JSONObject;
+import java.nio.charset.StandardCharsets;
+import java.text.MessageFormat;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.eventmesh.connector.prometheus.model.QueryPrometheusReq;
+import org.apache.eventmesh.connector.prometheus.model.QueryPrometheusRsp;
+import org.apache.eventmesh.connector.prometheus.source.config.PrometheusSourceConfig;
+import org.apache.eventmesh.openconnect.api.config.Config;
+import org.apache.eventmesh.openconnect.api.connector.ConnectorContext;
+import org.apache.eventmesh.openconnect.api.connector.SourceConnectorContext;
+import org.apache.eventmesh.openconnect.api.source.Source;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.ConnectRecord;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordOffset;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.data.RecordPartition;
+import org.apache.eventmesh.openconnect.offsetmgmt.api.storage.OffsetStorageReader;
+import org.apache.http.HttpStatus;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClientBuilder;
+import org.apache.http.util.EntityUtils;
+
+@Slf4j
+public class PrometheusSourceConnector implements Source {
+
+    private static final String INSTANCE_ID = "instanceId";
+
+    private OffsetStorageReader offsetStorageReader;
+
+    private PrometheusSourceConfig sourceConfig;
+
+    private CloseableHttpClient httpClient;
+
+    private QueryPrometheusReq queryPrometheusReq;
+
+    private Long curStartTime;
+
+    private Long initTime;
+
+    private Integer interval;
+
+    private String url;
+
+    @Override
+    public Class<? extends Config> configClass() {
+        return PrometheusSourceConfig.class;
+    }
+
+    @Override
+    public void init(Config config) throws Exception {
+        this.sourceConfig = (PrometheusSourceConfig) config;
+        doInit();
+    }
+
+    @Override
+    public void init(ConnectorContext connectorContext) throws Exception {
+        SourceConnectorContext sourceConnectorContext = (SourceConnectorContext) connectorContext;
+        this.sourceConfig = (PrometheusSourceConfig) sourceConnectorContext.getSourceConfig();
+        this.offsetStorageReader = sourceConnectorContext.getOffsetStorageReader();
+
+        doInit();
+    }
+
+    private void doInit() {
+        queryPrometheusReq = new QueryPrometheusReq();
+        queryPrometheusReq.setQuery(sourceConfig.getConnectorConfig().getQuery());
+        queryPrometheusReq.setStep(sourceConfig.getConnectorConfig().getStep());
+
+        interval = sourceConfig.getConnectorConfig().getInterval();
+        initTime = sourceConfig.getConnectorConfig().getInitTime();
+
+        url = MessageFormat.format("{0}/{1}", sourceConfig.getConnectorConfig().getAddress(), sourceConfig.getConnectorConfig().getApi());
+
+        httpClient = HttpClientBuilder.create().build();
+    }
+
+    @Override
+    public void start() throws Exception {
+        log.info("prometheus source connector start.");
+
+        Map<String, String> partitionMap = Map.of(INSTANCE_ID, sourceConfig.getConnectorConfig().getConnectorId());
+        RecordPartition recordPartition = new RecordPartition(partitionMap);
+        RecordOffset recordOffset = offsetStorageReader.readOffset(recordPartition);
+        if (recordOffset != null) {
+            Long pollOffset = (Long) recordOffset.getOffset().get("queueOffset");
+            if (pollOffset != null) {
+                // use offset
+                curStartTime = pollOffset;
+            } else if (initTime != null) {
+                // use preset time
+                curStartTime = initTime;
+            } else {
+                // use real time
+                curStartTime = System.currentTimeMillis() / 1000;
+            }
+        }
+    }
+
+    @Override
+    public void commit(ConnectRecord record) {
+        curStartTime += interval;

Review Comment:
   Do you need to add logic to update the offset in this method? Otherwise, it seems that the offset data read by `offsetStorageReader` has always been old.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: issues-help@eventmesh.apache.org


Re: [PR] [ISSUE #4415] Add Promethus source connector (eventmesh)

Posted by "willimpo (via GitHub)" <gi...@apache.org>.
willimpo closed pull request #4492: [ISSUE #4415] Add Promethus source connector
URL: https://github.com/apache/eventmesh/pull/4492


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: dev-help@eventmesh.apache.org