Skip to content

Commit 10a34cc

Browse files
authored
Allow the Razor extension to report telemetry (and initialize) (#79254)
2 parents fa44857 + 8a098a4 commit 10a34cc

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Composition;
6+
using Microsoft.CodeAnalysis.Contracts.Telemetry;
7+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
8+
using Microsoft.CodeAnalysis.Host.Mef;
9+
using Microsoft.CodeAnalysis.LanguageServer.Handler;
10+
using Roslyn.LanguageServer.Protocol;
11+
12+
namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.Razor;
13+
14+
[Shared]
15+
[ExportRazorStatelessLspService(typeof(RazorInitializer))]
16+
[method: ImportingConstructor]
17+
[method: Obsolete(MefConstruction.ImportingConstructorMessage, error: true)]
18+
internal sealed class RazorInitializer(Lazy<LanguageServerWorkspaceFactory> workspaceFactory, [Import(AllowDefault = true)] ITelemetryReporter? telemetryReporter) : ILspService, IOnInitialized
19+
{
20+
public Task OnInitializedAsync(ClientCapabilities clientCapabilities, RequestContext context, CancellationToken cancellationToken)
21+
{
22+
var razorInitializerService = context.GetService<AbstractRazorInitializer>();
23+
if (razorInitializerService is null)
24+
{
25+
// No initializer service registered, nothing to do.
26+
return Task.CompletedTask;
27+
}
28+
29+
razorInitializerService.Initialize(workspaceFactory.Value.HostWorkspace);
30+
31+
var razorTelemetryReporter = context.GetService<RazorTelemetryReporter>();
32+
if (telemetryReporter is not null && razorTelemetryReporter is not null)
33+
{
34+
razorTelemetryReporter.Initialize(new TelemetryReporterWrapper(telemetryReporter));
35+
}
36+
37+
return Task.CompletedTask;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using Microsoft.CodeAnalysis.Contracts.Telemetry;
6+
using Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
7+
8+
namespace Microsoft.CodeAnalysis.LanguageServer.HostWorkspace.Razor;
9+
10+
internal class TelemetryReporterWrapper(ITelemetryReporter telemetryReporter) : ILanguageServerTelemetryReporterWrapper
11+
{
12+
public void ReportEvent(string name, List<KeyValuePair<string, object?>> properties)
13+
{
14+
telemetryReporter.Log(name, properties);
15+
}
16+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
6+
7+
internal abstract class AbstractRazorInitializer : AbstractRazorLspService
8+
{
9+
internal abstract void Initialize(Workspace workspace);
10+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
8+
9+
internal interface ILanguageServerTelemetryReporterWrapper
10+
{
11+
void ReportEvent(string name, List<KeyValuePair<string, object?>> properties);
12+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
7+
namespace Microsoft.CodeAnalysis.ExternalAccess.Razor.Features;
8+
9+
internal abstract class RazorTelemetryReporter : AbstractRazorLspService
10+
{
11+
private ILanguageServerTelemetryReporterWrapper? _wrapper;
12+
13+
internal void Initialize(ILanguageServerTelemetryReporterWrapper wrapper)
14+
{
15+
_wrapper = wrapper;
16+
}
17+
18+
public void ReportEvent(string name, List<KeyValuePair<string, object?>> properties)
19+
{
20+
_wrapper?.ReportEvent(name, properties);
21+
}
22+
}

0 commit comments

Comments
 (0)