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/26 02:36:54 UTC
[GitHub] [iotdb] SteveYurongSu commented on a change in pull request #3057: Trigger module: alert manager sink
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