Skip to content

Commit

Permalink
PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
tippmar-nr committed Oct 24, 2023
1 parent 1597d0f commit eeaf0d1
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 11 deletions.
16 changes: 14 additions & 2 deletions src/Agent/NewRelic/Agent/Core/Agent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,12 +293,24 @@ public void HandleWrapperException(Exception exception)

public Stream TryGetStreamInjector(Stream stream, Encoding encoding, string contentType, string requestPath)
{
if (stream == null)
{
return null;
}

if (encoding == null)
{
return null;
}

var script = TryGetRUMScriptInternal(contentType, requestPath);
return script == null ? null : new BrowserMonitoringStreamInjector(() => script, stream, encoding);
}

public async Task TryInjectBrowserScriptAsync(string contentType, string requestPath, byte[] buffer, Stream baseStream, ITransaction transaction)
public async Task TryInjectBrowserScriptAsync(string contentType, string requestPath, byte[] buffer, Stream baseStream)
{
var transaction = _transactionService.GetCurrentInternalTransaction();

var script = TryGetRUMScriptInternal(contentType, requestPath);
var rumBytes = script == null ? null : Encoding.UTF8.GetBytes(script);

Expand All @@ -308,7 +320,7 @@ public async Task TryInjectBrowserScriptAsync(string contentType, string request
await baseStream.WriteAsync(buffer, 0, buffer.Length);
}
else
await BrowserScriptInjectionHelper.InjectBrowserScriptAsync(buffer, baseStream, () => rumBytes, transaction);
await BrowserScriptInjectionHelper.InjectBrowserScriptAsync(buffer, baseStream, rumBytes, transaction);
}

private string TryGetRUMScriptInternal(string contentType, string requestPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public static class BrowserScriptInjectionHelper
/// </summary>
/// <param name="buffer">UTF-8 encoded buffer representing the current page</param>
/// <param name="baseStream"></param>
/// <param name="getRumBytesFunc"></param>
/// <param name="rumBytes"></param>
/// <param name="transaction"></param>
/// <returns></returns>
public static async Task InjectBrowserScriptAsync(byte[] buffer, Stream baseStream,
Func<byte[]> getRumBytesFunc, ITransaction transaction)
byte[] rumBytes, ITransaction transaction)
{
var index = BrowserScriptInjectionIndexHelper.TryFindInjectionIndex(buffer);

Expand All @@ -34,7 +34,6 @@ public static async Task InjectBrowserScriptAsync(byte[] buffer, Stream baseStre

transaction?.LogFinest($"Injecting RUM script at byte index {index}.");

var rumBytes = getRumBytesFunc();

// Write everything up to the insertion index
await baseStream.WriteAsync(buffer, 0, index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,8 @@ public interface IAgent : IAgentExperimental
/// <param name="requestPath">The path of the request</param>
/// <param name="buffer">A UTF-8 encoded buffer of the content for this request</param>
/// <param name="baseStream">The stream into which the script (and buffer) should be injected</param>
/// <param name="transaction">the current transaction, used only for logging</param>
/// <returns></returns>
Task TryInjectBrowserScriptAsync(string contentType, string requestPath, byte[] buffer, Stream baseStream, ITransaction transaction);
Task TryInjectBrowserScriptAsync(string contentType, string requestPath, byte[] buffer, Stream baseStream);

/// <summary>
/// Returns the Trace Metadata of the currently executing transaction.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public override void WriteByte(byte value)
public override void Write(byte[] buffer, int offset, int count)
{
var curBuf = buffer.AsMemory(offset, count).ToArray();
_agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, curBuf, _baseStream, _agent.CurrentTransaction)
_agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, curBuf, _baseStream)
.GetAwaiter().GetResult();
}

Expand All @@ -72,7 +72,7 @@ public override Task WriteAsync(byte[] buffer, int offset, int count, Cancellati

public override async ValueTask WriteAsync(ReadOnlyMemory<byte> buffer, CancellationToken cancellationToken = default)
{
await _agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, buffer.ToArray(), _baseStream, _agent.CurrentTransaction);
await _agent.TryInjectBrowserScriptAsync(_context.Response.ContentType, _context.Request.Path.Value, buffer.ToArray(), _baseStream);
}

protected override void Dispose(bool disposing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ public async Task cross_agent_browser_monitor_injection_using_BrowserScriptInjec
using (var ms = new MemoryStream())
{
var dataBytes = Encoding.UTF8.GetBytes(data);
await BrowserScriptInjectionHelper.InjectBrowserScriptAsync(dataBytes, ms,
() => Encoding.UTF8.GetBytes("EXPECTED_RUM_LOADER_LOCATION"), null);
await BrowserScriptInjectionHelper.InjectBrowserScriptAsync(dataBytes, ms, Encoding.UTF8.GetBytes("EXPECTED_RUM_LOADER_LOCATION"), null);

await ms.FlushAsync();
ms.Position = 0;
Expand Down

0 comments on commit eeaf0d1

Please sign in to comment.