Skip to content

Commit

Permalink
PR comments and fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dudikeleti committed Nov 6, 2024
1 parent a554346 commit ed456fb
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool
{
Tracer.Instance.ScopeManager.Active.Root.Span.SetTag(evaluationErrorTag, string.Join(";", decoration.Errors));
}
else if (Tracer.Instance.ScopeManager.Active.Root.Span.GetTag(evaluationErrorTag) != null)
else if (Tracer.Instance.ScopeManager.Active.Span.GetTag(evaluationErrorTag) != null)
{
Tracer.Instance.ScopeManager.Active.Root.Span.SetTag(evaluationErrorTag, null);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@

using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading;
using Datadog.Trace.Debugger.Symbols;
using Datadog.Trace.Logging;
using Datadog.Trace.VendoredMicrosoftCode.System.Buffers;
using Datadog.Trace.VendoredMicrosoftCode.System.Collections.Immutable;

namespace Datadog.Trace.Debugger.SpanCodeOrigin
{
Expand All @@ -23,26 +20,13 @@ internal class SpanCodeOriginManager

private static readonly IDatadogLogger Log = DatadogLogging.GetLoggerFor(typeof(SpanCodeOriginManager));

private static object _globalInstanceLock = new();

private static bool _globalInstanceInitialized;

#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.
private static SpanCodeOriginManager _instance;
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring as nullable.

private readonly DebuggerSettings _settings = LiveDebugger.Instance?.Settings ?? DebuggerSettings.FromDefaultSource();

internal static SpanCodeOriginManager Instance =>
LazyInitializer.EnsureInitialized(
ref _instance,
ref _globalInstanceInitialized,
ref _globalInstanceLock);
internal static SpanCodeOriginManager Instance { get; } = new();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal void SetCodeOrigin(Span? span)
{
if (span == null || !this._settings.CodeOriginForSpansEnabled)
if (span == null || !_settings.CodeOriginForSpansEnabled)
{
return;
}
Expand Down
48 changes: 30 additions & 18 deletions tracer/test/Datadog.Trace.Tests/Debugger/SpanCodeOriginTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using Datadog.Trace.Configuration;
using Datadog.Trace.Configuration.Telemetry;
using Datadog.Trace.Debugger;
using Datadog.Trace.Debugger.SpanCodeOrigin;
using Datadog.Trace.VendoredMicrosoftCode.System.Collections.Immutable;
using FluentAssertions;
using Xunit;

namespace Datadog.Trace.Tests.Debugger
Expand Down Expand Up @@ -39,7 +41,7 @@ public void SetCodeOrigin_WhenDisabled_DoesNotSetTags()
SpanCodeOriginManager.Instance.SetCodeOrigin(span);

// Assert
Assert.Null(span.Tags.GetTag(CodeOriginTag + ".type"));
span.Tags.GetTag(CodeOriginTag + ".type").Should().BeNull();
}

[Fact]
Expand All @@ -54,13 +56,18 @@ public void SetCodeOrigin_WhenEnabled_SetsCorrectTags()
TestMethod(span);

// Assert
Assert.NotNull(span.Tags.GetTag($"{CodeOriginTag}.type"));
Assert.Equal("exit", span.Tags.GetTag($"{CodeOriginTag}.type"));

Assert.NotNull(span.Tags.GetTag($"{CodeOriginTag}.frames.0.method"));
Assert.Equal(nameof(TestMethod), span.Tags.GetTag($"{CodeOriginTag}.frames.0.method"));
Assert.NotNull(span.Tags.GetTag($"{CodeOriginTag}.frames.0.type"));
Assert.Contains(nameof(SpanCodeOriginTests), span.Tags.GetTag($"{CodeOriginTag}.frames.0.type"));
var codeOriginType = span.Tags.GetTag($"{CodeOriginTag}.type");
codeOriginType.Should().Be("exit");
var frame0Method = span.Tags.GetTag($"{CodeOriginTag}.frames.0.method");
frame0Method.Should().Be(nameof(TestMethod));
var frame0Type = span.Tags.GetTag($"{CodeOriginTag}.frames.0.type");
frame0Type.Should().Be(GetType().FullName);
var file = span.Tags.GetTag($"{CodeOriginTag}.frames.0.file");
file.Should().EndWith($"{nameof(SpanCodeOriginTests)}.cs");
var line = span.Tags.GetTag($"{CodeOriginTag}.frames.0.line");
line.Should().NotBeNullOrEmpty();
var column = span.Tags.GetTag($"{CodeOriginTag}.frames.0.column");
column.Should().NotBeNullOrEmpty();
}

[Fact]
Expand All @@ -76,40 +83,45 @@ public void SetCodeOrigin_WithMaxFramesLimit_RespectsLimit()

// Assert
var tags = ((List<KeyValuePair<string, string>>)(typeof(Datadog.Trace.Tagging.TagsList).GetField("_tags", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(span.Tags))).Select(i => i.Key).ToList();
Assert.Contains(tags, s => s.StartsWith($"{CodeOriginTag}.frames.0"));
Assert.Contains(tags, s => s.StartsWith($"{CodeOriginTag}.frames.1"));
Assert.DoesNotContain(tags, s => s.StartsWith($"{CodeOriginTag}.frames.2"));
tags.Should().Contain(s => s.StartsWith($"{CodeOriginTag}.frames.0"));
tags.Should().Contain(s => s.StartsWith($"{CodeOriginTag}.frames.1"));
tags.Should().NotContain(s => s.StartsWith($"{CodeOriginTag}.frames.2"));
}

private static void CreateCodeOriginManager(bool isEnable = false, int numberOfFrames = 8, string excludeFromFilter = "Datadog.Trace.Tests")
{
var overrideSettings = DebuggerSettings.FromSource(
new NameValueConfigurationSource(new()
{
{ ConfigurationKeys.Debugger.CodeOriginForSpansEnabled, isEnable.ToString() },
{ ConfigurationKeys.Debugger.CodeOriginMaxUserFrames, numberOfFrames.ToString() },
{ ConfigurationKeys.Debugger.ThirdPartyDetectionExcludes, excludeFromFilter }
}),
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);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void TestMethod(Span span)
{
SpanCodeOriginManager.Instance.SetCodeOrigin(span);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void DeepTestMethod1(Span span)
{
DeepTestMethod2(span);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void DeepTestMethod2(Span span)
{
DeepTestMethod3(span);
}

[MethodImpl(MethodImplOptions.NoInlining)]
private void DeepTestMethod3(Span span)
{
SpanCodeOriginManager.Instance.SetCodeOrigin(span);
Expand Down

0 comments on commit ed456fb

Please sign in to comment.