Skip to content

Commit a906642

Browse files
author
Andrew Hall
authored
Add telemetry for 'bad' experiences based on tag helpers (#10906)
Helps add telemetry for cases like [AB#2255138](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2255138/)
2 parents f499c03 + 11603e6 commit a906642

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed

src/Razor/src/Microsoft.AspNetCore.Razor.LanguageServer/Diagnostics/DocumentPullDiagnosticsEndpoint.cs

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Collections.Immutable;
7+
using System.Linq;
68
using System.Threading;
79
using System.Threading.Tasks;
810
using Microsoft.AspNetCore.Razor.Language;
11+
using Microsoft.AspNetCore.Razor.Language.Components;
912
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
1013
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
1114
using Microsoft.AspNetCore.Razor.PooledObjects;
15+
using Microsoft.AspNetCore.Razor.ProjectSystem;
1216
using Microsoft.AspNetCore.Razor.Telemetry;
1317
using Microsoft.CodeAnalysis.Razor.Diagnostics;
1418
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
@@ -26,6 +30,7 @@ internal class DocumentPullDiagnosticsEndpoint : IRazorRequestHandler<VSInternal
2630
private readonly IClientConnection _clientConnection;
2731
private readonly RazorTranslateDiagnosticsService _translateDiagnosticsService;
2832
private readonly ITelemetryReporter? _telemetryReporter;
33+
private ImmutableDictionary<ProjectKey, int> _lastReporedProjectTagHelperCount = ImmutableDictionary<ProjectKey, int>.Empty;
2934

3035
public DocumentPullDiagnosticsEndpoint(
3136
LanguageServerFeatureOptions languageServerFeatureOptions,
@@ -77,6 +82,8 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno
7782

7883
var razorDiagnostics = await GetRazorDiagnosticsAsync(documentSnapshot).ConfigureAwait(false);
7984

85+
await ReportRZ10012TelemetryAsync(documentContext, razorDiagnostics, cancellationToken).ConfigureAwait(false);
86+
8087
var (csharpDiagnostics, htmlDiagnostics) = await GetHtmlCSharpDiagnosticsAsync(documentContext, correlationId, cancellationToken).ConfigureAwait(false);
8188

8289
var diagnosticCount =
@@ -163,4 +170,59 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno
163170

164171
return (delegatedResponse.CSharpDiagnostics, delegatedResponse.HtmlDiagnostics);
165172
}
173+
174+
/// <summary>
175+
/// Reports telemetry for RZ10012 "Found markup element with unexpected name" to help track down potential issues
176+
/// with taghelpers being discovered (or lack thereof)
177+
/// </summary>
178+
private async ValueTask ReportRZ10012TelemetryAsync(DocumentContext documentContext, VSInternalDiagnosticReport[]? razorDiagnostics, CancellationToken cancellationToken)
179+
{
180+
if (razorDiagnostics is null)
181+
{
182+
return;
183+
}
184+
185+
if (_telemetryReporter is null)
186+
{
187+
return;
188+
}
189+
190+
var relevantDiagnosticsCount = razorDiagnostics.Sum(CountDiagnostics);
191+
if (relevantDiagnosticsCount == 0)
192+
{
193+
return;
194+
}
195+
196+
var tagHelpers = await documentContext.Project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false);
197+
var tagHelperCount = tagHelpers.Count();
198+
var shouldReport = false;
199+
200+
ImmutableInterlocked.AddOrUpdate(
201+
ref _lastReporedProjectTagHelperCount,
202+
documentContext.Project.Key,
203+
(k) =>
204+
{
205+
shouldReport = true;
206+
return tagHelperCount;
207+
},
208+
(k, currentValue) =>
209+
{
210+
shouldReport = currentValue != tagHelperCount;
211+
return tagHelperCount;
212+
});
213+
214+
if (shouldReport)
215+
{
216+
_telemetryReporter.ReportEvent(
217+
"RZ10012",
218+
Severity.Low,
219+
new("tagHelpers", tagHelperCount),
220+
new("RZ10012errors", relevantDiagnosticsCount),
221+
new("project", documentContext.Project.Key.Id));
222+
}
223+
224+
static int CountDiagnostics(VSInternalDiagnosticReport report)
225+
=> report.Diagnostics?.Count(d => d.Code == ComponentDiagnosticFactory.UnexpectedMarkupElement.Id)
226+
?? 0;
227+
}
166228
}

0 commit comments

Comments
 (0)