From 333ce46dac7f9c46fb2eead3c8a00a271c99373f Mon Sep 17 00:00:00 2001 From: Mike Nugent Date: Thu, 21 May 2026 10:05:20 -0400 Subject: [PATCH] SERVER-119085: Make networkCounters a function and duplicate some network counters into otel (#53511) GitOrigin-RevId: 9c348da072ccd87ec6dc34b306b132f40b0d8977 --- src/mongo/client/async_client.cpp | 6 +- src/mongo/client/dbclient_session.cpp | 4 +- .../server_status/server_status_servers.cpp | 2 +- src/mongo/db/stats/BUILD.bazel | 13 ++ src/mongo/db/stats/counters.cpp | 88 ++++++++----- src/mongo/db/stats/counters.h | 35 +++--- src/mongo/db/stats/counters_test.cpp | 117 ++++++++++++++++++ src/mongo/otel/metrics/metric_names.h | 12 ++ .../transport/asio/asio_session_impl.cpp | 6 +- .../transport/asio/asio_tcp_fast_open.cpp | 8 +- .../transport/asio/asio_transport_layer.cpp | 10 +- .../asio/asio_transport_layer_test.cpp | 2 +- src/mongo/transport/grpc/grpc_session.cpp | 13 +- src/mongo/transport/session_workflow.cpp | 5 +- src/mongo/transport/test_fixtures.h | 2 +- 15 files changed, 245 insertions(+), 78 deletions(-) create mode 100644 src/mongo/db/stats/counters_test.cpp diff --git a/src/mongo/client/async_client.cpp b/src/mongo/client/async_client.cpp index d40daddd142..7bdc704242e 100644 --- a/src/mongo/client/async_client.cpp +++ b/src/mongo/client/async_client.cpp @@ -301,7 +301,7 @@ Future AsyncDBClient::_call(Message request, int32_t msgId, const BatonHandle& baton, const CancellationToken& token) { - networkCounter.hitLogicalOut(NetworkCounter::ConnectionType::kEgress, request.size()); + globalNetworkCounter().hitLogicalOut(NetworkCounter::ConnectionType::kEgress, request.size()); auto swm = _compressorManager.compressMessage(request); if (!swm.isOK()) { @@ -344,8 +344,8 @@ Future AsyncDBClient::_waitForResponse(boost::optional msgId, ? _compressorManager.decompressMessage(response) : response; if (swMessage.isOK()) { - networkCounter.hitLogicalIn(NetworkCounter::ConnectionType::kEgress, - swMessage.getValue().size()); + globalNetworkCounter().hitLogicalIn(NetworkCounter::ConnectionType::kEgress, + swMessage.getValue().size()); } return swMessage; }); diff --git a/src/mongo/client/dbclient_session.cpp b/src/mongo/client/dbclient_session.cpp index 8d355121509..fcb7dae8781 100644 --- a/src/mongo/client/dbclient_session.cpp +++ b/src/mongo/client/dbclient_session.cpp @@ -536,7 +536,7 @@ Message DBClientSession::_call(Message& toSend, string* actualServer) { OpMsg::appendChecksum(&toSend); #endif } - networkCounter.hitLogicalOut(NetworkCounter::ConnectionType::kEgress, toSend.size()); + globalNetworkCounter().hitLogicalOut(NetworkCounter::ConnectionType::kEgress, toSend.size()); auto swm = _compressorManager.compressMessage(toSend); uassertStatusOK(swm.getStatus()); @@ -568,7 +568,7 @@ Message DBClientSession::_call(Message& toSend, string* actualServer) { if (response.operation() == dbCompressed) { response = uassertStatusOK(_compressorManager.decompressMessage(response)); } - networkCounter.hitLogicalIn(NetworkCounter::ConnectionType::kEgress, response.size()); + globalNetworkCounter().hitLogicalIn(NetworkCounter::ConnectionType::kEgress, response.size()); killSessionOnError.dismiss(); return response; diff --git a/src/mongo/db/commands/server_status/server_status_servers.cpp b/src/mongo/db/commands/server_status/server_status_servers.cpp index eff0495a113..6c678d6d4c8 100644 --- a/src/mongo/db/commands/server_status/server_status_servers.cpp +++ b/src/mongo/db/commands/server_status/server_status_servers.cpp @@ -67,7 +67,7 @@ public: BSONObj generateSection(OperationContext* opCtx, const BSONElement& configElement) const override { BSONObjBuilder b; - networkCounter.append(b); + globalNetworkCounter().append(b); appendMessageCompressionStats(&b); diff --git a/src/mongo/db/stats/BUILD.bazel b/src/mongo/db/stats/BUILD.bazel index bc7fe1f39dc..8f55a13c2aa 100644 --- a/src/mongo/db/stats/BUILD.bazel +++ b/src/mongo/db/stats/BUILD.bazel @@ -168,6 +168,7 @@ mongo_cc_library( "//src/mongo:base", "//src/mongo/db/commands/server_status:server_status_core", "//src/mongo/db/storage:index_entry_comparison", + "//src/mongo/otel/metrics:otel_metrics_service", "//src/mongo/util/concurrency:spin_lock", ], ) @@ -238,6 +239,18 @@ mongo_cc_unit_test( ], ) +mongo_cc_unit_test( + name = "counters_test", + srcs = [ + "counters_test.cpp", + ], + tags = ["mongo_unittest_seventh_group"], + deps = [ + ":counters", + "//src/mongo/otel/metrics:metrics_test_util", + ], +) + mongo_cc_unit_test( name = "db_stats_test", srcs = [ diff --git a/src/mongo/db/stats/counters.cpp b/src/mongo/db/stats/counters.cpp index 6ab0efbed93..afcb27ef8f7 100644 --- a/src/mongo/db/stats/counters.cpp +++ b/src/mongo/db/stats/counters.cpp @@ -34,6 +34,10 @@ #include "mongo/client/authenticate.h" #include "mongo/db/commands/server_status/server_status.h" #include "mongo/db/operation_context.h" +#include "mongo/otel/metrics/metric_names.h" +#include "mongo/otel/metrics/metric_unit.h" +#include "mongo/otel/metrics/metrics_service.h" +#include "mongo/util/static_immortal.h" #include @@ -43,6 +47,35 @@ namespace mongo { +using ::mongo::otel::metrics::MetricNames; +using ::mongo::otel::metrics::MetricsService; +using ::mongo::otel::metrics::MetricUnit; + +NetworkCounter::NetworkCounter() + : _ingressLogicalBytesIn(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkIngressBytesIn, + "Total number of logical bytes received from ingress (wire-protocol) clients.", + MetricUnit::kBytes)), + _ingressNumRequests(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkIngressNumRequests, + "Total number of requests received from ingress (wire-protocol) clients.", + MetricUnit::kOperations)), + _ingressLogicalBytesOut(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkIngressBytesOut, + "Total number of logical bytes sent to ingress (wire-protocol) clients.", + MetricUnit::kBytes)), + _egressLogicalBytesIn(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkEgressBytesIn, + "Total number of logical bytes received on egress (outbound client) connections.", + MetricUnit::kBytes)), + _egressNumRequests(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkEgressNumRequests, + "Total number of requests sent on egress (outbound client) connections.", + MetricUnit::kOperations)), + _egressLogicalBytesOut(MetricsService::instance().createInt64Counter( + MetricNames::kNetworkEgressBytesOut, + "Total number of logical bytes sent on egress (outbound client) connections.", + MetricUnit::kBytes)) {} void NetworkCounter::hitPhysicalIn(ConnectionType connectionType, long long bytes) { static const int64_t MAX = 1ULL << 60; @@ -75,36 +108,23 @@ void NetworkCounter::hitPhysicalOut(ConnectionType connectionType, long long byt } void NetworkCounter::hitLogicalIn(ConnectionType connectionType, long long bytes) { - static const int64_t MAX = 1ULL << 60; - auto& ref = connectionType == ConnectionType::kIngress ? _ingressTogether : _egressTogether; - - // don't care about the race as its just a counter - const bool overflow = ref->logicalBytesIn.loadRelaxed() > MAX; - - if (overflow) { - ref->logicalBytesIn.store(bytes); - // The requests field only gets incremented here (and not in hitPhysical) because the - // hitLogical and hitPhysical are each called for each operation. Incrementing it in both - // functions would double-count the number of operations. - ref->requests.store(1); + // The requests field only gets incremented here (and not in hitPhysical) because + // hitLogical and hitPhysical are each called for each operation. Incrementing it in both + // functions would double-count the number of operations. + if (connectionType == ConnectionType::kIngress) { + _ingressLogicalBytesIn.add(bytes); + _ingressNumRequests.add(1); } else { - ref->logicalBytesIn.fetchAndAdd(bytes); - ref->requests.fetchAndAdd(1); + _egressLogicalBytesIn.add(bytes); + _egressNumRequests.add(1); } } void NetworkCounter::hitLogicalOut(ConnectionType connectionType, long long bytes) { - static const int64_t MAX = 1ULL << 60; - auto& ref = connectionType == ConnectionType::kIngress ? _ingressLogicalBytesOut - : _egressLogicalBytesOut; - - // don't care about the race as its just a counter - const bool overflow = ref->loadRelaxed() > MAX; - - if (overflow) { - ref->store(bytes); + if (connectionType == ConnectionType::kIngress) { + _ingressLogicalBytesOut.add(bytes); } else { - ref->fetchAndAdd(bytes); + _egressLogicalBytesOut.add(bytes); } } @@ -121,26 +141,24 @@ void NetworkCounter::acceptedTFOIngress() { } void NetworkCounter::append(BSONObjBuilder& b) { - b.append("bytesIn", static_cast(_ingressTogether->logicalBytesIn.loadRelaxed())); - b.append("bytesOut", static_cast(_ingressLogicalBytesOut->loadRelaxed())); + b.append("bytesIn", _ingressLogicalBytesIn.valueForLegacyUse()); + b.append("bytesOut", _ingressLogicalBytesOut.valueForLegacyUse()); b.append("physicalBytesIn", static_cast(_ingressPhysicalBytesIn->loadRelaxed())); b.append("physicalBytesOut", static_cast(_ingressPhysicalBytesOut->loadRelaxed())); BSONObjBuilder egressBuilder(b.subobjStart("egress")); - egressBuilder.append("bytesIn", - static_cast(_egressTogether->logicalBytesIn.loadRelaxed())); - egressBuilder.append("bytesOut", static_cast(_egressLogicalBytesOut->loadRelaxed())); + egressBuilder.append("bytesIn", _egressLogicalBytesIn.valueForLegacyUse()); + egressBuilder.append("bytesOut", _egressLogicalBytesOut.valueForLegacyUse()); egressBuilder.append("physicalBytesIn", static_cast(_egressPhysicalBytesIn->loadRelaxed())); egressBuilder.append("physicalBytesOut", static_cast(_egressPhysicalBytesOut->loadRelaxed())); - egressBuilder.append("numRequests", - static_cast(_egressTogether->requests.loadRelaxed())); + egressBuilder.append("numRequests", _egressNumRequests.valueForLegacyUse()); egressBuilder.done(); b.append("numSlowDNSOperations", static_cast(_numSlowDNSOperations->loadRelaxed())); b.append("numSlowSSLOperations", static_cast(_numSlowSSLOperations->loadRelaxed())); - b.append("numRequests", static_cast(_ingressTogether->requests.loadRelaxed())); + b.append("numRequests", _ingressNumRequests.valueForLegacyUse()); BSONObjBuilder tfo; #ifdef __linux__ @@ -152,6 +170,11 @@ void NetworkCounter::append(BSONObjBuilder& b) { b.append("tcpFastOpen", tfo.obj()); } +NetworkCounter& globalNetworkCounter() { + static StaticImmortal instance; + return *instance; +} + const std::vector kAllMechanisms{std::string(auth::kMechanismMongoX509), std::string(auth::kMechanismSaslPlain), std::string(auth::kMechanismGSSAPI), @@ -325,7 +348,6 @@ void AuthCounter::append(BSONObjBuilder* b) { } -NetworkCounter networkCounter; AuthCounter authCounter; AggStageCounters aggStageCounters{"aggStageCounters."}; DotsAndDollarsFieldsCounters dotsAndDollarsFieldsCounters; diff --git a/src/mongo/db/stats/counters.h b/src/mongo/db/stats/counters.h index 0e69533fddd..641033465a6 100644 --- a/src/mongo/db/stats/counters.h +++ b/src/mongo/db/stats/counters.h @@ -37,6 +37,7 @@ #include "mongo/db/query/plan_executor.h" #include "mongo/db/stats/opcounters.h" #include "mongo/db/topology/cluster_role.h" +#include "mongo/otel/metrics/metrics_counter.h" #include "mongo/platform/atomic_word.h" #include "mongo/rpc/message.h" #include "mongo/util/aligned.h" @@ -63,6 +64,12 @@ namespace MONGO_MOD_PUBLIC mongo { class NetworkCounter { public: enum class ConnectionType { kIngress = 1, kEgress = 2 }; + + NetworkCounter(); + + NetworkCounter(const NetworkCounter&) = delete; + NetworkCounter& operator=(const NetworkCounter&) = delete; + // Increment the counters for the number of bytes read directly off the wire void hitPhysicalIn(ConnectionType connectionType, long long bytes); void hitPhysicalOut(ConnectionType connectionType, long long bytes); @@ -96,28 +103,21 @@ public: void append(BSONObjBuilder& b); private: + // Physical byte counters — not OTel-exported. CacheExclusive> _ingressPhysicalBytesIn{0}; CacheExclusive> _ingressPhysicalBytesOut{0}; - CacheExclusive> _egressPhysicalBytesIn{0}; CacheExclusive> _egressPhysicalBytesOut{0}; - // These two counters are always incremented at the same time, so - // we place them on the same cache line. We use - // CacheCombinedExclusive to ensure that they are combined within - // the scope of a constructive interference region, and protected - // from false sharing by padding out to destructive interference - // size. - struct Together { - AtomicWord logicalBytesIn{0}; - AtomicWord requests{0}; - }; + // Logical ingress counters. + otel::metrics::Counter& _ingressLogicalBytesIn; + otel::metrics::Counter& _ingressNumRequests; + otel::metrics::Counter& _ingressLogicalBytesOut; - CacheCombinedExclusive _ingressTogether{}; - CacheExclusive> _ingressLogicalBytesOut{0}; - - CacheCombinedExclusive _egressTogether{}; - CacheExclusive> _egressLogicalBytesOut{0}; + // Logical egress counters. + otel::metrics::Counter& _egressLogicalBytesIn; + otel::metrics::Counter& _egressNumRequests; + otel::metrics::Counter& _egressLogicalBytesOut; CacheExclusive> _numSlowDNSOperations{0}; CacheExclusive> _numSlowSSLOperations{0}; @@ -131,7 +131,8 @@ private: bool _tfoKernelSupportClient{false}; }; -extern NetworkCounter networkCounter; +/** Returns the process-global NetworkCounter. */ +NetworkCounter& globalNetworkCounter(); class AuthCounter { struct MechanismData; diff --git a/src/mongo/db/stats/counters_test.cpp b/src/mongo/db/stats/counters_test.cpp new file mode 100644 index 00000000000..7e5ce4baeb0 --- /dev/null +++ b/src/mongo/db/stats/counters_test.cpp @@ -0,0 +1,117 @@ +/** + * Copyright (C) 2026-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 + * . + * + * 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/db/stats/counters.h" + +#include "mongo/bson/bsonobjbuilder.h" +#include "mongo/otel/metrics/metric_names.h" +#include "mongo/otel/metrics/metrics_test_util.h" +#include "mongo/unittest/unittest.h" + +namespace mongo { +namespace { + +using ::mongo::otel::metrics::MetricNames; +using ::mongo::otel::metrics::OtelMetricsCapturer; + +BSONObj getNetworkBson(NetworkCounter& nc) { + BSONObjBuilder bob; + nc.append(bob); + return bob.obj(); +} + +TEST(NetworkCounterOtelTest, IngressCountersAreExported) { + OtelMetricsCapturer capturer; + if (!capturer.canReadMetrics()) { + return; + } + + NetworkCounter nc; + + nc.hitLogicalIn(NetworkCounter::ConnectionType::kIngress, 100); + nc.hitLogicalIn(NetworkCounter::ConnectionType::kIngress, 50); + nc.hitLogicalOut(NetworkCounter::ConnectionType::kIngress, 25); + + EXPECT_EQ(150, capturer.readInt64Counter(MetricNames::kNetworkIngressBytesIn)); + EXPECT_EQ(25, capturer.readInt64Counter(MetricNames::kNetworkIngressBytesOut)); + EXPECT_EQ(2, capturer.readInt64Counter(MetricNames::kNetworkIngressNumRequests)); +} + +TEST(NetworkCounterOtelTest, EgressCountersAreExported) { + OtelMetricsCapturer capturer; + if (!capturer.canReadMetrics()) { + return; + } + + NetworkCounter nc; + + nc.hitLogicalIn(NetworkCounter::ConnectionType::kEgress, 200); + nc.hitLogicalOut(NetworkCounter::ConnectionType::kEgress, 80); + + EXPECT_EQ(200, capturer.readInt64Counter(MetricNames::kNetworkEgressBytesIn)); + EXPECT_EQ(80, capturer.readInt64Counter(MetricNames::kNetworkEgressBytesOut)); + EXPECT_EQ(1, capturer.readInt64Counter(MetricNames::kNetworkEgressNumRequests)); +} + +TEST(NetworkCounterBsonTest, IngressBytesInAndNumRequests) { + NetworkCounter nc; + + nc.hitLogicalIn(NetworkCounter::ConnectionType::kIngress, 300); + nc.hitLogicalIn(NetworkCounter::ConnectionType::kIngress, 700); + + BSONObj obj = getNetworkBson(nc); + EXPECT_EQ(1000, obj["bytesIn"].numberLong()); + EXPECT_EQ(2, obj["numRequests"].numberLong()); +} + +TEST(NetworkCounterBsonTest, IngressBytesOut) { + NetworkCounter nc; + + nc.hitLogicalOut(NetworkCounter::ConnectionType::kIngress, 512); + + BSONObj obj = getNetworkBson(nc); + EXPECT_EQ(512, obj["bytesOut"].numberLong()); +} + +TEST(NetworkCounterBsonTest, EgressSubObject) { + NetworkCounter nc; + + nc.hitLogicalIn(NetworkCounter::ConnectionType::kEgress, 400); + nc.hitLogicalOut(NetworkCounter::ConnectionType::kEgress, 100); + + BSONObj obj = getNetworkBson(nc); + ASSERT_TRUE(obj.hasField("egress")); + BSONObj egress = obj["egress"].Obj(); + EXPECT_EQ(400, egress["bytesIn"].numberLong()); + EXPECT_EQ(100, egress["bytesOut"].numberLong()); + EXPECT_EQ(1, egress["numRequests"].numberLong()); +} + +} // namespace +} // namespace mongo diff --git a/src/mongo/otel/metrics/metric_names.h b/src/mongo/otel/metrics/metric_names.h index 0cb9685a04b..29e0c6c0ea9 100644 --- a/src/mongo/otel/metrics/metric_names.h +++ b/src/mongo/otel/metrics/metric_names.h @@ -91,6 +91,18 @@ class MONGO_MOD_FILE_PRIVATE MetricNameMaker{public : static constexpr MetricNam class MetricNames { public: // Networking & Observability Team Metrics + static constexpr MetricName kNetworkIngressBytesIn = + MetricNameMaker::make("serverStatus.network.bytesIn"); + static constexpr MetricName kNetworkIngressBytesOut = + MetricNameMaker::make("serverStatus.network.bytesOut"); + static constexpr MetricName kNetworkIngressNumRequests = + MetricNameMaker::make("serverStatus.network.numRequests"); + static constexpr MetricName kNetworkEgressBytesIn = + MetricNameMaker::make("serverStatus.network.egress.bytesIn"); + static constexpr MetricName kNetworkEgressBytesOut = + MetricNameMaker::make("serverStatus.network.egress.bytesOut"); + static constexpr MetricName kNetworkEgressNumRequests = + MetricNameMaker::make("serverStatus.network.egress.numRequests"); static constexpr MetricName kPrometheusFileExporterWrites = MetricNameMaker::make("metrics.prometheus_file_exporter.writes"); static constexpr MetricName kPrometheusFileExporterWritesFailed = diff --git a/src/mongo/transport/asio/asio_session_impl.cpp b/src/mongo/transport/asio/asio_session_impl.cpp index b76e5c1a467..25cf7284e6f 100644 --- a/src/mongo/transport/asio/asio_session_impl.cpp +++ b/src/mongo/transport/asio/asio_session_impl.cpp @@ -406,7 +406,7 @@ Future CommonAsioSession::sinkMessageImpl(Message message, const BatonHand .then([this, message /*keep the buffer alive*/]() { auto connectionType = _isIngressSession ? NetworkCounter::ConnectionType::kIngress : NetworkCounter::ConnectionType::kEgress; - networkCounter.hitPhysicalOut(connectionType, message.size()); + globalNetworkCounter().hitPhysicalOut(connectionType, message.size()); }) .onCompletion([this](Status status) { _asyncOpState.complete(); @@ -712,7 +712,7 @@ Future CommonAsioSession::sourceMessageImpl(const BatonHandle& baton) { : NetworkCounter::ConnectionType::kEgress; if (msgLen == kHeaderSize) { // This probably isn't a real case since all (current) messages have bodies. - networkCounter.hitPhysicalIn(connectionType, msgLen); + globalNetworkCounter().hitPhysicalIn(connectionType, msgLen); return Future::makeReady(Message(std::move(headerBuffer))); } @@ -722,7 +722,7 @@ Future CommonAsioSession::sourceMessageImpl(const BatonHandle& baton) { MsgData::View msgView(buffer.get()); return read(asio::buffer(msgView.data(), msgView.dataLen()), baton) .then([this, buffer = std::move(buffer), connectionType, msgLen]() mutable { - networkCounter.hitPhysicalIn(connectionType, msgLen); + globalNetworkCounter().hitPhysicalIn(connectionType, msgLen); return Message(std::move(buffer)); }); }) diff --git a/src/mongo/transport/asio/asio_tcp_fast_open.cpp b/src/mongo/transport/asio/asio_tcp_fast_open.cpp index 39ec5208a65..56dc618498e 100644 --- a/src/mongo/transport/asio/asio_tcp_fast_open.cpp +++ b/src/mongo/transport/asio/asio_tcp_fast_open.cpp @@ -138,14 +138,14 @@ bool tryTcpSockOpt(int opt, int val) { /** * Probe the socket API support for TFO-related options on TCP sockets, and - * record the results in the global `networkCounter` object. + * record the results in the global network counter object. */ void checkRelevantSocketOptionsAccepted() { #ifdef TCP_FASTOPEN - networkCounter.setTFOServerSupport(tryTcpSockOpt(TCP_FASTOPEN, 1)); + globalNetworkCounter().setTFOServerSupport(tryTcpSockOpt(TCP_FASTOPEN, 1)); #endif #ifdef TCP_FASTOPEN_CONNECT - networkCounter.setTFOClientSupport(tryTcpSockOpt(TCP_FASTOPEN_CONNECT, 1)); + globalNetworkCounter().setTFOClientSupport(tryTcpSockOpt(TCP_FASTOPEN_CONNECT, 1)); #endif } @@ -204,7 +204,7 @@ void checkEnabledByKernel(bool srv, bool cli) { int64_t k; // The kernel setting. f >> k; - networkCounter.setTFOKernelSetting(k); + globalNetworkCounter().setTFOKernelSetting(k); // Return an integer composed of all bits from 'm' that are missing from 'x'. auto maskBitsMissing = [](uint64_t x, uint64_t m) { diff --git a/src/mongo/transport/asio/asio_transport_layer.cpp b/src/mongo/transport/asio/asio_transport_layer.cpp index 62bde530940..d3c62626e52 100644 --- a/src/mongo/transport/asio/asio_transport_layer.cpp +++ b/src/mongo/transport/asio/asio_transport_layer.cpp @@ -925,7 +925,7 @@ StatusWith> AsioTransportLayer::connect( Milliseconds dnsResolveLatency = Date_t::now() - timeBefore; _dnsResolveStatsMillis.record(durationCount(dnsResolveLatency)); if (dnsResolveLatency > kSlowOperationThreshold) { - networkCounter.incrementNumSlowDNSOperations(); + globalNetworkCounter().incrementNumSlowDNSOperations(); } if (!swEndpoints.isOK()) { @@ -995,7 +995,7 @@ StatusWith> AsioTransportLayer::connect( Date_t timeAfter = Date_t::now(); if (timeAfter - timeBefore > kSlowOperationThreshold) { - networkCounter.incrementNumSlowSSLOperations(); + globalNetworkCounter().incrementNumSlowSSLOperations(); } if (finishLine->arriveStrongly()) { @@ -1210,7 +1210,7 @@ Future> AsioTransportLayer::asyncConnect( "DNS resolution while connecting to peer was slow", "peer"_attr = connector->peer, "duration"_attr = resolveLatency); - networkCounter.incrementNumSlowDNSOperations(); + globalNetworkCounter().incrementNumSlowDNSOperations(); } std::lock_guard lk(connector->mutex); @@ -1278,7 +1278,7 @@ Future> AsioTransportLayer::asyncConnect( connectionMetrics->onTLSHandshakeFinished(); if (duration > kSlowOperationThreshold) { - networkCounter.incrementNumSlowSSLOperations(); + globalNetworkCounter().incrementNumSlowSSLOperations(); } return Status::OK(); }); @@ -1642,7 +1642,7 @@ void AsioTransportLayer::_acceptConnection(GenericAcceptor& acceptor) { TcpInfoOption tcpi{}; peerSocket.get_option(tcpi); if (tcpi->tcpi_options & TCPI_OPT_SYN_DATA) - networkCounter.acceptedTFOIngress(); + globalNetworkCounter().acceptedTFOIngress(); } catch (const asio::system_error&) { } #endif diff --git a/src/mongo/transport/asio/asio_transport_layer_test.cpp b/src/mongo/transport/asio/asio_transport_layer_test.cpp index 7886cc725c4..37ec5ccf08a 100644 --- a/src/mongo/transport/asio/asio_transport_layer_test.cpp +++ b/src/mongo/transport/asio/asio_transport_layer_test.cpp @@ -978,7 +978,7 @@ void runTfoScenario(bool serverOn, bool clientOn, bool expectTfo) { auto extractTfoAccepted = [] { BSONObjBuilder bob; - networkCounter.append(bob); + globalNetworkCounter().append(bob); auto obj = bob.obj(); auto accepted = obj["tcpFastOpen"]["accepted"].numberInt(); return std::pair{accepted, obj}; diff --git a/src/mongo/transport/grpc/grpc_session.cpp b/src/mongo/transport/grpc/grpc_session.cpp index 840d8acd554..9682acc1f91 100644 --- a/src/mongo/transport/grpc/grpc_session.cpp +++ b/src/mongo/transport/grpc/grpc_session.cpp @@ -177,8 +177,8 @@ IngressSession::~IngressSession() { StatusWith IngressSession::_readFromStream() { if (auto maybeBuffer = _stream->read()) { - networkCounter.hitPhysicalIn(NetworkCounter::ConnectionType::kIngress, - MsgData::ConstView(maybeBuffer->get()).getLen()); + globalNetworkCounter().hitPhysicalIn(NetworkCounter::ConnectionType::kIngress, + MsgData::ConstView(maybeBuffer->get()).getLen()); return Message(std::move(*maybeBuffer)); } @@ -194,7 +194,8 @@ StatusWith IngressSession::_readFromStream() { Status IngressSession::_writeToStream(Message message) { if (_stream->write(message.sharedBuffer())) { - networkCounter.hitPhysicalOut(NetworkCounter::ConnectionType::kIngress, message.size()); + globalNetworkCounter().hitPhysicalOut(NetworkCounter::ConnectionType::kIngress, + message.size()); return Status::OK(); } @@ -293,8 +294,8 @@ Future EgressSession::_asyncReadFromStream() { }) .then([this, msg = std::move(msg)]() { _updateWireVersion(); - networkCounter.hitPhysicalIn(NetworkCounter::ConnectionType::kEgress, - MsgData::ConstView(msg->get()).getLen()); + globalNetworkCounter().hitPhysicalIn(NetworkCounter::ConnectionType::kEgress, + MsgData::ConstView(msg->get()).getLen()); return Message(std::move(*msg)); }); } @@ -318,7 +319,7 @@ Future EgressSession::_asyncWriteToStream(Message message) { }); }) .then([msgLen]() { - networkCounter.hitPhysicalOut(NetworkCounter::ConnectionType::kEgress, msgLen); + globalNetworkCounter().hitPhysicalOut(NetworkCounter::ConnectionType::kEgress, msgLen); }); } diff --git a/src/mongo/transport/session_workflow.cpp b/src/mongo/transport/session_workflow.cpp index ce86655e5d1..994ad9eacb3 100644 --- a/src/mongo/transport/session_workflow.cpp +++ b/src/mongo/transport/session_workflow.cpp @@ -811,7 +811,8 @@ Future SessionWorkflow::Impl::_dispatchWork() { return makeDbResponseErrorForRateLimiting(_work->in(), status); } - networkCounter.hitLogicalIn(NetworkCounter::ConnectionType::kIngress, _work->in().size()); + globalNetworkCounter().hitLogicalIn(NetworkCounter::ConnectionType::kIngress, + _work->in().size()); // Pass sourced Message to handler to generate response. _work->initOperation(); @@ -871,7 +872,7 @@ void SessionWorkflow::Impl::_acceptResponse(DbResponse response) { // the dbresponses continue to indicate the exhaust stream should continue. _nextWork = work.synthesizeExhaust(response); - networkCounter.hitLogicalOut(NetworkCounter::ConnectionType::kIngress, toSink.size()); + globalNetworkCounter().hitLogicalOut(NetworkCounter::ConnectionType::kIngress, toSink.size()); beforeCompressingExhaustResponse.executeIf( [&](auto&&) {}, [&](auto&&) { return work.hasCompressorId() && _nextWork; }); diff --git a/src/mongo/transport/test_fixtures.h b/src/mongo/transport/test_fixtures.h index c6a1663ddf3..358fac96fd3 100644 --- a/src/mongo/transport/test_fixtures.h +++ b/src/mongo/transport/test_fixtures.h @@ -340,7 +340,7 @@ struct NetworkConnectionStats { static NetworkConnectionStats get(NetworkCounter::ConnectionType type) { BSONObjBuilder bob; - networkCounter.append(bob); + globalNetworkCounter().append(bob); BSONObj metrics = bob.obj(); if (type == NetworkCounter::ConnectionType::kEgress) { metrics = metrics.getField("egress").Obj().getOwned();