Skip to content

Commit 3e9a6a1

Browse files
[cmdpal] WIP add telemetry (#38032)
Related to #37908 Closes zadjii-msft#520 --------- Co-authored-by: Mike Griese <[email protected]>
1 parent 14919df commit 3e9a6a1

16 files changed

+210
-5
lines changed

src/common/interop/shared_constants.h

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ namespace CommonSharedConstants
130130

131131
// used from quick access window
132132
const wchar_t CMDPAL_SHOW_EVENT[] = L"Local\\PowerToysCmdPal-ShowEvent-62336fcd-8611-4023-9b30-091a6af4cc5a";
133+
const wchar_t CMDPAL_EXIT_EVENT[] = L"Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd";
133134

134135
// Max DWORD for key code to disable keys.
135136
const DWORD VK_DISABLED = 0x100;

src/modules/cmdpal/CmdPalModuleInterface/dllmain.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <common/utils/resources.h>
1111
#include <common/utils/package.h>
1212
#include <common/utils/process_path.h>
13+
#include <common/interop/shared_constants.h>
1314
#include <Psapi.h>
1415
#include <TlHelp32.h>
1516
#include <common/utils/winapi_error.h>
@@ -43,6 +44,8 @@ class CmdPal : public PowertoyModuleIface
4344
//contains the non localized key of the powertoy
4445
std::wstring app_key;
4546

47+
HANDLE m_hTerminateEvent;
48+
4649
void LaunchApp(const std::wstring& appPath, const std::wstring& commandLineArgs, bool elevated)
4750
{
4851
std::wstring dir = std::filesystem::path(appPath).parent_path();
@@ -107,6 +110,11 @@ class CmdPal : public PowertoyModuleIface
107110

108111
if (hProcess != NULL)
109112
{
113+
SetEvent(m_hTerminateEvent);
114+
115+
// Wait for 1.5 seconds for the process to end correctly and stop etw tracer
116+
WaitForSingleObject(hProcess, 1500);
117+
110118
TerminateProcess(hProcess, 0);
111119
CloseHandle(hProcess);
112120
}
@@ -119,6 +127,8 @@ class CmdPal : public PowertoyModuleIface
119127
app_name = L"CmdPal";
120128
app_key = L"CmdPal";
121129
LoggerHelpers::init_logger(app_key, L"ModuleInterface", "CmdPal");
130+
131+
m_hTerminateEvent = CreateDefaultEvent(CommonSharedConstants::CMDPAL_EXIT_EVENT);
122132
}
123133

124134
~CmdPal()

src/modules/cmdpal/Microsoft.CmdPal.UI/App.xaml.cs

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The Microsoft Corporation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.CmdPal.Common.Helpers;
56
using Microsoft.CmdPal.Common.Services;
67
using Microsoft.CmdPal.Ext.Apps;
78
using Microsoft.CmdPal.Ext.Bookmarks;
@@ -22,6 +23,7 @@
2223
using Microsoft.CmdPal.UI.ViewModels.Models;
2324
using Microsoft.CommandPalette.Extensions;
2425
using Microsoft.Extensions.DependencyInjection;
26+
using Microsoft.PowerToys.Telemetry;
2527
using Microsoft.UI.Xaml;
2628

2729
// To learn more about WinUI, the WinUI project structure,
@@ -40,6 +42,8 @@ public partial class App : Application
4042

4143
public Window? AppWindow { get; private set; }
4244

45+
public ETWTrace EtwTrace { get; private set; } = new ETWTrace();
46+
4347
/// <summary>
4448
/// Gets the <see cref="IServiceProvider"/> instance to resolve application services.
4549
/// </summary>
@@ -55,6 +59,13 @@ public App()
5559
Services = ConfigureServices();
5660

5761
this.InitializeComponent();
62+
63+
NativeEventWaiter.WaitForEventLoop(
64+
"Local\\PowerToysCmdPal-ExitEvent-eb73f6be-3f22-4b36-aee3-62924ba40bfd", () =>
65+
{
66+
EtwTrace?.Dispose();
67+
Environment.Exit(0);
68+
});
5869
}
5970

6071
/// <summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class BeginInvoke : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class CmdPalDismissedOnEsc : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class CmdPalDismissedOnLostFocus : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class CmdPalHotkeySummoned : EventBase, IEvent
14+
{
15+
public bool Global { get; set; }
16+
17+
public CmdPalHotkeySummoned(bool global)
18+
{
19+
Global = global;
20+
}
21+
22+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class CmdPalInvokeResult : EventBase, IEvent
14+
{
15+
public string ResultKind { get; set; }
16+
17+
public CmdPalInvokeResult(CommandResultKind resultKind)
18+
{
19+
ResultKind = resultKind.ToString();
20+
}
21+
22+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class CmdPalProcessStarted : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class ColdLaunch : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class OpenPage : EventBase, IEvent
14+
{
15+
public int PageDepth { get; set; }
16+
17+
public OpenPage(int pageDepth)
18+
{
19+
PageDepth = pageDepth;
20+
}
21+
22+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation
2+
// The Microsoft Corporation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Diagnostics.Tracing;
6+
using Microsoft.CommandPalette.Extensions;
7+
using Microsoft.PowerToys.Telemetry;
8+
using Microsoft.PowerToys.Telemetry.Events;
9+
10+
namespace Microsoft.CmdPal.UI.Events;
11+
12+
[EventData]
13+
public class ReactivateInstance : EventBase, IEvent
14+
{
15+
public PartA_PrivTags PartA_PrivTags => PartA_PrivTags.ProductAndServiceUsage;
16+
}

src/modules/cmdpal/Microsoft.CmdPal.UI/MainWindow.xaml.cs

+5
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
using Microsoft.CmdPal.Common.Helpers;
88
using Microsoft.CmdPal.Common.Messages;
99
using Microsoft.CmdPal.Common.Services;
10+
using Microsoft.CmdPal.UI.Events;
1011
using Microsoft.CmdPal.UI.ViewModels;
1112
using Microsoft.CmdPal.UI.ViewModels.Messages;
1213
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.PowerToys.Telemetry;
1315
using Microsoft.UI.Composition;
1416
using Microsoft.UI.Composition.SystemBackdrops;
1517
using Microsoft.UI.Input;
@@ -368,6 +370,8 @@ internal void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
368370
else
369371
{
370372
PInvoke.ShowWindow(_hwnd, SHOW_WINDOW_CMD.SW_HIDE);
373+
374+
PowerToysTelemetry.Log.WriteEvent(new CmdPalDismissedOnLostFocus());
371375
}
372376
}
373377

@@ -460,6 +464,7 @@ private LRESULT HotKeyPrc(
460464
{
461465
var hotkey = _hotkeys[hotkeyIndex];
462466
var isRootHotkey = string.IsNullOrEmpty(hotkey.CommandId);
467+
PowerToysTelemetry.Log.WriteEvent(new CmdPalHotkeySummoned(isRootHotkey));
463468

464469
// Note to future us: the wParam will have the index of the hotkey we registered.
465470
// We can use that in the future to differentiate the hotkeys we've pressed

src/modules/cmdpal/Microsoft.CmdPal.UI/Microsoft.CmdPal.UI.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@
9090
</ItemGroup>
9191

9292
<ItemGroup>
93+
<ProjectReference Include="..\..\..\common\ManagedTelemetry\Telemetry\ManagedTelemetry.csproj" />
9394
<ProjectReference Include="..\Exts\Microsoft.CmdPal.Ext.System\Microsoft.CmdPal.Ext.System.csproj" />
9495
<ProjectReference Include="..\Exts\Microsoft.CmdPal.Ext.WebSearch\Microsoft.CmdPal.Ext.WebSearch.csproj" />
9596
<ProjectReference Include="..\Exts\Microsoft.CmdPal.Ext.Indexer\Microsoft.CmdPal.Ext.Indexer.csproj" />

src/modules/cmdpal/Microsoft.CmdPal.UI/Pages/ShellPage.xaml.cs

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
using CommunityToolkit.WinUI;
88
using ManagedCommon;
99
using Microsoft.CmdPal.Common.Services;
10+
using Microsoft.CmdPal.UI.Events;
1011
using Microsoft.CmdPal.UI.Settings;
1112
using Microsoft.CmdPal.UI.ViewModels;
1213
using Microsoft.CmdPal.UI.ViewModels.MainPage;
1314
using Microsoft.CmdPal.UI.ViewModels.Messages;
1415
using Microsoft.CommandPalette.Extensions;
1516
using Microsoft.Extensions.DependencyInjection;
17+
using Microsoft.PowerToys.Telemetry;
1618
using Microsoft.UI.Dispatching;
1719
using Microsoft.UI.Xaml.Controls;
1820
using Microsoft.UI.Xaml.Media.Animation;
@@ -91,6 +93,8 @@ public void Receive(NavigateBackMessage message)
9193
{
9294
// If we can't go back then we must be at the top and thus escape again should quit.
9395
WeakReferenceMessenger.Default.Send<DismissMessage>();
96+
97+
PowerToysTelemetry.Log.WriteEvent(new CmdPalDismissedOnEsc());
9498
}
9599
}
96100
}
@@ -185,6 +189,8 @@ private void PerformCommand(PerformCommandMessage message)
185189
pageViewModel,
186190
message.WithAnimation ? _slideRightTransition : _noAnimation);
187191

192+
PowerToysTelemetry.Log.WriteEvent(new OpenPage(RootFrame.BackStackDepth));
193+
188194
// Refocus on the Search for continual typing on the next search request
189195
SearchBox.Focus(Microsoft.UI.Xaml.FocusState.Programmatic);
190196

@@ -201,6 +207,7 @@ private void PerformCommand(PerformCommandMessage message)
201207
else if (command is IInvokableCommand invokable)
202208
{
203209
Logger.LogDebug($"Invoking command");
210+
PowerToysTelemetry.Log.WriteEvent(new BeginInvoke());
204211
HandleInvokeCommand(message, invokable);
205212
}
206213
}
@@ -317,6 +324,7 @@ private void HandleCommandResultOnUiThread(ICommandResult? result)
317324
{
318325
var kind = result.Kind;
319326
Logger.LogDebug($"handling {kind.ToString()}");
327+
PowerToysTelemetry.Log.WriteEvent(new CmdPalInvokeResult(kind));
320328
switch (kind)
321329
{
322330
case CommandResultKind.Dismiss:
@@ -344,7 +352,6 @@ private void HandleCommandResultOnUiThread(ICommandResult? result)
344352
{
345353
// Keep this page open, but hide the palette.
346354
WeakReferenceMessenger.Default.Send<DismissMessage>();
347-
348355
break;
349356
}
350357

src/modules/cmdpal/Microsoft.CmdPal.UI/Program.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
// See the LICENSE file in the project root for more information.
44

55
using ManagedCommon;
6+
using Microsoft.CmdPal.UI.Events;
7+
using Microsoft.PowerToys.Telemetry;
68
using Microsoft.Windows.AppLifecycle;
79

810
namespace Microsoft.CmdPal.UI;
@@ -30,9 +32,10 @@ private static int Main(string[] args)
3032

3133
Logger.InitializeLogger("\\CmdPal\\Logs\\");
3234
Logger.LogDebug($"Starting at {DateTime.UtcNow}");
35+
PowerToysTelemetry.Log.WriteEvent(new CmdPalProcessStarted());
3336

3437
WinRT.ComWrappersSupport.InitializeComWrappers();
35-
bool isRedirect = DecideRedirection();
38+
var isRedirect = DecideRedirection();
3639
if (!isRedirect)
3740
{
3841
Microsoft.UI.Xaml.Application.Start((p) =>
@@ -48,17 +51,19 @@ private static int Main(string[] args)
4851

4952
private static bool DecideRedirection()
5053
{
51-
bool isRedirect = false;
52-
AppActivationArguments args = AppInstance.GetCurrent().GetActivatedEventArgs();
53-
AppInstance keyInstance = AppInstance.FindOrRegisterForKey("randomKey");
54+
var isRedirect = false;
55+
var args = AppInstance.GetCurrent().GetActivatedEventArgs();
56+
var keyInstance = AppInstance.FindOrRegisterForKey("randomKey");
5457

5558
if (keyInstance.IsCurrent)
5659
{
60+
PowerToysTelemetry.Log.WriteEvent(new ColdLaunch());
5761
keyInstance.Activated += OnActivated;
5862
}
5963
else
6064
{
6165
isRedirect = true;
66+
PowerToysTelemetry.Log.WriteEvent(new ReactivateInstance());
6267
keyInstance.RedirectActivationToAsync(args).AsTask().ConfigureAwait(false);
6368
}
6469

0 commit comments

Comments
 (0)