Skip to content

Commit 1df0342

Browse files
authored
feat: Add transaction ID to intrinsic attributes for error events and traces regardless of DT/CAT settings (#2341)
* Always include transaction guid in transaction attributes * Update integration tests
1 parent e266b2e commit 1df0342

File tree

9 files changed

+80
-57
lines changed

9 files changed

+80
-57
lines changed

src/Agent/NewRelic/Agent/Core/Transformers/TransactionTransformer/TransactionAttributeMaker.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ private void SetIntrinsicAttributes(IAttributeValueCollection attribValues, Immu
4848

4949
_attribDefs.TransactionName.TrySetValue(attribValues, transactionMetricName.PrefixedName);
5050
_attribDefs.TransactionNameForError.TrySetValue(attribValues, transactionMetricName.PrefixedName);
51+
_attribDefs.Guid.TrySetValue(attribValues, immutableTransaction.Guid);
5152

5253
// Duration is just EndTime minus StartTime for non-web transactions and response time otherwise
5354
_attribDefs.Duration.TrySetValue(attribValues, immutableTransaction.ResponseTimeOrDuration);
@@ -161,7 +162,6 @@ private void SetIntrinsicAttributes(IAttributeValueCollection attribValues, Immu
161162

162163
if (_configurationService.Configuration.DistributedTracingEnabled)
163164
{
164-
_attribDefs.Guid.TrySetValue(attribValues, immutableTransaction.Guid);
165165
_attribDefs.DistributedTraceId.TrySetValue(attribValues, immutableTransaction.TraceId);
166166
_attribDefs.Priority.TrySetValue(attribValues, immutableTransaction.Priority);
167167
_attribDefs.Sampled.TrySetValue(attribValues, immutableTransaction.Sampled);

tests/Agent/IntegrationTests/IntegrationTestHelpers/Assertions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,11 @@ public static void TransactionEventHasAttributes(IEnumerable<KeyValuePair<string
758758
}
759759

760760
var actualValue = actualAttributes[expectedAttribute.Key] as string;
761+
if (actualValue == null && actualAttributes[expectedAttribute.Key].GetType() == typeof(bool))
762+
{
763+
actualValue = actualAttributes[expectedAttribute.Key].ToString().ToLowerInvariant();
764+
}
765+
761766
if (actualValue != expectedAttribute.Value)
762767
{
763768
builder.AppendFormat("Attribute named {0} in the transaction event had an unexpected value. Expected: {1}, Actual: {2}", expectedAttribute.Key, expectedAttribute.Value, actualValue);

tests/Agent/IntegrationTests/IntegrationTests/Errors/ErrorTraceMvc.cs

+13-11
Original file line numberDiff line numberDiff line change
@@ -80,25 +80,27 @@ public void Test()
8080
new Assertions.ExpectedMetric { metricName = @"Supportability/Transactions/allOther" },
8181
};
8282

83-
var expectedAttributes = new Dictionary<string, string>
83+
var metrics = _fixture.AgentLog.GetMetrics().ToList();
84+
var errorTraces = _fixture.AgentLog.GetErrorTraces().ToList();
85+
var transactionEvents = _fixture.AgentLog.GetTransactionEvents().ToList();
86+
var errorEvents = _fixture.AgentLog.GetErrorEvents().ToList();
87+
88+
var transactionId = transactionEvents[0].IntrinsicAttributes["guid"].ToString();
89+
90+
var expectedTransactionEventAttributes = new Dictionary<string, string>
8491
{
8592
{ "errorType", "System.Exception" },
8693
{ "errorMessage", "!Exception~Message!" },
94+
{ "error", "true" }
8795
};
8896

8997
var expectedErrorEventAttributes = new Dictionary<string, string>
9098
{
9199
{ "error.class", "System.Exception" },
92100
{ "error.message", "!Exception~Message!" },
101+
{ "guid", transactionId }
93102
};
94103

95-
var expectedErrorTransactionEventAttributes2 = new List<string> { "error" };
96-
97-
var metrics = _fixture.AgentLog.GetMetrics().ToList();
98-
var errorTraces = _fixture.AgentLog.GetErrorTraces().ToList();
99-
var transactionEvents = _fixture.AgentLog.GetTransactionEvents().ToList();
100-
var errorEvents = _fixture.AgentLog.GetErrorEvents().ToList();
101-
102104
NrAssert.Multiple(
103105
() => Assertions.MetricsExist(expectedMetrics, metrics),
104106
() => Assertions.MetricsDoNotExist(unexpectedMetrics, metrics),
@@ -108,12 +110,12 @@ public void Test()
108110
() => Assert.Equal("System.Exception", errorTraces[0].ExceptionClassName),
109111
() => Assert.Equal("!Exception~Message!", errorTraces[0].Message),
110112
() => Assert.NotEmpty(errorTraces[0].Attributes.StackTrace),
113+
() => Assert.Equal(errorTraces[0].Attributes.IntrinsicAttributes["guid"], transactionId),
111114
() => Assert.True(transactionEvents.Any(), "No transaction events found."),
112115
() => Assert.True(transactionEvents.Count == 1, $"Expected 1 transaction event but found {transactionEvents.Count}"),
113-
() => Assertions.TransactionEventHasAttributes(expectedAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[0]),
116+
() => Assertions.TransactionEventHasAttributes(expectedTransactionEventAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[0]),
114117
() => Assert.Single(errorEvents),
115-
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvents[0]),
116-
() => Assertions.TransactionEventHasAttributes(expectedErrorTransactionEventAttributes2, TransactionEventAttributeType.Intrinsic, transactionEvents[0])
118+
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvents[0])
117119
);
118120
}
119121
}

