Skip to content

feat:support polaris event. #1543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@
- [feat:support zuul circuit breaker fallback response.](https://github.com/Tencent/spring-cloud-tencent/pull/1522)
- [feat:support auth.](https://github.com/Tencent/spring-cloud-tencent/pull/1541)
- [feat:support smooth upgrade from tsf. ](https://github.com/Tencent/spring-cloud-tencent/pull/1542)
- [feat:support polaris event.](https://github.com/Tencent/spring-cloud-tencent/pull/1543)

1 change: 0 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,6 @@
<type>pom</type>
<scope>import</scope>
</dependency>

</dependencies>
</dependencyManagement>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,15 @@

package com.tencent.cloud.polaris.registry;

import java.time.LocalDateTime;

import com.tencent.cloud.polaris.PolarisDiscoveryProperties;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.event.FlowEvent;
import com.tencent.polaris.api.plugin.event.FlowEventConstants;
import com.tencent.polaris.api.pojo.ServiceEventKey;
import com.tencent.polaris.assembly.api.AssemblyAPI;
import com.tencent.polaris.client.flow.BaseFlow;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,6 +92,25 @@ protected void registerManagement() {

@Override
protected void deregister() {
// report thread end event.
if (this.registration.getPolarisContext() != null
&& this.registration.getPolarisContext().getExtensions() != null) {
Extensions extensions = this.registration.getPolarisContext().getExtensions();
FlowEvent.Builder flowEventBuilder = new FlowEvent.Builder()
.withEventType(ServiceEventKey.EventType.INSTANCE)
.withEventName(FlowEventConstants.EventName.InstanceThreadEnd)
.withTimestamp(LocalDateTime.now())
.withClientId(extensions.getValueContext().getClientId())
.withClientIp(extensions.getValueContext().getHost())
.withNamespace(polarisDiscoveryProperties.getNamespace())
.withService(registration.getServiceId())
.withHost(registration.getHost())
.withPort(registration.getPort());

FlowEvent flowEvent = flowEventBuilder.build();
BaseFlow.reportFlowEvent(extensions, flowEvent);
}

if (!this.registration.isRegisterEnabled()) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,12 @@ public class PolarisRegistration implements Registration {
private final SDKContext polarisContext;

private final StaticMetadataManager staticMetadataManager;

private String serviceId;
private final String host;
private final boolean isSecure;
private final ServletWebServerApplicationContext servletWebServerApplicationContext;
private final ReactiveWebServerApplicationContext reactiveWebServerApplicationContext;
private final List<PolarisRegistrationCustomizer> customizers;
private String serviceId;
private boolean registerEnabled = false;
private Map<String, String> metadata;
private Map<String, Map<String, String>> extendedMetadata;
Expand Down Expand Up @@ -164,15 +163,15 @@ public void customize() {
}
}

public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}

@Override
public String getServiceId() {
return serviceId;
}

public void setServiceId(String serviceId) {
this.serviceId = serviceId;
}

@Override
public String getHost() {
return host;
Expand Down Expand Up @@ -238,6 +237,10 @@ public boolean isRegisterEnabled() {
return registerEnabled;
}

public SDKContext getPolarisContext() {
return polarisContext;
}

@Override
public String toString() {
return "PolarisRegistration{" +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,10 @@ public static final class Modifier {
* Order of trace configuration modifier.
*/
public static Integer TRACE_ORDER = 2;

/**
* Order of push gateway event reporter modifier.
*/
public static Integer PUSH_GATEWAY_EVENT_ORDER = 2;
}
}
3 changes: 1 addition & 2 deletions spring-cloud-tencent-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<revision>2.0.2.0-Hoxton.SR12-SNAPSHOT</revision>

<!-- Dependencies -->
<polaris.version>2.0.0.1</polaris.version>
<polaris.version>2.0.1.0-RC1</polaris.version>
<guava.version>32.1.3-jre</guava.version>
<logback.version>1.2.13</logback.version>
<springdoc.version>1.7.0</springdoc.version>
Expand Down Expand Up @@ -146,7 +146,6 @@
<version>${revision}</version>
</dependency>

<!-- polaris starter-->
<dependency>
<groupId>com.tencent.cloud</groupId>
<artifactId>spring-cloud-starter-tencent-polaris-ratelimit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import com.tencent.cloud.polaris.context.config.extend.consul.ConsulProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfCoreProperties;
import com.tencent.cloud.polaris.context.config.extend.tsf.TsfInstanceMetadataProvider;
import com.tencent.cloud.polaris.context.event.PushGatewayEventReporterConfigModifier;
import com.tencent.cloud.polaris.context.event.PushGatewayEventReporterProperties;
import com.tencent.cloud.polaris.context.listener.PolarisContextApplicationEventListener;
import com.tencent.polaris.api.exception.PolarisException;
import com.tencent.polaris.client.api.SDKContext;
Expand Down Expand Up @@ -81,6 +83,18 @@ public PolarisAdminConfigModifier polarisAdminConfigModifier(PolarisAdminPropert
return new PolarisAdminConfigModifier(polarisAdminProperties);
}

@Bean
@ConditionalOnMissingBean
public PushGatewayEventReporterProperties pushGatewayEventReporterProperties() {
return new PushGatewayEventReporterProperties();
}

@Bean
@ConditionalOnMissingBean
public PushGatewayEventReporterConfigModifier pushGatewayEventReporterConfigModifier(PushGatewayEventReporterProperties pushGatewayEventReporterProperties) {
return new PushGatewayEventReporterConfigModifier(pushGatewayEventReporterProperties);
}

@Bean
@ConditionalOnMissingBean
public ConsulProperties consulProperties() {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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 +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@
"type": "java.lang.String",
"defaultValue": "./polaris/logs",
"description": "polaris log storage path."
},
{
"name": "spring.cloud.polaris.event.pushgateway.enabled",
"type": "java.lang.Boolean",
"description": "If push gateway event reporter is enabled.",
"default": false
},
{
"name": "spring.cloud.polaris.event.pushgateway.address",
"type": "java.lang.String",
"description": "Address of pushgateway. For example: 1.2.3.4:9091."
},
{
"name": "spring.cloud.polaris.event.pushgateway.eventQueueSize",
"type": "java.lang.Integer",
"description": "Queue size for push gateway event queue. Default is 1000.",
"default": 1000
},
{
"name": "spring.cloud.polaris.event.pushgateway.maxBatchSize",
"type": "java.lang.Integer",
"description": "Max batch size for push gateway event. Default is 100.",
"default": 100
}
],
"hints": []
Expand Down
Loading
Loading