Skip to content

Commit 36bc9b2

Browse files
ms-iankudinovalalitbmaxgolov
authored
Add endpoint, where telemetry request will be processed and stored to the file (#1071)
* Add enpoint, where telemetry request will be processed and stored to the file * Remove unnesessery dependency * Add if directive otherwise build can failed * Trying to solve cocurrent usage problem of the file --------- Co-authored-by: Lalit Kumar Bhasin <[email protected]> Co-authored-by: Max Golovanov <[email protected]>
1 parent 7e71857 commit 36bc9b2

File tree

3 files changed

+69
-17
lines changed

3 files changed

+69
-17
lines changed

tools/decoder/Decoder.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
using Newtonsoft.Json.Linq;
2727
using CsProtocol;
2828
using System.Linq;
29-
using Fiddler;
3029

3130
namespace CommonSchema
3231
{
@@ -328,7 +327,9 @@ public List<string> FromBondToJSONList(bool outputCompactJson = true, bool flatE
328327
}
329328
catch (EndOfStreamException)
330329
{
330+
#if NETCOREAPP
331331
Logger.LogDebug("End of Binary Stream");
332+
#endif
332333
}
333334
catch (Exception ex)
334335
{

tools/server/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,10 @@ public static void Main(string[] args)
3939
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
4040
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true, reloadOnChange: true)
4141
.AddEnvironmentVariables();
42+
var fileName = configurationBuilder.Build().GetSection("FileNameToStoreTelemetryData")?.Value;
43+
// clean the file before every launch of the server
44+
if (!string.IsNullOrEmpty(fileName) && File.Exists(fileName)) File.Delete(fileName);
45+
4246
})
4347
.ConfigureLogging(configureLogging: (hostingContext, logging) =>
4448
{

tools/server/Startup.cs

Lines changed: 63 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using System.IO;
1313
using System.Linq;
1414
using Microsoft.Extensions.Hosting;
15+
using Microsoft.Extensions.Configuration;
1516

1617
namespace CommonSchema
1718
{
@@ -20,6 +21,14 @@ namespace Server
2021
public class Startup
2122
{
2223
private int seq = 0;
24+
private static object locker = new Object();
25+
26+
public Startup(IConfiguration configuration)
27+
{
28+
Configuration = configuration;
29+
}
30+
31+
public IConfiguration Configuration { get; }
2332

2433
// This method gets called by the runtime. Use this method to add services to the container.
2534
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
@@ -54,24 +63,41 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
5463
try
5564
{
5665
string path = context.Request.Path.Value;
57-
if (path.StartsWith("/OneCollector/"))
66+
if (path.StartsWith("/OneCollectorWriteToFile"))
5867
{
59-
int length = Int32.Parse(context.Request.Headers["Content-Length"]);
60-
BinaryReader reader = new BinaryReader(context.Request.Body);
61-
62-
// Read body fully before decoding it
63-
byte[] buffer = reader.ReadBytes(length);
64-
65-
Dictionary<string, string> headers = new Dictionary<string, string>();
66-
foreach (KeyValuePair<string, StringValues> entry in context.Request.Headers)
68+
string result = DecodeTelemetryRequest(context, decoderLogger);
69+
var fileName = Configuration.GetSection("FileNameToStoreTelemetryData")?.Value;
70+
if (!string.IsNullOrEmpty(fileName))
6771
{
68-
// Our decoder only needs to know the 1st header value, do not need a multimap
69-
headers[entry.Key] = entry.Value.ElementAt(0);
70-
};
71-
Decoder decoder = new Decoder(headers, buffer);
72-
// Supply the logger
73-
decoder.Logger = decoderLogger;
74-
string result = decoder.ToJson(false, true, 2);
72+
lock (locker)
73+
{
74+
if (File.Exists(fileName))
75+
{
76+
var formattedResult = result.Replace("[", "").Replace("]", "");
77+
var currentContent = File.ReadAllText(fileName);
78+
var updatedContent = string.Concat(currentContent.Replace("]", ","), formattedResult, "]");
79+
File.WriteAllText(fileName, updatedContent);
80+
}
81+
else
82+
{
83+
File.AppendAllText(fileName, result);
84+
}
85+
}
86+
}
87+
else
88+
{
89+
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");
90+
}
91+
92+
// Echo the body converted to JSON array
93+
context.Response.StatusCode = 200;
94+
requestLogger.LogInformation(result);
95+
await context.Response.WriteAsync(result);
96+
}
97+
else
98+
if (path.StartsWith("/OneCollector/"))
99+
{
100+
string result = DecodeTelemetryRequest(context, decoderLogger);
75101

76102
// Echo the body converted to JSON array
77103
context.Response.StatusCode = 200;
@@ -126,6 +152,27 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILoggerF
126152
});
127153

128154
}
155+
156+
private static string DecodeTelemetryRequest(HttpContext context, ILogger decoderLogger)
157+
{
158+
int length = Int32.Parse(context.Request.Headers["Content-Length"]);
159+
BinaryReader reader = new BinaryReader(context.Request.Body);
160+
161+
// Read body fully before decoding it
162+
byte[] buffer = reader.ReadBytes(length);
163+
164+
Dictionary<string, string> headers = new Dictionary<string, string>();
165+
foreach (KeyValuePair<string, StringValues> entry in context.Request.Headers)
166+
{
167+
// Our decoder only needs to know the 1st header value, do not need a multimap
168+
headers[entry.Key] = entry.Value.ElementAt(0);
169+
};
170+
Decoder decoder = new Decoder(headers, buffer);
171+
// Supply the logger
172+
decoder.Logger = decoderLogger;
173+
string result = decoder.ToJson(false, true, 2);
174+
return result;
175+
}
129176
}
130177
}
131178
}

0 commit comments

Comments
 (0)