tests/Agent/IntegrationTests/IntegrationTests/Errors/ErrorTraceWebApi.cs

+16-16
Original file line numberDiff line numberDiff line change
@@ -50,43 +50,43 @@ public void Test()
5050
{
5151
var expectedMetrics = new List<Assertions.ExpectedMetric>
5252
{
53-
// error metrics
54-
new Assertions.ExpectedMetric {metricName = @"Errors/all", callCount = 1},
53+
// error metrics
54+
new Assertions.ExpectedMetric {metricName = @"Errors/all", callCount = 1},
5555
new Assertions.ExpectedMetric {metricName = @"Errors/allWeb", callCount = 1},
5656
new Assertions.ExpectedMetric {metricName = @"Errors/WebTransaction/WebAPI/Values/ThrowException", callCount = 1 },
5757

58-
// other
59-
new Assertions.ExpectedMetric { metricName = @"WebTransaction", callCount = 1 }
58+
// other
59+
new Assertions.ExpectedMetric { metricName = @"WebTransaction", callCount = 1 }
6060
};
6161

6262
var unexpectedMetrics = new List<Assertions.ExpectedMetric>
6363
{
6464
new Assertions.ExpectedMetric { metricName = @"OtherTransaction/all", callCount = 5 },
6565
};
6666

67+
var metrics = _fixture.AgentLog.GetMetrics().ToList();
68+
var errorTrace = _fixture.AgentLog.GetErrorTraces().ToList().FirstOrDefault();
69+
var transactionEvent = _fixture.AgentLog.GetTransactionEvents().ToList().FirstOrDefault();
70+
var errorEvent = _fixture.AgentLog.GetErrorEvents().FirstOrDefault();
71+
6772
var expectedErrorClass = "System.Exception";
6873
var expectedErrorMessage = "ExceptionMessage";
74+
var transactionId = transactionEvent.IntrinsicAttributes["guid"].ToString();
6975

70-
var expectedAttributes = new Dictionary<string, string>
76+
var expectedTransactionEventAttributes = new Dictionary<string, string>
7177
{
7278
{ "errorType", expectedErrorClass },
7379
{ "errorMessage", expectedErrorMessage },
80+
{ "error", "true" },
7481
};
7582

76-
7783
var expectedErrorEventAttributes = new Dictionary<string, string>
7884
{
7985
{ "error.class", expectedErrorClass },
8086
{ "error.message", expectedErrorMessage },
87+
{ "guid", transactionId },
8188
};
8289

83-
var expectedErrorTransactionEventAttributes2 = new List<string> { "error" };
84-
85-
var metrics = _fixture.AgentLog.GetMetrics().ToList();
86-
var errorTrace = _fixture.AgentLog.GetErrorTraces().ToList().FirstOrDefault();
87-
var transactionEvent = _fixture.AgentLog.GetTransactionEvents().ToList().FirstOrDefault();
88-
var errorEvent = _fixture.AgentLog.GetErrorEvents().FirstOrDefault();
89-
9090
NrAssert.Multiple(
9191
() => Assertions.MetricsExist(expectedMetrics, metrics),
9292
() => Assertions.MetricsDoNotExist(unexpectedMetrics, metrics),
@@ -96,10 +96,10 @@ public void Test()
9696
() => Assert.Equal("WebTransaction/WebAPI/Values/ThrowException", errorTrace.Path),
9797
() => Assert.Equal(expectedErrorClass, errorTrace.ExceptionClassName),
9898
() => Assert.Equal(expectedErrorMessage, errorTrace.Message),
99+
() => Assert.Equal(transactionId, errorTrace.Attributes.IntrinsicAttributes["guid"]),
99100
() => Assert.NotEmpty(errorTrace.Attributes.StackTrace),
100-
() => Assertions.TransactionEventHasAttributes(expectedAttributes, TransactionEventAttributeType.Intrinsic, transactionEvent),
101-
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvent),
102-
() => Assertions.TransactionEventHasAttributes(expectedErrorTransactionEventAttributes2, TransactionEventAttributeType.Intrinsic, transactionEvent)
101+
() => Assertions.TransactionEventHasAttributes(expectedTransactionEventAttributes, TransactionEventAttributeType.Intrinsic, transactionEvent),
102+
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvent)
103103
);
104104
}
105105
}

tests/Agent/IntegrationTests/IntegrationTests/Errors/ErrorTraceWebService.cs

+17-13
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ public void Test()
5252
{
5353
var expectedMetrics = new List<Assertions.ExpectedMetric>
5454
{
55-
// error metrics
56-
new Assertions.ExpectedMetric {metricName = @"Errors/all", callCount = 2},
55+
// error metrics
56+
new Assertions.ExpectedMetric {metricName = @"Errors/all", callCount = 2},
5757
new Assertions.ExpectedMetric {metricName = @"Errors/allWeb", callCount = 2},
5858
new Assertions.ExpectedMetric {metricName = @"Errors/WebTransaction/WebService/BasicWebService.TestWebService.ThrowException", callCount = 2},
5959

60-
// other
61-
new Assertions.ExpectedMetric {metricName = @"WebTransaction/WebService/BasicWebService.TestWebService.ThrowException", callCount = 2},
60+
// other
61+
new Assertions.ExpectedMetric {metricName = @"WebTransaction/WebService/BasicWebService.TestWebService.ThrowException", callCount = 2},
6262
new Assertions.ExpectedMetric {metricName = @"DotNet/BasicWebService.TestWebService.ThrowException", callCount = 2},
6363
new Assertions.ExpectedMetric {metricName = @"DotNet/BasicWebService.TestWebService.ThrowException", metricScope = "WebTransaction/WebService/BasicWebService.TestWebService.ThrowException", callCount = 2}
6464
};
@@ -70,10 +70,16 @@ public void Test()
7070
new Assertions.ExpectedMetric { metricName = @"OtherTransaction/all" },
7171
};
7272

73-
var expectedAttributes = new Dictionary<string, string>
73+
var metrics = _fixture.AgentLog.GetMetrics().ToList();
74+
var errorTraces = _fixture.AgentLog.GetErrorTraces().ToList();
75+
var transactionEvents = _fixture.AgentLog.GetTransactionEvents().ToList();
76+
var errorEvents = _fixture.AgentLog.GetErrorEvents();
77+
78+
var expectedTransactonEventAttributes = new Dictionary<string, string>
7479
{
7580
{ "errorType", ExpectedExceptionType },
7681
{ "errorMessage", "Oh no!" },
82+
{ "error", "true" },
7783
};
7884

7985
var expectedErrorEventAttributes = new Dictionary<string, string>
@@ -82,11 +88,6 @@ public void Test()
8288
{ "error.message", "Oh no!" },
8389
};
8490

85-
var metrics = _fixture.AgentLog.GetMetrics().ToList();
86-
var errorTraces = _fixture.AgentLog.GetErrorTraces().ToList();
87-
var transactionEvents = _fixture.AgentLog.GetTransactionEvents().ToList();
88-
var errorEvents = _fixture.AgentLog.GetErrorEvents();
89-
9091
NrAssert.Multiple(
9192
() => Assertions.MetricsExist(expectedMetrics, metrics),
9293
() => Assertions.MetricsDoNotExist(unexpectedMetrics, metrics),
@@ -96,16 +97,19 @@ public void Test()
9697
() => Assert.Equal(ExpectedExceptionType, errorTraces[0].ExceptionClassName),
9798
() => Assert.Equal("Oh no!", errorTraces[0].Message),
9899
() => Assert.NotEmpty(errorTraces[0].Attributes.StackTrace),
100+
() => Assert.NotNull(errorTraces[0].Attributes.IntrinsicAttributes["guid"]),
99101
() => Assert.Equal("WebTransaction/WebService/BasicWebService.TestWebService.ThrowException", errorTraces[1].Path),
100102
() => Assert.Equal(ExpectedExceptionType, errorTraces[1].ExceptionClassName),
101103
() => Assert.Equal("Oh no!", errorTraces[1].Message),
102104
() => Assert.NotEmpty(errorTraces[1].Attributes.StackTrace),
105+
() => Assert.NotNull(errorTraces[1].Attributes.IntrinsicAttributes["guid"]),
103106
() => Assert.True(transactionEvents.Any(), "No transaction events found."),
104107
() => Assert.True(transactionEvents.Count == 2, $"Expected 2 transaction event but found {transactionEvents.Count}"),
105-
() => Assertions.TransactionEventHasAttributes(expectedAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[0]),
106-
() => Assertions.TransactionEventHasAttributes(expectedAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[1]),
108+
() => Assertions.TransactionEventHasAttributes(expectedTransactonEventAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[0]),
109+
() => Assertions.TransactionEventHasAttributes(expectedTransactonEventAttributes, TransactionEventAttributeType.Intrinsic, transactionEvents[1]),
107110
() => Assert.Equal(2, errorEvents.Count()),
108-
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvents.FirstOrDefault())
111+
() => Assertions.ErrorEventHasAttributes(expectedErrorEventAttributes, EventAttributeType.Intrinsic, errorEvents.FirstOrDefault()),
112+
() => Assert.NotNull(errorEvents.FirstOrDefault().IntrinsicAttributes["guid"])
109113
);
110114
}
111115
}

tests/Agent/IntegrationTests/IntegrationTests/Owin/OwinCATChainTests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ public void Test()
7979
"parent.account",
8080
"parent.transportType",
8181
"parent.transportDuration",
82-
"guid",
8382
"traceId",
8483
"priority",
8584
"sampled"

tests/Agent/UnitTests/Core.UnitTest/Transformers/TransactionTransformer/ErrorEventMakerTests.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ public void GetErrorEvent_InTransaction_IfStatusCodeIs404_ContainsCorrectAttribu
127127
NrAssert.Multiple(
128128
() => Assert.That(errorEvent.IsSynthetics, Is.EqualTo(false)),
129129
() => Assert.That(agentAttributes, Has.Length.EqualTo(7)),
130-
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(7)),
130+
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(8)),
131131
() => Assert.That(userAttributes, Is.Empty),
132132

133133
() => Assert.That(agentAttributes, Does.Contain("queue_wait_time_ms")),
@@ -143,6 +143,7 @@ public void GetErrorEvent_InTransaction_IfStatusCodeIs404_ContainsCorrectAttribu
143143
() => Assert.That(intrinsicAttributes, Does.Contain("error.message")),
144144
() => Assert.That(intrinsicAttributes, Does.Contain("queueDuration")),
145145
() => Assert.That(intrinsicAttributes, Does.Contain("transactionName")),
146+
() => Assert.That(intrinsicAttributes, Does.Contain("guid")),
146147
() => Assert.That(intrinsicAttributes, Does.Contain("timestamp")),
147148
() => Assert.That(intrinsicAttributes, Does.Contain("type"))
148149
);
@@ -170,7 +171,7 @@ public void GetErrorEvent_InTransaction_WithException_ContainsCorrectAttributes(
170171
NrAssert.Multiple(
171172
() => Assert.That(errorEvent.IsSynthetics, Is.EqualTo(false)),
172173
() => Assert.That(agentAttributes, Has.Length.EqualTo(7)),
173-
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(7)),
174+
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(8)),
174175
() => Assert.That(userAttributes, Is.Empty),
175176

176177
() => Assert.That(agentAttributes, Does.Contain("queue_wait_time_ms")),
@@ -186,6 +187,7 @@ public void GetErrorEvent_InTransaction_WithException_ContainsCorrectAttributes(
186187
() => Assert.That(intrinsicAttributes, Does.Contain("error.message")),
187188
() => Assert.That(intrinsicAttributes, Does.Contain("queueDuration")),
188189
() => Assert.That(intrinsicAttributes, Does.Contain("transactionName")),
190+
() => Assert.That(intrinsicAttributes, Does.Contain("guid")),
189191
() => Assert.That(intrinsicAttributes, Does.Contain("timestamp")),
190192
() => Assert.That(intrinsicAttributes, Does.Contain("type"))
191193
);
@@ -222,7 +224,7 @@ public void GetErrorEvent_InTransaction_WithException_ContainsCorrectAttributes_
222224
() => Assert.That(errorEvent.IsSynthetics, Is.EqualTo(true)),
223225

224226
() => Assert.That(agentAttributes, Has.Length.EqualTo(7)),
225-
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(16)),
227+
() => Assert.That(intrinsicAttributes, Has.Length.EqualTo(17)),
226228
() => Assert.That(userAttributes, Has.Length.EqualTo(1)),
227229

228230
() => Assert.That(agentAttributes, Does.Contain("queue_wait_time_ms")),
@@ -238,6 +240,7 @@ public void GetErrorEvent_InTransaction_WithException_ContainsCorrectAttributes_
238240
() => Assert.That(intrinsicAttributes, Does.Contain("error.message")),
239241
() => Assert.That(intrinsicAttributes, Does.Contain("queueDuration")),
240242
() => Assert.That(intrinsicAttributes, Does.Contain("transactionName")),
243+
() => Assert.That(intrinsicAttributes, Does.Contain("guid")),
241244
() => Assert.That(intrinsicAttributes, Does.Contain("timestamp")),
242245
() => Assert.That(intrinsicAttributes, Does.Contain("type")),
243246
() => Assert.That(intrinsicAttributes, Does.Contain("nr.syntheticsJobId")),

0 commit comments

Comments
 (0)