Skip to content

Commit

Permalink
Add Better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
chidozieononiwu committed Jul 16, 2024
1 parent 5e2bb10 commit fed7483
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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"];
}
Expand Down
6 changes: 6 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Languages/GoLanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using System.Threading.Tasks;
using ApiView;
using APIViewWeb.Helpers;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Configuration;

namespace APIViewWeb
{
Expand All @@ -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}\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License.

using System.IO;
using Microsoft.ApplicationInsights;

namespace APIViewWeb
{
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System;
using System.IO;
using Microsoft.ApplicationInsights;

namespace APIViewWeb
{
Expand All @@ -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(
Expand Down
35 changes: 26 additions & 9 deletions src/dotnet/APIView/APIViewWeb/Languages/LanguageProcessor.cs
Original file line number Diff line number Diff line change
@@ -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);
Expand Down Expand Up @@ -42,6 +49,8 @@ public override async Task<CodeFile> 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;
Expand All @@ -51,21 +60,29 @@ public override async Task<CodeFile> 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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. <Pythonhome>/Scripts/protocolGen
// Env variable PROTOCOLPARSERPATH is set to <pythonhome>/Scripts/protocolGen where parser is located
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
using System.Threading.Tasks;
using ApiView;
using APIViewWeb.Helpers;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Configuration;

namespace APIViewWeb
{
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
6 changes: 6 additions & 0 deletions src/dotnet/APIView/APIViewWeb/Languages/XmlLanguageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Licensed under the MIT License.

using System.IO;
using Microsoft.ApplicationInsights;
using Microsoft.Extensions.Configuration;

namespace APIViewWeb
{
Expand All @@ -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(
Expand Down

0 comments on commit fed7483

Please sign in to comment.