diff --git a/neteng/fboss/bgp/cpp/BgpServiceBase.cpp b/neteng/fboss/bgp/cpp/BgpServiceBase.cpp index 77221b7..0e75014 100644 --- a/neteng/fboss/bgp/cpp/BgpServiceBase.cpp +++ b/neteng/fboss/bgp/cpp/BgpServiceBase.cpp @@ -37,6 +37,7 @@ #include "neteng/fboss/bgp/cpp/BgpProfiler.h" #include "neteng/fboss/bgp/cpp/BgpServiceBase.h" #include "neteng/fboss/bgp/cpp/BgpServiceUtil.h" +#include "neteng/fboss/bgp/cpp/common/BuildInfo.h" #include "neteng/fboss/bgp/cpp/common/EvbUtils.h" #include "neteng/fboss/bgp/cpp/peer/PeerManagerBase.h" #include "neteng/fboss/bgp/cpp/stats/StatsBase.h" @@ -92,6 +93,11 @@ TBgpNetwork toBgpNetwork(const TIpPrefix& prefix, const TBgpPath& path) { namespace facebook::bgp { +void BgpServiceBase::getBuildInfo( + std::map& info) { + info = BuildInfo::toMap(); +} + BgpServiceBase::BgpServiceBase( PeerManagerBase& peerMgr, std::shared_ptr configManager, diff --git a/neteng/fboss/bgp/cpp/BgpServiceBase.h b/neteng/fboss/bgp/cpp/BgpServiceBase.h index 7889448..fa25ae2 100644 --- a/neteng/fboss/bgp/cpp/BgpServiceBase.h +++ b/neteng/fboss/bgp/cpp/BgpServiceBase.h @@ -16,6 +16,7 @@ #pragma once +#include #include #include #include @@ -53,6 +54,8 @@ class BgpServiceBase bool enable_thrift_protection); ~BgpServiceBase() override = default; + void getBuildInfo(std::map& info) override; + /** * [Logging] * diff --git a/neteng/fboss/bgp/cpp/common/BuildInfo.cpp b/neteng/fboss/bgp/cpp/common/BuildInfo.cpp index 68d3acf..9928c84 100644 --- a/neteng/fboss/bgp/cpp/common/BuildInfo.cpp +++ b/neteng/fboss/bgp/cpp/common/BuildInfo.cpp @@ -88,6 +88,27 @@ const char* BuildInfo::getBuildMode() { return BuildInfo_kBuildMode; } +std::map BuildInfo::toMap() { + const auto packageName = std::string(getBuildPackageName()).empty() + ? "bgp" + : getBuildPackageName(); + const auto packageVersion = std::string(getBuildPackageVersion()).empty() + ? getBuildRevision() + : getBuildPackageVersion(); + return { + {"build_package_name", packageName}, + {"build_package_version", packageVersion}, + {"build_package_info", + fmt::format("{}-{}", packageName, packageVersion)}, + {"build_user", getBuildUser()}, + {"build_time", getBuildTime()}, + {"build_host", getBuildHost()}, + {"build_path", getBuildPath()}, + {"build_platform", getBuildPlatform()}, + {"build_revision", getBuildRevision()}, + }; +} + std::string BuildInfo::toDebugString() { return fmt::format( "\n Built by: {}\n" diff --git a/neteng/fboss/bgp/cpp/common/BuildInfo.h b/neteng/fboss/bgp/cpp/common/BuildInfo.h index 5d06dd9..d5c4872 100644 --- a/neteng/fboss/bgp/cpp/common/BuildInfo.h +++ b/neteng/fboss/bgp/cpp/common/BuildInfo.h @@ -17,6 +17,7 @@ #pragma once #include +#include #include #include @@ -46,6 +47,9 @@ class BuildInfo { static const char* getBuildTool(); static const char* getBuildMode(); + /** Returns build metadata using the fb303-compatible build_* keys. */ + static std::map toMap(); + /** * Returns a formatted debug string with all build information. * Used for gflags version string (--version output). diff --git a/neteng/fboss/bgp/cpp/tests/BgpServiceBaseTest.cpp b/neteng/fboss/bgp/cpp/tests/BgpServiceBaseTest.cpp index aed103e..8b1464c 100644 --- a/neteng/fboss/bgp/cpp/tests/BgpServiceBaseTest.cpp +++ b/neteng/fboss/bgp/cpp/tests/BgpServiceBaseTest.cpp @@ -17,6 +17,7 @@ #include #include +#include #include #include @@ -498,4 +499,14 @@ TEST_F(BgpServiceBaseTestFixture, GetProcessUptimeSecondsTest) { EXPECT_GE(uptime2, uptime1); } +TEST_F(BgpServiceBaseTestFixture, GetBuildInfoReturnsRunningBinaryMetadata) { + std::map info; + + service_->getBuildInfo(info); + + EXPECT_EQ(info.at("build_package_name"), "bgp"); + EXPECT_FALSE(info.at("build_package_version").empty()); + EXPECT_EQ(info.at("build_package_version"), info.at("build_revision")); +} + } // namespace facebook::bgp diff --git a/neteng/fboss/bgp/cpp/tests/BuildInfoTest.cpp b/neteng/fboss/bgp/cpp/tests/BuildInfoTest.cpp index c9376a6..63a9e92 100644 --- a/neteng/fboss/bgp/cpp/tests/BuildInfoTest.cpp +++ b/neteng/fboss/bgp/cpp/tests/BuildInfoTest.cpp @@ -48,3 +48,14 @@ TEST(BuildInfoTest, LogWritesFieldLabelsToStream) { EXPECT_NE(std::string::npos, output.find("Built by")); EXPECT_NE(std::string::npos, output.find("Build Revision")); } + +TEST(BuildInfoTest, ToMapContainsCliVersionFields) { + const auto info = BuildInfo::toMap(); + + EXPECT_EQ(info.at("build_package_name"), "bgp"); + EXPECT_EQ( + info.at("build_package_version"), BuildInfo::getBuildRevision()); + EXPECT_EQ(info.at("build_revision"), BuildInfo::getBuildRevision()); + EXPECT_EQ(info.at("build_user"), BuildInfo::getBuildUser()); + EXPECT_EQ(info.at("build_platform"), BuildInfo::getBuildPlatform()); +} diff --git a/neteng/fboss/bgp/if/bgp_thrift.thrift b/neteng/fboss/bgp/if/bgp_thrift.thrift index 9607396..a3b7081 100644 --- a/neteng/fboss/bgp/if/bgp_thrift.thrift +++ b/neteng/fboss/bgp/if/bgp_thrift.thrift @@ -1422,6 +1422,11 @@ enum ClearBgpNeighborDirection { // @lint-ignore THRIFTCHECKS facebook-service-deprecated existing service inheritance is out of scope for this API addition service TBgpService extends fb303.FacebookService { + /** + * Return build metadata for the running BGP process. + */ + map getBuildInfo(); + /** * [Logging] *