Skip to content

Commit

Permalink
Reduce the amount of telemetry emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-allen-msft committed Oct 25, 2024
1 parent 45b65b3 commit cfad35a
Show file tree
Hide file tree
Showing 19 changed files with 92 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.CodeActions;
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.CodeActions;
Expand Down Expand Up @@ -57,7 +58,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSCodeActionParams reque
}

var correlationId = Guid.NewGuid();
using var __ = _telemetryReporter.TrackLspRequest(LspEndpointName, LanguageServerConstants.RazorLanguageServerName, correlationId);
using var __ = _telemetryReporter.TrackLspRequest(LspEndpointName, LanguageServerConstants.RazorLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.CodeActionRazorTelemetryThresholdMS), correlationId);
cancellationToken.ThrowIfCancellationRequested();

return await _codeActionsService.GetCodeActionsAsync(request, documentContext, _supportsCodeActionResolve, correlationId, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Razor.LanguageServer.EndpointContracts;
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;

Expand Down Expand Up @@ -77,7 +78,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(CompletionParams request
}

var correlationId = Guid.NewGuid();
using var _ = _telemetryReporter?.TrackLspRequest(Methods.TextDocumentCompletionName, LanguageServerConstants.RazorLanguageServerName, correlationId);
using var _ = _telemetryReporter?.TrackLspRequest(Methods.TextDocumentCompletionName, LanguageServerConstants.RazorLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.CompletionRazorTelemetryThresholdMS), correlationId);
var completionList = await _completionListProvider.GetCompletionListAsync(
hostDocumentIndex,
completionContext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Diagnostics;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Diagnostics;
Expand All @@ -30,7 +31,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;
private ImmutableDictionary<ProjectKey, int> _lastReportedProjectTagHelperCount = ImmutableDictionary<ProjectKey, int>.Empty;

public DocumentPullDiagnosticsEndpoint(
LanguageServerFeatureOptions languageServerFeatureOptions,
Expand Down Expand Up @@ -71,7 +72,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(VSInternalDocumentDiagno
}

var correlationId = Guid.NewGuid();
using var __ = _telemetryReporter?.TrackLspRequest(VSInternalMethods.DocumentPullDiagnosticName, LanguageServerConstants.RazorLanguageServerName, correlationId);
using var __ = _telemetryReporter?.TrackLspRequest(VSInternalMethods.DocumentPullDiagnosticName, LanguageServerConstants.RazorLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.DiagnosticsRazorTelemetryThresholdMS), correlationId);
var documentContext = context.DocumentContext;
if (documentContext is null)
{
Expand Down Expand Up @@ -202,7 +203,7 @@ private async ValueTask ReportRZ10012TelemetryAsync(DocumentContext documentCont
var shouldReport = false;

ImmutableInterlocked.AddOrUpdate(
ref _lastReporedProjectTagHelperCount,
ref _lastReportedProjectTagHelperCount,
documentContext.Project.Key,
(k) =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Location = Microsoft.VisualStudio.LanguageServer.Protocol.Location;
Expand Down Expand Up @@ -70,7 +71,7 @@ public void ApplyCapabilities(VSInternalServerCapabilities serverCapabilities, V
}

var mapCodeCorrelationId = mapperParams.MapCodeCorrelationId ?? Guid.NewGuid();
using var ts = _telemetryReporter.TrackLspRequest(VSInternalMethods.WorkspaceMapCodeName, LanguageServerConstants.RazorLanguageServerName, mapCodeCorrelationId);
using var ts = _telemetryReporter.TrackLspRequest(VSInternalMethods.WorkspaceMapCodeName, LanguageServerConstants.RazorLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.MapCodeRazorTelemetryThresholdMS), mapCodeCorrelationId);

return await HandleMappingsAsync(mapperParams.Mappings, mapCodeCorrelationId, cancellationToken).ConfigureAwait(false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.Telemetry;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.Protocol;

namespace Microsoft.AspNetCore.Razor.LanguageServer.Semantic;
Expand Down Expand Up @@ -49,7 +50,7 @@ public TextDocumentIdentifier GetTextDocumentIdentifier(SemanticTokensRangeParam
var colorBackground = _razorLSPOptionsMonitor.CurrentValue.ColorBackground;

var correlationId = Guid.NewGuid();
using var _ = _telemetryReporter?.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, LanguageServerConstants.RazorLanguageServerName, correlationId);
using var _ = _telemetryReporter?.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, LanguageServerConstants.RazorLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.SemanticTokensRazorTelemetryThresholdMS), correlationId);

var data = await _semanticTokensInfoService.GetSemanticTokensAsync(documentContext, request.Range.ToLinePositionSpan(), colorBackground, correlationId, cancellationToken).ConfigureAwait(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ internal interface ITelemetryReporter
TelemetryScope BeginBlock(string name, Severity severity, Property property);
TelemetryScope BeginBlock(string name, Severity severity, Property property1, Property property2);
TelemetryScope BeginBlock(string name, Severity severity, Property property1, Property property2, Property property3);
TelemetryScope BeginBlock(string name, Severity severity, params Property[] properties);
TelemetryScope BeginBlock(string name, Severity severity, TimeSpan minTimeToReport, params Property[] properties);

TelemetryScope TrackLspRequest(string lspMethodName, string lspServerName, Guid correlationId);
TelemetryScope TrackLspRequest(string lspMethodName, string lspServerName, TimeSpan minTimeToReport, Guid correlationId);

void ReportEvent(string name, Severity severity);
void ReportEvent(string name, Severity severity, Property property);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public TelemetryScope BeginBlock(string name, Severity severity, Property proper
public TelemetryScope BeginBlock(string name, Severity severity, Property property1, Property property2, Property property3)
=> TelemetryScope.Null;

public TelemetryScope BeginBlock(string name, Severity severity, params Property[] properties)
public TelemetryScope BeginBlock(string name, Severity severity, TimeSpan minTimeToReport, params Property[] properties)
=> TelemetryScope.Null;

public void ReportEvent(string name, Severity severity)
Expand All @@ -52,7 +52,7 @@ public void ReportFault(Exception exception, string? message, params object?[] @
{
}

public TelemetryScope TrackLspRequest(string lspMethodName, string lspServerName, Guid correlationId)
public TelemetryScope TrackLspRequest(string lspMethodName, string lspServerName, TimeSpan minTimeToReport, Guid correlationId)
=> TelemetryScope.Null;

public void ReportRequestTiming(string name, string? language, TimeSpan queuedDuration, TimeSpan requestDuration, TelemetryResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ internal sealed class TelemetryScope : IDisposable
private readonly Severity _severity;
private readonly Property[] _properties;
private readonly Stopwatch _stopwatch;
private readonly TimeSpan _minTimeToReport;
private bool _disposed;

private TelemetryScope()
Expand All @@ -32,6 +33,7 @@ private TelemetryScope()
private TelemetryScope(
ITelemetryReporter reporter,
string name,
TimeSpan minTimeToReport,
Severity severity,
Property[] properties)
{
Expand All @@ -45,6 +47,7 @@ private TelemetryScope(
_properties = properties;

_stopwatch = StopwatchPool.Default.Get();
_minTimeToReport = minTimeToReport;
_stopwatch.Restart();
}

Expand All @@ -59,10 +62,14 @@ public void Dispose()

_stopwatch.Stop();

// We know that we were created with an array of at least length one.
_properties[^1] = new("eventscope.ellapsedms", _stopwatch.ElapsedMilliseconds);
var elapsed = _stopwatch.Elapsed;
if (elapsed >= _minTimeToReport)
{
// We know that we were created with an array of at least length one.
_properties[^1] = new("eventscope.ellapsedms", _stopwatch.ElapsedMilliseconds);

_reporter.ReportEvent(_name, _severity, _properties);
_reporter.ReportEvent(_name, _severity, _properties);
}

StopwatchPool.Default.Return(_stopwatch);
}
Expand All @@ -71,15 +78,15 @@ public static TelemetryScope Create(ITelemetryReporter reporter, string name, Se
{
var array = new Property[1];

return new(reporter, name, severity, array);
return new(reporter, name, TimeSpan.Zero, severity, array);
}

public static TelemetryScope Create(ITelemetryReporter reporter, string name, Severity severity, Property property)
{
var array = new Property[2];
array[0] = property;

return new(reporter, name, severity, array);
return new(reporter, name, TimeSpan.Zero, severity, array);
}

public static TelemetryScope Create(ITelemetryReporter reporter, string name, Severity severity, Property property1, Property property2)
Expand All @@ -88,7 +95,7 @@ public static TelemetryScope Create(ITelemetryReporter reporter, string name, Se
array[0] = property1;
array[1] = property2;

return new(reporter, name, severity, array);
return new(reporter, name, TimeSpan.Zero, severity, array);
}

public static TelemetryScope Create(ITelemetryReporter reporter, string name, Severity severity, Property property1, Property property2, Property property3)
Expand All @@ -98,15 +105,15 @@ public static TelemetryScope Create(ITelemetryReporter reporter, string name, Se
array[1] = property2;
array[2] = property3;

return new(reporter, name, severity, array);
return new(reporter, name, TimeSpan.Zero, severity, array);
}

public static TelemetryScope Create(ITelemetryReporter reporter, string name, Severity severity, Property[] properties)
public static TelemetryScope Create(ITelemetryReporter reporter, string name, Severity severity, TimeSpan minTimeToReport, Property[] properties)
{
var array = new Property[properties.Length + 1];

Array.Copy(properties, array, properties.Length);

return new(reporter, name, severity, array);
return new(reporter, name, minTimeToReport, severity, array);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

namespace Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;

/// <summary>
/// A set of constants used to reduce the telemetry emitted to the set that help us understand
/// which LSP is taking the most time in the case that the overall call is lengthy.
/// </summary>
internal class TelemetryThresholds
{
internal const int CodeActionRazorTelemetryThresholdMS = 2000;
internal const int CodeActionSubLSPTelemetryThresholdMS = 1000;

internal const int CompletionRazorTelemetryThresholdMS = 4000;
internal const int CompletionSubLSPTelemetryThresholdMS = 2000;

internal const int DiagnosticsRazorTelemetryThresholdMS = 4000;
internal const int DiagnosticsSubLSPTelemetryThresholdMS = 2000;

internal const int MapCodeRazorTelemetryThresholdMS = 2000;
internal const int MapCodeSubLSPTelemetryThresholdMS = 1000;

internal const int SemanticTokensRazorTelemetryThresholdMS = 2000;
internal const int SemanticTokensSubLSPTelemetryThresholdMS = 1000;
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using Microsoft.CodeAnalysis.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.CodeAnalysis.Razor.Workspaces;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.CodeAnalysis.Remote.Razor.ProjectSystem;
using Microsoft.CodeAnalysis.Text;

Expand All @@ -26,7 +27,10 @@ internal class RemoteCSharpSemanticTokensProvider(IFilePathService filePathServi

public async Task<int[]?> GetCSharpSemanticTokensResponseAsync(DocumentContext documentContext, ImmutableArray<LinePositionSpan> csharpRanges, Guid correlationId, CancellationToken cancellationToken)
{
using var _ = _telemetryReporter.TrackLspRequest(nameof(SemanticTokensRange.GetSemanticTokensAsync), Constants.ExternalAccessServerName, correlationId);
using var _ = _telemetryReporter.TrackLspRequest(nameof(SemanticTokensRange.GetSemanticTokensAsync),
Constants.ExternalAccessServerName,
TimeSpan.FromMilliseconds(TelemetryThresholds.SemanticTokensRazorTelemetryThresholdMS),
correlationId);

// We have a razor document, lets find the generated C# document
Debug.Assert(documentContext is RemoteDocumentContext, "This method only works on document snapshots created in the OOP process");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Cohost;
using Microsoft.CodeAnalysis.Razor.Remote;
using Microsoft.CodeAnalysis.Razor.SemanticTokens;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.CodeAnalysis.Text;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Razor.Settings;
Expand Down Expand Up @@ -79,7 +80,7 @@ public ImmutableArray<Registration> GetRegistrations(VSInternalClientCapabilitie
var colorBackground = _clientSettingsManager.GetClientSettings().AdvancedSettings.ColorBackground;

var correlationId = Guid.NewGuid();
using var _ = _telemetryReporter.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, RazorLSPConstants.CohostLanguageServerName, correlationId);
using var _ = _telemetryReporter.TrackLspRequest(Methods.TextDocumentSemanticTokensRangeName, RazorLSPConstants.CohostLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.SemanticTokensRazorTelemetryThresholdMS), correlationId);

var tokens = await _remoteServiceInvoker.TryInvokeAsync<IRemoteSemanticTokensService, int[]?>(
razorDocument.Project.Solution,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.CodeActions;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Threading;
Expand Down Expand Up @@ -56,7 +57,7 @@ internal partial class RazorCustomMessageTarget

var textBuffer = virtualDocumentSnapshot.Snapshot.TextBuffer;
var lspMethodName = Methods.TextDocumentCodeActionName;
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, codeActionParams.CorrelationId);
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.CodeActionSubLSPTelemetryThresholdMS), codeActionParams.CorrelationId);
var requests = _requestInvoker.ReinvokeRequestOnMultipleServersAsync<VSCodeActionParams, IReadOnlyList<VSInternalCodeAction>>(
textBuffer,
lspMethodName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Microsoft.AspNetCore.Razor.PooledObjects;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Completion;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using Microsoft.VisualStudio.Razor.Snippets;
Expand Down Expand Up @@ -121,7 +122,7 @@ internal partial class RazorCustomMessageTarget
var textBuffer = virtualDocumentSnapshot.Snapshot.TextBuffer;
var lspMethodName = Methods.TextDocumentCompletion.Name;
ReinvocationResponse<VSInternalCompletionList?>? response;
using (_telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, request.CorrelationId))
using (_telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.CompletionSubLSPTelemetryThresholdMS), request.CorrelationId))
{
response = await _requestInvoker.ReinvokeRequestOnServerAsync<CompletionParams, VSInternalCompletionList?>(
textBuffer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.CodeAnalysis.Razor.Logging;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Protocol.Diagnostics;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.ContainedLanguage;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using StreamJsonRpc;
Expand Down Expand Up @@ -78,7 +79,7 @@ internal partial class RazorCustomMessageTarget
};

var lspMethodName = VSInternalMethods.DocumentPullDiagnosticName;
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, delegatedLanguageServerName, correlationId);
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, delegatedLanguageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.DiagnosticsSubLSPTelemetryThresholdMS), correlationId);
var response = await _requestInvoker.ReinvokeRequestOnServerAsync<VSInternalDocumentDiagnosticsParams, VSInternalDiagnosticReport[]?>(
virtualDocument.Snapshot.TextBuffer,
lspMethodName,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.Razor.Protocol;
using Microsoft.CodeAnalysis.Razor.Workspaces.Telemetry;
using Microsoft.VisualStudio.LanguageServer.Protocol;
using StreamJsonRpc;

Expand Down Expand Up @@ -39,7 +41,7 @@ internal partial class RazorCustomMessageTarget
var textBuffer = delegationDetails.Value.TextBuffer;
var lspMethodName = VSInternalMethods.WorkspaceMapCodeName;
var languageServerName = delegationDetails.Value.LanguageServerName;
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, request.MapCodeCorrelationId);
using var _ = _telemetryReporter.TrackLspRequest(lspMethodName, languageServerName, TimeSpan.FromMilliseconds(TelemetryThresholds.MapCodeSubLSPTelemetryThresholdMS), request.MapCodeCorrelationId);

var response = await _requestInvoker.ReinvokeRequestOnServerAsync<VSInternalMapCodeParams, WorkspaceEdit?>(
textBuffer,
Expand Down
Loading

0 comments on commit cfad35a

Please sign in to comment.