Skip to content

Commit 8cc3dd2

Browse files
committed
refactor code
1 parent 2800371 commit 8cc3dd2

File tree

4 files changed

+445
-298
lines changed

4 files changed

+445
-298
lines changed

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MassTransit/FilterInjection/AddMassTransitIntegration.cs

Lines changed: 14 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77

88
using System;
99
using System.ComponentModel;
10-
using System.Reflection;
1110
using Datadog.Trace.ClrProfiler.CallTarget;
1211
using Datadog.Trace.Configuration;
13-
using Datadog.Trace.DuckTyping;
1412
using Datadog.Trace.Logging;
1513

1614
namespace Datadog.Trace.ClrProfiler.AutoInstrumentation.MassTransit.FilterInjection;
@@ -93,79 +91,43 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
9391

9492
var collectionType = collection.GetType();
9593

96-
// Find the MassTransit and DI assemblies
97-
Assembly? massTransitAssembly = null;
98-
Assembly? diAssembly = null;
99-
100-
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
101-
{
102-
var assemblyName = assembly.GetName().Name;
103-
if (assemblyName == "MassTransit")
104-
{
105-
massTransitAssembly = assembly;
106-
}
107-
else if (assemblyName == "Microsoft.Extensions.DependencyInjection.Abstractions")
108-
{
109-
diAssembly = assembly;
110-
}
111-
112-
if (massTransitAssembly != null && diAssembly != null)
113-
{
114-
break;
115-
}
116-
}
117-
118-
if (massTransitAssembly == null)
119-
{
120-
Log.Debug("MassTransit AddMassTransitIntegration: Could not find MassTransit assembly");
121-
return;
122-
}
123-
124-
var configureReceiveEndpointType = massTransitAssembly.GetType("MassTransit.IConfigureReceiveEndpoint");
94+
// Get IConfigureReceiveEndpoint type from MassTransit assembly
95+
var configureReceiveEndpointType = MassTransitCommon.GetConfigureReceiveEndpointType();
12596
if (configureReceiveEndpointType == null)
12697
{
12798
Log.Debug("MassTransit AddMassTransitIntegration: Could not find IConfigureReceiveEndpoint type");
12899
return;
129100
}
130101

131102
// Create a reverse duck type instance that implements IConfigureReceiveEndpoint
132-
var datadogImpl = new DatadogConfigureReceiveEndpoint();
133-
var datadogProxy = DuckType.CreateReverse(configureReceiveEndpointType, datadogImpl);
134-
103+
var datadogProxy = MassTransitCommon.CreateConfigureReceiveEndpointProxy(new DatadogConfigureReceiveEndpoint());
135104
if (datadogProxy == null)
136105
{
137106
Log.Debug("MassTransit AddMassTransitIntegration: Could not create reverse duck type for IConfigureReceiveEndpoint");
138107
return;
139108
}
140109

141-
if (diAssembly == null)
142-
{
143-
Log.Debug("MassTransit AddMassTransitIntegration: Could not find DI Abstractions assembly");
144-
return;
145-
}
146-
147-
// Find ServiceDescriptor type
148-
var serviceDescriptorType = diAssembly.GetType("Microsoft.Extensions.DependencyInjection.ServiceDescriptor");
110+
// Find ServiceDescriptor and ServiceLifetime types from DI assembly
111+
var serviceDescriptorType = MassTransitCommon.GetServiceDescriptorType();
149112
if (serviceDescriptorType == null)
150113
{
151114
Log.Debug("MassTransit AddMassTransitIntegration: Could not find ServiceDescriptor type");
152115
return;
153116
}
154117

155-
// Find ServiceLifetime enum
156-
var serviceLifetimeType = diAssembly.GetType("Microsoft.Extensions.DependencyInjection.ServiceLifetime");
118+
var serviceLifetimeType = MassTransitCommon.GetServiceLifetimeType();
157119
if (serviceLifetimeType == null)
158120
{
159121
Log.Debug("MassTransit AddMassTransitIntegration: Could not find ServiceLifetime type");
160122
return;
161123
}
162124

163125
// Create ServiceDescriptor for our implementation (Scoped lifetime)
164-
// ServiceDescriptor.Describe(typeof(IConfigureReceiveEndpoint), _ => datadogProxy, ServiceLifetime.Scoped)
165126
var scopedLifetime = Enum.ToObject(serviceLifetimeType, 1); // ServiceLifetime.Scoped = 1
166127

