winget-cli

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

commit 921817308a37ad7bf04e86259808331d348d3ccd
parent 888431f972ee803560cd7fa90ae99422f1599acb
Author: JohnMcPMS <johnmcp@microsoft.com>
Date:   Thu, 23 Jan 2020 15:14:03 -0800

Add a telemetry event at startup (#21)


Diffstat:
Msrc/AppInstallerCLICore/Core.cpp | 2++
Msrc/AppInstallerCommonCore/AppInstallerTelemetry.cpp | 53++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/AppInstallerCommonCore/Public/AppInstallerRuntime.h | 8++++++--
Msrc/AppInstallerCommonCore/Public/AppInstallerTelemetry.h | 3+++
Msrc/AppInstallerCommonCore/Runtime.cpp | 32++++++++++++++++++++++++++++++++
Msrc/AppInstallerCommonCore/pch.h | 1+
Msrc/AppInstallerTestExeInstaller/AppInstallerTestExeInstaller.vcxproj | 6++++++
7 files changed, 102 insertions(+), 3 deletions(-)

diff --git a/src/AppInstallerCLICore/Core.cpp b/src/AppInstallerCLICore/Core.cpp @@ -21,6 +21,8 @@ namespace AppInstaller::CLI Logging::AddDefaultFileLogger(); Logging::EnableWilFailureTelemetry(); + Logging::Telemetry().LogStartup(); + // Convert incoming wide char args to UTF8 std::vector<std::string> utf8Args; for (int i = 1; i < argc; ++i) diff --git a/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp b/src/AppInstallerCommonCore/AppInstallerTelemetry.cpp @@ -4,8 +4,26 @@ #include "Public/AppInstallerTelemetry.h" #include "Public/AppInstallerLogging.h" +#include "Public/AppInstallerRuntime.h" #include "Public/AppInstallerStrings.h" +// Helper to print a GUID +std::ostream& operator<<(std::ostream& out, const GUID& guid) +{ + wchar_t buffer[256]; + + if (StringFromGUID2(guid, buffer, ARRAYSIZE(buffer))) + { + out << AppInstaller::Utility::ConvertToUTF8(buffer); + } + else + { + out << "error"; + } + + return out; +} + namespace AppInstaller::Logging { namespace @@ -14,6 +32,19 @@ namespace AppInstaller::Logging { Telemetry().LogFailure(info); } + + GUID CreateGuid() + { + GUID result{}; + (void)CoCreateGuid(&result); + return result; + } + + const GUID* GetActivityId() + { + static GUID activityId = CreateGuid(); + return &activityId; + } } TelemetryTraceLogger::TelemetryTraceLogger() @@ -36,8 +67,10 @@ namespace AppInstaller::Logging { if (g_IsTelemetryProviderEnabled) { - TraceLoggingWrite(g_hTelemetryProvider, + TraceLoggingWriteActivity(g_hTelemetryProvider, "FailureInfo", + GetActivityId(), + nullptr, TraceLoggingHResult(failure.hr, "hr"), TraceLoggingWideString(failure.pszMessage, "message"), TraceLoggingString(failure.pszModule, "module"), @@ -57,6 +90,24 @@ namespace AppInstaller::Logging }()); } + void TelemetryTraceLogger::LogStartup() noexcept + { + std::string version = Runtime::GetClientVersion(); + + if (g_IsTelemetryProviderEnabled) + { + TraceLoggingWriteActivity(g_hTelemetryProvider, + "ClientStartup", + GetActivityId(), + nullptr, + TraceLoggingCountedString(version.c_str(), static_cast<ULONG>(version.size()), "version"), + TelemetryPrivacyDataTag(PDT_ProductAndServicePerformance), + TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES)); + } + + AICLI_LOG(CLI, Info, << "AppInstallerCLI, version [" << version << "], activity [" << *GetActivityId() << ']'); + } + void EnableWilFailureTelemetry() { wil::SetResultLoggingCallback(wilResultLoggingCallback); diff --git a/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h b/src/AppInstallerCommonCore/Public/AppInstallerRuntime.h @@ -1,16 +1,20 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. #pragma once - -#include <filesystem> #include "AppInstallerArchitecture.h" #include "AppInstallerCLICore.h" +#include <filesystem> +#include <string> + namespace AppInstaller::Runtime { // Determines whether the process is running in a packaged context or not. bool IsRunningInPackagedContext(); + // Determines the current version of the client and returns it. + std::string GetClientVersion(); + // Gets the path to the temp location. std::filesystem::path GetPathToTemp(); diff --git a/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h b/src/AppInstallerCommonCore/Public/AppInstallerTelemetry.h @@ -28,6 +28,9 @@ namespace AppInstaller::Logging // Logs the failure info. void LogFailure(const wil::FailureInfo& failure) noexcept; + // Logs the initial process startup. + void LogStartup() noexcept; + private: TelemetryTraceLogger(); }; diff --git a/src/AppInstallerCommonCore/Runtime.cpp b/src/AppInstallerCommonCore/Runtime.cpp @@ -25,6 +25,38 @@ namespace AppInstaller::Runtime return result; } + std::string GetClientVersion() + { + using namespace std::string_literals; + + if (IsRunningInPackagedContext()) + { + UINT32 bufferLength = 0; + LONG gcpiResult = GetCurrentPackageId(&bufferLength, nullptr); + THROW_HR_IF(E_UNEXPECTED, gcpiResult != ERROR_INSUFFICIENT_BUFFER); + + std::unique_ptr<byte[]> buffer = std::make_unique<byte[]>(bufferLength); + + gcpiResult = GetCurrentPackageId(&bufferLength, buffer.get()); + if (FAILED_WIN32_LOG(gcpiResult)) + { + return "error"s; + } + + PACKAGE_ID* packageId = reinterpret_cast<PACKAGE_ID*>(buffer.get()); + PACKAGE_VERSION& version = packageId->version; + + std::ostringstream strstr; + strstr << version.Major << '.' << version.Minor << '.' << version.Build << '.' << version.Revision; + + return strstr.str(); + } + else + { + return "unknown"s; + } + } + std::filesystem::path GetPathToTemp() { if (IsRunningInPackagedContext()) diff --git a/src/AppInstallerCommonCore/pch.h b/src/AppInstallerCommonCore/pch.h @@ -21,6 +21,7 @@ #include <memory> #include <ostream> #include <string> +#include <sstream> #include <string_view> #include <type_traits> #include <vector> diff --git a/src/AppInstallerTestExeInstaller/AppInstallerTestExeInstaller.vcxproj b/src/AppInstallerTestExeInstaller/AppInstallerTestExeInstaller.vcxproj @@ -105,6 +105,7 @@ <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ConformanceMode>true</ConformanceMode> + <LanguageStandard>stdcpp17</LanguageStandard> </ClCompile> <Link> <SubSystem>Console</SubSystem> @@ -121,6 +122,8 @@ <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ConformanceMode>true</ConformanceMode> + <LanguageStandard>stdcpp17</LanguageStandard> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> </ClCompile> <Link> <SubSystem>Console</SubSystem> @@ -135,6 +138,8 @@ <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ConformanceMode>true</ConformanceMode> + <LanguageStandard>stdcpp17</LanguageStandard> + <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> </ClCompile> <Link> <SubSystem>Console</SubSystem> @@ -151,6 +156,7 @@ <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ConformanceMode>true</ConformanceMode> + <LanguageStandard>stdcpp17</LanguageStandard> </ClCompile> <Link> <SubSystem>Console</SubSystem>