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

Add configuration telemetry events #3152

Merged
merged 9 commits into from
Apr 15, 2023
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Primary work done, needs tests and a few more minor things
JohnMcPMS committed Apr 13, 2023
commit 0816961453fe0b7ec629b65531920dccfbd5b6e7
10 changes: 8 additions & 2 deletions src/AppInstallerCLICore/Workflows/ConfigurationFlow.cpp
Original file line number Diff line number Diff line change
@@ -271,7 +271,8 @@ namespace AppInstaller::CLI::Workflow
if (FAILED(resultInformation.ResultCode()))
{
AICLI_LOG(Config, Error, << "Failed to get unit details for " << Utility::ConvertToUTF8(unit.UnitName()) << " : 0x" <<
Logging::SetHRFormat << resultInformation.ResultCode() << '\n' << Utility::ConvertToUTF8(resultInformation.Description()));
Logging::SetHRFormat << resultInformation.ResultCode() << '\n' << Utility::ConvertToUTF8(resultInformation.Description()) << '\n' <<
Utility::ConvertToUTF8(resultInformation.Details()));
}
}

@@ -350,7 +351,8 @@ namespace AppInstaller::CLI::Workflow
else
{
AICLI_LOG(Config, Error, << "Configuration unit " << Utility::ConvertToUTF8(unit.UnitName()) << "[" << Utility::ConvertToUTF8(unit.Identifier()) << "] failed with code 0x"
<< Logging::SetHRFormat << resultInformation.ResultCode() << " and error message:\n" << Utility::ConvertToUTF8(resultInformation.Description()));
<< Logging::SetHRFormat << resultInformation.ResultCode() << " and error message:\n" << Utility::ConvertToUTF8(resultInformation.Description()) << '\n'
<< Utility::ConvertToUTF8(resultInformation.Details()));
// TODO: Improve error reporting for failures: use message, known HRs, getting HR system string, etc.
m_context.Reporter.Error() << " "_liv << Resource::String::ConfigurationUnitFailed << " 0x"_liv << Logging::SetHRFormat << resultInformation.ResultCode() << std::endl;
}
@@ -400,6 +402,10 @@ namespace AppInstaller::CLI::Workflow

// Set the processor to the current level of the logging.
processor.MinimumLevel(ConvertLevel(Logging::Log().GetLevel()));
// Use same activity as the overall winget command
processor.ActivityIdentifier(*Logging::Telemetry().GetActivityId());
// Apply winget telemetry setting to configuration
processor.GenerateTelemetryEvents(!Settings::User().Get<Settings::Setting::TelemetryDisable>());

// Route the configuration diagnostics into the context's diagnostics logging
processor.Diagnostics([&context](const winrt::Windows::Foundation::IInspectable&, const DiagnosticInformation& diagnostics)
30 changes: 1 addition & 29 deletions src/AppInstallerCommonCore/Public/AppInstallerRuntime.h
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
#pragma once
#include <AppInstallerVersions.h>
#include <winget/LocIndependent.h>
#include <winget/Runtime.h>

#include <filesystem>
#include <memory>
@@ -11,22 +12,6 @@

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.
Utility::LocIndString GetClientVersion();

// Determines the current version of the package if running in a packaged context.
Utility::LocIndString GetPackageVersion();

// Gets a string representation of the OS version for debugging purposes.
Utility::LocIndString GetOSVersion();

// Gets the OS region.
// This can be used as the current market.
std::string GetOSRegion();

// Sets the runtime path state name globally.
void SetRuntimePathStateName(std::string name);

@@ -120,22 +105,9 @@ namespace AppInstaller::Runtime
// Gets a new temp file path.
std::filesystem::path GetNewTempFilePath();

// Determines whether the current OS version is >= the given one.
// We treat the given Version struct as a standard 4 part Windows OS version.
bool IsCurrentOSVersionGreaterThanOrEqual(const Utility::Version& version);

// Determines whether the process is running with administrator privileges.
bool IsRunningAsAdmin();

// Determines whether the process is running with local system context.
bool IsRunningAsSystem();

// Determines whether developer mode is enabled.
bool IsDevModeEnabled();

// Returns true if this is a release build; false if not.
inline constexpr bool IsReleaseBuild();

// Gets the default user agent string for the Windows Package Manager.
Utility::LocIndString GetDefaultUserAgent();

173 changes: 0 additions & 173 deletions src/AppInstallerCommonCore/Runtime.cpp
Original file line number Diff line number Diff line change
@@ -36,20 +36,11 @@ namespace AppInstaller::Runtime
#ifndef WINGET_DISABLE_FOR_FUZZING
constexpr std::string_view s_SecureSettings_Relative_Packaged = "pkg"sv;
#endif
constexpr std::string_view s_PreviewBuildSuffix = "-preview"sv;
constexpr std::string_view s_RuntimePath_Unpackaged_DefaultState = "defaultState"sv;

static std::optional<std::string> s_runtimePathStateName;
static wil::srwlock s_runtimePathStateNameLock;

// Gets a boolean indicating whether the current process has identity.
bool DoesCurrentProcessHaveIdentity()
{
UINT32 length = 0;
LONG result = GetPackageFamilyName(GetCurrentProcess(), &length, nullptr);
return (result != APPMODEL_ERROR_NO_PACKAGE);
}

// Gets the path to the root of the package containing the current process.
std::filesystem::path GetPackagePath()
{
@@ -112,19 +103,6 @@ namespace AppInstaller::Runtime
return Utility::ConvertToUTF8(packageId->name);
}

// Gets the package version; only succeeds if running in a packaged context.
std::optional<PACKAGE_VERSION> GetPACKAGE_VERSION()
{
std::unique_ptr<byte[]> buffer = GetPACKAGE_ID();
if (!buffer)
{
return {};
}

PACKAGE_ID* packageId = reinterpret_cast<PACKAGE_ID*>(buffer.get());
return packageId->version;
}

#ifndef AICLI_DISABLE_TEST_HOOKS
static std::map<PathName, PathDetails> s_Path_TestHook_Overrides;
#endif
@@ -230,104 +208,6 @@ namespace AppInstaller::Runtime
};
}

