feat:support polaris event. (#1523)
parent
0cd479f220
commit
364200ece8
@ -1,3 +0,0 @@
|
|||||||
# Auto Configuration
|
|
||||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
|
||||||
com.tencent.cloud.plugin.protection.SecurityProtectionAutoConfiguration
|
|
@ -0,0 +1 @@
|
|||||||
|
com.tencent.cloud.plugin.protection.SecurityProtectionAutoConfiguration
|
@ -0,0 +1,65 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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 com.tencent.cloud.polaris.context.event;
|
||||||
|
|
||||||
|
import com.tencent.cloud.common.constant.OrderConstant;
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisConfigModifier;
|
||||||
|
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
|
||||||
|
import com.tencent.polaris.api.utils.StringUtils;
|
||||||
|
import com.tencent.polaris.factory.config.ConfigurationImpl;
|
||||||
|
import com.tencent.polaris.plugins.event.pushgateway.PushGatewayEventReporterConfig;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Modifier for push gateway event reporter.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PushGatewayEventReporterConfigModifier implements PolarisConfigModifier {
|
||||||
|
|
||||||
|
private final PushGatewayEventReporterProperties properties;
|
||||||
|
|
||||||
|
public PushGatewayEventReporterConfigModifier(PushGatewayEventReporterProperties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void modify(ConfigurationImpl configuration) {
|
||||||
|
configuration.getGlobal().getEventReporter().getReporters()
|
||||||
|
.add(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE);
|
||||||
|
|
||||||
|
PushGatewayEventReporterConfig pushGatewayEventReporterConfig = new PushGatewayEventReporterConfig();
|
||||||
|
if (!properties.isEnabled() || StringUtils.isBlank(properties.getAddress())) {
|
||||||
|
pushGatewayEventReporterConfig.setEnable(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
pushGatewayEventReporterConfig.setEnable(true);
|
||||||
|
}
|
||||||
|
pushGatewayEventReporterConfig.setAddress(properties.getAddress());
|
||||||
|
pushGatewayEventReporterConfig.setEventQueueSize(properties.getEventQueueSize());
|
||||||
|
pushGatewayEventReporterConfig.setMaxBatchSize(properties.getMaxBatchSize());
|
||||||
|
|
||||||
|
configuration.getGlobal().getEventReporter()
|
||||||
|
.setPluginConfig(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE, pushGatewayEventReporterConfig);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getOrder() {
|
||||||
|
return OrderConstant.Modifier.PUSH_GATEWAY_EVENT_ORDER;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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 com.tencent.cloud.polaris.context.event;
|
||||||
|
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Properties for Polaris push gateway event reporter.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
@ConfigurationProperties(prefix = "spring.cloud.polaris.event.pushgateway")
|
||||||
|
public class PushGatewayEventReporterProperties {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* If push gateway event enabled.
|
||||||
|
*/
|
||||||
|
private boolean enabled = false;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Address of pushgateway. For example: 1.2.3.4:9091.
|
||||||
|
*/
|
||||||
|
private String address;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Queue size for push gateway event queue. Default is 1000.
|
||||||
|
*/
|
||||||
|
private int eventQueueSize = 1000;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Max batch size for push gateway event. Default is 100.
|
||||||
|
*/
|
||||||
|
private int maxBatchSize = 100;
|
||||||
|
|
||||||
|
public boolean isEnabled() {
|
||||||
|
return enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEnabled(boolean enabled) {
|
||||||
|
this.enabled = enabled;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAddress() {
|
||||||
|
return address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAddress(String address) {
|
||||||
|
this.address = address;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getEventQueueSize() {
|
||||||
|
return eventQueueSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setEventQueueSize(int eventQueueSize) {
|
||||||
|
this.eventQueueSize = eventQueueSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getMaxBatchSize() {
|
||||||
|
return maxBatchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxBatchSize(int maxBatchSize) {
|
||||||
|
this.maxBatchSize = maxBatchSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PushGatewayEventReporterProperties{" +
|
||||||
|
"enabled=" + enabled +
|
||||||
|
", address='" + address + '\'' +
|
||||||
|
", eventQueueSize=" + eventQueueSize +
|
||||||
|
", maxBatchSize=" + maxBatchSize +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,64 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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 com.tencent.cloud.polaris.context.event;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||||
|
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||||
|
import com.tencent.polaris.api.config.plugin.DefaultPlugins;
|
||||||
|
import com.tencent.polaris.plugins.event.pushgateway.PushGatewayEventReporterConfig;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PushGatewayEventReporterConfigModifier}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PushGatewayEventReporterConfigModifierTest {
|
||||||
|
|
||||||
|
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.enabled=true")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.address=1.2.3.4:9091")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.eventQueueSize=123")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.maxBatchSize=456");
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
PolarisSDKContextManager.innerDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testModify() {
|
||||||
|
this.applicationContextRunner.run(context -> {
|
||||||
|
PolarisSDKContextManager polarisSDKContextManager = context.getBean(PolarisSDKContextManager.class);
|
||||||
|
PushGatewayEventReporterConfig config = polarisSDKContextManager.getSDKContext().
|
||||||
|
getConfig().getGlobal().getEventReporter()
|
||||||
|
.getPluginConfig(DefaultPlugins.PUSH_GATEWAY_EVENT_REPORTER_TYPE, PushGatewayEventReporterConfig.class);
|
||||||
|
assertThat(config.isEnable()).isTrue();
|
||||||
|
assertThat(config.getAddress()).isEqualTo("1.2.3.4:9091");
|
||||||
|
assertThat(config.getEventQueueSize()).isEqualTo(123);
|
||||||
|
assertThat(config.getMaxBatchSize()).isEqualTo(456);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
/*
|
||||||
|
* Tencent is pleased to support the open source community by making spring-cloud-tencent available.
|
||||||
|
*
|
||||||
|
* Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
|
||||||
|
*
|
||||||
|
* Licensed under the BSD 3-Clause License (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://opensource.org/licenses/BSD-3-Clause
|
||||||
|
*
|
||||||
|
* 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 com.tencent.cloud.polaris.context.event;
|
||||||
|
|
||||||
|
import com.tencent.cloud.polaris.context.PolarisSDKContextManager;
|
||||||
|
import com.tencent.cloud.polaris.context.config.PolarisContextAutoConfiguration;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||||
|
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test for {@link PushGatewayEventReporterProperties}.
|
||||||
|
*
|
||||||
|
* @author Haotian Zhang
|
||||||
|
*/
|
||||||
|
public class PushGatewayEventReporterPropertiesTest {
|
||||||
|
|
||||||
|
private final ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
|
||||||
|
.withConfiguration(AutoConfigurations.of(PolarisContextAutoConfiguration.class))
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.enabled=true")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.address=1.2.3.4:9091")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.eventQueueSize=123")
|
||||||
|
.withPropertyValues("spring.cloud.polaris.event.pushgateway.maxBatchSize=456");
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
void setUp() {
|
||||||
|
PolarisSDKContextManager.innerDestroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetAndSet() {
|
||||||
|
this.applicationContextRunner.run(context -> {
|
||||||
|
PushGatewayEventReporterProperties properties = context.getBean(PushGatewayEventReporterProperties.class);
|
||||||
|
assertThat(properties.isEnabled()).isTrue();
|
||||||
|
assertThat(properties.getAddress()).isEqualTo("1.2.3.4:9091");
|
||||||
|
assertThat(properties.getEventQueueSize()).isEqualTo(123);
|
||||||
|
assertThat(properties.getMaxBatchSize()).isEqualTo(456);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue