Skip to content

Commit 57e001a

Browse files
authored
chore: Add Transaction.Guid to Activity finest log entries. (#3469)
1 parent 28f1fab commit 57e001a

4 files changed

Lines changed: 58 additions & 8 deletions

File tree

src/Agent/NewRelic/Agent/Core/OpenTelemetryBridge/Tracing/ActivityBridgeSegmentHelpers.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static void ProcessActivityTags(this ISegment segment, object originalAct
2929
string activityId = activity.Id;
3030
string displayName = activity.DisplayName;
3131

32-
string activityLogPrefix = ActivityLogPrefixHelpers.ActivityLogPrefix(activityId, activityKind, displayName);
32+
string activityLogPrefix = ActivityLogPrefixHelpers.ActivityLogPrefix(GetTransactionGuidFromSegment(segment), activityId, activityKind, displayName);
3333

3434
Log.Debug($"{activityLogPrefix} has stopped.");
3535

@@ -694,15 +694,20 @@ public static void AddExceptionEventInformationToSegment(this ISegment segment,
694694
}
695695
}
696696
}
697+
}
697698

699+
private static string GetTransactionGuidFromSegment(ISegment segment)
700+
{
701+
var internalSegment = segment as IInternalSpan;
702+
return internalSegment?.GetTransactionGuid();
698703
}
699704
}
700705

701706
public static class ActivityLogPrefixHelpers
702707
{
703-
public static string ActivityLogPrefix(string activityId, int activityKindInt, string activityDisplayName)
708+
public static string ActivityLogPrefix(string transactionGuid, string activityId, int activityKindInt, string activityDisplayName)
704709
{
705-
return $"Activity {activityId} (Kind: {(ActivityKind)activityKindInt}, DisplayName: {activityDisplayName})";
710+
return $"Trx {transactionGuid}: Activity {activityId} (Kind: {(ActivityKind)activityKindInt}, DisplayName: {activityDisplayName})";
706711
}
707712
}
708713

src/Agent/NewRelic/Agent/Core/Segments/IInternalSpan.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,6 @@ public interface IInternalSpan : ISegment, ISegmentExperimental, ISpan
1212
ErrorData ErrorData { get; set; }
1313

1414
string TryGetActivityTraceId();
15-
}
15+
16+
string GetTransactionGuid();
17+
}

src/Agent/NewRelic/Agent/Core/Segments/Segment.cs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,10 +307,10 @@ private void UpdateCurrentActivity()
307307
}
308308
else
309309
{
310-
Log.Finest("Segment.RemoveSegmentFromCallStack: NoOpSegment, setting Activity.Current to null");
310+
Log.Finest($"Trx {GetTransactionGuid()}: Segment.RemoveSegmentFromCallStack: NoOpSegment, setting Activity.Current to null");
311311
ActivityBridgeHelpers.SetCurrentActivity(null);
312312

313-
if (Log.IsFinestEnabled) Log.Finest($"Segment.RemoveSegmentFromCallStack: Activity.Current is now: {((dynamic)ActivityBridgeHelpers.GetCurrentActivity())?.Id ?? "null"}");
313+
if (Log.IsFinestEnabled) Log.Finest($"Trx {GetTransactionGuid()}: Segment.RemoveSegmentFromCallStack: Activity.Current is now: {((dynamic)ActivityBridgeHelpers.GetCurrentActivity())?.Id ?? "null"}");
314314
}
315315
}
316316

@@ -337,15 +337,20 @@ public ITransaction GetTransactionFromSegment()
337337
return _transactionSegmentState as ITransaction;
338338
}
339339

340+
public string GetTransactionGuid()
341+
{
342+
return (_transactionSegmentState as IInternalTransaction)?.Guid ?? "empty";
343+
}
344+
340345
public bool ActivityStartedTransaction { get; set; } = false;
341346

