Skip to content

Commit e2100ac

Browse files
committed
chore: Add defensive exception handling to MassTransitHelpers
Make MassTransitHelpers a static class and wrap URI parsing in GetQueueDataFromUri with a try-catch to prevent exceptions from propagating to callers (e.g. relative URIs, malformed percent-encoding). On any failure, returns default MassTransitQueueData with "Unknown" queue name.
1 parent 62bdb44 commit e2100ac

1 file changed

Lines changed: 70 additions & 63 deletions

File tree

  • src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Helpers

src/Agent/NewRelic/Agent/Extensions/NewRelic.Agent.Extensions/Helpers/MassTransit.cs

Lines changed: 70 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class MassTransitQueueData
1313
public MessageBrokerDestinationType DestinationType { get; set; } = MessageBrokerDestinationType.Queue;
1414
}
1515

16-
public class MassTransitHelpers
16+
public static class MassTransitHelpers
1717
{
1818
public static MassTransitQueueData GetQueueData(Uri sourceAddress, Uri fallbackAddress = null)
1919
{
@@ -36,74 +36,81 @@ private static MassTransitQueueData GetQueueDataFromUri(Uri sourceAddress)
3636
if (sourceAddress == null)
3737
return data;
3838

39-
var scheme = sourceAddress.Scheme.ToLowerInvariant();
40-
41-
// Short-form addressing schemes used by any transport: queue://, topic://, exchange://
42-
switch (scheme)
39+
try
4340
{
44-
case "topic":
45-
data.QueueName = GetLastPathSegment(sourceAddress);
46-
data.DestinationType = MessageBrokerDestinationType.Topic;
47-
return data;
48-
case "queue":
49-
data.QueueName = GetLastPathSegment(sourceAddress);
50-
data.DestinationType = MessageBrokerDestinationType.Queue;
51-
return data;
52-
case "exchange":
53-
data.QueueName = GetLastPathSegment(sourceAddress);
54-
data.DestinationType = MessageBrokerDestinationType.Queue;
41+
var scheme = sourceAddress.Scheme.ToLowerInvariant();
42+
43+
// Short-form addressing schemes used by any transport: queue://, topic://, exchange://
44+
switch (scheme)
45+
{
46+
case "topic":
47+
data.QueueName = GetLastPathSegment(sourceAddress);
48+
data.DestinationType = MessageBrokerDestinationType.Topic;
49+
return data;
50+
case "queue":
51+
data.QueueName = GetLastPathSegment(sourceAddress);
52+
data.DestinationType = MessageBrokerDestinationType.Queue;
53+
return data;
54+
case "exchange":
55+
data.QueueName = GetLastPathSegment(sourceAddress);
56+
data.DestinationType = MessageBrokerDestinationType.Queue;
57+
return data;
58+
}
59+
60+
// MassTransit Rider embeds path prefixes (e.g. /kafka/, /event-hub/) regardless
61+
// of the bus transport scheme. Check for these before scheme-specific parsing.
62+
if (TryParseRiderPrefix(sourceAddress, data))
5563
return data;
56-
}
5764

58-
// MassTransit Rider embeds path prefixes (e.g. /kafka/, /event-hub/) regardless
59-
// of the bus transport scheme. Check for these before scheme-specific parsing.
60-
if (TryParseRiderPrefix(sourceAddress, data))
61-
return data;
65+
// Transport-specific parsing by URI scheme
66+
switch (scheme)
67+
{
68+
case "kafka":
69+
data.QueueName = GetLastPathSegment(sourceAddress);
70+
data.DestinationType = MessageBrokerDestinationType.Topic;
71+
break;
72+
73+
case "sb":
74+
ParseServiceBusUri(sourceAddress, data);
75+
break;
76+
77+
case "amazonsqs":
78+
data.QueueName = GetLastPathSegment(sourceAddress);
79+
data.DestinationType = GetDestinationTypeFromQueryParam(sourceAddress);
80+
if (data.DestinationType == MessageBrokerDestinationType.Queue && HasQueryParam(sourceAddress, "temporary", "true"))
81+
data.DestinationType = MessageBrokerDestinationType.TempQueue;
82+
break;
83+
84+
case "activemq":
85+
case "amqp":
86+
case "amqps":
87+
data.QueueName = GetLastPathSegment(sourceAddress);
88+
data.DestinationType = GetDestinationTypeFromQueryParam(sourceAddress);
89+
if (data.DestinationType == MessageBrokerDestinationType.Queue && HasQueryParam(sourceAddress, "temporary", "true"))
90+
data.DestinationType = MessageBrokerDestinationType.TempQueue;
91+
break;
92+
93+
case "rabbitmq":
94+
case "rabbitmqs":
95+
ParseRabbitMqUri(sourceAddress, data);
96+
break;
97+
98+
case "loopback":
99+
data.QueueName = GetLastPathSegment(sourceAddress);
100+
break;
101+
102+
default:
103+
// Graceful fallback for unknown transports: try last path segment
104+
data.QueueName = GetLastPathSegment(sourceAddress);
105+
break;
106+
}
62107

63-
// Transport-specific parsing by URI scheme
64-
switch (scheme)
108+
return data;
109+
}
110+
catch
65111
{
66-
case "kafka":
67-
data.QueueName = GetLastPathSegment(sourceAddress);
68-
data.DestinationType = MessageBrokerDestinationType.Topic;
69-
break;
70-
71-
case "sb":
72-
ParseServiceBusUri(sourceAddress, data);
73-
break;
74-
75-
case "amazonsqs":
76-
data.QueueName = GetLastPathSegment(sourceAddress);
77-
data.DestinationType = GetDestinationTypeFromQueryParam(sourceAddress);
78-
if (data.DestinationType == MessageBrokerDestinationType.Queue && HasQueryParam(sourceAddress, "temporary", "true"))
79-
data.DestinationType = MessageBrokerDestinationType.TempQueue;
80-
break;
81-
82-
case "activemq":
83-
case "amqp":
84-
case "amqps":
85-
data.QueueName = GetLastPathSegment(sourceAddress);
86-
data.DestinationType = GetDestinationTypeFromQueryParam(sourceAddress);
87-
if (data.DestinationType == MessageBrokerDestinationType.Queue && HasQueryParam(sourceAddress, "temporary", "true"))
88-
data.DestinationType = MessageBrokerDestinationType.TempQueue;
89-
break;
90-
91-
case "rabbitmq":
92-
case "rabbitmqs":
93-
ParseRabbitMqUri(sourceAddress, data);
94-
break;
95-
96-
case "loopback":
97-
data.QueueName = GetLastPathSegment(sourceAddress);
98-
break;
99-
100-
default:
101-
// Graceful fallback for unknown transports: try last path segment
102-
data.QueueName = GetLastPathSegment(sourceAddress);
103-
break;
112+
return data;
104113
}
105-
106-
return data;
107114
}
108115

109116
private static bool TryParseRiderPrefix(Uri sourceAddress, MassTransitQueueData data)

0 commit comments

Comments
 (0)