-
Notifications
You must be signed in to change notification settings - Fork 971
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
[Task] <Support discovery monitoring instance through http_sd> #1625
Comments
@tomsun28 Please assign this issue to me. |
Hi, I feel that we need to carefully consider the following designs
|
Thanks for your advice. I will check the code and consider how to design it better. |
@tomsun28 Hi, I found that Do you mean that Hertzbeat needs to support this feature? |
Hi, it’s like this👍👍. We discover and create monitoring instances automatically throght the http_sd endpoint response content. I thinks there is a difficulty that to design how and when to create monitoring instances and how to dispaly them. |
Hi, @tomsun28
Thus, here are my thoughts: I write a demo as well: public abstract class AbstractCollector<P extends CommonProtocol, D> implements HertzBeatCollector<P, D> {
private P protocol;
public void collect(CollectRep.MetricsData.Builder builder, long monitorId, String app, Metrics metrics) {
protocol = constructNewProtocol();
CollectClient<D> collectClient = null;
try {
// 检查参数
if (!isValidProtocol(protocol)) {
return;
}
//todo 对于通过http_sd定义的集群监控,可以在这里循环进行以下步骤,最后统一聚合到CollectRep.MetricsData.Builder即可
// 创建客户端
collectClient = createCollectClientTemplate(protocol);
// 连接远程客户端
collectClient.connect();
// 采集数据
processCollectedData(builder, collectClient.fetchData());
}catch (Exception exception) {
// 异常处理,随时可以实现并扩展
DefaultExceptionHandler.getInstance().onHandle(exception, builder, null);
}finally {
closeGracefully(collectClient, null);
}
}
public abstract CollectClient<D> createCollectClientTemplate(P protocol) throws Exception;
/**
* the protocol this collect instance support
* @return protocol str
*/
public abstract String supportProtocol();
private P constructNewProtocol() {
// clone a new protocol
// redefine base config here, like http_sd
// 这里直接面向CommonProtocol编码即可
return null;
}
private void closeGracefully(CollectClient<D> collectClient, Logger logger) {
if (Objects.isNull(collectClient)) {
return;
}
try {
collectClient.close();
} catch (Exception exception) {
if (Objects.nonNull(logger) && logger.isWarnEnabled()) {
logger.warn("Failed to close CollectClient: {}", CommonUtil.getMessageFromThrowable(exception));
}
}
}
} SmtpDemo: // Define the class of protocol and response data when you extend AbstractCollector
public class SmtpDemo extends AbstractCollector<SmtpProtocol, String> {
@Override
public boolean isValidProtocol(SmtpProtocol protocol) {
// check SmtpProtocol here...
return true;
}
@Override
public CollectClient<String> createCollectClientTemplate(SmtpProtocol protocol) {
// create client and define each action here...
String host = protocol.getHost();
String port = protocol.getPort();
int timeout = CollectUtil.getTimeout(protocol.getTimeout());
SMTP smtp = new SMTP();
smtp.setConnectTimeout(timeout);
smtp.setCharset(StandardCharsets.UTF_8);
return new CollectClient<>() {
@Override
public void connect() throws Exception {
// connect remote client...
smtp.connect(host, Integer.parseInt(port));
}
@Override
public String fetchData() throws Exception {
// fetch data from remote...
smtp.helo(protocol.getEmail());
return smtp.getReplyString();
}
@Override
public void close() throws Exception {
// close client...
smtp.disconnect();
}
};
}
@Override
public void processCollectedData(CollectRep.MetricsData.Builder builder, String collectedData) {
// transfer collected data and build HertzBeat data here...
}
@Override
public String supportProtocol() {
return null;
}
} What are the benefits if we refactor Collector as mentioned about?
It will be a dramatical refactor and needs to modify each Collector. |
Hi, better design, 👍👍👍 I think this is a great abstraction of collect capabilities and protocol. |
Maybe creating a wrapper for |
Yeah, 👍👍 Geat! Can you sumbit a pr for this, now we are publishing new version, we can merge this after release. |
I will create a pr tonight. |
Description
Refer:
https://prometheus.io/docs/prometheus/latest/configuration/configuration/#http_sd_config
https://prometheus.io/docs/prometheus/latest/http_sd/
Task List
No response
The text was updated successfully, but these errors were encountered: