Skip to content
This repository was archived by the owner on Jan 21, 2026. It is now read-only.

Commit 6c712ff

Browse files
authored
Add telemetry for HTTP commands (#430)
1 parent e53fe35 commit 6c712ff

20 files changed

+162
-54
lines changed

TELEMETRY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The telemetry feature collects the following data:
4040
| >=5.0 | For the `pref` command, whether a `get` or `set` was issued and which preference was accessed. If not a well-known preference, the name is hashed. The value is not collected. |
4141
| >=5.0 | For the `set header` command, the header name being set. If not a well-known header, the name is hashed. The value is not collected. |
4242
| >=5.0 | For the `connect` command, whether or not a specific special-case for `dotnet new webapi` was used and, whether or not it was bypassed via preference. |
43+
| >=5.0 | For all HTTP commands (e.g. GET, POST, PUT, etc), whether or not each of the options was specified. The values of the options are not collected. |
4344

4445
## See also
4546

src/Microsoft.HttpRepl/Commands/BaseHttpCommand.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
using Microsoft.HttpRepl.Preferences;
1818
using Microsoft.HttpRepl.Resources;
1919
using Microsoft.HttpRepl.Suggestions;
20+
using Microsoft.HttpRepl.Telemetry;
21+
using Microsoft.HttpRepl.Telemetry.Events;
2022
using Microsoft.Repl;
2123
using Microsoft.Repl.Commanding;
2224
using Microsoft.Repl.ConsoleHandling;
@@ -49,17 +51,19 @@ public abstract class BaseHttpCommand : CommandWithStructuredInputBase<HttpState
4951

5052
private readonly IFileSystem _fileSystem;
5153
private readonly IPreferences _preferences;
54+
private readonly ITelemetry _telemetry;
5255

5356
public override string Name => Verb;
5457

5558
protected abstract string Verb { get; }
5659

5760
protected abstract bool RequiresBody { get; }
5861

59-
protected BaseHttpCommand(IFileSystem fileSystem, IPreferences preferences)
62+
protected BaseHttpCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry)
6063
{
6164
_fileSystem = fileSystem;
6265
_preferences = preferences;
66+
_telemetry = telemetry;
6367
}
6468

6569
public override CommandInputSpecification InputSpec
@@ -101,10 +105,12 @@ protected override async Task ExecuteAsync(IShellState shellState, HttpState pro
101105

102106
if (programState.BaseAddress == null && (commandInput.Arguments.Count == 0 || !Uri.TryCreate(commandInput.Arguments[0].Text, UriKind.Absolute, out _)))
103107
{
104-
shellState.ConsoleManager.Error.WriteLine(Resources.Strings.Error_NoBasePath.SetColor(programState.ErrorColor));
108+
shellState.ConsoleManager.Error.WriteLine(Strings.Error_NoBasePath.SetColor(programState.ErrorColor));
105109
return;
106110
}
107111

112+
SendTelemetry(commandInput);
113+
108114
if (programState.SwaggerEndpoint != null)
109115
{
110116
await CreateDirectoryStructureForSwaggerEndpointAsync(shellState, programState, cancellationToken).ConfigureAwait(false);
@@ -750,5 +756,23 @@ private static string GetExampleBody(string path, ref string contentType, string
750756
IDirectoryStructure structure = httpState.Structure?.TraverseTo(rootRelativePath);
751757
return structure?.RequestInfo?.GetRequestBodyForContentType(ref contentType, method);
752758
}
759+
760+
private void SendTelemetry(DefaultCommandInput<ICoreParseResult> commandInput)
761+
{
762+
HttpCommandEvent httpCommandEvent = new HttpCommandEvent(
763+
method: Verb.ToUpperInvariant(),
764+
isPathSpecified: commandInput.Arguments.Count > 0,
765+
isHeaderSpecified: commandInput.Options[HeaderOption].Any(),
766+
isResponseHeadersFileSpecified: commandInput.Options[ResponseHeadersFileOption].Any(),
767+
isResponseBodyFileSpecified: commandInput.Options[ResponseBodyFileOption].Any(),
768+
isNoFormattingSpecified: commandInput.Options[NoFormattingOption].Any(),
769+
isStreamingSpecified: commandInput.Options[StreamingOption].Any(),
770+
isNoBodySpecified: RequiresBody && commandInput.Options[NoBodyOption].Any(),
771+
isRequestBodyFileSpecified: RequiresBody && commandInput.Options[BodyFileOption].Any(),
772+
isRequestBodyContentSpecified: RequiresBody && commandInput.Options[BodyContentOption].Any()
773+
);
774+
775+
_telemetry.TrackEvent(httpCommandEvent);
776+
}
753777
}
754778
}

