Skip to content

Commit

Permalink
[continuous profiler] create delegates for export methods (open-telem…
Browse files Browse the repository at this point in the history
  • Loading branch information
lachmatt authored Jan 12, 2024
1 parent cb83bfc commit 5a8580c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

#if NET6_0_OR_GREATER

using System.Reflection;

namespace OpenTelemetry.AutoInstrumentation.ContinuousProfiler;

internal class BufferProcessor
Expand All @@ -14,18 +12,16 @@ internal class BufferProcessor

private readonly bool _threadSamplingEnabled;
private readonly bool _allocationSamplingEnabled;
private readonly object _continuousProfilerExporter;
private readonly MethodInfo _exportThreadSamplesMethod;
private readonly MethodInfo _exportAllocationSamplesMethod;
private readonly Action<byte[], int> _exportThreadSamplesMethod;
private readonly Action<byte[], int> _exportAllocationSamplesMethod;
private readonly byte[] _buffer = new byte[BufferSize];

public BufferProcessor(bool threadSamplingEnabled, bool allocationSamplingEnabled, object continuousProfilerExporter, MethodInfo exportThreadSamplesMethod, MethodInfo exportAllocationSamplesMethod)
public BufferProcessor(bool threadSamplingEnabled, bool allocationSamplingEnabled, Action<byte[], int> threadSamplesMethod, Action<byte[], int> allocationSamplesMethod)
{
_threadSamplingEnabled = threadSamplingEnabled;
_allocationSamplingEnabled = allocationSamplingEnabled;
_continuousProfilerExporter = continuousProfilerExporter;
_exportThreadSamplesMethod = exportThreadSamplesMethod;
_exportAllocationSamplesMethod = exportAllocationSamplesMethod;
_exportThreadSamplesMethod = threadSamplesMethod;
_exportAllocationSamplesMethod = allocationSamplesMethod;
}

public void Process()
Expand All @@ -49,7 +45,7 @@ private void ProcessThreadSamples()
return;
}

_exportThreadSamplesMethod.Invoke(_continuousProfilerExporter, new object[] { _buffer, read });
_exportThreadSamplesMethod(_buffer, read);
}

private void ProcessAllocationSamples()
Expand All @@ -60,7 +56,7 @@ private void ProcessAllocationSamples()
return;
}

_exportAllocationSamplesMethod.Invoke(_continuousProfilerExporter, new object[] { _buffer, read });
_exportAllocationSamplesMethod(_buffer, read);
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#if NET6_0_OR_GREATER

using System.Globalization;
using System.Reflection;
using OpenTelemetry.AutoInstrumentation.Logging;

namespace OpenTelemetry.AutoInstrumentation.ContinuousProfiler;
Expand Down Expand Up @@ -55,10 +56,13 @@ public static void Initialize(bool threadSamplingEnabled, bool allocationSamplin
return;
}

var threadSamplesMethod = exportThreadSamplesMethod.CreateDelegate<Action<byte[], int>>(continuousProfilerExporter);
var allocationSamplesMethod = exportAllocationSamplesMethod.CreateDelegate<Action<byte[], int>>(continuousProfilerExporter);

// TODO Graceful shutdown and Task.Delay https://github.com/open-telemetry/opentelemetry-dotnet-instrumentation/issues/3216
var thread = new Thread(() =>
{
SampleReadingThread(new BufferProcessor(threadSamplingEnabled, allocationSamplingEnabled, continuousProfilerExporter, exportThreadSamplesMethod, exportAllocationSamplesMethod), exportInterval);
SampleReadingThread(new BufferProcessor(threadSamplingEnabled, allocationSamplingEnabled, threadSamplesMethod, allocationSamplesMethod), exportInterval);
})
{
Name = BackgroundThreadName,
Expand Down

0 comments on commit 5a8580c

Please sign in to comment.