Skip to content

Commit 8d23640

Browse files
committed
Add debug log in case evaluation result is null and it shouldn't be null
1 parent c6bd674 commit 8d23640

File tree

3 files changed

+55
-17
lines changed

3 files changed

+55
-17
lines changed

tracer/src/Datadog.Trace/Debugger/Expressions/ExpressionEvaluationResult.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ internal ref struct ExpressionEvaluationResult
2020

2121
internal List<EvaluationError> Errors { get; set; }
2222

23-
internal bool HasError => Errors is { Count: > 0 };
23+
internal readonly bool HasError => Errors is { Count: > 0 };
2424

25-
internal bool IsNull()
25+
internal readonly bool IsNull()
2626
{
2727
return Template == null && Condition == null && Metric == null && Decorations?.Length == 0 && Errors == null;
2828
}
2929

3030
internal struct DecorationResult
3131
{
32-
public string TagName { get; set; }
32+
internal string TagName { get; set; }
3333

34-
public string Value { get; set; }
34+
internal string Value { get; set; }
3535

36-
public EvaluationError[] Errors { get; set; }
36+
internal EvaluationError[] Errors { get; set; }
3737
}
3838
}

tracer/src/Datadog.Trace/Debugger/Expressions/ProbeInfo.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ internal record struct ProbeInfo(
4141

4242
internal string[] Tags { get; } = Tags;
4343

44-
public TargetSpan? TargetSpan { get; } = TargetSpan;
44+
internal TargetSpan? TargetSpan { get; } = TargetSpan;
4545

46-
public CaptureLimitInfo CaptureLimitInfo { get; } = CaptureLimitInfo;
46+
internal CaptureLimitInfo CaptureLimitInfo { get; } = CaptureLimitInfo;
4747
}
4848

4949
internal readonly record struct CaptureLimitInfo(
@@ -52,12 +52,12 @@ internal readonly record struct CaptureLimitInfo(
5252
int MaxLength,
5353
int MaxFieldCount)
5454
{
55-
public int MaxReferenceDepth { get; } = MaxReferenceDepth;
55+
internal int MaxReferenceDepth { get; } = MaxReferenceDepth;
5656

57-
public int MaxCollectionSize { get; } = MaxCollectionSize;
57+
internal int MaxCollectionSize { get; } = MaxCollectionSize;
5858

59-
public int MaxLength { get; } = MaxLength;
59+
internal int MaxLength { get; } = MaxLength;
6060

61-
public int MaxFieldCount { get; } = MaxFieldCount;
61+
internal int MaxFieldCount { get; } = MaxFieldCount;
6262
}
6363
}

tracer/src/Datadog.Trace/Debugger/Expressions/ProbeProcessor.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Unless explicitly stated otherwise all files in this repository are licensed under the Apache 2 License.
33
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2017 Datadog, Inc.
44
// </copyright>
5+
56
#nullable enable
67

