Skip to content

Commit

Permalink
Add telemetry for 'bad' experiences based on tag helpers (#10906)
Browse files Browse the repository at this point in the history
Helps add telemetry for cases like
[AB#2255138](https://devdiv.visualstudio.com/DevDiv/_workitems/edit/2255138/)
  • Loading branch information
ryzngard authored Sep 23, 2024
2 parents f499c03 + 11603e6 commit a906642
Showing 1 changed file with 62 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@

using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.Language;
using Microsoft.AspNetCore.Razor.Language.Components;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.AspNetCore.Razor.LanguageServer.Hosting;
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.AspNetCore.Razor.ProjectSystem;
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.Diagnostics;
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
Expand All @@ -26,6 +30,7 @@ internal class DocumentPullDiagnosticsEndpoint : IRazorRequestHandler<VSInternal
private readonly IClientConnection _clientConnection;
private readonly RazorTranslateDiagnosticsService _translateDiagnosticsService;
private readonly ITelemetryReporter? _telemetryReporter;
private ImmutableDictionary<ProjectKey, int> _lastReporedProjectTagHelperCount = ImmutableDictionary<ProjectKey, int>.Empty;

public DocumentPullDiagnosticsEndpoint(
LanguageServerFeatureOptions languageServerFeatureOptions,
Expand Down Expand Up @@ -77,6 +82,8 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno

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

await ReportRZ10012TelemetryAsync(documentContext, razorDiagnostics, cancellationToken).ConfigureAwait(false);

var (csharpDiagnostics, htmlDiagnostics) = await GetHtmlCSharpDiagnosticsAsync(documentContext, correlationId, cancellationToken).ConfigureAwait(false);

var diagnosticCount =
Expand Down Expand Up @@ -163,4 +170,59 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno

return (delegatedResponse.CSharpDiagnostics, delegatedResponse.HtmlDiagnostics);
}

/// <summary>
/// Reports telemetry for RZ10012 "Found markup element with unexpected name" to help track down potential issues
/// with taghelpers being discovered (or lack thereof)
/// </summary>
private async ValueTask ReportRZ10012TelemetryAsync(DocumentContext documentContext, VSInternalDiagnosticReport[]? razorDiagnostics, CancellationToken cancellationToken)
{
if (razorDiagnostics is null)
{
return;
}

if (_telemetryReporter is null)
{
return;
}

var relevantDiagnosticsCount = razorDiagnostics.Sum(CountDiagnostics);
if (relevantDiagnosticsCount == 0)
{
return;
}

var tagHelpers = await documentContext.Project.GetTagHelpersAsync(cancellationToken).ConfigureAwait(false);
var tagHelperCount = tagHelpers.Count();
var shouldReport = false;

ImmutableInterlocked.AddOrUpdate(
ref _lastReporedProjectTagHelperCount,
documentContext.Project.Key,
(k) =>
{
shouldReport = true;
return tagHelperCount;
},
(k, currentValue) =>
{
shouldReport = currentValue != tagHelperCount;
return tagHelperCount;
});

if (shouldReport)
{
_telemetryReporter.ReportEvent(
"RZ10012",
Severity.Low,
new("tagHelpers", tagHelperCount),
new("RZ10012errors", relevantDiagnosticsCount),
new("project", documentContext.Project.Key.Id));
}

static int CountDiagnostics(VSInternalDiagnosticReport report)
=> report.Diagnostics?.Count(d => d.Code == ComponentDiagnosticFactory.UnexpectedMarkupElement.Id)
?? 0;
}
}

0 comments on commit a906642

Please sign in to comment.