Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use mph to avoid exit-time destructors #12

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ permissions:
jobs:
build:
name: Build and Test
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
permissions:
contents: read
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ on:
jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
timeout-minutes: 60
permissions:
security-events: write
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sonarcloud.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ permissions:
jobs:
build:
name: Build and analyze
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
env:
BUILD_WRAPPER_OUT_DIR: build_wrapper_output_directory
permissions:
Expand Down
20 changes: 18 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,18 @@ option(WITH_TESTING "Whether to enable tests" ON)
option(WITH_EXAMPLES "Whether to enable examples" ON)
option(INSTALL_RESOURCE_DETECTORS "Whether to install Resource Detectors" ON)

include(FetchContent)

find_package(opentelemetry-cpp CONFIG REQUIRED)

FetchContent_Declare(
qlibs_mph
GIT_REPOSITORY https://github.com/qlibs/mph
GIT_TAG v5.0.2
)

FetchContent_MakeAvailable(qlibs_mph)

add_library(utils OBJECT)
target_sources(
utils
Expand All @@ -31,13 +41,19 @@ target_sources(
set_target_properties(
utils
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD 20
CXX_STANDARD_REQUIRED YES
CXX_EXTENSIONS NO
POSITION_INDEPENDENT_CODE ON
)

target_link_libraries(utils PUBLIC opentelemetry-cpp::resources)
target_link_libraries(
utils
PUBLIC opentelemetry-cpp::resources
PRIVATE qlibs_mph
)

target_include_directories(utils PUBLIC ${qlibs_mph_SOURCE_DIR})

add_library(helpers STATIC $<TARGET_OBJECTS:utils>)
target_include_directories(helpers PUBLIC ${CMAKE_SOURCE_DIR}/src)
Expand Down
103 changes: 63 additions & 40 deletions src/os_utils.cpp
Original file line number Diff line number Diff line change
@@ -1,66 +1,89 @@
#include "os_utils.h"

#include <unordered_map>
#include <array>
#include <string_view>
#include <utility>

#if defined(__SIZEOF_INT128__) && __SIZEOF_INT128__ && __cplusplus >= 202002L
# include <mph>
# define USE_MPH
# define CONTAINER std::array
# define CONTAINER_CONSTEXPR constexpr
#else
# include <unordered_map>
# define CONTAINER std::unordered_map
# define CONTAINER_CONSTEXPR const
#endif

#include <opentelemetry/sdk/resource/semantic_conventions.h>

using std::literals::operator""sv;

std::string get_host_arch(std::string_view machine)
{
static const std::unordered_map<std::string_view, const char*> machine_to_arch{
{"x86_64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kAmd64},
{"amd64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kAmd64},
{"aarch64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm64},
{"arm64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm64},
{"arm", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm32},
{"armv7l", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm32},
{"ppc", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc32},
{"ppc64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc64},
{"ppc64le", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc64},
{"s390x", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kS390x},
{"i386", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"i486", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"i586", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"i686", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"x86", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"i86pc", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"x86pc", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
{"ia64", opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kIa64}
static CONTAINER_CONSTEXPR auto machine_to_arch = CONTAINER{
std::pair{"x86_64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kAmd64},
std::pair{"amd64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kAmd64},
std::pair{"aarch64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm64},
std::pair{"arm64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm64},
std::pair{"arm"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm32},
std::pair{"armv7l"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kArm32},
std::pair{"ppc"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc32},
std::pair{"ppc64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc64},
std::pair{"ppc64le"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kPpc64},
std::pair{"s390x"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kS390x},
std::pair{"i386"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"i486"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"i586"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"i686"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"x86"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"i86pc"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"x86pc"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kX86},
std::pair{"ia64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kIa64}
};

#ifdef USE_MPH
if (const auto arch = mph::find<machine_to_arch>(machine); arch) {
return arch.value;
}
#else
if (const auto it = machine_to_arch.find(machine); it != machine_to_arch.end()) {
return it->second;
}
#endif

return {machine.data(), machine.length()};
}

std::string get_os_type(std::string_view os)
{
static const std::unordered_map<std::string_view, const char*> os_to_type{
{"Linux", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kLinux},
{"Windows_NT", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"CYGWIN_NT-5.1", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"CYGWIN_NT-6.1", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"CYGWIN_NT-6.1-WOW64", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"CYGWIN_NT-10.0", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"MINGW32_NT-6.1", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"MSYS_NT-6.1", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"MINGW32_NT-10.0", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"MSYS_NT-10.0", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
{"DragonFly", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kDragonflybsd},
{"FreeBSD", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kFreebsd},
{"HP-UX", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kHpux},
{"AIX", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kAix},
{"Darwin", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kDarwin},
{"NetBSD", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kNetbsd},
{"OpenBSD", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kOpenbsd},
{"SunOS", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kSolaris},
{"OS/390", opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kZOs}
static CONTAINER_CONSTEXPR auto os_to_type = CONTAINER{
std::pair{"Linux"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kLinux},
std::pair{"Windows_NT"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows},
std::pair{"DragonFly"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kDragonflybsd},
std::pair{"FreeBSD"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kFreebsd},
std::pair{"HP-UX"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kHpux},
std::pair{"AIX"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kAix},
std::pair{"Darwin"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kDarwin},
std::pair{"NetBSD"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kNetbsd},
std::pair{"OpenBSD"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kOpenbsd},
std::pair{"SunOS"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kSolaris},
std::pair{"OS/390"sv, opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kZOs}
};

#ifdef USE_MPH
if (const auto type = mph::find<os_to_type>(os); type) {
return type.value;
}
#else
if (const auto it = os_to_type.find(os); it != os_to_type.end()) {
return it->second;
}
#endif

if (os.starts_with("CYGWIN") || os.starts_with("MINGW") || os.starts_with("MSYS")) {
return opentelemetry::sdk::resource::SemanticConventions::OsTypeValues::kWindows;
}

return {os.data(), os.length()};
}