Skip to content

Commit

Permalink
[improve] improve plugin (#2637)
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuTianyou authored Aug 30, 2024
1 parent 89be682 commit d49cb37
Show file tree
Hide file tree
Showing 19 changed files with 378 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ public enum PluginType {
/**
* do something after alter
*/
POST_ALERT
POST_ALERT,
/**
* do something after collect
*/
POST_COLLECT
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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.
*/

// Generated by the protocol buffer compiler. DO NOT EDIT!
// source: cluster_msg.proto

package org.apache.hertzbeat.common.entity.plugin;

import java.util.List;
import lombok.Data;
import org.apache.hertzbeat.common.entity.manager.ParamDefine;

/**
* The configuration file of the plugin, including parameters and other information
*/
@Data
public class PluginConfig {

private List<ParamDefine> params;
}
Original file line number Diff line number Diff line change
@@ -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.hertzbeat.common.entity.plugin;


import java.util.List;
import lombok.Builder;
import lombok.Data;
import org.apache.hertzbeat.common.entity.job.Configmap;

/**
* plugin context
*/
@Builder
@Data
public class PluginContext {

/**
* params
*/
List<Configmap> params;

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@
import org.apache.hertzbeat.common.entity.manager.NoticeTemplate;
import org.apache.hertzbeat.common.queue.CommonDataQueue;
import org.apache.hertzbeat.manager.service.NoticeConfigService;
import org.apache.hertzbeat.manager.service.PluginService;
import org.apache.hertzbeat.manager.support.exception.AlertNoticeException;
import org.apache.hertzbeat.manager.support.exception.IgnoreException;
import org.apache.hertzbeat.plugin.PostAlertPlugin;
import org.apache.hertzbeat.plugin.Plugin;
import org.apache.hertzbeat.plugin.runner.PluginRunner;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

Expand All @@ -50,18 +51,18 @@ public class DispatcherAlarm implements InitializingBean {
private final NoticeConfigService noticeConfigService;
private final AlertStoreHandler alertStoreHandler;
private final Map<Byte, AlertNotifyHandler> alertNotifyHandlerMap;
private final PluginService pluginService;
private final PluginRunner pluginRunner;

public DispatcherAlarm(AlerterWorkerPool workerPool,
CommonDataQueue dataQueue,
NoticeConfigService noticeConfigService,
AlertStoreHandler alertStoreHandler,
List<AlertNotifyHandler> alertNotifyHandlerList, PluginService pluginService) {
List<AlertNotifyHandler> alertNotifyHandlerList, PluginRunner pluginRunner) {
this.workerPool = workerPool;
this.dataQueue = dataQueue;
this.noticeConfigService = noticeConfigService;
this.alertStoreHandler = alertStoreHandler;
this.pluginService = pluginService;
this.pluginRunner = pluginRunner;
alertNotifyHandlerMap = Maps.newHashMapWithExpectedSize(alertNotifyHandlerList.size());
alertNotifyHandlerList.forEach(r -> alertNotifyHandlerMap.put(r.type(), r));
}
Expand Down Expand Up @@ -130,8 +131,11 @@ public void run() {
alertStoreHandler.store(alert);
// Notice distribution
sendNotify(alert);
// Execute the plugin if enable
pluginService.pluginExecute(Plugin.class, plugin -> plugin.alert(alert), (plugin, configMapList) -> plugin.alert(alert, configMapList));
// Execute the plugin if enable (Compatible with old version plugins, will be removed in later versions)
pluginRunner.pluginExecute(Plugin.class, plugin -> plugin.alert(alert));
// Execute the plugin if enable with params
pluginRunner.pluginExecute(PostAlertPlugin.class, (afterAlertPlugin, pluginContext) -> afterAlertPlugin.execute(alert, pluginContext));

}
} catch (IgnoreException ignored) {
} catch (InterruptedException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.apache.hertzbeat.common.entity.dto.PluginUpload;
import org.apache.hertzbeat.common.entity.job.Configmap;
import org.apache.hertzbeat.common.entity.manager.PluginMetadata;
import org.apache.hertzbeat.common.entity.plugin.PluginContext;
import org.apache.hertzbeat.manager.pojo.dto.PluginParam;
import org.apache.hertzbeat.manager.pojo.dto.PluginParametersVO;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -63,7 +63,18 @@ public interface PluginService {
* @param execute run plugin logic
* @param <T> plugin type
*/
<T> void pluginExecute(Class<T> clazz, Consumer<T> execute, BiConsumer<T, List<Configmap>> biConsumer);
<T> void pluginExecute(Class<T> clazz, Consumer<T> execute);


/**
* execute plugin
*
* @param clazz plugin interface
* @param execute run plugin logic
* @param <T> plugin type
*/
<T> void pluginExecute(Class<T> clazz, BiConsumer<T, PluginContext> execute);


/**
* delete plugin
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* 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.hertzbeat.manager.service.impl;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.entity.plugin.PluginContext;
import org.apache.hertzbeat.manager.service.PluginService;
import org.apache.hertzbeat.plugin.runner.PluginRunner;
import org.springframework.stereotype.Service;


/**
* default plugin runner
*/
@Service
@RequiredArgsConstructor
@Slf4j
public class DefaultPluginRunner implements PluginRunner {

private final PluginService pluginService;

@Override
public <T> void pluginExecute(Class<T> clazz, Consumer<T> execute) {
try {
pluginService.pluginExecute(clazz, execute);
} catch (Exception e) {
log.error("plugin execute failed", e);
}
}

@Override
public <T> void pluginExecute(Class<T> clazz, BiConsumer<T, PluginContext> execute) {
try {
pluginService.pluginExecute(clazz, execute);
} catch (Exception e) {
log.error("plugin execute failed", e);
}
}
}
Loading

0 comments on commit d49cb37

Please sign in to comment.