342347
public void MakeActivityCurrent()
343348
{
344-
if (Log.IsFinestEnabled) Log.Finest($"Segment.MakeActivityCurrent: Setting Activity.Current to this segment's activity: {_activity?.Id ?? "null"}");
349+
if (Log.IsFinestEnabled) Log.Finest($"Trx {GetTransactionGuid()}: Segment.MakeActivityCurrent: Setting Activity.Current to this segment's activity: {_activity?.Id ?? "null"}");
345350

346351
_activity?.MakeCurrent();
347352

348-
if (Log.IsFinestEnabled) Log.Finest($"Segment.MakeActivityCurrent: Activity.Current is now: {((dynamic)ActivityBridgeHelpers.GetCurrentActivity())?.Id ?? "null"}");
353+
if (Log.IsFinestEnabled) Log.Finest($"Trx {GetTransactionGuid()}: Segment.MakeActivityCurrent: Activity.Current is now: {((dynamic)ActivityBridgeHelpers.GetCurrentActivity())?.Id ?? "null"}");
349354
}
350355

351356
// We start and end segments on different threads (sometimes) so we need _relativeEndTicks

tests/Agent/UnitTests/Core.UnitTest/Segments/SegmentTests.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,18 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using NewRelic.Agent.Api.Experimental;
7+
using NewRelic.Agent.Configuration;
8+
using NewRelic.Agent.Core.Attributes;
9+
using NewRelic.Agent.Core.CallStack;
10+
using NewRelic.Agent.Core.DistributedTracing;
11+
using NewRelic.Agent.Core.DistributedTracing.Samplers;
612
using NewRelic.Agent.Core.Errors;
713
using NewRelic.Agent.Core.Metrics;
814
using NewRelic.Agent.Core.Segments.Tests;
15+
using NewRelic.Agent.Core.Time;
916
using NewRelic.Agent.Core.Transactions;
17+
using NewRelic.Agent.Core.Wrapper.AgentWrapperApi.Builders;
1018
using NewRelic.Agent.Core.Wrapper.AgentWrapperApi.Data;
1119
using NewRelic.Agent.Extensions.Api.Experimental;
1220
using NUnit.Framework;
@@ -532,4 +540,34 @@ public void SpanEventEvent_Constructor_SetsPropertiesCorrectly()
532540
Assert.That(spanEvent.Attributes, Is.Not.Null);
533541
});
534542
}
543+
544+
[Test]
545+
public void GetTransactionGuid_ReturnsSuccessfully()
546+
{
547+
var priority = 0.0f;
548+
var configuration = Mock.Create<IConfiguration>();
549+
Mock.Arrange(() => configuration.TransactionEventsEnabled).Returns(true);
550+
551+
// Initialize the Transaction with its dependencies
552+
var attribDefSvc = new AttributeDefinitionService((f) => new AttributeDefinitions(f));
553+
var databaseStatementParser = Mock.Create<IDatabaseStatementParser>();
554+
var distributedTracePayloadHandler = Mock.Create<IDistributedTracePayloadHandler>();
555+
556+
var transaction = new Transaction(configuration, Mock.Create<ITransactionName>(), Mock.Create<ISimpleTimer>(),
557+
DateTime.UtcNow, Mock.Create<ICallStackManager>(), Mock.Create<IDatabaseService>(),
558+
priority, databaseStatementParser, distributedTracePayloadHandler, Mock.Create<IErrorService>(), attribDefSvc.AttributeDefs, Mock.Create<ISamplerService>());
559+
var segment = new Segment(transaction.GetTransactionSegmentState(), new MethodCallData("Type", "Method", 1));
560+
var transactionGuid = segment.GetTransactionGuid();
561+
Assert.That(transactionGuid, Is.Not.Null);
562+
Assert.That(transactionGuid, Is.EqualTo(transaction.Guid));
563+
}
564+
565+
[Test]
566+
public void GetTransactionGuid_ReturnsEmptyWhenNoGuid()
567+
{
568+
var segment = new Segment(TransactionSegmentStateHelpers.GetItransactionSegmentState(), new MethodCallData("Type", "Method", 1));
569+
var transactionGuid = segment.GetTransactionGuid();
570+
Assert.That(transactionGuid, Is.Not.Null);
571+
Assert.That(transactionGuid, Is.EqualTo("empty"));
572+
}
535573
}

0 commit comments

Comments
 (0)