Skip to content
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

fix tls ipv6 metric (#6736) #6783

Merged
Merged
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
32 changes: 24 additions & 8 deletions dbms/src/Server/MetricsPrometheus.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2022 PingCAP, Ltd.
// Copyright 2023 PingCAP, Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -103,7 +103,7 @@ class MetricHandlerFactory : public Poco::Net::HTTPRequestHandlerFactory
std::shared_ptr<Poco::Net::HTTPServer> getHTTPServer(
const TiFlashSecurityConfig & security_config,
const std::weak_ptr<prometheus::Collectable> & collectable,
const String & metrics_port)
const String & address)
{
Poco::Net::Context::Ptr context = new Poco::Net::Context(
Poco::Net::Context::TLSV1_2_SERVER_USE,
Expand All @@ -125,8 +125,7 @@ std::shared_ptr<Poco::Net::HTTPServer> getHTTPServer(
Poco::Net::SecureServerSocket socket(context);

Poco::Net::HTTPServerParams::Ptr http_params = new Poco::Net::HTTPServerParams;

Poco::Net::SocketAddress addr("0.0.0.0", std::stoi(metrics_port));
Poco::Net::SocketAddress addr = Poco::Net::SocketAddress(address);
socket.bind(addr, true);
socket.listen();
auto server = std::make_shared<Poco::Net::HTTPServer>(new MetricHandlerFactory(collectable), socket, http_params);
Expand All @@ -136,6 +135,17 @@ std::shared_ptr<Poco::Net::HTTPServer> getHTTPServer(
constexpr Int64 MILLISECOND = 1000;
constexpr Int64 INIT_DELAY = 5;

namespace
{
inline bool isIPv6(const String & input_address)
{
if (input_address.empty())
return false;
char str[INET6_ADDRSTRLEN];
return inet_pton(AF_INET6, input_address.c_str(), str) == 1;
}
} // namespace

MetricsPrometheus::MetricsPrometheus(
Context & context,
const AsynchronousMetrics & async_metrics_,
Expand Down Expand Up @@ -202,17 +212,23 @@ MetricsPrometheus::MetricsPrometheus(
if (conf.hasOption(status_metrics_port) || !conf.hasOption(status_metrics_addr))
{
auto metrics_port = conf.getString(status_metrics_port, DB::toString(DEFAULT_METRICS_PORT));
auto listen_host = conf.getString("listen_host", "0.0.0.0");
String addr;
if (isIPv6(listen_host))
addr = "[" + listen_host + "]:" + metrics_port;
else
addr = listen_host + ":" + metrics_port;
if (security_config.has_tls_config)
{
server = getHTTPServer(security_config, tiflash_metrics.registry, metrics_port);
server = getHTTPServer(security_config, tiflash_metrics.registry, addr);
server->start();
LOG_INFO(log, "Enable prometheus secure pull mode; Metrics Port = {}", metrics_port);
LOG_INFO(log, "Enable prometheus secure pull mode; Listen Host = {}, Metrics Port = {}", listen_host, metrics_port);
}
else
{
exposer = std::make_shared<prometheus::Exposer>(metrics_port);
exposer = std::make_shared<prometheus::Exposer>(addr);
exposer->RegisterCollectable(tiflash_metrics.registry);
LOG_INFO(log, "Enable prometheus pull mode; Metrics Port = {}", metrics_port);
LOG_INFO(log, "Enable prometheus pull mode; Listen Host = {}, Metrics Port = {}", listen_host, metrics_port);
}
}
else
Expand Down