You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by GitBox <gi...@apache.org> on 2021/04/24 12:13:12 UTC
[GitHub] [iotdb] mzp0514 opened a new pull request #3057: Sink
mzp0514 opened a new pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057
## Description
### Content1 ...
### Content2 ...
### Content3 ...
<!--
In each section, please describe design decisions made, including:
- Choice of algorithms
- Behavioral aspects. What configuration values are acceptable? How are corner cases and error
conditions handled, such as when there are insufficient resources?
- Class organization and design (how the logic is split between classes, inheritance, composition,
design patterns)
- Method organization and design (how the logic is split between methods, parameters and return types)
- Naming (class, method, API, configuration, HTTP endpoint, names of emitted metrics)
-->
<!-- It's good to describe an alternative design (or mention an alternative name) for every design
(or naming) decision point and compare the alternatives with the designs that you've implemented
(or the names you've chosen) to highlight the advantages of the chosen designs and names. -->
<!-- If there was a discussion of the design of the feature implemented in this PR elsewhere
(e. g. a "Proposal" issue, any other issue, or a thread in the development mailing list),
link to that discussion from this PR description and explain what have changed in your final design
compared to your original proposal or the consensus version in the end of the discussion.
If something hasn't changed since the original discussion, you can omit a detailed discussion of
those aspects of the design here, perhaps apart from brief mentioning for the sake of readability
of this PR description. -->
<!-- Some of the aspects mentioned above may be omitted for simple and small changes. -->
<hr>
This PR has:
- [ ] been self-reviewed.
- [ ] concurrent read
- [ ] concurrent write
- [ ] concurrent read and write
- [ ] added documentation for new or modified features or behaviors.
- [ ] added Javadocs for most classes and all non-trivial methods.
- [ ] added or updated version, __license__, or notice information
- [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious
for an unfamiliar reader.
- [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold
for code coverage.
- [ ] added integration tests.
- [ ] been tested in a test IoTDB cluster.
<!-- Check the items by putting "x" in the brackets for the done things. Not all of these items
apply to every PR. Remove the items which are not done or not relevant to the PR. None of the items
from the checklist above are strictly necessary, but it would be very helpful if you at least
self-review the PR. -->
<hr>
##### Key changed/added classes (or packages if there are too many classes) in this PR
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619939050
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerConfiguration.java
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Configuration;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+public class AlertManagerConfiguration implements Configuration {
+
+ private final String endpoint;
+
+ public AlertManagerConfiguration(String endpoint) {
+
+ this.endpoint = endpoint;
+ }
+
+ public String getEndpoint() {
+ return endpoint;
+ }
+
Review comment:
Remove the line here :D
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: Sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619930439
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingTriggerExample.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.util.HashMap;
+
+public class AlertingTriggerExample implements Trigger {
Review comment:
Considering that there is already a `TriggerExample` under this package, I suggest to change the name of `AlertingTriggerExample` (or change the name of `TriggerExample`)
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on pull request #3057: Sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#issuecomment-826454931
Please rename this PR to:
[IOTDB-1319] Trigger module: alert manager sink
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619947338
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
Ok, I see. But it's not cost-effective to create a client on every event, we'd better to find a way to solve it.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] mzp0514 commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
mzp0514 commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r620053299
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
This problem is resolved. XD
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r620055692
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
Good job, bro.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] LebronAl merged pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
LebronAl merged pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r621008046
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingExample.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+public class AlertingExample implements Trigger {
+
+ private final AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
+
+ private String alertname;
+
+ private final HashMap<String, String> labels = new HashMap<>();
+
+ private final HashMap<String, String> annotations = new HashMap<>();
+
+ @Override
+ public void onCreate(TriggerAttributes attributes) throws Exception {
+
+ AlertManagerConfiguration alertManagerConfiguration =
+ new AlertManagerConfiguration("http://127.0.0.1:9093/api/v2/alerts");
+
+ alertManagerHandler.open(alertManagerConfiguration);
+
+ alertname = "alert_test";
+
+ labels.put("series", "root.ln.wf01.wt01.temperature");
+ labels.put("value", "");
+ labels.put("severity", "");
+
+ annotations.put("summary", "high temperature");
+ annotations.put("description", "{{.alertname}}: {{.series}} is {{.value}}");
+ }
+
Review comment:
Should we override `onStart() / onStop()`?
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingExample.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+public class AlertingExample implements Trigger {
+
+ private final AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
+
+ private String alertname;
+
+ private final HashMap<String, String> labels = new HashMap<>();
+
+ private final HashMap<String, String> annotations = new HashMap<>();
+
+ @Override
+ public void onCreate(TriggerAttributes attributes) throws Exception {
+
+ AlertManagerConfiguration alertManagerConfiguration =
+ new AlertManagerConfiguration("http://127.0.0.1:9093/api/v2/alerts");
+
+ alertManagerHandler.open(alertManagerConfiguration);
+
+ alertname = "alert_test";
+
+ labels.put("series", "root.ln.wf01.wt01.temperature");
+ labels.put("value", "");
+ labels.put("severity", "");
+
+ annotations.put("summary", "high temperature");
+ annotations.put("description", "{{.alertname}}: {{.series}} is {{.value}}");
+ }
+
Review comment:
Can the client be reopened?
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] mzp0514 commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
mzp0514 commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619938177
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
I have tried but the thread will be blocked in this way...
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on pull request #3057: Sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#issuecomment-826454931
Please rename this PR to:
[IOTDB-1319] Trigger module: alert manager sink
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: Sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619930439
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingTriggerExample.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.util.HashMap;
+
+public class AlertingTriggerExample implements Trigger {
Review comment:
Considering that there is already a `TriggerExample` under this package, I suggest to change the name of `AlertingTriggerExample` (or change the name of `TriggerExample`)
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerEvent.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class AlertManagerEvent implements Event {
+
+ private final Map<String, String> labels;
+
+ private final Map<String, String> annotations;
+
+ public AlertManagerEvent(String alertname) {
+ this.labels = new HashMap<>();
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(String alertname, Map<String, String> extraLabels) {
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(
+ String alertname, Map<String, String> extraLabels, Map<String, String> annotations) {
+
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = new HashMap<>();
+
+ for (Map.Entry<String, String> entry : annotations.entrySet()) {
+ this.annotations.put(entry.getKey(), fillTemplate(this.labels, entry.getValue()));
+ }
+ }
+
+ public Map<String, String> getAnnotations() {
+ return annotations;
+ }
+
+ public Map<String, String> getLabels() {
+ return labels;
+ }
+
+ private static String fillTemplate(Map<String, String> map, String template) {
+ if (template == null || map == null) return null;
Review comment:
Always remember not to omit `{}`.
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingTriggerExample.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.util.HashMap;
+
+public class AlertingTriggerExample implements Trigger {
+
+ AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
Review comment:
It's better to make member variables private.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
+
+ CloseableHttpResponse response = client.execute(request);
+
+ if (response.getStatusLine().getStatusCode() != 200) {
+ throw new SinkException(response.getStatusLine().toString());
+ }
+ }
+ }
+
+ private static String eventToJson(AlertManagerEvent event) throws SinkException {
Review comment:
Add some unit tests for this method :D
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesHandler.java
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.executor.IPlanExecutor;
+import org.apache.iotdb.db.qp.executor.PlanExecutor;
+import org.apache.iotdb.db.qp.physical.PhysicalPlan;
+import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan;
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class TimeSeriesHandler implements Handler<TimeSeriesConfiguration, TimeSeriesEvent> {
Review comment:
Remove this file.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesEvent.java
##########
@@ -0,0 +1,41 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+public class TimeSeriesEvent implements Event {
Review comment:
Remove this file.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerConfiguration.java
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Configuration;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+public class AlertManagerConfiguration implements Configuration {
+
+ private final String endpoint;
+
+ public AlertManagerConfiguration(String endpoint) {
+
+ this.endpoint = endpoint;
+ }
+
+ public String getEndpoint() {
+ return endpoint;
+ }
+
+ @Override
+ public boolean equals(Object o) {
Review comment:
We don't need to override `equals` and `hashCode` now.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
Can the `client` here be reused? Try to execute `HttpClients.createDefault()` in `onCreate() / onStart()` and execute `client.close()` in `onDrop / onStop()`?
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerEvent.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class AlertManagerEvent implements Event {
+
+ private final Map<String, String> labels;
+
+ private final Map<String, String> annotations;
+
+ public AlertManagerEvent(String alertname) {
+ this.labels = new HashMap<>();
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(String alertname, Map<String, String> extraLabels) {
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(
+ String alertname, Map<String, String> extraLabels, Map<String, String> annotations) {
+
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = new HashMap<>();
+
+ for (Map.Entry<String, String> entry : annotations.entrySet()) {
+ this.annotations.put(entry.getKey(), fillTemplate(this.labels, entry.getValue()));
+ }
+ }
+
+ public Map<String, String> getAnnotations() {
+ return annotations;
+ }
+
+ public Map<String, String> getLabels() {
+ return labels;
+ }
+
+ private static String fillTemplate(Map<String, String> map, String template) {
+ if (template == null || map == null) return null;
+ StringBuffer sb = new StringBuffer();
+ Matcher m = Pattern.compile("\\{\\{\\.\\w+}}").matcher(template);
Review comment:
Make the return value of `Pattern.compile("\\{\\{\\.\\w+}}")` a `private static final` field to improve performance.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesConfiguration.java
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.sink.api.Configuration;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class TimeSeriesConfiguration implements Configuration {
Review comment:
Remove this file.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerConfiguration.java
##########
@@ -0,0 +1,40 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Configuration;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+public class AlertManagerConfiguration implements Configuration {
+
+ private final String endpoint;
+
+ public AlertManagerConfiguration(String endpoint) {
+
+ this.endpoint = endpoint;
+ }
+
+ public String getEndpoint() {
+ return endpoint;
+ }
+
Review comment:
Remove the line here :D
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
Ok, I see. But it's not cost-effective to create a client on every event, we'd better to find a way to solve it.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
SteveYurongSu commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619933663
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerEvent.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class AlertManagerEvent implements Event {
+
+ private final Map<String, String> labels;
+
+ private final Map<String, String> annotations;
+
+ public AlertManagerEvent(String alertname) {
+ this.labels = new HashMap<>();
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(String alertname, Map<String, String> extraLabels) {
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(
+ String alertname, Map<String, String> extraLabels, Map<String, String> annotations) {
+
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = new HashMap<>();
+
+ for (Map.Entry<String, String> entry : annotations.entrySet()) {
+ this.annotations.put(entry.getKey(), fillTemplate(this.labels, entry.getValue()));
+ }
+ }
+
+ public Map<String, String> getAnnotations() {
+ return annotations;
+ }
+
+ public Map<String, String> getLabels() {
+ return labels;
+ }
+
+ private static String fillTemplate(Map<String, String> map, String template) {
+ if (template == null || map == null) return null;
Review comment:
Always remember not to omit `{}`.
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingTriggerExample.java
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.util.HashMap;
+
+public class AlertingTriggerExample implements Trigger {
+
+ AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
Review comment:
It's better to make member variables private.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
+
+ CloseableHttpResponse response = client.execute(request);
+
+ if (response.getStatusLine().getStatusCode() != 200) {
+ throw new SinkException(response.getStatusLine().toString());
+ }
+ }
+ }
+
+ private static String eventToJson(AlertManagerEvent event) throws SinkException {
Review comment:
Add some unit tests for this method :D
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesHandler.java
##########
@@ -0,0 +1,72 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.exception.StorageEngineException;
+import org.apache.iotdb.db.exception.metadata.StorageGroupNotSetException;
+import org.apache.iotdb.db.exception.query.QueryProcessException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.qp.executor.IPlanExecutor;
+import org.apache.iotdb.db.qp.executor.PlanExecutor;
+import org.apache.iotdb.db.qp.physical.PhysicalPlan;
+import org.apache.iotdb.db.qp.physical.crud.InsertRowPlan;
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class TimeSeriesHandler implements Handler<TimeSeriesConfiguration, TimeSeriesEvent> {
Review comment:
Remove this file.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesEvent.java
##########
@@ -0,0 +1,41 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+public class TimeSeriesEvent implements Event {
Review comment:
Remove this file.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerConfiguration.java
##########
@@ -0,0 +1,63 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Configuration;
+
+import org.apache.commons.lang3.builder.EqualsBuilder;
+import org.apache.commons.lang3.builder.HashCodeBuilder;
+
+public class AlertManagerConfiguration implements Configuration {
+
+ private final String endpoint;
+
+ public AlertManagerConfiguration(String endpoint) {
+
+ this.endpoint = endpoint;
+ }
+
+ public String getEndpoint() {
+ return endpoint;
+ }
+
+ @Override
+ public boolean equals(Object o) {
Review comment:
We don't need to override `equals` and `hashCode` now.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
Can the `client` here be reused? Try to execute `HttpClients.createDefault()` in `onCreate() / onStart()` and execute `client.close()` in `onDrop / onStop()`?
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerEvent.java
##########
@@ -0,0 +1,80 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Event;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class AlertManagerEvent implements Event {
+
+ private final Map<String, String> labels;
+
+ private final Map<String, String> annotations;
+
+ public AlertManagerEvent(String alertname) {
+ this.labels = new HashMap<>();
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(String alertname, Map<String, String> extraLabels) {
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = null;
+ }
+
+ public AlertManagerEvent(
+ String alertname, Map<String, String> extraLabels, Map<String, String> annotations) {
+
+ this.labels = extraLabels;
+ this.labels.put("alertname", alertname);
+ this.annotations = new HashMap<>();
+
+ for (Map.Entry<String, String> entry : annotations.entrySet()) {
+ this.annotations.put(entry.getKey(), fillTemplate(this.labels, entry.getValue()));
+ }
+ }
+
+ public Map<String, String> getAnnotations() {
+ return annotations;
+ }
+
+ public Map<String, String> getLabels() {
+ return labels;
+ }
+
+ private static String fillTemplate(Map<String, String> map, String template) {
+ if (template == null || map == null) return null;
+ StringBuffer sb = new StringBuffer();
+ Matcher m = Pattern.compile("\\{\\{\\.\\w+}}").matcher(template);
Review comment:
Make the return value of `Pattern.compile("\\{\\{\\.\\w+}}")` a `private static final` field to improve performance.
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/ts/TimeSeriesConfiguration.java
##########
@@ -0,0 +1,51 @@
+/*
+ * 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.iotdb.db.sink.ts;
+
+import org.apache.iotdb.db.exception.metadata.IllegalPathException;
+import org.apache.iotdb.db.metadata.PartialPath;
+import org.apache.iotdb.db.sink.api.Configuration;
+import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
+
+public class TimeSeriesConfiguration implements Configuration {
Review comment:
Remove this file.
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] mzp0514 commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
mzp0514 commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r619938177
##########
File path: server/src/main/java/org/apache/iotdb/db/sink/alertmanager/AlertManagerHandler.java
##########
@@ -0,0 +1,91 @@
+/*
+ * 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.iotdb.db.sink.alertmanager;
+
+import org.apache.iotdb.db.sink.api.Handler;
+import org.apache.iotdb.db.sink.exception.SinkException;
+
+import com.google.gson.Gson;
+import com.google.gson.reflect.TypeToken;
+import org.apache.http.client.methods.CloseableHttpResponse;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+
+import java.lang.reflect.Type;
+import java.util.Map;
+
+public class AlertManagerHandler
+ implements Handler<
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration,
+ org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent> {
+
+ private HttpPost request;
+
+ @Override
+ public void open(org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration configuration)
+ throws Exception {
+ this.request = new HttpPost(configuration.getEndpoint());
+ request.setHeader("Accept", "application/json");
+ request.setHeader("Content-type", "application/json");
+ }
+
+ @Override
+ public void onEvent(AlertManagerEvent event) throws Exception {
+
+ String json = eventToJson(event);
+
+ request.setEntity(new StringEntity(json));
+
+ try (CloseableHttpClient client = HttpClients.createDefault()) {
Review comment:
I have tried but the thread will be blocked in this way...
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [iotdb] mzp0514 commented on a change in pull request #3057: [IOTDB-1319] Trigger module: alert manager sink
Posted by GitBox <gi...@apache.org>.
mzp0514 commented on a change in pull request #3057:
URL: https://github.com/apache/iotdb/pull/3057#discussion_r621937375
##########
File path: example/trigger/src/main/java/org/apache/iotdb/trigger/AlertingExample.java
##########
@@ -0,0 +1,98 @@
+/*
+ * 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.iotdb.trigger;
+
+import org.apache.iotdb.db.engine.trigger.api.Trigger;
+import org.apache.iotdb.db.engine.trigger.api.TriggerAttributes;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerConfiguration;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerEvent;
+import org.apache.iotdb.db.sink.alertmanager.AlertManagerHandler;
+
+import java.io.IOException;
+import java.util.HashMap;
+
+public class AlertingExample implements Trigger {
+
+ private final AlertManagerHandler alertManagerHandler = new AlertManagerHandler();
+
+ private String alertname;
+
+ private final HashMap<String, String> labels = new HashMap<>();
+
+ private final HashMap<String, String> annotations = new HashMap<>();
+
+ @Override
+ public void onCreate(TriggerAttributes attributes) throws Exception {
+
+ AlertManagerConfiguration alertManagerConfiguration =
+ new AlertManagerConfiguration("http://127.0.0.1:9093/api/v2/alerts");
+
+ alertManagerHandler.open(alertManagerConfiguration);
+
+ alertname = "alert_test";
+
+ labels.put("series", "root.ln.wf01.wt01.temperature");
+ labels.put("value", "");
+ labels.put("severity", "");
+
+ annotations.put("summary", "high temperature");
+ annotations.put("description", "{{.alertname}}: {{.series}} is {{.value}}");
+ }
+
Review comment:
updated
--
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.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org