78
using System;
@@ -349,6 +350,8 @@ private ExpressionEvaluationResult Evaluate(DebuggerSnapshotCreator snapshotCrea
349350

350351
if (evaluationResult.IsNull())
351352
{
353+
LogEvaluationState(snapshotCreator.MethodScopeMembers);
354+
352355
Log.Error("Evaluation result should not be null. Probe: {ProbeId}", ProbeInfo.ProbeId);
353356
evaluationResult.Errors = new List<EvaluationError> { new() { Message = $"Evaluation result is null. Probe ID: {ProbeInfo.ProbeId}" } };
354357
return evaluationResult;
@@ -390,15 +393,45 @@ private ExpressionEvaluationResult Evaluate(DebuggerSnapshotCreator snapshotCrea
390393
return evaluationResult;
391394
}
392395

393-
private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool shouldStopCapture, ExpressionEvaluationResult evaluationResult)
396+
private void LogEvaluationState(MethodScopeMembers methodScopeMembers)
394397
{
395-
if (!TryGetScope(out var scope))
398+
if (_templates == null
399+
&& _condition == null
400+
&& _metric == null
401+
&& _spanDecorations == null)
402+
{
403+
return;
404+
}
405+
406+
// this should never happen, but if it does, we want to log it for further investigation
407+
Log.Error("Evaluation state error: we thought that evaluation result is null but that not true. probably we are using an incorrect version of ProbeExpressionEvaluator");
408+
409+
try
396410
{
397411
if (Log.IsEnabled(LogEventLevel.Debug))
398412
{
399-
Log.Debug("No active scope available, skipping span decoration. Probe: {ProbeId}", ProbeInfo.ProbeId);
413+
var instance = methodScopeMembers.InvocationTarget;
414+
var members = methodScopeMembers.Members?.Select(m => new { Name = m.Name, Type = m.Type?.FullName ?? m.Type?.Name }).ToList();
415+
string? membersAsString = null;
416+
if (members?.Any() == true)
417+
{
418+
membersAsString = string.Join(";", members);
419+
}
420+
421+
Log.Error("Evaluation state error: Target Method: Type = {Type}, Name = {Name}. Method Members: {Members}", instance.Type?.FullName ?? instance.Type?.Name, instance.Name, membersAsString);
400422
}
423+
}
424+
catch
425+
{
426+
// ignored
427+
}
428+
}
401429

430+
private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool shouldStopCapture, ExpressionEvaluationResult evaluationResult)
431+
{
432+
if (!TryGetScope(out var scope))
433+
{
434+
Log.Debug("No active scope available, skipping span decoration. Probe: {ProbeId}", ProbeInfo.ProbeId);
402435
return;
403436
}
404437

@@ -411,6 +444,11 @@ private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool
411444
var probeIdTag = $"{DynamicPrefix}{decoration.TagName}.probe_id";
412445
ISpan? targetSpan = null;
413446

447+
if (ProbeInfo.TargetSpan == null)
448+
{
449+
Log.Error("We can't set the {Tag} tag. Probe ID: {ProbeId}, because target span is null", decoration.TagName, ProbeInfo.ProbeId);
450+
}
451+
414452
switch (ProbeInfo.TargetSpan)
415453
{
416454
case TargetSpan.Root:
@@ -420,13 +458,13 @@ private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool
420458
targetSpan = scope.Span;
421459
break;
422460
default:
423-
Log.Error("Invalid target span. Probe: {ProbeId}", ProbeInfo.ProbeId);
461+
Log.Error("We can't set the {Tag} tag. Probe ID: {ProbeId}, because target span {Span} is invalid", decoration.TagName, ProbeInfo.ProbeId, ProbeInfo.TargetSpan);
424462
break;
425463
}
426464

427465
if (targetSpan == null)
428466
{
429-
Log.Warning("No root span or active span is available, so we can't set the {Tag} tag. Probe ID: {ProbeId}", decoration.TagName, ProbeInfo.ProbeId);
467+
Log.Warning("We can't set the {Tag} tag. Probe ID: {ProbeId}, because the chosen span {Span} is not available", decoration.TagName, ProbeInfo.ProbeId, ProbeInfo.TargetSpan);
430468
continue;
431469
}
432470

@@ -444,7 +482,7 @@ private void SetSpanDecoration(DebuggerSnapshotCreator snapshotCreator, ref bool
444482
attachedTags = true;
445483
if (Log.IsEnabled(LogEventLevel.Debug))
446484
{
447-
Log.Debug("Successfully attached tag {Tag} to span {Span}. ProbID={ProbeId}", decoration.TagName, targetSpan.SpanId, ProbeInfo.ProbeId);
485+
Log.Debug("Successfully attached tag {Tag} to span {Span}. ProbID: {ProbeId}", decoration.TagName, targetSpan.SpanId, ProbeInfo.ProbeId);
448486
}
449487
}
450488

0 commit comments

Comments
 (0)