Telemetry.h (5095B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include <AppInstallerLanguageUtilities.h> 5 #include <winrt/Microsoft.Management.Configuration.h> 6 #include <winrt/Windows.Foundation.Collections.h> 7 #include "ConfigurationUnitResultInformation.h" 8 #include "ConfigurationSet.h" 9 #include "TestConfigurationSetResult.h" 10 #include "ApplyConfigurationSetResult.h" 11 12 #include <cguid.h> 13 #include <string> 14 #include <string_view> 15 #include <vector> 16 17 namespace winrt::Microsoft::Management::Configuration::implementation 18 { 19 // Provides the ability to write telemetry events. 20 struct TelemetryTraceLogger 21 { 22 TelemetryTraceLogger(); 23 24 TelemetryTraceLogger(const TelemetryTraceLogger&) = default; 25 TelemetryTraceLogger& operator=(const TelemetryTraceLogger&) = default; 26 27 TelemetryTraceLogger(TelemetryTraceLogger&&) = default; 28 TelemetryTraceLogger& operator=(TelemetryTraceLogger&&) = default; 29 30 // Control whether this trace logger is enabled at runtime. 31 // Returns the previous value. 32 bool EnableRuntime(bool value); 33 34 // Returns a value indicating whether the logger is enabled. 35 bool IsEnabled() const; 36 37 // Sets the current activity identifier. 38 void SetActivityId(const guid& value); 39 40 // Return address of m_activityId 41 const GUID* GetActivityId() const; 42 43 // Store the passed in name of the caller 44 void SetCaller(std::string_view caller); 45 46 // Get the current caller value 47 std::string_view GetCaller() const; 48 49 static constexpr std::string_view GetAction = "get"; 50 static constexpr std::string_view ApplyAction = "apply"; 51 static constexpr std::string_view TestAction = "test"; 52 static constexpr std::string_view ExportAction = "export"; 53 54 // Logs information about running a configuration unit. 55 // The caller is expected to only call this for failures from publicly available units. 56 void LogConfigUnitRun( 57 const guid& setIdentifier, 58 const guid& unitIdentifier, 59 hstring unitName, 60 hstring moduleName, 61 ConfigurationUnitIntent unitIntent, 62 ConfigurationUnitIntent runIntent, 63 std::string_view action, 64 hresult result, 65 ConfigurationUnitResultSource failurePoint, 66 std::wstring_view settingNames) const noexcept; 67 68 // Logs information about running a configuration unit in the appropriate conditions. 69 void LogConfigUnitRunIfAppropriate( 70 const guid& setIdentifier, 71 const Configuration::ConfigurationUnit& unit, 72 ConfigurationUnitIntent runIntent, 73 std::string_view action, 74 const IConfigurationUnitResultInformation& resultInformation) const noexcept; 75 76 // The summary information for a specific unit intent. 77 struct ProcessingSummaryForIntent 78 { 79 ConfigurationUnitIntent Intent; 80 uint32_t Count; 81 uint32_t Run; 82 uint32_t Failed; 83 }; 84 85 // Logs a processing summary event for a configuration set. 86 void LogConfigProcessingSummary( 87 const guid& setIdentifier, 88 std::string_view inputHash, 89 ConfigurationUnitIntent runIntent, 90 hresult result, 91 ConfigurationUnitResultSource failurePoint, 92 const ProcessingSummaryForIntent& assertSummary, 93 const ProcessingSummaryForIntent& informSummary, 94 const ProcessingSummaryForIntent& applySummary) const noexcept; 95 96 // Logs a processing summary event for a configuration set test run. 97 void LogConfigProcessingSummaryForTest( 98 const ConfigurationSet& configurationSet, 99 const TestConfigurationSetResult& result) const noexcept; 100 101 // Logs a processing summary event for a configuration set test run exception. 102 void LogConfigProcessingSummaryForTestException( 103 const ConfigurationSet& configurationSet, 104 hresult error, 105 const TestConfigurationSetResult& result) const noexcept; 106 107 // Logs a processing summary event for a configuration set apply run. 108 void LogConfigProcessingSummaryForApply( 109 const ConfigurationSet& configurationSet, 110 const ApplyConfigurationSetResult& result) const noexcept; 111 112 // Logs a processing summary event for a configuration set apply run exception. 113 void LogConfigProcessingSummaryForApplyException( 114 const ConfigurationSet& configurationSet, 115 hresult error, 116 const ApplyConfigurationSetResult& result) const noexcept; 117 118 protected: 119 bool IsTelemetryEnabled() const noexcept; 120 121 CopyConstructibleAtomic<bool> m_isRuntimeEnabled{ true }; 122 123 GUID m_activityId = GUID_NULL; 124 std::string m_version; 125 std::string m_caller; 126 }; 127 }