Skip to content

Commit

Permalink
fix tls ipv6 metric (pingcap#6736) (pingcap#6783)
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot authored Feb 9, 2023
1 parent 10d99c4 commit 4816a7c
Showing 1 changed file with 24 additions and 8 deletions.
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

0 comments on commit 4816a7c

Please sign in to comment.