167128
// Create a factory func that returns our proxy instance
168-
var serviceProviderType = diAssembly.GetType("System.IServiceProvider") ?? typeof(IServiceProvider);
129+
var diAssembly = MassTransitCommon.GetDiAbstractionsAssembly();
130+
var serviceProviderType = diAssembly?.GetType("System.IServiceProvider") ?? typeof(IServiceProvider);
169131
var funcType = typeof(Func<,>).MakeGenericType(serviceProviderType, configureReceiveEndpointType);
170132

171133
// Create a delegate that returns our singleton proxy
@@ -182,11 +144,11 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
182144
var factory = factoryMethod.Invoke(null, new[] { datadogProxy });
183145

184146
// Find the ServiceDescriptor constructor: ServiceDescriptor(Type serviceType, Func<IServiceProvider, object> factory, ServiceLifetime lifetime)
185-
var descriptorCtor = serviceDescriptorType.GetConstructor(new[] { typeof(Type), funcType, serviceLifetimeType });
147+
var descriptorCtor = serviceDescriptorType.GetConstructor([typeof(Type), funcType, serviceLifetimeType]);
186148
if (descriptorCtor == null)
187149
{
188150
// Try the simpler overload
189-
descriptorCtor = serviceDescriptorType.GetConstructor(new[] { typeof(Type), typeof(object), serviceLifetimeType });
151+
descriptorCtor = serviceDescriptorType.GetConstructor([typeof(Type), typeof(object), serviceLifetimeType]);
190152
}
191153

192154
if (descriptorCtor == null)
@@ -195,11 +157,11 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
195157
return;
196158
}
197159

198-
var descriptor = descriptorCtor.Invoke(new[] { configureReceiveEndpointType, factory, scopedLifetime });
160+
var descriptor = descriptorCtor.Invoke([configureReceiveEndpointType, factory, scopedLifetime]);
199161

200162
// Add the descriptor to the collection
201163
// collection.Add(descriptor)
202-
var addMethod = collectionType.GetMethod("Add", new[] { serviceDescriptorType });
164+
var addMethod = collectionType.GetMethod("Add", [serviceDescriptorType]);
203165
if (addMethod == null)
204166
{
205167
// Try ICollection<ServiceDescriptor>.Add
@@ -209,8 +171,8 @@ private static void RegisterDatadogConfigureReceiveEndpoint(object collection)
209171

210172
if (addMethod != null)
211173
{
212-
addMethod.Invoke(collection, new[] { descriptor });
213-
Log.Information("MassTransit AddMassTransitIntegration: Successfully registered DatadogConfigureReceiveEndpoint");
174+
addMethod.Invoke(collection, [descriptor]);
175+
Log.Debug("MassTransit AddMassTransitIntegration: Successfully registered DatadogConfigureReceiveEndpoint");
214176
}
215177
else
216178
{

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/MassTransit/FilterInjection/DatadogConfigureReceiveEndpoint.cs

Lines changed: 20 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#nullable enable
77

88
using System;
9-
using System.Reflection;
109
using Datadog.Trace.DuckTyping;
1110
using Datadog.Trace.Logging;
1211

@@ -36,122 +35,35 @@ public void Configure(string name, object configurator)
3635
return;
3736
}
3837

39-
try
40-
{
41-
Log.Debug("DatadogConfigureReceiveEndpoint: Configuring endpoint {EndpointName} with {ConfiguratorType}", name, configurator.GetType().FullName);
38+
Log.Debug("DatadogConfigureReceiveEndpoint: Configuring endpoint {EndpointName} with {ConfiguratorType}", name, configurator.GetType().FullName);
4239

43-
// Inject consume filter via IConsumePipeConfigurator.AddPipeSpecification
44-
InjectConsumeFilter(configurator);
40+
// Inject consume filter via IConsumePipeConfigurator.AddPipeSpecification
41+
InjectConsumeFilter(configurator);
4542

46-
// Note: Send and Publish filters cannot be injected at the receive endpoint level because
47-
// IReceiveEndpointConfigurator doesn't expose AddPipeSpecification for SendContext/PublishContext.
48-
// However, publish operations are still captured by the underlying transport instrumentation
49-
// (e.g., RabbitMQ's basic.publish). For MassTransit-specific publish spans, we would need to
50-
// hook at the bus level via IBusControl or IPublishEndpoint.Publish.
51-
}
52-
catch (Exception ex)
53-
{
54-
Log.Warning(ex, "DatadogConfigureReceiveEndpoint: Failed to configure endpoint {EndpointName}", name);
55-
}
43+
// Note: Send and Publish filters cannot be injected at the receive endpoint level because
44+
// IReceiveEndpointConfigurator doesn't expose AddPipeSpecification for SendContext/PublishContext.
45+
// However, publish operations are still captured by the underlying transport instrumentation
46+
// (e.g., RabbitMQ's basic.publish). For MassTransit-specific publish spans, we would need to
47+
// hook at the bus level via IBusControl or IPublishEndpoint.Publish.
5648
}
5749

