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

feat(dotnet): buffer-length check & queue #1139

Merged
merged 2 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using ReadMe.HarJsonObjectModels;

namespace ReadMe.HarJsonTranslationLogics
Expand All @@ -28,7 +27,7 @@ public HarJsonBuilder(RequestDelegate next, HttpContext context, IConfiguration
this.configValues = configValues;
}

public async Task<string> BuildHar()
internal async Task<Root> BuildHar()
{
Root harObj = new Root();
harObj._id = Guid.NewGuid().ToString();
Expand All @@ -37,8 +36,7 @@ public async Task<string> BuildHar()
harObj.clientIPAddress = this.context.Connection.RemoteIpAddress.ToString();
harObj.group = this.BuildGroup();
harObj.request = new RequestMain(await this.BuildLog());
string harJsonObj = JsonConvert.SerializeObject(new List<Root>() { harObj });
return harJsonObj;
return harObj;
}

private Group BuildGroup()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ namespace ReadMe.HarJsonTranslationLogics
{
class ReadMeApiCaller
{
private readonly string harJsonObject;
private readonly string harJsonObjects;
private readonly string apiKey;

public ReadMeApiCaller(string harJsonObject, string apiKey)
public ReadMeApiCaller(string harJsonObjects, string apiKey)
{
this.harJsonObject = harJsonObject;
this.harJsonObjects = harJsonObjects;
this.apiKey = apiKey;
}

Expand All @@ -25,7 +25,7 @@ public void SendHarObjToReadMeApi()
request.AddHeader("Content-Type", "application/json");
string apiKey = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(this.apiKey + ":"));
request.AddHeader("Authorization", apiKey);
request.AddParameter("application/json", this.harJsonObject, ParameterType.RequestBody);
request.AddParameter("application/json", this.harJsonObjects, ParameterType.RequestBody);
client.ExecuteAsync(request);
}
catch (Exception)
Expand Down
18 changes: 15 additions & 3 deletions packages/dotnet/ReadMe/Metrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using ReadMe.HarJsonObjectModels;
using ReadMe.HarJsonTranslationLogics;

Expand All @@ -11,12 +12,14 @@ public class Metrics
{
private readonly RequestDelegate next;
private readonly IConfiguration configuration;
private readonly List<Root> harQueue;
private Group group;

public Metrics(RequestDelegate next, IConfiguration configuration)
{
this.next = next;
this.configuration = configuration;
this.harQueue = new List<Root>();
}

public async Task InvokeAsync(HttpContext context)
Expand Down Expand Up @@ -44,9 +47,18 @@ public async Task InvokeAsync(HttpContext context)
context.Request.EnableBuffering();
HarJsonBuilder harJsonBuilder = new HarJsonBuilder(this.next, context, this.configuration, configValues);

string harJsonObj = await harJsonBuilder.BuildHar();
ReadMeApiCaller readmeApiCaller = new ReadMeApiCaller(harJsonObj, configValues.apiKey);
readmeApiCaller.SendHarObjToReadMeApi();
var harObj = await harJsonBuilder.BuildHar();
lock (this.harQueue)
{
this.harQueue.Add(harObj);
if (this.harQueue.Count >= configValues.options.bufferLength)
{
string serializaedHars = JsonConvert.SerializeObject(this.harQueue);
ReadMeApiCaller readmeApiCaller = new ReadMeApiCaller(serializaedHars, configValues.apiKey);
readmeApiCaller.SendHarObjToReadMeApi();
this.harQueue.Clear();
}
}
}
else
{
Expand Down
Loading