diff --git a/src/OpenTelemetry.AutoInstrumentation.Native/cor_profiler.cpp b/src/OpenTelemetry.AutoInstrumentation.Native/cor_profiler.cpp index ca9894465..300a0e1bf 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Native/cor_profiler.cpp +++ b/src/OpenTelemetry.AutoInstrumentation.Native/cor_profiler.cpp @@ -123,13 +123,14 @@ HRESULT STDMETHODCALLTYPE CorProfiler::Initialize(IUnknown* cor_profiler_info_un } info10 = nullptr; - Logger::Info("Environment variables:"); - for (auto&& env_var : env_vars_to_display) + if (IsDebugEnabled()) { - WSTRING env_var_value = GetEnvironmentValue(env_var); - if (IsDebugEnabled() || !env_var_value.empty()) + const auto env_variables = GetEnvironmentVariables(env_vars_prefixes_to_display); + Logger::Info("Environment variables:"); + + for (const auto& env_variable : env_variables) { - Logger::Info(" ", env_var, "=", env_var_value); + Logger::Info(" ", env_variable); } } diff --git a/src/OpenTelemetry.AutoInstrumentation.Native/environment_variables.h b/src/OpenTelemetry.AutoInstrumentation.Native/environment_variables.h index 17103aed7..986a45460 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Native/environment_variables.h +++ b/src/OpenTelemetry.AutoInstrumentation.Native/environment_variables.h @@ -94,6 +94,11 @@ const WSTRING dotnet_shared_store = WStr("DOTNET_SHARED_STORE"); // for more information about this environment variable. const WSTRING dotnet_startup_hooks = WStr("DOTNET_STARTUP_HOOKS"); +const WSTRING prefix_cor = WStr("COR_"); +const WSTRING prefix_coreclr = WStr("CORECLR_"); +const WSTRING prefix_dotnet = WStr("DOTNET_"); +const WSTRING prefix_otel = WStr("OTEL_"); + } // namespace environment } // namespace trace diff --git a/src/OpenTelemetry.AutoInstrumentation.Native/otel_profiler_constants.h b/src/OpenTelemetry.AutoInstrumentation.Native/otel_profiler_constants.h index 39c0bad3c..7be096e00 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Native/otel_profiler_constants.h +++ b/src/OpenTelemetry.AutoInstrumentation.Native/otel_profiler_constants.h @@ -4,30 +4,14 @@ #include #include "environment_variables.h" -#include "logger.h" namespace trace { - -const WSTRING env_vars_to_display[]{environment::clr_profiler_enabled, - environment::debug_enabled, - environment::profiler_home_path, - environment::integrations_path, - environment::include_process_names, - environment::exclude_process_names, - environment::enabled_integrations, - environment::disabled_integrations, - environment::log_directory, - environment::clr_disable_optimizations, - environment::clr_enable_inlining, - environment::clr_enable_ngen, - environment::dump_il_rewrite_enabled, - environment::azure_app_services, - environment::azure_app_services_app_pool_id, - environment::azure_app_services_cli_telemetry_profile_value, - environment::dotnet_additional_deps, - environment::dotnet_shared_store, - environment::dotnet_startup_hooks}; +const std::vector env_vars_prefixes_to_display{environment::prefix_cor, + environment::prefix_coreclr, + environment::prefix_dotnet, + environment::prefix_otel, + environment::azure_app_services_app_pool_id}; const WSTRING skip_assembly_prefixes[]{ WStr("Microsoft.AI"), diff --git a/src/OpenTelemetry.AutoInstrumentation.Native/util.cpp b/src/OpenTelemetry.AutoInstrumentation.Native/util.cpp index fec4a647a..d6def737e 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Native/util.cpp +++ b/src/OpenTelemetry.AutoInstrumentation.Native/util.cpp @@ -1,13 +1,15 @@ #include "util.h" -#include "miniutf.hpp" #include "pal.h" #include #include -#include -#include //NOLINT +#include #include +#ifdef MACOS +extern char** environ; +#endif + namespace trace { @@ -97,6 +99,54 @@ std::vector GetEnvironmentValues(const WSTRING& name) return GetEnvironmentValues(name, L','); } + +std::vector GetEnvironmentVariables(const std::vector &prefixes) +{ + std::vector env_strings; +#ifdef _WIN32 + // Documentation for GetEnvironmentStrings: https://learn.microsoft.com/en-us/windows/win32/api/processenv/nf-processenv-getenvironmentstrings#remarks + const auto env_variables = GetEnvironmentStrings(); + int prev = 0; + for (int i = 0;; i++) { + if (env_variables[i] != '\0') { + continue; + } + + auto env_variable = WSTRING(env_variables + prev, env_variables + i); + for (const auto& prefix : prefixes) + { + if (env_variable.find(prefix) == 0) + { + env_strings.push_back(env_variable); + break; + } + } + + prev = i + 1; + if (env_variables[i + 1] == '\0') + { + break; + } + } + + FreeEnvironmentStrings(env_variables); +#else + for (char** current = environ; *current; current++) + { + auto env_variable = ToWSTRING(ToString(*current)); + for (const auto& prefix : prefixes) + { + if (env_variable.find(prefix) == 0) + { + env_strings.push_back(env_variable); + break; + } + } + } +#endif + return env_strings; +} + constexpr char HexMap[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'}; WSTRING HexStr(const void* dataPtr, int len) diff --git a/src/OpenTelemetry.AutoInstrumentation.Native/util.h b/src/OpenTelemetry.AutoInstrumentation.Native/util.h index b2def781c..63e72a0ce 100644 --- a/src/OpenTelemetry.AutoInstrumentation.Native/util.h +++ b/src/OpenTelemetry.AutoInstrumentation.Native/util.h @@ -32,6 +32,9 @@ std::vector GetEnvironmentValues(const WSTRING& name, const wchar_t del // GetEnvironmentValues calls GetEnvironmentValues with a semicolon delimiter. std::vector GetEnvironmentValues(const WSTRING& name); +// GetEnvironmentVariables returns list of all environment variable +std::vector GetEnvironmentVariables(const std::vector &prefixes); + // Convert Hex to string WSTRING HexStr(const void* data, int len);