winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4cd1f5246c47cc676480591f081a86f623821bf0
parent 1b4feddcf49b7e4f06479d163f0aa6222f992b44
Author: JohnMcPMS <johnmcp@microsoft.com>
Date:   Fri,  5 Mar 2021 13:07:35 -0800

Add setting to allow control over telemetry event writing (#777)


Diffstat:
M.github/actions/spelling/patterns.txt | 3+--
Msrc/AppInstallerCommonCore/AppInstallerTelemetry.cpp | 31+++++++++++++++++++++----------
Msrc/AppInstallerCommonCore/Public/AppInstallerTelemetry.h | 9+++++++++
Msrc/AppInstallerCommonCore/Public/winget/UserSettings.h | 8+++++---
Msrc/AppInstallerCommonCore/UserSettings.cpp | 64+++++++++++++++++-----------------------------------------------
5 files changed, 53 insertions(+), 62 deletions(-)

diff --git a/.github/actions/spelling/patterns.txt b/.github/actions/spelling/patterns.txt @@ -32,4 +32,4 @@ El proyecto .* diferentes http://rfc3161.gtm.corp.microsoft.com/TSS/HttpTspServer # schema regex -"pattern": .*$- \ No newline at end of file +"pattern": .*$ diff --git a/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp b/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp @@ -6,6 +6,7 @@ #include "Public/AppInstallerRuntime.h" #include "Public/AppInstallerSHA256.h" #include "Public/AppInstallerStrings.h" +#include "winget/UserSettings.h" #define AICLI_TraceLoggingStringView(_sv_,_name_) TraceLoggingCountedUtf8String(_sv_.data(), static_cast<ULONG>(_sv_.size()), _name_) @@ -34,18 +35,10 @@ namespace AppInstaller::Logging { static const uint32_t s_RootExecutionId = 0; - // Used to disable telemetry on the fly. - std::atomic_bool s_isTelemetryEnabled{ true }; - std::atomic_uint32_t s_executionStage{ 0 }; std::atomic_uint32_t s_subExecutionId{ s_RootExecutionId }; - bool IsTelemetryEnabled() - { - return g_IsTelemetryProviderEnabled && s_isTelemetryEnabled; - } - void __stdcall wilResultLoggingCallback(const wil::FailureInfo& info) noexcept { Telemetry().LogFailure(info); @@ -67,7 +60,10 @@ namespace AppInstaller::Logging TelemetryTraceLogger::TelemetryTraceLogger() { + // TODO: Needs to be made a singleton registration/removal in the future RegisterTraceLogging(); + + m_isSettingEnabled = !Settings::User().Get<Settings::Setting::TelemetryDisable>(); } TelemetryTraceLogger::~TelemetryTraceLogger() @@ -81,6 +77,16 @@ namespace AppInstaller::Logging return instance; } + bool TelemetryTraceLogger::DisableRuntime() + { + return m_isRuntimeEnabled.exchange(false); + } + + void TelemetryTraceLogger::EnableRuntime() + { + m_isRuntimeEnabled = true; + } + void TelemetryTraceLogger::LogFailure(const wil::FailureInfo& failure) const noexcept { if (IsTelemetryEnabled()) @@ -500,6 +506,11 @@ namespace AppInstaller::Logging } } + bool TelemetryTraceLogger::IsTelemetryEnabled() const noexcept + { + return g_IsTelemetryProviderEnabled && m_isSettingEnabled && m_isRuntimeEnabled; + } + #ifndef AICLI_DISABLE_TEST_HOOKS static std::shared_ptr<TelemetryTraceLogger> s_TelemetryTraceLogger_TestOverride; #endif @@ -523,14 +534,14 @@ namespace AppInstaller::Logging DisableTelemetryScope::DisableTelemetryScope() { - m_token = s_isTelemetryEnabled.exchange(false); + m_token = Telemetry().DisableRuntime(); } DisableTelemetryScope::~DisableTelemetryScope() { if (m_token) { - s_isTelemetryEnabled = true; + Telemetry().EnableRuntime(); } } diff --git a/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h b/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h @@ -26,6 +26,10 @@ namespace AppInstaller::Logging // Gets the singleton instance of this type. static TelemetryTraceLogger& GetInstance(); + // Control whether this trace logger is enabled at runtime. + bool DisableRuntime(); + void EnableRuntime(); + // Logs the failure info. void LogFailure(const wil::FailureInfo& failure) const noexcept; @@ -112,6 +116,11 @@ namespace AppInstaller::Logging protected: TelemetryTraceLogger(); + + bool IsTelemetryEnabled() const noexcept; + + bool m_isSettingEnabled = true; + std::atomic_bool m_isRuntimeEnabled{ true }; }; // Helper to make the call sites look clean. diff --git a/src/AppInstallerCommonCore/Public/winget/UserSettings.h b/src/AppInstallerCommonCore/Public/winget/UserSettings.h @@ -10,11 +10,11 @@ #include <variant> #include <vector> +using namespace std::chrono_literals; +using namespace std::string_view_literals; + namespace AppInstaller::Settings { - using namespace std::chrono_literals; - using namespace std::string_view_literals; - // The type of argument. enum class UserSettingsType { @@ -54,6 +54,7 @@ namespace AppInstaller::Settings EFUninstall, EFImport, EFExport, + TelemetryDisable, Max }; @@ -91,6 +92,7 @@ namespace AppInstaller::Settings SETTINGMAPPING_SPECIALIZATION(Setting::EFUninstall, bool, bool, false, ".experimentalFeatures.uninstall"sv); SETTINGMAPPING_SPECIALIZATION(Setting::EFImport, bool, bool, false, ".experimentalFeatures.import"sv); SETTINGMAPPING_SPECIALIZATION(Setting::EFExport, bool, bool, false, ".experimentalFeatures.export"sv); + SETTINGMAPPING_SPECIALIZATION(Setting::TelemetryDisable, bool, bool, false, ".telemetry.disable"sv); // Used to deduce the SettingVariant type; making a variant that includes std::monostate and all SettingMapping types. template <size_t... I> diff --git a/src/AppInstallerCommonCore/UserSettings.cpp b/src/AppInstallerCommonCore/UserSettings.cpp @@ -151,6 +151,14 @@ namespace AppInstaller::Settings namespace details { + // Stamps out a validate function that simply returns the input value. +#define WINGET_VALIDATE_PASS_THROUGH(_setting_) \ + std::optional<SettingMapping<Setting::_setting_>::value_t> \ + SettingMapping<Setting::_setting_>::Validate(const SettingMapping<Setting::_setting_>::json_t& value) \ + { \ + return value; \ + } + std::optional<SettingMapping<Setting::AutoUpdateTimeInMinutes>::value_t> SettingMapping<Setting::AutoUpdateTimeInMinutes>::Validate(const SettingMapping<Setting::AutoUpdateTimeInMinutes>::json_t& value) { @@ -181,53 +189,15 @@ namespace AppInstaller::Settings return {}; } - std::optional<SettingMapping<Setting::EFExperimentalCmd>::value_t> - SettingMapping<Setting::EFExperimentalCmd>::Validate(const SettingMapping<Setting::EFExperimentalCmd>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFExperimentalArg>::value_t> - SettingMapping<Setting::EFExperimentalArg>::Validate(const SettingMapping<Setting::EFExperimentalArg>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFExperimentalMSStore>::value_t> - SettingMapping<Setting::EFExperimentalMSStore>::Validate(const SettingMapping<Setting::EFExperimentalMSStore>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFList>::value_t> - SettingMapping<Setting::EFList>::Validate(const SettingMapping<Setting::EFList>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFExperimentalUpgrade>::value_t> - SettingMapping<Setting::EFExperimentalUpgrade>::Validate(const SettingMapping<Setting::EFExperimentalUpgrade>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFUninstall>::value_t> - SettingMapping<Setting::EFUninstall>::Validate(const SettingMapping<Setting::EFUninstall>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFImport>::value_t> - SettingMapping<Setting::EFImport>::Validate(const SettingMapping<Setting::EFImport>::json_t& value) - { - return value; - } - - std::optional<SettingMapping<Setting::EFExport>::value_t> - SettingMapping<Setting::EFExport>::Validate(const SettingMapping<Setting::EFExport>::json_t& value) - { - return value; - } + WINGET_VALIDATE_PASS_THROUGH(EFExperimentalCmd) + WINGET_VALIDATE_PASS_THROUGH(EFExperimentalArg) + WINGET_VALIDATE_PASS_THROUGH(EFExperimentalMSStore) + WINGET_VALIDATE_PASS_THROUGH(EFList) + WINGET_VALIDATE_PASS_THROUGH(EFExperimentalUpgrade) + WINGET_VALIDATE_PASS_THROUGH(EFUninstall) + WINGET_VALIDATE_PASS_THROUGH(EFImport) + WINGET_VALIDATE_PASS_THROUGH(EFExport) + WINGET_VALIDATE_PASS_THROUGH(TelemetryDisable) } UserSettings::UserSettings() : m_type(UserSettingsType::Default)