| 1 | /*++ |
| 2 | |
| 3 | Copyright (c) Microsoft. All rights reserved. |
| 4 | |
| 5 | Module Name: |
| 6 | |
| 7 | WslTelemetry.cpp |
| 8 | |
| 9 | Abstract: |
| 10 | |
| 11 | This file contains tracing function definitions. |
| 12 | |
| 13 | --*/ |
| 14 | |
| 15 | #include "wslservice.h" |
| 16 | #include "ExecutionContext.h" |
| 17 | #include "WslTelemetry.h" |
| 18 | |
| 19 | #define WSL_TELEMETRY_KEYWORDS (MICROSOFT_KEYWORD_CRITICAL_DATA | MICROSOFT_KEYWORD_MEASURES | MICROSOFT_KEYWORD_TELEMETRY) |
| 20 | |
| 21 | TRACELOGGING_DEFINE_PROVIDER( |
| 22 | LxssTelemetryProvider, |
| 23 | "Microsoft.Windows.Subsystem.Lxss", |
| 24 | // {d90b9468-67f0-5b3b-42cc-82ac81ffd960} |
| 25 | (0xd90b9468, 0x67f0, 0x5b3b, 0x42, 0xcc, 0x82, 0xac, 0x81, 0xff, 0xd9, 0x60), |
| 26 | TraceLoggingOptionMicrosoftTelemetry()); |
| 27 | |
| 28 | TRACELOGGING_DEFINE_PROVIDER( |
| 29 | WslServiceTelemetryProvider, |
| 30 | "Microsoft.Windows.Lxss.Manager", |
| 31 | // {b99cdb5a-039c-5046-e672-1a0de0a40211} |
| 32 | (0xb99cdb5a, 0x039c, 0x5046, 0xe6, 0x72, 0x1a, 0x0d, 0xe0, 0xa4, 0x02, 0x11), |
| 33 | TraceLoggingOptionMicrosoftTelemetry()); |
| 34 | |
| 35 | TRACELOGGING_DEFINE_PROVIDER( |
| 36 | WslcTelemetryProvider, |
| 37 | "Microsoft.Windows.Wslc", |
| 38 | // {0383CE62-8F86-4766-AFB2-9D66A7FB1E90} |
| 39 | (0x383ce62, 0x8f86, 0x4766, 0xaf, 0xb2, 0x9d, 0x66, 0xa7, 0xfb, 0x1e, 0x90), |
| 40 | TraceLoggingOptionMicrosoftTelemetry()); |
| 41 | |
| 42 | #ifdef DEBUG |
| 43 | #define HRESULT_STRING_VALUE \ |
| 44 | , TraceLoggingValue(wsl::windows::common::wslutil::ErrorCodeToString(failure->hr).c_str(), "HRESULTString") |
| 45 | #else |
| 46 | #define HRESULT_STRING_VALUE |
| 47 | #endif |
| 48 | |
| 49 | TraceLoggingHProvider g_hTraceLoggingProvider; |
| 50 | |
| 51 | namespace WslTraceLoggingInternal { |
| 52 | static bool g_disableTelemetryByDefault = true; |
| 53 | static std::atomic<long> g_ClientsWithTelemetryEnabled = 0; |
| 54 | static std::atomic<long> g_ClientsWithTelemetryDisabled = 0; |
| 55 | } // namespace WslTraceLoggingInternal |
| 56 | |
| 57 | WslTraceLoggingClient::WslTraceLoggingClient(bool TelemetryEnabled) : m_clientTelemetryEnabled(TelemetryEnabled) |
| 58 | { |
| 59 | if (m_clientTelemetryEnabled) |
| 60 | { |
| 61 | ++WslTraceLoggingInternal::g_ClientsWithTelemetryEnabled; |
| 62 | } |
| 63 | else |
| 64 | { |
| 65 | ++WslTraceLoggingInternal::g_ClientsWithTelemetryDisabled; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | WslTraceLoggingClient::~WslTraceLoggingClient() |
| 70 | { |
| 71 | if (m_clientTelemetryEnabled) |
| 72 | { |
| 73 | --WslTraceLoggingInternal::g_ClientsWithTelemetryEnabled; |
| 74 | WI_ASSERT(WslTraceLoggingInternal::g_ClientsWithTelemetryEnabled >= 0); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | --WslTraceLoggingInternal::g_ClientsWithTelemetryDisabled; |
| 79 | WI_ASSERT(WslTraceLoggingInternal::g_ClientsWithTelemetryDisabled >= 0); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | void WslTraceLoggingInitialize(_In_ const TraceLoggingHProvider provider, _In_ BOOLEAN DisableTelemetryByDefault, _In_ std::optional<TLG_PENABLECALLBACK> callback) |
| 84 | { |
| 85 | WI_ASSERT(!g_hTraceLoggingProvider); |
| 86 | |
| 87 | WslTraceLoggingInternal::g_disableTelemetryByDefault = (DisableTelemetryByDefault != FALSE); |
| 88 | g_hTraceLoggingProvider = provider; |
| 89 | |
| 90 | if (callback.has_value()) |
| 91 | { |
| 92 | TraceLoggingRegisterEx(g_hTraceLoggingProvider, callback.value(), nullptr); |
| 93 | } |
| 94 | else |
| 95 | { |
| 96 | TraceLoggingRegister(g_hTraceLoggingProvider); |
| 97 | } |
| 98 | |
| 99 | wil::g_pfnResultLoggingCallback = [](wil::FailureInfo* failure, PWSTR, size_t) WI_NOEXCEPT { |
| 100 | if (failure->type == wil::FailureType::Exception || failure->type == wil::FailureType::Return) |
| 101 | { |
| 102 | wsl::windows::common::ExecutionContext::CollectError(failure->hr); |
| 103 | } |
| 104 | |
| 105 | if (g_hTraceLoggingProvider == LxssTelemetryProvider) |
| 106 | { |
| 107 | // Do not log telemetry for the internal invalid command line argument |
| 108 | // error. |
| 109 | |
| 110 | if (failure->hr == WSL_E_INVALID_USAGE) |
| 111 | { |
| 112 | return; |
| 113 | } |
| 114 | |
| 115 | switch (failure->type) |
| 116 | { |
| 117 | case wil::FailureType::Exception: |
| 118 | case wil::FailureType::FailFast: |
| 119 | WSL_LOG( |
| 120 | "LxssException", |
| 121 | TraceLoggingLevel(WINEVENT_LEVEL_ERROR), |
| 122 | TraceLoggingValue(failure->pszFile, "File"), |
| 123 | TraceLoggingValue(failure->pszFunction, "FunctionName"), |
| 124 | TraceLoggingValue(failure->uLineNumber, "Line number"), |
| 125 | TraceLoggingValue(static_cast<DWORD>(failure->type), "Type"), |
| 126 | TraceLoggingHexUInt32(failure->hr, "HRESULT"), |
| 127 | TraceLoggingValue(failure->pszMessage, "Message"), |
| 128 | TraceLoggingValue(failure->pszCode, "Code") HRESULT_STRING_VALUE); |
| 129 | |
| 130 | break; |
| 131 | |
| 132 | default: |
| 133 | WSL_LOG( |
| 134 | "LxssVerboseLog", |
| 135 | TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), |
| 136 | TraceLoggingValue(failure->pszFile, "File"), |
| 137 | TraceLoggingValue(failure->pszFunction, "FunctionName"), |
| 138 | TraceLoggingValue(failure->uLineNumber, "Line number"), |
| 139 | TraceLoggingValue(static_cast<DWORD>(failure->type), "Type"), |
| 140 | TraceLoggingHexUInt32(failure->hr, "HRESULT"), |
| 141 | TraceLoggingValue(failure->pszMessage, "Message"), |
| 142 | TraceLoggingValue(failure->pszCode, "Code") HRESULT_STRING_VALUE); |
| 143 | |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | else |
| 148 | { |
| 149 | switch (failure->type) |
| 150 | { |
| 151 | case wil::FailureType::Exception: |
| 152 | case wil::FailureType::FailFast: |
| 153 | WSL_LOG( |
| 154 | "Error", |
| 155 | TraceLoggingLevel(WINEVENT_LEVEL_ERROR), |
| 156 | TraceLoggingValue(failure->pszFile, "file"), |
| 157 | TraceLoggingValue(failure->uLineNumber, "linenumber"), |
| 158 | TraceLoggingValue(static_cast<DWORD>(failure->type), "type"), |
| 159 | TraceLoggingValue(failure->cFailureCount, "failurecount"), |
| 160 | TraceLoggingValue(GetCurrentThreadId(), "threadid"), |
| 161 | TraceLoggingHexUInt32(failure->hr, "hr"), |
| 162 | TraceLoggingValue(failure->pszMessage, "message"), |
| 163 | TraceLoggingValue(failure->pszCode, "code"), |
| 164 | TraceLoggingValue(failure->pszFunction, "function") HRESULT_STRING_VALUE); |
| 165 | |
| 166 | break; |
| 167 | |
| 168 | default: |
| 169 | WSL_LOG( |
| 170 | "VerboseLog", |
| 171 | TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE), |
| 172 | TraceLoggingValue(failure->pszFile, "file"), |
| 173 | TraceLoggingValue(failure->uLineNumber, "linenumber"), |
| 174 | TraceLoggingValue(failure->cFailureCount, "failurecount"), |
| 175 | TraceLoggingValue(GetCurrentThreadId(), "threadid"), |
| 176 | TraceLoggingHexUInt32(failure->hr, "hr"), |
| 177 | TraceLoggingValue(failure->pszMessage, "message"), |
| 178 | TraceLoggingValue(failure->pszCode, "code"), |
| 179 | TraceLoggingValue(failure->pszFunction, "function") HRESULT_STRING_VALUE); |
| 180 | |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | }; |
| 185 | } |
| 186 | |
| 187 | void WslTraceLoggingUninitialize() |
| 188 | { |
| 189 | wil::g_pfnResultLoggingCallback = nullptr; |
| 190 | TraceLoggingUnregister(g_hTraceLoggingProvider); |
| 191 | } |
| 192 | |
| 193 | bool WslTraceLoggingShouldDisableTelemetry() noexcept |
| 194 | { |
| 195 | return (WslTraceLoggingInternal::g_ClientsWithTelemetryDisabled > 0) || |
| 196 | (WslTraceLoggingInternal::g_disableTelemetryByDefault && (WslTraceLoggingInternal::g_ClientsWithTelemetryEnabled == 0)); |
| 197 | } |