supprt gRPC alarm hook. (#4344)

* support gRPC hook.

* revert submodule commit.

* revert submodule commitid.

* finish logical.

* remove author anonatation.

* fix ci.

* revert submodule.

* update logical.

* update logical.

* update logical.

* fix

* fix

* fix empty settings.

* fix.

Co-authored-by: 吴晟 Wu Sheng <wu.sheng@foxmail.com>
This commit is contained in:
Jared Tan 2020-02-12 16:20:20 +08:00 committed by GitHub
parent e5366c0921
commit 7ff9a0cfd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 595 additions and 66 deletions

View File

@ -3,6 +3,7 @@ Alarm core is driven by a collection of rules, which are defined in `config/alar
There are two parts in alarm rule definition. There are two parts in alarm rule definition.
1. [Alarm rules](#rules). They define how metrics alarm should be triggered, what conditions should be considered. 1. [Alarm rules](#rules). They define how metrics alarm should be triggered, what conditions should be considered.
1. [Webhooks](#webhook). The list of web service endpoint, which should be called after the alarm is triggered. 1. [Webhooks](#webhook). The list of web service endpoint, which should be called after the alarm is triggered.
1. [gRPCHook](#gRPCHook). The host and port of remote gRPC method, which should be called after the alarm is triggered.
## Rules ## Rules
Alarm rule is constituted by following keys Alarm rule is constituted by following keys
@ -113,6 +114,24 @@ Example as following
}] }]
``` ```
## gRPCHook
The alarm message will send through remote gRPC method by `Protobuf` content type.
The message format with following key information which are defined in `oap-server/server-alarm-plugin/src/main/proto/alarm-hook.proto`.
Part of protocol looks as following:
```protobuf
message AlarmMessage {
int64 scopeId = 1;
string scope = 2;
string name = 3;
int64 id0 = 4;
int64 id1 = 5;
string ruleName = 6;
string alarmMessage = 7;
int64 startTime = 8;
}
```
## Update the settings dynamically ## Update the settings dynamically
Since 6.5.0, the alarm settings can be updated dynamically at runtime by [Dynamic Configuration](dynamic-config.md), Since 6.5.0, the alarm settings can be updated dynamically at runtime by [Dynamic Configuration](dynamic-config.md),
which will override the settings in `alarm-settings.yml`. which will override the settings in `alarm-settings.yml`.

View File

@ -25,8 +25,6 @@ import java.util.List;
import java.util.Set; import java.util.Set;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import lombok.AccessLevel;
import lombok.Getter;
import org.apache.skywalking.apm.commons.datacarrier.DataCarrier; import org.apache.skywalking.apm.commons.datacarrier.DataCarrier;
import org.apache.skywalking.apm.commons.datacarrier.consumer.IConsumer; import org.apache.skywalking.apm.commons.datacarrier.consumer.IConsumer;
import org.apache.skywalking.oap.server.core.analysis.metrics.DoubleValueHolder; import org.apache.skywalking.oap.server.core.analysis.metrics.DoubleValueHolder;
@ -36,6 +34,7 @@ import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo; import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
import org.apache.skywalking.oap.server.core.analysis.metrics.MultiIntValuesHolder; import org.apache.skywalking.oap.server.core.analysis.metrics.MultiIntValuesHolder;
import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata; import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;
import org.apache.skywalking.oap.server.core.exporter.ExportData;
import org.apache.skywalking.oap.server.core.exporter.ExportEvent; import org.apache.skywalking.oap.server.core.exporter.ExportEvent;
import org.apache.skywalking.oap.server.core.exporter.MetricValuesExportService; import org.apache.skywalking.oap.server.core.exporter.MetricValuesExportService;
import org.apache.skywalking.oap.server.exporter.grpc.ExportMetricValue; import org.apache.skywalking.oap.server.exporter.grpc.ExportMetricValue;
@ -46,10 +45,11 @@ import org.apache.skywalking.oap.server.exporter.grpc.SubscriptionsResp;
import org.apache.skywalking.oap.server.exporter.grpc.ValueType; import org.apache.skywalking.oap.server.exporter.grpc.ValueType;
import org.apache.skywalking.oap.server.exporter.provider.MetricFormatter; import org.apache.skywalking.oap.server.exporter.provider.MetricFormatter;
import org.apache.skywalking.oap.server.library.client.grpc.GRPCClient; import org.apache.skywalking.oap.server.library.client.grpc.GRPCClient;
import org.apache.skywalking.oap.server.library.util.GRPCStreamStatus;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
public class GRPCExporter extends MetricFormatter implements MetricValuesExportService, IConsumer<GRPCExporter.ExportData> { public class GRPCExporter extends MetricFormatter implements MetricValuesExportService, IConsumer<ExportData> {
private static final Logger logger = LoggerFactory.getLogger(GRPCExporter.class); private static final Logger logger = LoggerFactory.getLogger(GRPCExporter.class);
private GRPCExporterSetting setting; private GRPCExporterSetting setting;
@ -101,26 +101,28 @@ public class GRPCExporter extends MetricFormatter implements MetricValuesExportS
return; return;
} }
ExportStatus status = new ExportStatus(); GRPCStreamStatus status = new GRPCStreamStatus();
StreamObserver<ExportMetricValue> streamObserver = exportServiceFutureStub.withDeadlineAfter(10, TimeUnit.SECONDS) StreamObserver<ExportMetricValue> streamObserver = exportServiceFutureStub.withDeadlineAfter(
.export(new StreamObserver<ExportResponse>() { 10, TimeUnit.SECONDS)
@Override .export(
public void onNext( new StreamObserver<ExportResponse>() {
ExportResponse response) { @Override
public void onNext(
ExportResponse response) {
} }
@Override @Override
public void onError( public void onError(
Throwable throwable) { Throwable throwable) {
status.done(); status.done();
} }
@Override @Override
public void onCompleted() { public void onCompleted() {
status.done(); status.done();
} }
}); });
AtomicInteger exportNum = new AtomicInteger(); AtomicInteger exportNum = new AtomicInteger();
data.forEach(row -> { data.forEach(row -> {
ExportMetricValue.Builder builder = ExportMetricValue.newBuilder(); ExportMetricValue.Builder builder = ExportMetricValue.newBuilder();
@ -167,9 +169,8 @@ public class GRPCExporter extends MetricFormatter implements MetricValuesExportS
long sleepTime = 0; long sleepTime = 0;
long cycle = 100L; long cycle = 100L;
/**
* For memory safe of oap, we must wait for the peer confirmation. //For memory safe of oap, we must wait for the peer confirmation.
*/
while (!status.isDone()) { while (!status.isDone()) {
try { try {
sleepTime += cycle; sleepTime += cycle;
@ -178,14 +179,18 @@ public class GRPCExporter extends MetricFormatter implements MetricValuesExportS
} }
if (sleepTime > 2000L) { if (sleepTime > 2000L) {
logger.warn("Export {} metrics to {}:{}, wait {} milliseconds.", exportNum.get(), setting.getTargetHost(), setting logger.warn(
.getTargetPort(), sleepTime); "Export {} metrics to {}:{}, wait {} milliseconds.", exportNum.get(), setting.getTargetHost(),
setting
.getTargetPort(), sleepTime
);
cycle = 2000L; cycle = 2000L;
} }
} }
logger.debug("Exported {} metrics to {}:{} in {} milliseconds.", exportNum.get(), setting.getTargetHost(), setting logger.debug(
.getTargetPort(), sleepTime); "Exported {} metrics to {}:{} in {} milliseconds.", exportNum.get(), setting.getTargetHost(), setting
.getTargetPort(), sleepTime);
} }
@Override @Override
@ -197,27 +202,4 @@ public class GRPCExporter extends MetricFormatter implements MetricValuesExportS
public void onExit() { public void onExit() {
} }
@Getter(AccessLevel.PRIVATE)
public class ExportData {
private MetricsMetaInfo meta;
private Metrics metrics;
public ExportData(MetricsMetaInfo meta, Metrics metrics) {
this.meta = meta;
this.metrics = metrics;
}
}
private class ExportStatus {
private boolean done = false;
private void done() {
done = true;
}
public boolean isDone() {
return done;
}
}
} }

View File

@ -24,6 +24,7 @@ import java.util.LinkedList;
import java.util.List; import java.util.List;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo; import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata; import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;
import org.apache.skywalking.oap.server.core.exporter.ExportData;
import org.apache.skywalking.oap.server.core.exporter.ExportEvent; import org.apache.skywalking.oap.server.core.exporter.ExportEvent;
import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine; import org.apache.skywalking.oap.server.core.source.DefaultScopeDefine;
import org.apache.skywalking.oap.server.exporter.grpc.MetricExportServiceGrpc; import org.apache.skywalking.oap.server.exporter.grpc.MetricExportServiceGrpc;
@ -97,12 +98,12 @@ public class GRPCExporterTest {
exporter.onExit(); exporter.onExit();
} }
private List<GRPCExporter.ExportData> dataList() { private List<ExportData> dataList() {
List<GRPCExporter.ExportData> dataList = new LinkedList<>(); List<ExportData> dataList = new LinkedList<>();
dataList.add(exporter.new ExportData(metaInfo, new MockMetrics())); dataList.add(new ExportData(metaInfo, new MockMetrics()));
dataList.add(exporter.new ExportData(metaInfo, new MockIntValueMetrics())); dataList.add(new ExportData(metaInfo, new MockIntValueMetrics()));
dataList.add(exporter.new ExportData(metaInfo, new MockLongValueMetrics())); dataList.add(new ExportData(metaInfo, new MockLongValueMetrics()));
dataList.add(exporter.new ExportData(metaInfo, new MockDoubleValueMetrics())); dataList.add(new ExportData(metaInfo, new MockDoubleValueMetrics()));
return dataList; return dataList;
} }
} }

View File

@ -27,6 +27,7 @@
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<artifactId>server-alarm-plugin</artifactId> <artifactId>server-alarm-plugin</artifactId>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>org.apache.skywalking</groupId> <groupId>org.apache.skywalking</groupId>
@ -38,7 +39,40 @@
<artifactId>library-util</artifactId> <artifactId>library-util</artifactId>
<version>${project.version}</version> <version>${project.version}</version>
</dependency> </dependency>
<dependency>
<groupId>io.grpc</groupId>
<artifactId>grpc-testing</artifactId>
</dependency>
</dependencies> </dependencies>
<build>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>${protobuf-maven-plugin.version}</version>
<configuration>
<!--
The version of protoc must match protobuf-java. If you don't depend on
protobuf-java directly, you will be transitively depending on the
protobuf-java version that grpc depends on.
-->
<protocArtifact>com.google.protobuf:protoc:${com.google.protobuf.protoc.version}:exe:${os.detected.classifier}
</protocArtifact>
<pluginId>grpc-java</pluginId>
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${protoc-gen-grpc-java.plugin.version}:exe:${os.detected.classifier}
</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project> </project>

View File

@ -18,23 +18,22 @@
package org.apache.skywalking.oap.server.core.alarm.provider; package org.apache.skywalking.oap.server.core.alarm.provider;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.oap.server.configuration.api.ConfigChangeWatcher;
import org.apache.skywalking.oap.server.core.Const;
import org.apache.skywalking.oap.server.core.alarm.AlarmModule;
import org.apache.skywalking.oap.server.library.module.ModuleProvider;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.oap.server.configuration.api.ConfigChangeWatcher;
import org.apache.skywalking.oap.server.core.Const;
import org.apache.skywalking.oap.server.core.alarm.AlarmModule;
import org.apache.skywalking.oap.server.core.alarm.provider.grpc.GRPCAlarmSetting;
import org.apache.skywalking.oap.server.library.module.ModuleProvider;
/** /**
* Alarm rules' settings can be dynamically updated via configuration center(s), * Alarm rules' settings can be dynamically updated via configuration center(s), this class is responsible for
* this class is responsible for monitoring the configuration and parsing them * monitoring the configuration and parsing them into {@link Rules} and {@link #runningContext}.
* into {@link Rules} and {@link #runningContext}.
* *
* @since 6.5.0 * @since 6.5.0
*/ */
@ -106,4 +105,8 @@ public class AlarmRulesWatcher extends ConfigChangeWatcher {
public List<String> getWebHooks() { public List<String> getWebHooks() {
return this.rules.getWebhooks(); return this.rules.getWebhooks();
} }
public GRPCAlarmSetting getGrpchookSetting() {
return this.rules.getGrpchookSetting();
}
} }

View File

@ -28,6 +28,7 @@ import org.apache.skywalking.oap.server.core.alarm.MetaInAlarm;
import org.apache.skywalking.oap.server.core.alarm.MetricsNotify; import org.apache.skywalking.oap.server.core.alarm.MetricsNotify;
import org.apache.skywalking.oap.server.core.alarm.ServiceInstanceMetaInAlarm; import org.apache.skywalking.oap.server.core.alarm.ServiceInstanceMetaInAlarm;
import org.apache.skywalking.oap.server.core.alarm.ServiceMetaInAlarm; import org.apache.skywalking.oap.server.core.alarm.ServiceMetaInAlarm;
import org.apache.skywalking.oap.server.core.alarm.provider.grpc.GRPCCallback;
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics; import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo; import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata; import org.apache.skywalking.oap.server.core.analysis.metrics.WithMetadata;
@ -110,6 +111,7 @@ public class NotifyHandler implements MetricsNotify {
public void init(AlarmCallback... callbacks) { public void init(AlarmCallback... callbacks) {
List<AlarmCallback> allCallbacks = new ArrayList<>(Arrays.asList(callbacks)); List<AlarmCallback> allCallbacks = new ArrayList<>(Arrays.asList(callbacks));
allCallbacks.add(new WebhookCallback(alarmRulesWatcher)); allCallbacks.add(new WebhookCallback(alarmRulesWatcher));
allCallbacks.add(new GRPCCallback(alarmRulesWatcher));
core.start(allCallbacks); core.start(allCallbacks);
} }

View File

@ -24,6 +24,7 @@ import lombok.AccessLevel;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
import lombok.ToString; import lombok.ToString;
import org.apache.skywalking.oap.server.core.alarm.provider.grpc.GRPCAlarmSetting;
@Setter(AccessLevel.PUBLIC) @Setter(AccessLevel.PUBLIC)
@Getter(AccessLevel.PUBLIC) @Getter(AccessLevel.PUBLIC)
@ -31,6 +32,7 @@ import lombok.ToString;
public class Rules { public class Rules {
private List<AlarmRule> rules; private List<AlarmRule> rules;
private List<String> webhooks; private List<String> webhooks;
private GRPCAlarmSetting grpchookSetting;
public Rules() { public Rules() {
this.rules = new ArrayList<>(); this.rules = new ArrayList<>();

View File

@ -24,6 +24,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import org.apache.skywalking.oap.server.core.alarm.provider.grpc.GRPCAlarmSetting;
import org.yaml.snakeyaml.Yaml; import org.yaml.snakeyaml.Yaml;
/** /**
@ -81,6 +82,22 @@ public class RulesReader {
rules.getWebhooks().add((String) url); rules.getWebhooks().add((String) url);
}); });
} }
Map grpchooks = (Map) yamlData.get("gRPCHook");
if (grpchooks != null) {
GRPCAlarmSetting grpcAlarmSetting = new GRPCAlarmSetting();
Object targetHost = grpchooks.get("target_host");
if (targetHost != null) {
grpcAlarmSetting.setTargetHost((String) targetHost);
}
Object targetPort = grpchooks.get("target_port");
if (targetPort != null) {
grpcAlarmSetting.setTargetPort((Integer) targetPort);
}
rules.setGrpchookSetting(grpcAlarmSetting);
}
} }
return rules; return rules;

View File

@ -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.
*
*/
package org.apache.skywalking.oap.server.core.alarm.provider.grpc;
import java.util.Objects;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@EqualsAndHashCode
public class GRPCAlarmSetting {
private String targetHost;
private int targetPort;
public boolean isEmptySetting() {
return Objects.isNull(targetHost);
}
}

View File

@ -0,0 +1,163 @@
/*
* 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.skywalking.oap.server.core.alarm.provider.grpc;
import io.grpc.stub.StreamObserver;
import java.util.List;
import java.util.concurrent.TimeUnit;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.oap.server.core.alarm.AlarmCallback;
import org.apache.skywalking.oap.server.core.alarm.AlarmMessage;
import org.apache.skywalking.oap.server.core.alarm.grpc.AlarmServiceGrpc;
import org.apache.skywalking.oap.server.core.alarm.grpc.Response;
import org.apache.skywalking.oap.server.core.alarm.provider.AlarmRulesWatcher;
import org.apache.skywalking.oap.server.library.client.grpc.GRPCClient;
import org.apache.skywalking.oap.server.library.util.GRPCStreamStatus;
/**
* Use SkyWalking alarm grpc API call a remote methods.
*/
@Slf4j
public class GRPCCallback implements AlarmCallback {
private AlarmRulesWatcher alarmRulesWatcher;
private GRPCAlarmSetting alarmSetting;
private AlarmServiceGrpc.AlarmServiceStub alarmServiceStub;
private GRPCClient grpcClient;
public GRPCCallback(AlarmRulesWatcher alarmRulesWatcher) {
this.alarmRulesWatcher = alarmRulesWatcher;
alarmSetting = alarmRulesWatcher.getGrpchookSetting();
if (alarmSetting != null && !alarmSetting.isEmptySetting()) {
grpcClient = new GRPCClient(alarmSetting.getTargetHost(), alarmSetting.getTargetPort());
grpcClient.connect();
alarmServiceStub = AlarmServiceGrpc.newStub(grpcClient.getChannel());
}
}
@Override
public void doAlarm(List<AlarmMessage> alarmMessage) {
if (alarmSetting.isEmptySetting()) {
return;
}
// recreate gRPC client and stub if host and port configuration changed.
onGRPCAlarmSettingUpdated(alarmRulesWatcher.getGrpchookSetting());
GRPCStreamStatus status = new GRPCStreamStatus();
if (alarmServiceStub == null) {
return;
}
StreamObserver<org.apache.skywalking.oap.server.core.alarm.grpc.AlarmMessage> streamObserver =
alarmServiceStub.withDeadlineAfter(10, TimeUnit.SECONDS).doAlarm(new StreamObserver<Response>() {
@Override
public void onNext(Response response) {
// ignore empty response
}
@Override
public void onError(Throwable throwable) {
status.done();
if (log.isDebugEnabled()) {
log.debug("Send alarm message failed: {}", throwable.getMessage());
}
}
@Override
public void onCompleted() {
status.done();
if (log.isDebugEnabled()) {
log.debug("Send alarm message successful.");
}
}
});
alarmMessage.forEach(message -> {
org.apache.skywalking.oap.server.core.alarm.grpc.AlarmMessage.Builder builder =
org.apache.skywalking.oap.server.core.alarm.grpc.AlarmMessage.newBuilder();
builder.setScopeId(message.getScopeId());
builder.setScope(message.getScope());
builder.setName(message.getName());
builder.setId0(message.getId0());
builder.setId1(message.getId1());
builder.setRuleName(message.getRuleName());
builder.setAlarmMessage(message.getAlarmMessage());
builder.setStartTime(message.getStartTime());
streamObserver.onNext(builder.build());
});
streamObserver.onCompleted();
long sleepTime = 0;
long cycle = 100L;
// For memory safe of oap, we must wait for the peer confirmation.
while (!status.isDone()) {
try {
sleepTime += cycle;
Thread.sleep(cycle);
} catch (InterruptedException ignored) {
}
if (log.isDebugEnabled()) {
log.debug("Send {} alarm message to {}:{}.", alarmMessage.size(),
alarmSetting.getTargetHost(), alarmSetting.getTargetPort()
);
}
if (sleepTime > 2000L) {
log.warn("Send {} alarm message to {}:{}, wait {} milliseconds.", alarmMessage.size(),
alarmSetting.getTargetHost(), alarmSetting.getTargetPort(), sleepTime
);
cycle = 2000L;
}
}
}
private void onGRPCAlarmSettingUpdated(GRPCAlarmSetting grpcAlarmSetting) {
if (grpcAlarmSetting == null) {
if (grpcClient != null) {
grpcClient.shutdown();
}
alarmServiceStub = null;
alarmSetting = null;
log.warn("gRPC alarm hook settings about host is empty, shutdown the old gRPC client.");
return;
}
if (!grpcAlarmSetting.equals(alarmSetting)) {
if (grpcClient != null) {
grpcClient.shutdown();
}
grpcClient = new GRPCClient(grpcAlarmSetting.getTargetHost(), grpcAlarmSetting.getTargetPort());
grpcClient.connect();
alarmServiceStub = AlarmServiceGrpc.newStub(grpcClient.getChannel());
}
}
}

View File

@ -0,0 +1,42 @@
/*
* 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.
*
*/
syntax = "proto3";
option java_multiple_files = true;
option java_package = "org.apache.skywalking.oap.server.core.alarm.grpc";
service AlarmService {
rpc doAlarm (stream AlarmMessage) returns (Response) {
}
}
message AlarmMessage {
int64 scopeId = 1;
string scope = 2;
string name = 3;
int64 id0 = 4;
int64 id1 = 5;
string ruleName = 6;
string alarmMessage = 7;
int64 startTime = 8;
}
message Response {
}

View File

@ -31,6 +31,7 @@ import org.mockito.Spy;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@ -78,6 +79,8 @@ public class AlarmRulesWatcherTest {
assertEquals(2, alarmRulesWatcher.getRules().size()); assertEquals(2, alarmRulesWatcher.getRules().size());
assertEquals(2, alarmRulesWatcher.getWebHooks().size()); assertEquals(2, alarmRulesWatcher.getWebHooks().size());
assertNotNull(alarmRulesWatcher.getGrpchookSetting());
assertEquals(9888, alarmRulesWatcher.getGrpchookSetting().getTargetPort());
assertEquals(2, alarmRulesWatcher.getRunningContext().size()); assertEquals(2, alarmRulesWatcher.getRunningContext().size());
} }
@ -92,6 +95,7 @@ public class AlarmRulesWatcherTest {
assertEquals(0, alarmRulesWatcher.getRules().size()); assertEquals(0, alarmRulesWatcher.getRules().size());
assertEquals(0, alarmRulesWatcher.getWebHooks().size()); assertEquals(0, alarmRulesWatcher.getWebHooks().size());
assertNull(alarmRulesWatcher.getGrpchookSetting());
assertEquals(0, alarmRulesWatcher.getRunningContext().size()); assertEquals(0, alarmRulesWatcher.getRunningContext().size());
} }

View File

@ -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.skywalking.oap.server.core.alarm.provider.grpc;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.apache.skywalking.oap.server.core.alarm.grpc.AlarmMessage;
import org.apache.skywalking.oap.server.core.alarm.grpc.AlarmServiceGrpc;
import org.apache.skywalking.oap.server.core.alarm.grpc.Response;
import org.apache.skywalking.oap.server.library.server.ServerException;
import org.apache.skywalking.oap.server.library.server.grpc.GRPCHandler;
import org.apache.skywalking.oap.server.library.server.grpc.GRPCServer;
@Slf4j
public class AlarmMockReceiver {
public static void main(String[] args) throws ServerException, InterruptedException {
GRPCServer server = new GRPCServer("localhost", 9888);
server.initialize();
server.addHandler(new MockAlarmHandler());
server.start();
while (true) {
Thread.sleep(20000L);
}
}
public static class MockAlarmHandler extends AlarmServiceGrpc.AlarmServiceImplBase implements GRPCHandler {
@Override public StreamObserver<AlarmMessage> doAlarm(StreamObserver<Response> responseObserver) {
return new StreamObserver<AlarmMessage>() {
@Override
public void onNext(AlarmMessage value) {
log.info("received alarm message: {}", value.toString());
}
@Override
public void onError(Throwable throwable) {
responseObserver.onError(throwable);
if (log.isDebugEnabled()) {
log.debug("received alarm message error.");
}
responseObserver.onCompleted();
}
@Override
public void onCompleted() {
responseObserver.onNext(Response.newBuilder().build());
responseObserver.onCompleted();
if (log.isDebugEnabled()) {
log.debug("received alarm message completed.");
}
}
};
}
}
}

View File

@ -0,0 +1,79 @@
/*
* 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.skywalking.oap.server.core.alarm.provider.grpc;
import com.google.common.collect.Lists;
import java.util.List;
import org.apache.skywalking.oap.server.core.alarm.AlarmMessage;
import org.apache.skywalking.oap.server.core.alarm.provider.AlarmRulesWatcher;
import org.apache.skywalking.oap.server.core.alarm.provider.Rules;
import org.apache.skywalking.oap.server.core.query.entity.Scope;
import org.junit.Before;
import org.junit.Test;
public class GRPChookCallbackTest {
private GRPCCallback grpcCallback;
private AlarmRulesWatcher alarmRulesWatcher;
private List<AlarmMessage> alarmMessageList;
@Before
public void init() throws Exception {
GRPCAlarmSetting setting = new GRPCAlarmSetting();
setting.setTargetHost("127.0.0.1");
setting.setTargetPort(9888);
Rules rules = new Rules();
rules.setGrpchookSetting(setting);
alarmRulesWatcher = new AlarmRulesWatcher(rules, null);
grpcCallback = new GRPCCallback(alarmRulesWatcher);
mockAlarmMessage();
}
@Test
public void doAlarm() {
grpcCallback.doAlarm(alarmMessageList);
}
@Test
public void testGauchoSettingClean() {
GRPCAlarmSetting grpcAlarmSetting = new GRPCAlarmSetting();
Rules rules = new Rules();
rules.setGrpchookSetting(grpcAlarmSetting);
alarmRulesWatcher = new AlarmRulesWatcher(rules, null);
grpcCallback = new GRPCCallback(alarmRulesWatcher);
grpcCallback.doAlarm(alarmMessageList);
}
private void mockAlarmMessage() {
AlarmMessage alarmMessage = new AlarmMessage();
alarmMessage.setId0(1);
alarmMessage.setId1(2);
alarmMessage.setScope(Scope.Service.name());
alarmMessage.setName("mock alarm message");
alarmMessage.setAlarmMessage("message");
alarmMessage.setRuleName("mock_rule");
alarmMessage.setStartTime(System.currentTimeMillis());
alarmMessageList = Lists.newArrayList(alarmMessage);
}
}

View File

@ -44,3 +44,7 @@ webhooks:
- http://127.0.0.1/notify/ - http://127.0.0.1/notify/
- http://127.0.0.1/go-wechat/ - http://127.0.0.1/go-wechat/
gRPCHook:
target_host: 127.0.0.1
target_port: 9888

View File

@ -43,3 +43,7 @@ webhooks:
# - http://127.0.0.1/notify/ # - http://127.0.0.1/notify/
# - http://127.0.0.1/go-wechat/ # - http://127.0.0.1/go-wechat/
gRPCHook:
# target_host: 127.0.0.1
# target_port: 9888

View File

@ -0,0 +1,34 @@
/*
* 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.skywalking.oap.server.core.exporter;
import lombok.Getter;
import org.apache.skywalking.oap.server.core.analysis.metrics.Metrics;
import org.apache.skywalking.oap.server.core.analysis.metrics.MetricsMetaInfo;
@Getter
public class ExportData {
private MetricsMetaInfo meta;
private Metrics metrics;
public ExportData(MetricsMetaInfo meta, Metrics metrics) {
this.meta = meta;
this.metrics = metrics;
}
}

View File

@ -0,0 +1,31 @@
/*
* 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.skywalking.oap.server.library.util;
import lombok.Getter;
@Getter
public class GRPCStreamStatus {
private volatile boolean done = false;
public void done() {
done = true;
}
}