From 67800b07618b5b03263aa6da9ee21f6b06227351 Mon Sep 17 00:00:00 2001 From: mapill-msft <58245272+mapill-msft@users.noreply.github.com> Date: Mon, 9 Dec 2019 09:34:28 -0800 Subject: [PATCH] Adding telemetry shared items project to package manager client project (#4) * Adding Telemetry shared project to the PackageManager client project * Adding Privacy Statement and updating telemetry statement in readme. --- README.md | 6 ++ privacy.md | 3 + src/AppInstallerCLI/AppInstallerCLI.vcxproj | 1 + src/AppInstallerCLI/main.cpp | 4 + src/AppInstallerCLI/pch.h | 2 + src/AppInstallerClient.sln | 11 +++ src/Telemetry/MicrosoftTelemetry.h | 87 +++++++++++++++++++++ src/Telemetry/Telemetry.vcxitems | 24 ++++++ src/Telemetry/TraceLogging.cpp | 57 ++++++++++++++ src/Telemetry/TraceLogging.h | 70 +++++++++++++++++ src/Telemetry/WinEventLogLevels.h | 11 +++ 11 files changed, 276 insertions(+) create mode 100644 privacy.md create mode 100644 src/Telemetry/MicrosoftTelemetry.h create mode 100644 src/Telemetry/Telemetry.vcxitems create mode 100644 src/Telemetry/TraceLogging.cpp create mode 100644 src/Telemetry/TraceLogging.h create mode 100644 src/Telemetry/WinEventLogLevels.h diff --git a/README.md b/README.md index b81a84e564..5667c306ea 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,9 @@ provided by the bot. You will only need to do this once across all repos using o This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [opencode@microsoft.com](mailto:opencode@microsoft.com) with any additional questions or comments. + + +Data/Telemetry + +This project collects usage data and sends it to Microsoft to help improve our products and services. See the privacy statement for more details. + diff --git a/privacy.md b/privacy.md new file mode 100644 index 0000000000..a5a9e8ea1f --- /dev/null +++ b/privacy.md @@ -0,0 +1,3 @@ +# Data Collection + +The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry as described in the repository. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft's privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices. diff --git a/src/AppInstallerCLI/AppInstallerCLI.vcxproj b/src/AppInstallerCLI/AppInstallerCLI.vcxproj index e65294e371..d93d971c2c 100644 --- a/src/AppInstallerCLI/AppInstallerCLI.vcxproj +++ b/src/AppInstallerCLI/AppInstallerCLI.vcxproj @@ -69,6 +69,7 @@ + diff --git a/src/AppInstallerCLI/main.cpp b/src/AppInstallerCLI/main.cpp index 8d60f50748..0688c92f32 100644 --- a/src/AppInstallerCLI/main.cpp +++ b/src/AppInstallerCLI/main.cpp @@ -10,6 +10,7 @@ using namespace AppInstaller::CLI; int wmain(int argc, wchar_t const** argv) try { init_apartment(); + RegisterTraceLogging(); RootCommand root; Invocation invocation{ argc, argv }; @@ -32,6 +33,7 @@ int wmain(int argc, wchar_t const** argv) try catch (const CommandException& ce) { commandToExecute->OutputHelp(std::wcout, &ce); + UnRegisterTraceLogging(); return 1; } @@ -48,6 +50,8 @@ int wmain(int argc, wchar_t const** argv) try { } + + UnRegisterTraceLogging(); } // End of the line exceptions that are not ever expected catch (const winrt::hresult_error&) diff --git a/src/AppInstallerCLI/pch.h b/src/AppInstallerCLI/pch.h index 2cdcb53cf5..41212a32f3 100644 --- a/src/AppInstallerCLI/pch.h +++ b/src/AppInstallerCLI/pch.h @@ -6,3 +6,5 @@ #include #include +#include +#include "TraceLogging.h" diff --git a/src/AppInstallerClient.sln b/src/AppInstallerClient.sln index 42b13737cf..644d4e0b60 100644 --- a/src/AppInstallerClient.sln +++ b/src/AppInstallerClient.sln @@ -7,7 +7,15 @@ Project("{C7167F0D-BC9F-4E6E-AFE1-012C56B48DB5}") = "AppInstallerClientPackage", EndProject Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "AppInstallerCLI", "AppInstallerCLI\AppInstallerCLI.vcxproj", "{5B6F90DF-FD19-4BAE-83D9-24DAD128E777}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{60618CAC-2995-4DF9-9914-45C6FC02C995}" +EndProject +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "Telemetry", "Telemetry\Telemetry.vcxitems", "{4B988559-DDBD-4456-9300-0C3AAD12C466}" +EndProject Global + GlobalSection(SharedMSBuildProjectFiles) = preSolution + Telemetry\Telemetry.vcxitems*{4b988559-ddbd-4456-9300-0c3aad12c466}*SharedItemsImports = 9 + Telemetry\Telemetry.vcxitems*{5b6f90df-fd19-4bae-83d9-24dad128e777}*SharedItemsImports = 4 + EndGlobalSection GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Debug|ARM = Debug|ARM @@ -73,6 +81,9 @@ Global GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {4B988559-DDBD-4456-9300-0C3AAD12C466} = {60618CAC-2995-4DF9-9914-45C6FC02C995} + EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {B6FDB70C-A751-422C-ACD1-E35419495857} EndGlobalSection diff --git a/src/Telemetry/MicrosoftTelemetry.h b/src/Telemetry/MicrosoftTelemetry.h new file mode 100644 index 0000000000..7752516fb8 --- /dev/null +++ b/src/Telemetry/MicrosoftTelemetry.h @@ -0,0 +1,87 @@ +/* ++ + +Copyright (c) Microsoft Corporation. All rights reserved. +Licensed under the MIT License. See LICENSE in the project root for license information. + +Module Name: + + MicrosoftTelemetry.h + +Abstract: + + Macro definitions used by this project's TraceLogging ETW providers: + + - Configuration macros that select the ETW Provider Groups to be used by + this project. + - Constants for tags that are commonly used in Microsoft's + TraceLogging-based ETW. + + Different versions of this file use different definitions for the + TraceLoggingOption configuration macros. The definitions in this file are + empty. As a result, providers using this configuration file will not join + any ETW Provider Groups and will not be given any special treatment by + group-sensitive ETW listeners. + +Environment: + + User mode or kernel mode. + +--*/ + +#pragma once + +// Configuration macro for use in TRACELOGGING_DEFINE_PROVIDER. The definition +// in this file configures the provider as a normal (non-telemetry) provider. +#define TraceLoggingOptionMicrosoftTelemetry() \ + // Empty definition for TraceLoggingOptionMicrosoftTelemetry + +// Configuration macro for use in TRACELOGGING_DEFINE_PROVIDER. The definition +// in this file configures the provider as a normal (non-telemetry) provider. +#define TraceLoggingOptionWindowsCoreTelemetry() \ + // Empty definition for TraceLoggingOptionWindowsCoreTelemetry + +// Event privacy tags. Use the PDT macro values for the tag parameter, e.g.: +// TraceLoggingWrite(..., +// TelemetryPrivacyDataTag(PDT_BrowsingHistory | PDT_ProductAndServiceUsage), +// ...); +#define TelemetryPrivacyDataTag(tag) TraceLoggingUInt64((tag), "PartA_PrivTags") +#define PDT_BrowsingHistory 0x0000000000000002u +#define PDT_DeviceConnectivityAndConfiguration 0x0000000000000800u +#define PDT_InkingTypingAndSpeechUtterance 0x0000000000020000u +#define PDT_ProductAndServicePerformance 0x0000000001000000u +#define PDT_ProductAndServiceUsage 0x0000000002000000u +#define PDT_SoftwareSetupAndInventory 0x0000000080000000u + +// Event categories specified via keywords, e.g.: +// TraceLoggingWrite(..., +// TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES), +// ...); +#define MICROSOFT_KEYWORD_CRITICAL_DATA 0x0000800000000000 // Bit 47 +#define MICROSOFT_KEYWORD_MEASURES 0x0000400000000000 // Bit 46 +#define MICROSOFT_KEYWORD_TELEMETRY 0x0000200000000000 // Bit 45 +#define MICROSOFT_KEYWORD_RESERVED_44 0x0000100000000000 // Bit 44 (reserved for future assignment) + +// Event categories specified via event tags, e.g.: +// TraceLoggingWrite(..., +// TraceLoggingEventTag(MICROSOFT_EVENTTAG_REALTIME_LATENCY), +// ...); +#define MICROSOFT_EVENTTAG_DROP_USER_IDS 0x00008000 +#define MICROSOFT_EVENTTAG_AGGREGATE 0x00010000 +#define MICROSOFT_EVENTTAG_DROP_PII_EXCEPT_IP 0x00020000 +#define MICROSOFT_EVENTTAG_COSTDEFERRED_LATENCY 0x00040000 +#define MICROSOFT_EVENTTAG_CORE_DATA 0x00080000 +#define MICROSOFT_EVENTTAG_INJECT_XTOKEN 0x00100000 +#define MICROSOFT_EVENTTAG_REALTIME_LATENCY 0x00200000 +#define MICROSOFT_EVENTTAG_NORMAL_LATENCY 0x00400000 +#define MICROSOFT_EVENTTAG_CRITICAL_PERSISTENCE 0x00800000 +#define MICROSOFT_EVENTTAG_NORMAL_PERSISTENCE 0x01000000 +#define MICROSOFT_EVENTTAG_DROP_PII 0x02000000 +#define MICROSOFT_EVENTTAG_HASH_PII 0x04000000 +#define MICROSOFT_EVENTTAG_MARK_PII 0x08000000 + +// Field categories specified via field tags, e.g.: +// TraceLoggingWrite(..., +// TraceLoggingString(szUser, "UserName", "User's name", MICROSOFT_FIELDTAG_HASH_PII), +// ...); +#define MICROSOFT_FIELDTAG_DROP_PII 0x04000000 +#define MICROSOFT_FIELDTAG_HASH_PII 0x08000000 diff --git a/src/Telemetry/Telemetry.vcxitems b/src/Telemetry/Telemetry.vcxitems new file mode 100644 index 0000000000..12788fc320 --- /dev/null +++ b/src/Telemetry/Telemetry.vcxitems @@ -0,0 +1,24 @@ + + + + $(MSBuildAllProjects);$(MSBuildThisFileFullPath) + true + {4b988559-ddbd-4456-9300-0c3aad12c466} + + + + %(AdditionalIncludeDirectories);$(MSBuildThisFileDirectory) + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/Telemetry/TraceLogging.cpp b/src/Telemetry/TraceLogging.cpp new file mode 100644 index 0000000000..faa396fb9b --- /dev/null +++ b/src/Telemetry/TraceLogging.cpp @@ -0,0 +1,57 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#include "pch.h" +#include "TraceLogging.h" + +// GUID for Microsoft.PackageManager.Client : {c0cf606f-569b-5c20-27d9-88a745fa2175} +TRACELOGGING_DEFINE_PROVIDER( + g_hTelemetryProvider, + "Microsoft.PackageManager.Client", + (0xc0cf606f, 0x569b, 0x5c20, 0x27, 0xd9, 0x88, 0xa7, 0x45, 0xfa, 0x21, 0x75), + TraceLoggingOptionMicrosoftTelemetry()); + +bool g_IsTelemetryProviderEnabled{}; +UCHAR g_TelemetryProviderLevel{}; +ULONGLONG g_TelemetryProviderMatchAnyKeyword{}; +GUID g_TelemetryProviderActivityId{}; + +void WINAPI TelemetryProviderEnabledCallback( + _In_ LPCGUID /*sourceId*/, + _In_ ULONG isEnabled, + _In_ UCHAR level, + _In_ ULONGLONG matchAnyKeyword, + _In_ ULONGLONG /*matchAllKeywords*/, + _In_opt_ PEVENT_FILTER_DESCRIPTOR /*filterData*/, + _In_opt_ PVOID /*callbackContext*/) +{ + g_IsTelemetryProviderEnabled = !!isEnabled; + g_TelemetryProviderLevel = level; + g_TelemetryProviderMatchAnyKeyword = matchAnyKeyword; +} + +void RegisterTraceLogging() +{ + HRESULT hr = S_OK; + + TraceLoggingRegisterEx(g_hTelemetryProvider, TelemetryProviderEnabledCallback, nullptr); + //Generate the ActivityId used to track the session + hr = CoCreateGuid(&g_TelemetryProviderActivityId); + if (FAILED(hr)) + { + TraceLoggingWriteActivity( + g_hTelemetryProvider, + "CreateGuidError", + nullptr, + nullptr, + TraceLoggingHResult(hr), + TraceLoggingKeyword(MICROSOFT_KEYWORD_MEASURES)); + + g_TelemetryProviderActivityId = GUID_NULL; + }; +} + +void UnRegisterTraceLogging() +{ + TraceLoggingUnregister(g_hTelemetryProvider); +} diff --git a/src/Telemetry/TraceLogging.h b/src/Telemetry/TraceLogging.h new file mode 100644 index 0000000000..b9d8337da4 --- /dev/null +++ b/src/Telemetry/TraceLogging.h @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +#pragma once + +#include "WinEventLogLevels.h" +#include + +#include "MicrosoftTelemetry.h" + +// Keywords +#define KEYWORD_REPEATER 0x0000000000000001 +#define KEYWORD_SCROLLER 0x0000000000000002 +#define KEYWORD_PTR 0x0000000000000004 +#define KEYWORD_SCROLLVIEWER 0x0000000000000008 +#define KEYWORD_SWIPECONTROL 0x0000000000000010 +#define KEYWORD_COMMANDBARFLYOUT 0x0000000000000020 + +// Common output formats +#define TRACE_MSG_METH L"%s[0x%p]()\n" +#define TRACE_MSG_METH_DBL L"%s[0x%p](%lf)\n" +#define TRACE_MSG_METH_DBL_DBL L"%s[0x%p](%lf, %lf)\n" +#define TRACE_MSG_METH_DBL_INT L"%s[0x%p](%lf, %d)\n" +#define TRACE_MSG_METH_DBL_DBL_INT L"%s[0x%p](%lf, %lf, %d)\n" +#define TRACE_MSG_METH_DBL_DBL_FLT L"%s[0x%p](%lf, %lf, %f)\n" +#define TRACE_MSG_METH_DBL_DBL_STR L"%s[0x%p](%lf, %lf, %s)\n" +#define TRACE_MSG_METH_FLT L"%s[0x%p](%f)\n" +#define TRACE_MSG_METH_FLT_FLT L"%s[0x%p](%f, %f)\n" +#define TRACE_MSG_METH_FLT_FLT_FLT L"%s[0x%p](%f, %f, %f)\n" +#define TRACE_MSG_METH_FLT_FLT_FLT_FLT L"%s[0x%p](%f, %f, %f, %f)\n" +#define TRACE_MSG_METH_FLT_FLT_STR_INT L"%s[0x%p](%f, %f, %s, %d)\n" +#define TRACE_MSG_METH_INT L"%s[0x%p](%d)\n" +#define TRACE_MSG_METH_INT_INT L"%s[0x%p](%d, %d)\n" +#define TRACE_MSG_METH_PTR L"%s[0x%p](0x%p)\n" +#define TRACE_MSG_METH_PTR_PTR L"%s[0x%p](0x%p, 0x%p)\n" +#define TRACE_MSG_METH_PTR_DBL L"%s[0x%p](0x%p, %lf)\n" +#define TRACE_MSG_METH_PTR_INT L"%s[0x%p](0x%p, %d)\n" +#define TRACE_MSG_METH_PTR_STR L"%s[0x%p](0x%p, %s)\n" +#define TRACE_MSG_METH_STR L"%s[0x%p](%s)\n" +#define TRACE_MSG_METH_STR_STR L"%s[0x%p](%s, %s)\n" +#define TRACE_MSG_METH_STR_DBL L"%s[0x%p](%s, %lf)\n" +#define TRACE_MSG_METH_STR_FLT L"%s[0x%p](%s, %f)\n" +#define TRACE_MSG_METH_STR_INT L"%s[0x%p](%s, %d)\n" +#define TRACE_MSG_METH_STR_STR_STR L"%s[0x%p](%s, %s, %s)\n" +#define TRACE_MSG_METH_STR_INT_INT L"%s[0x%p](%s, %d, %d)\n" +#define TRACE_MSG_METH_STR_FLT_FLT L"%s[0x%p](%s, %f, %f)\n" +#define TRACE_MSG_METH_STR_STR_FLT L"%s[0x%p](%s, %s, %f)\n" +#define TRACE_MSG_METH_STR_STR_INT_INT L"%s[0x%p](%s, %s, %d, %d)\n" + +#define TRACE_MSG_METH_METH L"%s[0x%p] - calls %s()\n" +#define TRACE_MSG_METH_METH_INT L"%s[0x%p] - calls %s(%d)\n" +#define TRACE_MSG_METH_METH_STR L"%s[0x%p] - calls %s(%s)\n" +#define TRACE_MSG_METH_METH_STR_STR L"%s[0x%p] - calls %s(%s, %s)\n" +#define TRACE_MSG_METH_METH_FLT_STR L"%s[0x%p] - calls %s(%f, %s)\n" +#define TRACE_MSG_METH_METH_FLT_FLT_FLT L"%s[0x%p] - calls %s(%f, %f, %f)\n" + +// Current method name +#define METH_NAME StringUtil::Utf8ToUtf16(__FUNCTION__).c_str() + +// TraceLogging provider name for telemetry. +#define TELEMETRY_PROVIDER_NAME "Microsoft.PackageManager.Client" + +TRACELOGGING_DECLARE_PROVIDER(g_hTelemetryProvider); +extern bool g_IsTelemetryProviderEnabled; +extern UCHAR g_TelemetryProviderLevel; +extern ULONGLONG g_TelemetryProviderMatchAnyKeyword; +extern GUID g_TelemetryProviderActivityId; + +extern void RegisterTraceLogging(); +extern void UnRegisterTraceLogging(); diff --git a/src/Telemetry/WinEventLogLevels.h b/src/Telemetry/WinEventLogLevels.h new file mode 100644 index 0000000000..6d8121ec0c --- /dev/null +++ b/src/Telemetry/WinEventLogLevels.h @@ -0,0 +1,11 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See LICENSE in the project root for license information. + +// From winmeta.h + +#define WINEVENT_LEVEL_LOG_ALWAYS 0x0 +#define WINEVENT_LEVEL_CRITICAL 0x1 +#define WINEVENT_LEVEL_ERROR 0x2 +#define WINEVENT_LEVEL_WARNING 0x3 +#define WINEVENT_LEVEL_INFO 0x4 +#define WINEVENT_LEVEL_VERBOSE 0x5 \ No newline at end of file