Skip to content

Commit

Permalink
feat:add admin http handler.
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyeBeFreeman committed Sep 27, 2024
1 parent e69df8c commit 9dc473c
Show file tree
Hide file tree
Showing 19 changed files with 613 additions and 811 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.tencent.polaris.api.config.Configuration;
import com.tencent.polaris.api.config.global.StatReporterConfig;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.factory.config.global.AdminConfigImpl;
import com.tencent.polaris.factory.config.global.StatReporterConfigImpl;
import com.tencent.polaris.plugins.stat.prometheus.handler.PrometheusHandlerConfig;
import com.tencent.polaris.test.common.TestUtils;
Expand All @@ -37,10 +38,10 @@ public class PrometheusHttpServerTest {
@Test
public void testHttpServerWithPort() throws IOException {
Configuration configuration = TestUtils.configWithEnvAddress();
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(18080);
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
prometheusHandlerConfig.setHost("0.0.0.0");
prometheusHandlerConfig.setPort(18080);
prometheusHandlerConfig.setPath("/metric");
prometheusHandlerConfig.setType("pull");
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
Expand All @@ -58,10 +59,10 @@ public void testHttpServerWithPort() throws IOException {
@Test
public void testHttpServerRandomPort() throws IOException {
Configuration configuration = TestUtils.configWithEnvAddress();
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(0);
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
prometheusHandlerConfig.setHost("0.0.0.0");
prometheusHandlerConfig.setPort(0);
prometheusHandlerConfig.setPath("/metric");
prometheusHandlerConfig.setType("pull");
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
Expand All @@ -85,10 +86,10 @@ public void testHttpServerRandomPort() throws IOException {
@Test
public void testHttpServerWithPath() throws IOException {
Configuration configuration = TestUtils.configWithEnvAddress();
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl)configuration.getGlobal().getStatReporter();
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setHost("0.0.0.0");
((AdminConfigImpl) configuration.getGlobal().getAdmin()).setPort(18081);
StatReporterConfigImpl statReporterConfig = (StatReporterConfigImpl) configuration.getGlobal().getStatReporter();
PrometheusHandlerConfig prometheusHandlerConfig = new PrometheusHandlerConfig();
prometheusHandlerConfig.setHost("0.0.0.0");
prometheusHandlerConfig.setPort(18081);
prometheusHandlerConfig.setPath("/customMetrics");
prometheusHandlerConfig.setType("pull");
statReporterConfig.setPluginConfig(StatReporterConfig.DEFAULT_REPORTER_PROMETHEUS, prometheusHandlerConfig);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ public class SDKContext extends Destroyable implements InitContext, AutoCloseabl
* 构造器
*
* @param configuration 配置
* @param plugins 插件工厂
* @param valueContext 上下文
* @param plugins 插件工厂
* @param valueContext 上下文
*/
public SDKContext(Configuration configuration, Manager plugins, ValueContext valueContext) {
this.configuration = configuration;
Expand Down Expand Up @@ -311,7 +311,7 @@ public synchronized void init() throws PolarisException {
}
extensions.init(configuration, plugins, valueContext);
plugins.postContextInitPlugins(extensions);
extensions.initHttpServer(plugins);
extensions.initHttpServer(configuration, plugins);
reportClient(extensions);
registerDestroyHook(extensions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ global:
#描述: 事件上报插件名列表
reporters:
- logger
# 描述:Admin相关的配置
admin:
# 描述:Admin的监听的IP
host: 0.0.0.0
# 描述:Admin监听的端口
port: 28080
#描述: 监控及日志数据上报相关配置
statReporter:
#描述: 是否启用上报
Expand All @@ -97,19 +103,10 @@ global:
#默认值:pull
#范围:pull|push
type: pull
#描述: 设置 prometheus http-server 的监听端口
#类型:int
#默认值: 28080, 默认会自动从 [28080, 28090] 进行逐个探测占用
#如果设置为负数,则不会开启默认的http-server,如果设置为0,则随机选择一个可用端口进行启动 http-server
port: 28080
#描述: 设置 prometheus http-server 的拉取path
#类型:string
#默认值: /metric
path: /metric
#描述: 设置 prometheus http-server 的监听的IP
#类型:string
#默认值: 0.0.0.0
host: 0.0.0.0
# #描述: 设置 pushgateway 的地址, 仅 type == push 时生效
# #类型:string
# #默认 global.serverConnector.addresses[0]:9091
Expand Down Expand Up @@ -296,10 +293,6 @@ provider:
lossless:
# 是否启用优雅上下线
enable: false
# 优雅上下线功能需要对外提供接口,接口的监听IP
host: 0.0.0.0
# 优雅上下线功能需要对外提供接口,接口的监听端口
port: 28080
# 优雅上线时,如果没有启动探测,则兜底延迟多久会进行上线
delayRegisterInterval: 30s
# 优雅上线的探测周期
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 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.polaris.api.config.global;

import com.tencent.polaris.api.config.verify.Verifier;

/**
* Admin Config.
*
* @author Haotian Zhang
*/
public interface AdminConfig extends Verifier {

/**
* Admin的监听IP
*
* @return host
*/
String getHost();

/**
* Admin的监听端口
*
* @return port
*/
int getPort();
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ public interface GlobalConfig extends Verifier {
*/
EventReporterConfig getEventReporter();

/**
* Configuration of prefix of "global.admin"
*
* @return
*/
AdminConfig getAdmin();

/**
* 获取地理位置提供者配置信息
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,22 @@ public interface LosslessConfig extends Verifier {

/**
* 是否启用无损上下线
*
* @return 启用无损上下线
*/
boolean isEnable();

/**
* 无损下线和获取上线状态的监听IP
* @return host
*/
String getHost();

/**
* 无损下线和获取上线状态的监听端口
* @return port
*/
int getPort();

/**
* 如果没有实现健康检查,延迟注册的时间,单位毫秒
*
* @return long
*/
long getDelayRegisterInterval();

/**
* 获取健康探测的间隔时间
*
* @return long
*/
long getHealthCheckInterval();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 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.polaris.factory.config.global;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.tencent.polaris.api.config.global.AdminConfig;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.factory.util.ConfigUtils;

public class AdminConfigImpl implements AdminConfig {

@JsonProperty
private String host;

@JsonProperty
private Integer port;

@Override
public String getHost() {
return host;
}

public void setHost(String host) {
this.host = host;
}

@Override
public int getPort() {
return port;
}

public void setPort(Integer port) {
this.port = port;
}

@Override
public void verify() {
ConfigUtils.validateString(host, "global.admin.host");
ConfigUtils.validatePositiveInteger(port, "global.admin.port");
}

@Override
public void setDefault(Object defaultObject) {
if (null != defaultObject) {
AdminConfig adminConfig = (AdminConfig) defaultObject;
if (null == port || 0 == port) {
setPort(adminConfig.getPort());
}
if (StringUtils.isBlank(host)) {
setHost(adminConfig.getHost());
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public class GlobalConfigImpl implements GlobalConfig {
private EventReporterConfigImpl eventReporter;

@JsonProperty
private LocationConfigImpl location;
private AdminConfigImpl admin;

@JsonProperty
private LocationConfigImpl location;

@Override
public SystemConfigImpl getSystem() {
Expand All @@ -80,7 +82,6 @@ public ServerConnectorConfigImpl getServerConnector() {
return serverConnector;
}


@Override
public LocationConfig getLocation() {
return location;
Expand Down Expand Up @@ -127,6 +128,11 @@ public EventReporterConfigImpl getEventReporter() {
return eventReporter;
}

@Override
public AdminConfigImpl getAdmin() {
return admin;
}

@Override
public void verify() {
ConfigUtils.validateNull(system, "system");
Expand All @@ -138,6 +144,7 @@ public void verify() {
ConfigUtils.validateNull(statReporter, "statReporter");
ConfigUtils.validateNull(traceReporter, "traceReporter");
ConfigUtils.validateNull(eventReporter, "eventReporter");
ConfigUtils.validateNull(admin, "admin");

system.verify();
api.verify();
Expand All @@ -155,6 +162,7 @@ public void verify() {
statReporter.verify();
traceReporter.verify();
eventReporter.verify();
admin.verify();
}

@Override
Expand All @@ -177,6 +185,9 @@ public void setDefault(Object defaultObject) {
if (null == eventReporter) {
eventReporter = new EventReporterConfigImpl();
}
if (null == admin) {
admin = new AdminConfigImpl();
}
if (null == location) {
location = new LocationConfigImpl();
}
Expand All @@ -201,6 +212,7 @@ public void setDefault(Object defaultObject) {
statReporter.setDefault(globalConfig.getStatReporter());
traceReporter.setDefault(globalConfig.getTraceReporter());
eventReporter.setDefault(globalConfig.getEventReporter());
admin.setDefault(globalConfig.getAdmin());
location.setDefault(globalConfig.getLocation());
}
}
Expand All @@ -215,6 +227,7 @@ public String toString() {
", statReporter=" + statReporter +
", traceReporter=" + traceReporter +
", eventReporter=" + eventReporter +
", admin=" + admin +
", location=" + location +
'}';
}
Expand Down
Loading

0 comments on commit 9dc473c

Please sign in to comment.