Skip to content

Commit

Permalink
Fix tests in CI
Browse files Browse the repository at this point in the history
  • Loading branch information
dudikeleti committed Nov 7, 2024
1 parent 872901b commit 9a1a33f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ internal void SetCodeOrigin(Span? span)

private void AddExitSpanTag(Span span)
{
var frames = ArrayPool<FrameInfo>.Shared.Rent(this._settings.CodeOriginMaxUserFrames);
var frames = ArrayPool<FrameInfo>.Shared.Rent(_settings.CodeOriginMaxUserFrames);
try
{
var framesLength = PopulateUserFrames(frames);
Expand Down Expand Up @@ -98,7 +98,7 @@ private void AddExitSpanTag(Span span)

private int PopulateUserFrames(FrameInfo[] frames)
{
var stackTrace = new StackTrace(skipFrames: 3, fNeedFileInfo: true);
var stackTrace = new StackTrace(fNeedFileInfo: true);
var stackFrames = stackTrace.GetFrames();

if (stackFrames == null!)
Expand Down
51 changes: 36 additions & 15 deletions tracer/test/Datadog.Trace.Tests/Debugger/SpanCodeOriginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void SetCodeOrigin_WhenSpanIsNull_DoesNotThrow()
public void SetCodeOrigin_WhenDisabled_DoesNotSetTags()
{
// Arrange
CreateCodeOriginManager();
using var settingsSetter = SetCodeOriginManagerSettings();

var span = new Span(new SpanContext(1, 2, SamplingPriority.UserKeep), DateTimeOffset.UtcNow);

Expand All @@ -48,7 +48,7 @@ public void SetCodeOrigin_WhenDisabled_DoesNotSetTags()
public void SetCodeOrigin_WhenEnabled_SetsCorrectTags()
{
// Arrange
CreateCodeOriginManager(true);
using var settingsSetter = SetCodeOriginManagerSettings(true);

var span = new Span(new SpanContext(1, 2, SamplingPriority.UserKeep), DateTimeOffset.UtcNow);

Expand All @@ -74,7 +74,7 @@ public void SetCodeOrigin_WhenEnabled_SetsCorrectTags()
public void SetCodeOrigin_WithMaxFramesLimit_RespectsLimit()
{
// Arrange
CreateCodeOriginManager(true, 2);
using var settingsSetter = SetCodeOriginManagerSettings(true, 2);

var span = new Span(new SpanContext(1, 2, SamplingPriority.UserKeep), DateTimeOffset.UtcNow);

Expand All @@ -88,19 +88,11 @@ public void SetCodeOrigin_WithMaxFramesLimit_RespectsLimit()
tags.Should().NotContain(s => s.StartsWith($"{CodeOriginTag}.frames.2"));
}

private static void CreateCodeOriginManager(bool isEnable = false, int numberOfFrames = 8, string excludeFromFilter = "Datadog.Trace.Tests")
private static IDisposable SetCodeOriginManagerSettings(bool isEnable = false, int numberOfFrames = 8, string excludeFromFilter = "Datadog.Trace.Tests")
{
var overrideSettings = DebuggerSettings.FromSource(
new NameValueConfigurationSource(
new NameValueCollection
{
{ ConfigurationKeys.Debugger.CodeOriginForSpansEnabled, isEnable.ToString() },
{ ConfigurationKeys.Debugger.CodeOriginMaxUserFrames, numberOfFrames.ToString() },
{ ConfigurationKeys.Debugger.ThirdPartyDetectionExcludes, excludeFromFilter }
}),
NullConfigurationTelemetry.Instance);
var instance = SpanCodeOriginManager.Instance;
instance.GetType().GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(instance, overrideSettings);
var setter = new CodeOriginSettingsSetter();
setter.Set(isEnable, numberOfFrames, excludeFromFilter);
return setter;
}

[MethodImpl(MethodImplOptions.NoInlining)]
Expand All @@ -126,5 +118,34 @@ private void DeepTestMethod3(Span span)
{
SpanCodeOriginManager.Instance.SetCodeOrigin(span);
}

internal class CodeOriginSettingsSetter : IDisposable
{
private DebuggerSettings _original;

public void Dispose()
{
var instance = SpanCodeOriginManager.Instance;
instance.GetType().GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(instance, _original);
}

internal void Set(bool isEnable, int numberOfFrames, string excludeFromFilter)
{
var instance = SpanCodeOriginManager.Instance;
_original = (DebuggerSettings)instance.GetType().GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance).GetValue(instance);

var overrideSettings = DebuggerSettings.FromSource(
new NameValueConfigurationSource(
new NameValueCollection
{
{ ConfigurationKeys.Debugger.CodeOriginForSpansEnabled, isEnable.ToString() },
{ ConfigurationKeys.Debugger.CodeOriginMaxUserFrames, numberOfFrames.ToString() },
{ ConfigurationKeys.Debugger.ThirdPartyDetectionExcludes, excludeFromFilter }
}),
NullConfigurationTelemetry.Instance);

instance.GetType().GetField("_settings", BindingFlags.NonPublic | BindingFlags.Instance).SetValue(instance, overrideSettings);
}
}
}
}

0 comments on commit 9a1a33f

Please sign in to comment.