Skip to content

Commit

Permalink
[feature]Hertzbeat custom plugin. (#1973)
Browse files Browse the repository at this point in the history
  • Loading branch information
zqr10159 authored May 13, 2024
1 parent a69d05a commit de0de1c
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 1 deletion.
1 change: 1 addition & 0 deletions .licenserc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ header:
- '**/NOTICE'
- '.all-contributorsrc'
- '**/*.AbstractCollect'
- '**/*.Plugin'
- '**/*.MockMaker'
- '.prettierrc'
- '.browserslistrc'
Expand Down
5 changes: 5 additions & 0 deletions manager/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-push</artifactId>
</dependency>
<!-- plugin -->
<dependency>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-plugin</artifactId>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.collect.Maps;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.alert.AlerterWorkerPool;
import org.apache.hertzbeat.common.entity.alerter.Alert;
Expand All @@ -30,6 +31,7 @@
import org.apache.hertzbeat.manager.service.NoticeConfigService;
import org.apache.hertzbeat.manager.support.exception.AlertNoticeException;
import org.apache.hertzbeat.manager.support.exception.IgnoreException;
import org.apache.hertzbeat.plugin.Plugin;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -61,7 +63,7 @@ public DispatcherAlarm(AlerterWorkerPool workerPool,
}

@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
// Start alarm distribution
DispatchTask dispatchTask = new DispatchTask();
for (int i = 0; i < DISPATCH_THREADS; i++) {
Expand Down Expand Up @@ -113,6 +115,11 @@ public void run() {
alertStoreHandler.store(alert);
// Notice distribution
sendNotify(alert);
// Execute the plugin
ServiceLoader<Plugin> loader = ServiceLoader.load(Plugin.class, Plugin.class.getClassLoader());
for (Plugin plugin : loader) {
plugin.alert(alert);
}
}
} catch (IgnoreException ignored) {
} catch (InterruptedException e) {
Expand Down
44 changes: 44 additions & 0 deletions plugin/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hertzbeat</artifactId>
<groupId>org.apache.hertzbeat</groupId>
<version>2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hertzbeat-plugin</artifactId>
<name>${project.artifactId}</name>
<properties>
<maven-jar-plugin.version>3.2.0</maven-jar-plugin.version>
<maven-assembly-plugin.version>3.3.0</maven-assembly-plugin.version>
</properties>

<dependencies>
<!-- common -->
<dependency>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-common</artifactId>
</dependency>
</dependencies>


</project>
32 changes: 32 additions & 0 deletions plugin/src/main/java/org/apache/hertzbeat/plugin/Plugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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.plugin;

import org.apache.hertzbeat.common.entity.alerter.Alert;

/**
* Plugin
*/
public interface Plugin {

/*
* execute when alert
*/
void alert(Alert alert);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* 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.plugin.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.hertzbeat.common.entity.alerter.Alert;
import org.apache.hertzbeat.plugin.Plugin;

/**
* DemoPlugin
*/
@Slf4j
public class DemoPluginImpl implements Plugin {
/*
* execute when alert
*/
@Override
public void alert(Alert alert) {
log.info("DemoPluginImpl alert: {}", alert);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
org.apache.hertzbeat.plugin.impl.DemoPluginImpl
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<module>warehouse</module>
<module>remoting</module>
<module>push</module>
<module>plugin</module>
</modules>

<properties>
Expand Down Expand Up @@ -177,6 +178,12 @@
<artifactId>hertzbeat-push</artifactId>
<version>${hertzbeat.version}</version>
</dependency>
<!-- plugin -->
<dependency>
<groupId>org.apache.hertzbeat</groupId>
<artifactId>hertzbeat-plugin</artifactId>
<version>${hertzbeat.version}</version>
</dependency>
<!-- spring -->
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down

0 comments on commit de0de1c

Please sign in to comment.