From fed74835cc5188dcc6c4b7fce673afc97741ef09 Mon Sep 17 00:00:00 2001 From: Chidozie Ononiwu Date: Tue, 16 Jul 2024 15:35:29 -0700 Subject: [PATCH] Add Better logging --- .../Languages/CSharpLanguageService.cs | 10 +----- .../APIViewWeb/Languages/GoLanguageService.cs | 6 ++++ .../Languages/JavaLanguageService.cs | 5 +++ .../Languages/JavaScriptLanguageService.cs | 5 +++ .../APIViewWeb/Languages/LanguageProcessor.cs | 35 ++++++++++++++----- .../Languages/ProtocolLanguageService.cs | 3 +- .../Languages/PythonLanguageService.cs | 2 +- .../Languages/SwaggerLanguageService.cs | 4 ++- .../Languages/TypeSpecLanguageService.cs | 2 +- .../Languages/XmlLanguageService.cs | 6 ++++ 10 files changed, 56 insertions(+), 22 deletions(-) diff --git a/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs index 11da3e8b0cf6..7aa9d7745b12 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/CSharpLanguageService.cs @@ -5,17 +5,9 @@ using System.Collections.Generic; using System.IO; using System.Text.RegularExpressions; -using System.Threading; -using System.Threading.Tasks; using ApiView; -using NuGet.Common; -using NuGet.Packaging; -using NuGet.Protocol; -using NuGet.Protocol.Core.Types; -using NuGet.Versioning; using Microsoft.ApplicationInsights; using Microsoft.Extensions.Configuration; -using System.Linq; namespace APIViewWeb { @@ -28,7 +20,7 @@ public class CSharpLanguageService : LanguageProcessor public override string ProcessName => _csharpParserToolPath; public override string VersionString { get; } = "27"; - public CSharpLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) + public CSharpLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient) { _csharpParserToolPath = configuration["CSHARPPARSEREXECUTABLEPATH"]; } diff --git a/src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs index a00a439c4513..4b7ca985a544 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs @@ -8,6 +8,8 @@ using System.Threading.Tasks; using ApiView; using APIViewWeb.Helpers; +using Microsoft.ApplicationInsights; +using Microsoft.Extensions.Configuration; namespace APIViewWeb { @@ -18,6 +20,10 @@ public class GoLanguageService : LanguageProcessor public override string ProcessName { get; } = "apiviewgo"; public override string VersionString { get; } = "2"; + public GoLanguageService(TelemetryClient telemetryClient) : base(telemetryClient) + { + } + public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonFilePath) { return $"\"{originalName}\" \"{jsonFilePath}\""; diff --git a/src/dotnet/APIView/APIViewWeb/Languages/JavaLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/JavaLanguageService.cs index d4d6f0d7aeb6..e9065ad013a2 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/JavaLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/JavaLanguageService.cs @@ -2,6 +2,7 @@ // Licensed under the MIT License. using System.IO; +using Microsoft.ApplicationInsights; namespace APIViewWeb { @@ -12,6 +13,10 @@ public class JavaLanguageService : LanguageProcessor public override string ProcessName { get; } = "java"; public override string VersionString { get; } = "apiview-java-processor-1.31.0.jar"; + public JavaLanguageService(TelemetryClient telemetryClient) : base(telemetryClient) + { + } + public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath) { var jarPath = Path.Combine( diff --git a/src/dotnet/APIView/APIViewWeb/Languages/JavaScriptLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/JavaScriptLanguageService.cs index 863ede64d63f..64384abb4bf5 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/JavaScriptLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/JavaScriptLanguageService.cs @@ -3,6 +3,7 @@ using System; using System.IO; +using Microsoft.ApplicationInsights; namespace APIViewWeb { @@ -13,6 +14,10 @@ public class JavaScriptLanguageService : LanguageProcessor public override string ProcessName { get; } = "node"; public override string VersionString { get; } = "1.0.8"; + public JavaScriptLanguageService(TelemetryClient telemetryClient) : base(telemetryClient) + { + } + public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonFilePath) { var jsPath = Path.Combine( diff --git a/src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs b/src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs index 72f08bee6a82..ec5c55dcf7dd 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs @@ -1,15 +1,22 @@ using System; using System.Diagnostics; using System.IO; -using System.IO.Compression; using System.Threading.Tasks; using ApiView; using APIViewWeb.Helpers; +using Microsoft.ApplicationInsights; namespace APIViewWeb { public abstract class LanguageProcessor: LanguageService { + private readonly TelemetryClient _telemetryClient; + + public LanguageProcessor(TelemetryClient telemetryClient) + { + _telemetryClient = telemetryClient; + } + public abstract string ProcessName { get; } public abstract string VersionString { get; } public abstract string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath); @@ -42,6 +49,8 @@ public override async Task GetCodeFileAsync(string originalName, Strea { var arguments = GetProcessorArguments(originalName, tempDirectory, jsonFilePath); var processStartInfo = new ProcessStartInfo(ProcessName, arguments); + string processErrors = String.Empty; + processStartInfo.WorkingDirectory = tempDirectory; processStartInfo.RedirectStandardError = true; processStartInfo.RedirectStandardOutput = true; @@ -51,21 +60,29 @@ public override async Task GetCodeFileAsync(string originalName, Strea process.WaitForExit(); if (process.ExitCode != 0) { - throw new InvalidOperationException( - "Processor failed: " + Environment.NewLine + + processErrors = "Processor failed: " + Environment.NewLine + "stdout: " + Environment.NewLine + process.StandardOutput.ReadToEnd() + Environment.NewLine + "stderr: " + Environment.NewLine + - process.StandardError.ReadToEnd() + Environment.NewLine); + process.StandardError.ReadToEnd() + Environment.NewLine; + throw new InvalidOperationException(processErrors); } } - using (var codeFileStream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.None)) + if (File.Exists(jsonFilePath)) + { + using (var codeFileStream = new FileStream(jsonFilePath, FileMode.Open, FileAccess.Read, FileShare.None)) + { + CodeFile codeFile = await CodeFile.DeserializeAsync(stream: codeFileStream, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(this.Name)); + codeFile.VersionString = VersionString; + codeFile.Language = Name; + return codeFile; + } + } + else { - CodeFile codeFile = await CodeFile.DeserializeAsync(stream: codeFileStream, doTreeStyleParserDeserialization: LanguageServiceHelpers.UseTreeStyleParser(this.Name)); - codeFile.VersionString = VersionString; - codeFile.Language = Name; - return codeFile; + _telemetryClient.TrackTrace($"Processor failed to generate json file {jsonFilePath} with error {processErrors}"); + throw new InvalidOperationException($"Processor failed to generate json file {jsonFilePath}"); } } finally diff --git a/src/dotnet/APIView/APIViewWeb/Languages/ProtocolLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/ProtocolLanguageService.cs index 036594d3b7f8..42a7b901f4a3 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/ProtocolLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/ProtocolLanguageService.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.ApplicationInsights; using Microsoft.Extensions.Configuration; namespace APIViewWeb @@ -14,7 +15,7 @@ public class ProtocolLanguageService : LanguageProcessor private readonly string _protocolProcessor; public override string ProcessName => _protocolProcessor; - public ProtocolLanguageService(IConfiguration configuration) + public ProtocolLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient) { // protocolGen is located in python's scripts path e.g. /Scripts/protocolGen // Env variable PROTOCOLPARSERPATH is set to /Scripts/protocolGen where parser is located diff --git a/src/dotnet/APIView/APIViewWeb/Languages/PythonLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/PythonLanguageService.cs index 7520efe0f368..b3c9c358a179 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/PythonLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/PythonLanguageService.cs @@ -21,7 +21,7 @@ public class PythonLanguageService : LanguageProcessor public override string VersionString { get; } = "0.3.12"; public override string ProcessName => _pythonExecutablePath; - public PythonLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) + public PythonLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient) { _pythonExecutablePath = configuration["PYTHONEXECUTABLEPATH"] ?? "python"; _telemetryClient = telemetryClient; diff --git a/src/dotnet/APIView/APIViewWeb/Languages/SwaggerLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/SwaggerLanguageService.cs index 4ed438dc85d5..97b44b1b5da4 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/SwaggerLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/SwaggerLanguageService.cs @@ -6,6 +6,8 @@ using System.Threading.Tasks; using ApiView; using APIViewWeb.Helpers; +using Microsoft.ApplicationInsights; +using Microsoft.Extensions.Configuration; namespace APIViewWeb { @@ -19,7 +21,7 @@ public class SwaggerLanguageService : LanguageProcessor public override string ProcessName => throw new NotImplementedException(); - public SwaggerLanguageService() + public SwaggerLanguageService(TelemetryClient telemetryClient) : base(telemetryClient) { IsReviewGenByPipeline = true; } diff --git a/src/dotnet/APIView/APIViewWeb/Languages/TypeSpecLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/TypeSpecLanguageService.cs index f521ef9ebd59..0318850c309d 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/TypeSpecLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/TypeSpecLanguageService.cs @@ -24,7 +24,7 @@ public class TypeSpecLanguageService : LanguageProcessor public override string VersionString { get; } = "0"; public override string ProcessName => throw new NotImplementedException(); - public TypeSpecLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) + public TypeSpecLanguageService(IConfiguration configuration, TelemetryClient telemetryClient) : base(telemetryClient) { IsReviewGenByPipeline = true; _typeSpecSpecificPathPrefix = "/specification"; diff --git a/src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs b/src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs index 99763ef77b96..8e8dd52e1134 100644 --- a/src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs +++ b/src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs @@ -2,6 +2,8 @@ // Licensed under the MIT License. using System.IO; +using Microsoft.ApplicationInsights; +using Microsoft.Extensions.Configuration; namespace APIViewWeb { @@ -12,6 +14,10 @@ public class XmlLanguageService : LanguageProcessor public override string ProcessName { get; } = "java"; public override string VersionString { get; } = "apiview-java-processor-1.31.0.jar"; + public XmlLanguageService(TelemetryClient telemetryClient) : base(telemetryClient) + { + } + public override string GetProcessorArguments(string originalName, string tempDirectory, string jsonPath) { var jarPath = Path.Combine(