bool IsRunningInPackagedContext()
{
static bool result = DoesCurrentProcessHaveIdentity();
return result;
}

LocIndString GetClientVersion()
{
using namespace std::string_literals;

// Major and minor come directly from version.h
std::ostringstream strstr;
strstr << VERSION_MAJOR << '.' << VERSION_MINOR << '.';

// Build comes from the package for now, if packaged.
if (IsRunningInPackagedContext())
{
auto version = GetPACKAGE_VERSION();

if (!version)
{
// In the extremely unlikely event of a failure, this is merely a sentinel value
// to indicated such. The only other option is to completely prevent execution,
// which seems unnecessary.
return LocIndString{ "error"sv };
}

strstr << version->Build;
}
else
{
strstr << VERSION_BUILD;
}

if (!IsReleaseBuild())
{
strstr << s_PreviewBuildSuffix;
}

return LocIndString{ strstr.str() };
}

LocIndString GetPackageVersion()
{
using namespace std::string_literals;

if (IsRunningInPackagedContext())
{
auto version = GetPACKAGE_VERSION();

if (!version)
{
// In the extremely unlikely event of a failure, this is merely a sentinel value
// to indicated such. The only other option is to completely prevent execution,
// which seems unnecessary.
return LocIndString{ "error"sv };
}

std::ostringstream strstr;
strstr << GetPackageName() << " v" << version->Major << '.' << version->Minor << '.' << version->Build << '.' << version->Revision;

return LocIndString{ strstr.str() };
}
else
{
// Calling code should avoid calling in when this is the case.
return LocIndString{ "none"sv };
}
}

#ifndef WINGET_DISABLE_FOR_FUZZING
LocIndString GetOSVersion()
{
winrt::Windows::System::Profile::AnalyticsInfo analyticsInfo{};
auto versionInfo = analyticsInfo.VersionInfo();

uint64_t version = std::stoull(Utility::ConvertToUTF8(versionInfo.DeviceFamilyVersion()));
uint16_t parts[4];

for (size_t i = 0; i < ARRAYSIZE(parts); ++i)
{
parts[i] = version & 0xFFFF;
version = version >> 16;
}

std::ostringstream strstr;
strstr << Utility::ConvertToUTF8(versionInfo.DeviceFamily()) << " v" << parts[3] << '.' << parts[2] << '.' << parts[1] << '.' << parts[0];

return LocIndString{ strstr.str() };
}

std::string GetOSRegion()
{
winrt::Windows::Globalization::GeographicRegion region;
return Utility::ConvertToUTF8(region.CodeTwoLetter());
}
#endif

void SetRuntimePathStateName(std::string name)
{
auto suitablePathPart = MakeSuitablePathPart(name);
@@ -700,50 +580,6 @@ namespace AppInstaller::Runtime
return tempFilePath;
}

