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 endpoint, where telemetry request will be processed and stored to the file #1071

Merged
merged 10 commits into from
Oct 3, 2023
1 change: 0 additions & 1 deletion tools/decoder/Decoder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
using Newtonsoft.Json.Linq;
using CsProtocol;
using System.Linq;
using Fiddler;
Copy link
Contributor

Choose a reason for hiding this comment

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

My minor concern is we still need to make sure it works with Fiddler too. I honestly don't remember why we need this using, but if you can manually check that the Fiddler Inspector compiles Okay, that'd be great.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@maxgolov , I will check. By Fiddler Inspector do you mean project OneDSInspector?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yup! Thank you so much. If we can't remove that, maybe we can use #if or something like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it was added by actually: #1070


namespace CommonSchema
{
Expand Down
4 changes: 4 additions & 0 deletions tools/server/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ public static void Main(string[] args)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables();
var fileName = configurationBuilder.Build().GetSection("FileNameToStoreTelemetryData")?.Value;
// clean the file before every launch of the server
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName)) File.Delete(fileName);

})
.ConfigureLogging(configureLogging: (hostingContext, logging) =>
{
Expand Down
75 changes: 59 additions & 16 deletions tools/server/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.IO;
using System.Linq;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Configuration;

namespace CommonSchema
{
Expand All @@ -21,6 +22,13 @@ public class Startup
{
private int seq = 0;

public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(/* IServiceCollection services */)
Expand Down Expand Up @@ -54,24 +62,38 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
try
{
string path = context.Request.Path.Value;
if (path.StartsWith("/OneCollector/"))
if (path.StartsWith("/OneCollectorWriteToFile"))
{
int length = Int32.Parse(context.Request.Headers["Content-Length"]);
BinaryReader reader = new BinaryReader(context.Request.Body);

// Read body fully before decoding it
byte[] buffer = reader.ReadBytes(length);

Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (KeyValuePair<string, StringValues> entry in context.Request.Headers)
string result = DecodeTelemetryRequest(context, decoderLogger);
var fileName = Configuration.GetSection("FileNameToStoreTelemetryData")?.Value;
if (!string.IsNullOrEmpty(fileName))
{
// Our decoder only needs to know the 1st header value, do not need a multimap
headers[entry.Key] = entry.Value.ElementAt(0);
};
Decoder decoder = new Decoder(headers, buffer);
// Supply the logger
decoder.Logger = decoderLogger;
string result = decoder.ToJson(false, true, 2);
if (File.Exists(fileName))
{
var formattedResult = result.Replace("[", "").Replace("]", "");
var currentContent = File.ReadAllText(fileName);
var updatedContent = string.Concat(currentContent.Replace("]", ","), formattedResult, "]");
File.WriteAllText(fileName, updatedContent);
}
else
{
File.AppendAllText(fileName, result);
}
}
else
{
requestLogger.LogError("Parameter FileNameToStoreTelemetryData from appsettings.json, where data should be stored is not specified. As a result telemetry data won't be saved to file, but will be visible in the console");
}

// Echo the body converted to JSON array
context.Response.StatusCode = 200;
requestLogger.LogInformation(result);
await context.Response.WriteAsync(result);
}
else
if (path.StartsWith("/OneCollector/"))
{
string result = DecodeTelemetryRequest(context, decoderLogger);

// Echo the body converted to JSON array
context.Response.StatusCode = 200;
Expand Down Expand Up @@ -126,6 +148,27 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
});

}

private static string DecodeTelemetryRequest(HttpContext context, ILogger decoderLogger)
{
int length = Int32.Parse(context.Request.Headers["Content-Length"]);
BinaryReader reader = new BinaryReader(context.Request.Body);

// Read body fully before decoding it
byte[] buffer = reader.ReadBytes(length);

Dictionary<string, string> headers = new Dictionary<string, string>();
foreach (KeyValuePair<string, StringValues> entry in context.Request.Headers)
{
// Our decoder only needs to know the 1st header value, do not need a multimap
headers[entry.Key] = entry.Value.ElementAt(0);
};
Decoder decoder = new Decoder(headers, buffer);
// Supply the logger
decoder.Logger = decoderLogger;
string result = decoder.ToJson(false, true, 2);
return result;
}
}
}
}