Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add telemetry for 'bad' experiences based on tag helpers #10906

Merged
merged 3 commits into from
Sep 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
Expand All @@ -25,6 +29,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 @@ -74,6 +79,8 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno

var razorDiagnostics = await GetRazorDiagnosticsAsync(documentContext, cancellationToken).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 @@ -160,4 +167,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();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: .Length

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));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this PII? Do have PIIProperty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It gets labeled in the server to not depend on client side labeling and ensure correct handling

}

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