bool IsCurrentOSVersionGreaterThanOrEqual(const Utility::Version& version)
{
DWORD versionParts[3] = {};

for (size_t i = 0; i < ARRAYSIZE(versionParts) && i < version.GetParts().size(); ++i)
{
versionParts[i] = static_cast<DWORD>(std::min(static_cast<decltype(version.GetParts()[i].Integer)>(std::numeric_limits<DWORD>::max()), version.GetParts()[i].Integer));
}

OSVERSIONINFOEXW osVersionInfo{};
osVersionInfo.dwOSVersionInfoSize = sizeof(osVersionInfo);
osVersionInfo.dwMajorVersion = versionParts[0];
osVersionInfo.dwMinorVersion = versionParts[1];
osVersionInfo.dwBuildNumber = versionParts[2];
osVersionInfo.wServicePackMajor = 0;
osVersionInfo.wServicePackMinor = 0;

DWORD mask = VER_MAJORVERSION | VER_MINORVERSION | VER_BUILDNUMBER | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR;

DWORDLONG conditions = 0;
VER_SET_CONDITION(conditions, VER_MAJORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_MINORVERSION, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_BUILDNUMBER, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_SERVICEPACKMAJOR, VER_GREATER_EQUAL);
VER_SET_CONDITION(conditions, VER_SERVICEPACKMINOR, VER_GREATER_EQUAL);

BOOL result = VerifyVersionInfoW(&osVersionInfo, mask, conditions);
if (!result)
{
THROW_LAST_ERROR_IF(GetLastError() != ERROR_OLD_WIN_VERSION);
}
return !!result;
}

bool IsRunningAsAdmin()
{
return wil::test_token_membership(nullptr, SECURITY_NT_AUTHORITY, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS);
}

bool IsRunningAsSystem()
{
return wil::test_token_membership(nullptr, SECURITY_NT_AUTHORITY, SECURITY_LOCAL_SYSTEM_RID);
}

// Determines whether developer mode is enabled.
// Does not account for the group policy value which takes precedence over this registry value.
bool IsDevModeEnabled()
@@ -760,15 +596,6 @@ namespace AppInstaller::Runtime
}
}

constexpr bool IsReleaseBuild()
{
#ifdef WINGET_ENABLE_RELEASE_BUILD
return true;
#else
return false;
#endif
}

// Using "standard" user agent format
// Keeping `winget-cli` for historical reasons
Utility::LocIndString GetDefaultUserAgent()
17 changes: 9 additions & 8 deletions src/AppInstallerSharedLib/Public/AppInstallerErrors.h
Original file line number Diff line number Diff line change
@@ -166,14 +166,15 @@
#define WINGET_CONFIG_ERROR_WARNING_NOT_ACCEPTED ((HRESULT)0x8A15C00B)

// Configuration Processor Errors
#define WINGET_CONFIG_ERROR_UNIT_NOT_INSTALLED ((HRESULT)0x8A15C101)
#define WINGET_CONFIG_ERROR_UNIT_NOT_FOUND_REPOSITORY ((HRESULT)0x8A15C102)
#define WINGET_CONFIG_ERROR_UNIT_MULTIPLE_MATCHES ((HRESULT)0x8A15C103)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_GET ((HRESULT)0x8A15C104)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_TEST ((HRESULT)0x8A15C105)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_SET ((HRESULT)0x8A15C106)
#define WINGET_CONFIG_ERROR_UNIT_MODULE_CONFLICT ((HRESULT)0x8A15C107)
#define WINGET_CONFIG_ERROR_UNIT_IMPORT_MODULE ((HRESULT)0x8A15C108)
#define WINGET_CONFIG_ERROR_UNIT_NOT_INSTALLED ((HRESULT)0x8A15C101)
#define WINGET_CONFIG_ERROR_UNIT_NOT_FOUND_REPOSITORY ((HRESULT)0x8A15C102)
#define WINGET_CONFIG_ERROR_UNIT_MULTIPLE_MATCHES ((HRESULT)0x8A15C103)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_GET ((HRESULT)0x8A15C104)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_TEST ((HRESULT)0x8A15C105)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_SET ((HRESULT)0x8A15C106)
#define WINGET_CONFIG_ERROR_UNIT_MODULE_CONFLICT ((HRESULT)0x8A15C107)
#define WINGET_CONFIG_ERROR_UNIT_IMPORT_MODULE ((HRESULT)0x8A15C108)
#define WINGET_CONFIG_ERROR_UNIT_INVOKE_INVALID_RESULT ((HRESULT)0x8A15C109)

namespace AppInstaller
{
Loading