Skip to content

Commit

Permalink
fix: fall back to unordered_map if mph cannot be used
Browse files Browse the repository at this point in the history
  • Loading branch information
sjinks committed Oct 2, 2024
1 parent a039639 commit 6f72ea1
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/os_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,24 @@
#include <string_view>
#include <utility>

#include <mph>
#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;

namespace {

constexpr auto machine_to_arch = std::array{
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},
Expand All @@ -32,7 +42,7 @@ constexpr auto machine_to_arch = std::array{
std::pair{"ia64"sv, opentelemetry::sdk::resource::SemanticConventions::HostArchValues::kIa64}
};

constexpr auto os_to_type = std::array{
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},
Expand All @@ -50,18 +60,30 @@ constexpr auto os_to_type = std::array{

std::string get_host_arch(std::string_view machine)
{
#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)
{
#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;
Expand Down

0 comments on commit 6f72ea1

Please sign in to comment.