Skip to content

Commit

Permalink
This is an automated cherry-pick of pingcap#6736
Browse files Browse the repository at this point in the history
Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
  • Loading branch information
ywqzzy authored and ti-chi-bot committed Feb 9, 2023
1 parent 10d99c4 commit fa3300a
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 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,29 @@ 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));
<<<<<<< HEAD
if (security_config.has_tls_config)
{
server = getHTTPServer(security_config, tiflash_metrics.registry, 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 (context.getSecurityConfig()->hasTlsConfig())
{
server = getHTTPServer(context, tiflash_metrics.registry, addr);
>>>>>>> 50e025a3f1 (fix tls ipv6 metric (#6736))
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 fa3300a

Please sign in to comment.