5850
private static void InjectConsumeFilter(object configurator)
5951
{
60-
try
52+
var filterSpec = MassTransitCommon.CreatePipeSpecificationProxy(new DatadogConsumePipeSpecification());
53+
if (filterSpec == null)
6154
{
62-
var configuratorType = configurator.GetType();
63-
64-
// Find MassTransit assembly
65-
Assembly? massTransitAssembly = null;
66-
Assembly? greenPipesAssembly = null;
67-
68-
foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
69-
{
70-
var assemblyName = assembly.GetName().Name;
71-
if (assemblyName == "MassTransit")
72-
{
73-
massTransitAssembly = assembly;
74-
}
75-
else if (assemblyName == "GreenPipes")
76-
{
77-
greenPipesAssembly = assembly;
78-
}
79-
80-
if (massTransitAssembly != null && greenPipesAssembly != null)
81-
{
82-
break;
83-
}
84-
}
85-
86-
if (massTransitAssembly == null)
87-
{
88-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find MassTransit assembly");
89-
return;
90-
}
91-
92-
if (greenPipesAssembly == null)
93-
{
94-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find GreenPipes assembly");
95-
return;
96-
}
97-
98-
// Get ConsumeContext type
99-
var consumeContextType = massTransitAssembly.GetType("MassTransit.ConsumeContext");
100-
if (consumeContextType == null)
101-
{
102-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find ConsumeContext type");
103-
return;
104-
}
105-
106-
// Get IPipeSpecification<ConsumeContext> type
107-
var pipeSpecOpenType = greenPipesAssembly.GetType("GreenPipes.IPipeSpecification`1");
108-
if (pipeSpecOpenType == null)
109-
{
110-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find IPipeSpecification<> type");
111-
return;
112-
}
113-
114-
var pipeSpecType = pipeSpecOpenType.MakeGenericType(consumeContextType);
115-
116-
// Create our filter specification via reverse duck typing
117-
var filterSpecImpl = new DatadogConsumePipeSpecification();
118-
var filterSpec = DuckType.CreateReverse(pipeSpecType, filterSpecImpl);
119-
120-
if (filterSpec == null)
121-
{
122-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not create reverse duck type for IPipeSpecification<ConsumeContext>");
123-
return;
124-
}
125-
126-
// Find the AddPipeSpecification method on the configurator
127-
// IConsumePipeConfigurator.AddPipeSpecification(IPipeSpecification<ConsumeContext>)
128-
var addPipeSpecMethod = configuratorType.GetMethod("AddPipeSpecification", new[] { pipeSpecType });
129-
130-
if (addPipeSpecMethod == null)
131-
{
132-
// Try finding the method on interfaces
133-
foreach (var iface in configuratorType.GetInterfaces())
134-
{
135-
addPipeSpecMethod = iface.GetMethod("AddPipeSpecification", new[] { pipeSpecType });
136-
if (addPipeSpecMethod != null)
137-
{
138-
break;
139-
}
140-
}
141-
}
142-
143-
if (addPipeSpecMethod == null)
144-
{
145-
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find AddPipeSpecification method");
146-
return;
147-
}
148-
149-
addPipeSpecMethod.Invoke(configurator, new[] { filterSpec });
150-
Log.Debug("DatadogConfigureReceiveEndpoint: Successfully injected consume filter specification");
55+
Log.Debug("DatadogConfigureReceiveEndpoint: Could not create pipe specification proxy");
56+
return;
15157
}
152-
catch (Exception ex)
58+
59+
var addPipeSpecMethod = MassTransitCommon.FindAddPipeSpecificationMethod(configurator.GetType());
60+
if (addPipeSpecMethod == null)
15361
{
154-
Log.Debug(ex, "DatadogConfigureReceiveEndpoint: Failed to inject consume filter");
62+
Log.Debug("DatadogConfigureReceiveEndpoint: Could not find AddPipeSpecification method");
63+
return;
15564
}
65+
66+
addPipeSpecMethod.Invoke(configurator, new[] { filterSpec });
67+
Log.Debug("DatadogConfigureReceiveEndpoint: Successfully injected consume filter specification");
15668
}
15769
}

0 commit comments

Comments
 (0)