diff options
| author | Mark Benvenuto <mark.benvenuto@mongodb.com> | 2023-05-02 11:54:43 -0400 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2023-05-10 02:21:42 +0000 |
| commit | fc832685b99221cffb1f5bb5a4ff5ad3e1c416b2 (patch) | |
| tree | a14fd82effc1a017d75c87467e04f63f2c4623fe | |
| parent | e766fdc40fc3131d3646e2c00733c7cc2158d358 (diff) | |
SERVER-76619 Add setParameter to enable libcurl's verbose loggingr4.4.22-rc2r4.4.22
(cherry picked from commit bf9e8a4844e720b90cc6a939b23e08f7aeb0df20)
| -rw-r--r-- | src/mongo/util/net/SConscript | 2 | ||||
| -rw-r--r-- | src/mongo/util/net/http_client_curl.cpp | 53 | ||||
| -rw-r--r-- | src/mongo/util/net/http_client_options.cpp | 36 | ||||
| -rw-r--r-- | src/mongo/util/net/http_client_options.h | 46 | ||||
| -rw-r--r-- | src/mongo/util/net/http_client_options.idl | 38 |
5 files changed, 171 insertions, 4 deletions
diff --git a/src/mongo/util/net/SConscript b/src/mongo/util/net/SConscript index 17bf060c292..464d78233b5 100644 --- a/src/mongo/util/net/SConscript +++ b/src/mongo/util/net/SConscript @@ -211,6 +211,8 @@ else: env.Library( target='http_client_impl', source=[ + 'http_client_options.idl', + 'http_client_options.cpp', 'http_client_winhttp.cpp' if env.TargetOSIs('windows') else 'http_client_curl.cpp', ], LIBDEPS=[ diff --git a/src/mongo/util/net/http_client_curl.cpp b/src/mongo/util/net/http_client_curl.cpp index 82433b4caf0..57290d0f8ed 100644 --- a/src/mongo/util/net/http_client_curl.cpp +++ b/src/mongo/util/net/http_client_curl.cpp @@ -27,6 +27,8 @@ * it in the license file. */ +#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kNetwork + #include "mongo/platform/basic.h" #include <cstddef> @@ -47,6 +49,7 @@ #include "mongo/db/commands/server_status.h" #include "mongo/executor/connection_pool.h" #include "mongo/executor/connection_pool_stats.h" +#include "mongo/logv2/log.h" #include "mongo/platform/mutex.h" #include "mongo/stdx/unordered_map.h" #include "mongo/transport/transport_layer.h" @@ -58,12 +61,12 @@ #include "mongo/util/functional.h" #include "mongo/util/net/hostandport.h" #include "mongo/util/net/http_client.h" +#include "mongo/util/net/http_client_options.h" #include "mongo/util/processinfo.h" #include "mongo/util/strong_weak_finish_line.h" #include "mongo/util/system_clock_source.h" #include "mongo/util/timer.h" - namespace mongo { namespace { @@ -217,6 +220,47 @@ long longSeconds(Seconds tm) { return static_cast<long>(durationCount<Seconds>(tm)); } + +StringData enumToString(curl_infotype type) { + switch (type) { + case CURLINFO_TEXT: + return "TEXT"_sd; + case CURLINFO_HEADER_IN: + return "HEADER_IN"_sd; + case CURLINFO_HEADER_OUT: + return "HEADER_OUT"_sd; + case CURLINFO_DATA_IN: + return "DATA_IN"_sd; + case CURLINFO_DATA_OUT: + return "DATA_OUT"_sd; + case CURLINFO_SSL_DATA_IN: + return "SSL_DATA_IN"_sd; + case CURLINFO_SSL_DATA_OUT: + return "SSL_DATA_OUT"_sd; + default: + return "unknown"_sd; + } +} + +int curlDebugCallback(CURL* handle, curl_infotype type, char* data, size_t size, void* clientp) { + switch (type) { + case CURLINFO_TEXT: + case CURLINFO_HEADER_IN: + case CURLINFO_HEADER_OUT: + case CURLINFO_DATA_IN: + case CURLINFO_DATA_OUT: + LOGV2_DEBUG(7661901, + 1, + "Curl", + "type"_attr = enumToString(type), + "message"_attr = StringData(data, size)); + [[fallthrough]]; + + default: + return 0; + } +} + CurlEasyHandle createCurlEasyHandle(Protocols protocol) { CurlEasyHandle handle(curl_easy_init()); uassert(ErrorCodes::InternalError, "Curl initialization failed", handle); @@ -247,9 +291,10 @@ CurlEasyHandle createCurlEasyHandle(Protocols protocol) { } // TODO: CURLOPT_EXPECT_100_TIMEOUT_MS? - // TODO: consider making this configurable, defaults to stderr - // curl_easy_setopt(handle.get(), CURLOPT_VERBOSE, 1); - // curl_easy_setopt(_handle.get(), CURLOPT_DEBUGFUNCTION , ???); + if (httpClientOptions.verboseLogging.loadRelaxed()) { + curl_easy_setopt(handle.get(), CURLOPT_VERBOSE, 1); + curl_easy_setopt(handle.get(), CURLOPT_DEBUGFUNCTION, curlDebugCallback); + } return handle; } diff --git a/src/mongo/util/net/http_client_options.cpp b/src/mongo/util/net/http_client_options.cpp new file mode 100644 index 00000000000..7e96be222ac --- /dev/null +++ b/src/mongo/util/net/http_client_options.cpp @@ -0,0 +1,36 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#include "mongo/util/net/http_client_options.h" + +namespace mongo { + +HttpClientOptions httpClientOptions; + +} diff --git a/src/mongo/util/net/http_client_options.h b/src/mongo/util/net/http_client_options.h new file mode 100644 index 00000000000..ebf4611677a --- /dev/null +++ b/src/mongo/util/net/http_client_options.h @@ -0,0 +1,46 @@ +/** + * Copyright (C) 2023-present MongoDB, Inc. + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the Server Side Public License, version 1, + * as published by MongoDB, Inc. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * Server Side Public License for more details. + * + * You should have received a copy of the Server Side Public License + * along with this program. If not, see + * <http://www.mongodb.com/licensing/server-side-public-license>. + * + * As a special exception, the copyright holders give permission to link the + * code of portions of this program with the OpenSSL library under certain + * conditions as described in each individual source file and distribute + * linked combinations including the program with the OpenSSL library. You + * must comply with the Server Side Public License in all respects for + * all of the code used other than as permitted herein. If you modify file(s) + * with this exception, you may extend this exception to your version of the + * file(s), but you are not obligated to do so. If you do not wish to do so, + * delete this exception statement from your version. If you delete this + * exception statement from all source files in the program, then also delete + * it in the license file. + */ + +#pragma once + +#include "mongo/platform/atomic_word.h" + +namespace mongo { +struct HttpClientOptions { + /** + * Boolean flag that indicates whether verbose logging for http clients should be enabled. + * + * Note: only affects new handles. This means that if connection pooling is in use, this will + * not take affect on existing connections. + */ + AtomicWord<bool> verboseLogging; +}; + +extern HttpClientOptions httpClientOptions; +} // namespace mongo diff --git a/src/mongo/util/net/http_client_options.idl b/src/mongo/util/net/http_client_options.idl new file mode 100644 index 00000000000..b53b5e5541b --- /dev/null +++ b/src/mongo/util/net/http_client_options.idl @@ -0,0 +1,38 @@ +# Copyright (C) 2023-present MongoDB, Inc. +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the Server Side Public License, version 1, +# as published by MongoDB, Inc. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# Server Side Public License for more details. +# +# You should have received a copy of the Server Side Public License +# along with this program. If not, see +# <http://www.mongodb.com/licensing/server-side-public-license>. +# +# As a special exception, the copyright holders give permission to link the +# code of portions of this program with the OpenSSL library under certain +# conditions as described in each individual source file and distribute +# linked combinations including the program with the OpenSSL library. You +# must comply with the Server Side Public License in all respects for +# all of the code used other than as permitted herein. If you modify file(s) +# with this exception, you may extend this exception to your version of the +# file(s), but you are not obligated to do so. If you do not wish to do so, +# delete this exception statement from your version. If you delete this +# exception statement from all source files in the program, then also delete +# it in the license file. + +global: + cpp_namespace: "mongo" + cpp_includes: + - "mongo/util/net/http_client_options.h" + +server_parameters: + httpVerboseLogging: + description: Boolean flag that indicates whether verbose logging for http clients should be enabled. + set_at: [ startup, runtime ] + default: false + cpp_varname: "httpClientOptions.verboseLogging" |