src/Microsoft.HttpRepl/Commands/DeleteCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class DeleteCommand : BaseHttpCommand
1011
{
11-
public DeleteCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public DeleteCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "delete";
1415

src/Microsoft.HttpRepl/Commands/GetCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class GetCommand : BaseHttpCommand
1011
{
11-
public GetCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public GetCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "get";
1415

src/Microsoft.HttpRepl/Commands/HeadCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class HeadCommand : BaseHttpCommand
1011
{
11-
public HeadCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public HeadCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "head";
1415

src/Microsoft.HttpRepl/Commands/OptionsCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class OptionsCommand : BaseHttpCommand
1011
{
11-
public OptionsCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public OptionsCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "options";
1415

src/Microsoft.HttpRepl/Commands/PatchCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class PatchCommand : BaseHttpCommand
1011
{
11-
public PatchCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public PatchCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "patch";
1415

src/Microsoft.HttpRepl/Commands/PostCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class PostCommand : BaseHttpCommand
1011
{
11-
public PostCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public PostCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "post";
1415

src/Microsoft.HttpRepl/Commands/PutCommand.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@
33

44
using Microsoft.HttpRepl.FileSystem;
55
using Microsoft.HttpRepl.Preferences;
6+
using Microsoft.HttpRepl.Telemetry;
67

78
namespace Microsoft.HttpRepl.Commands
89
{
910
public class PutCommand : BaseHttpCommand
1011
{
11-
public PutCommand(IFileSystem fileSystem, IPreferences preferences) : base(fileSystem, preferences) { }
12+
public PutCommand(IFileSystem fileSystem, IPreferences preferences, ITelemetry telemetry) : base(fileSystem, preferences, telemetry) { }
1213

1314
protected override string Verb => "put";
1415

src/Microsoft.HttpRepl/Program.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,18 +110,18 @@ private static void ComposeDependencies(ref IConsoleManager consoleManager, ref
110110
dispatcher.AddCommandWithTelemetry(telemetry, new ChangeDirectoryCommand());
111111
dispatcher.AddCommandWithTelemetry(telemetry, new ClearCommand());
112112
dispatcher.AddCommandWithTelemetry(telemetry, new ConnectCommand(preferences, telemetry));
113-
dispatcher.AddCommandWithTelemetry(telemetry, new DeleteCommand(fileSystem, preferences));
113+
dispatcher.AddCommandWithTelemetry(telemetry, new DeleteCommand(fileSystem, preferences, telemetry));
114114
dispatcher.AddCommandWithTelemetry(telemetry, new EchoCommand());
115115
dispatcher.AddCommandWithTelemetry(telemetry, new ExitCommand());
116-
dispatcher.AddCommandWithTelemetry(telemetry, new HeadCommand(fileSystem, preferences));
116+
dispatcher.AddCommandWithTelemetry(telemetry, new HeadCommand(fileSystem, preferences, telemetry));
117117
dispatcher.AddCommandWithTelemetry(telemetry, new HelpCommand());
118-
dispatcher.AddCommandWithTelemetry(telemetry, new GetCommand(fileSystem, preferences));
118+
dispatcher.AddCommandWithTelemetry(telemetry, new GetCommand(fileSystem, preferences, telemetry));
119119
dispatcher.AddCommandWithTelemetry(telemetry, new ListCommand(preferences));
120-
dispatcher.AddCommandWithTelemetry(telemetry, new OptionsCommand(fileSystem, preferences));
121-
dispatcher.AddCommandWithTelemetry(telemetry, new PatchCommand(fileSystem, preferences));
120+
dispatcher.AddCommandWithTelemetry(telemetry, new OptionsCommand(fileSystem, preferences, telemetry));
121+
dispatcher.AddCommandWithTelemetry(telemetry, new PatchCommand(fileSystem, preferences, telemetry));
122122
dispatcher.AddCommandWithTelemetry(telemetry, new PrefCommand(preferences, telemetry));
123-
dispatcher.AddCommandWithTelemetry(telemetry, new PostCommand(fileSystem, preferences));
124-
dispatcher.AddCommandWithTelemetry(telemetry, new PutCommand(fileSystem, preferences));
123+
dispatcher.AddCommandWithTelemetry(telemetry, new PostCommand(fileSystem, preferences, telemetry));
124+
dispatcher.AddCommandWithTelemetry(telemetry, new PutCommand(fileSystem, preferences, telemetry));
125125
dispatcher.AddCommandWithTelemetry(telemetry, new RunCommand(fileSystem));
126126
dispatcher.AddCommandWithTelemetry(telemetry, new SetHeaderCommand(telemetry));
127127
dispatcher.AddCommandWithTelemetry(telemetry, new UICommand(new UriLauncher(), preferences));

0 commit comments

Comments
 (0)