-
Notifications
You must be signed in to change notification settings - Fork 55
/
ExtensionsLoader.cs
244 lines (194 loc) · 14.5 KB
/
ExtensionsLoader.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
using NewRelic.Agent.Extensions.Providers;
using NewRelic.Agent.Extensions.Providers.Wrapper;
using NewRelic.Core.CodeAttributes;
using NewRelic.Core.Logging;
using NewRelic.TypeInstantiation;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace NewRelic.Agent.Core.Utilities
{
[NrCoveredByIntegrationTests]
public class ExtensionsLoader
{
/// <summary>
/// The list of wrappers and the filepath of the assembly where they can be found
/// </summary>
private static Dictionary<string, string> _dynamicLoadWrapperAssemblies = new Dictionary<string, string>();
private static string _installPathExtensionsDirectory;
/// <summary>
/// These assemblies are automatically autoreflected upon agent startup.
/// </summary>
private static string[] _autoReflectedAssemblies;
public static void Initialize(string installPathExtensionsDirectory)
{
_installPathExtensionsDirectory = installPathExtensionsDirectory;
_dynamicLoadWrapperAssemblies = new Dictionary<string, string>() {
{ "BuildCommonServicesWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore.dll") },
{ "GenericHostWebHostBuilderExtensionsWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore.dll") },
{ "NewRelic.Providers.Wrapper.AspNetCore.InvokeActionMethodAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore.dll") },
{ "BuildCommonServicesWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "GenericHostWebHostBuilderExtensionsWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "InvokeActionMethodAsyncWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "ResponseCompressionBodyOnWriteWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "PageActionInvokeHandlerAsyncWrapper6Plus", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNetCore6Plus.dll") },
{ "ResolveAppWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Owin.dll") },
{ "AspNet.CreateEventExecutionStepsTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.ExecuteStepTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.FinishPipelineRequestTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.OnErrorTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.GetRouteDataTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.CallHandlerTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.FilterTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "AspNet.AspPagesTransactionNameTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.AspNet.dll") },
{ "OdbcCommandTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OleDbCommandTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "SqlCommandTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "SqlCommandTracerAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "SqlCommandWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "SqlCommandWrapperAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "DataReaderTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "DataReaderTracerAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "DataReaderWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "DataReaderWrapperAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionTracer", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionTracerAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
{ "OpenConnectionWrapperAsync", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Sql.dll") },
//The NewRelic.Providers.Wrapper.SerilogLogging.dll depends on the Serilog.dll; therefore, it should
//only be loaded by the agent when Serilog is used otherwise assembly load exception will occur.
{ "SerilogCreateLoggerWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.SerilogLogging.dll") },
{ "SerilogDispatchWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.SerilogLogging.dll") },
// Kafka
{ "KafkaProducerWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Kafka.dll") },
{ "KafkaSerializerWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Kafka.dll") },
{ "KafkaConsumerWrapper", Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Wrapper.Kafka.dll") }
};
var nonAutoReflectedAssemblies = _dynamicLoadWrapperAssemblies.Values.Distinct().ToList();
//Add this to the log.
nonAutoReflectedAssemblies.ForEach(x =>
{
Log.Info($"The following assembly will be loaded on-demand: {x}");
});
if (!AgentInstallConfiguration.IsNet46OrAbove)
{
var asmPath = Path.Combine(_installPathExtensionsDirectory, "NewRelic.Providers.Storage.AsyncLocal.dll");
Log.Info($"The following assembly is not applicable based on installed Framework: {asmPath}");
nonAutoReflectedAssemblies.Add(asmPath);
}
var assemblyFiles = GetAssemblyFilesFromFolder(_installPathExtensionsDirectory);
//remove assemblies that are not
_autoReflectedAssemblies = assemblyFiles
.Where(x => !nonAutoReflectedAssemblies.Contains(x, StringComparer.OrdinalIgnoreCase)).ToArray();
}
/// <summary>
/// A list of dynamically loaded assemblies and whether or not they've been loaded
/// </summary>
private static Dictionary<string, IWrapper[]> _dynamicLoadAssemblyStatus = new Dictionary<string, IWrapper[]>();
/// <summary>
/// Automatically inspect assemblies in the install path and load/instantiate all items of type T.
/// This method will exclude NotAutoReflected assemblies. These assemblies are only loaded on-demand so as
/// to avoid TypeLoad exceptions from missing types.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
private static IEnumerable<T> AutoLoadExtensions<T>()
{
Log.Info($"Loading extensions of type {typeof(T)} from folder: {_installPathExtensionsDirectory}");
var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths<T>(_autoReflectedAssemblies);
foreach (var ex in result.Exceptions)
{
Log.Warn(ex, "An exception occurred while loading an extension");
}
return result.Instances;
}
public static IEnumerable<IWrapper> LoadWrappers()
{
try
{
//Load all wrappers, except for the ones that are loaded upon their first usage (dynamic wrappers).
//This avoids TypeLoad exceptions for unsupported types contained in those wrappers
var wrappers = AutoLoadExtensions<IWrapper>()
.Where(wrapper => wrapper != null);
return wrappers;
}
catch (Exception ex)
{
Log.Error(ex, "Failed to load wrappers");
throw;
}
}
private static List<string> GetAssemblyFilesFromFolder(string folder)
{
if (folder == null || !Directory.Exists(folder))
{
return new List<string>();
}
var assemblyPaths = Directory.GetFiles(folder, "*.dll", SearchOption.TopDirectoryOnly);
return assemblyPaths.ToList();
}
/// <summary>
/// Loads all of the context storage factories.
/// </summary>
/// <returns></returns>
public static IEnumerable<IContextStorageFactory> LoadContextStorageFactories()
{
var contextStorageFactories = AutoLoadExtensions<IContextStorageFactory>().ToList();
if (contextStorageFactories.Count == 0)
{
Log.Warn("No context storage factories were loaded from the extensions directory.");
return new IContextStorageFactory[] { };
}
foreach (var factory in contextStorageFactories)
{
Log.Debug("Available storage type : {0} ({1})", factory.GetType().FullName, factory.IsValid);
}
return contextStorageFactories.Where(IsValid);
}
private static object _loadDynamicWrapperLockObj = new object();
public static IEnumerable<IWrapper> LoadDynamicWrapper(string assemblyPath)
{
lock (_loadDynamicWrapperLockObj)
{
if (!_dynamicLoadAssemblyStatus.TryGetValue(assemblyPath.ToLower(), out var wrappers))
{
var result = TypeInstantiator.ExportedInstancesFromAssemblyPaths<IWrapper>(assemblyPath);
foreach (var ex in result.Exceptions)
{
Log.Warn(ex, "An exception occurred while loading an extension from assembly {path}", assemblyPath);
}
wrappers = result.Instances.ToArray();
_dynamicLoadAssemblyStatus[assemblyPath.ToLower()] = wrappers;
if (Log.IsFinestEnabled)
{
Log.Finest($"Dynamically loaded wrappers from assembly {assemblyPath}: {string.Join(", ", (IEnumerable<IWrapper>)wrappers)}");
}
}
return wrappers;
}
}
public static IEnumerable<IWrapper> TryGetDynamicWrapperInstance(string requestedWrapperName)
{
if (!_dynamicLoadWrapperAssemblies.TryGetValue(requestedWrapperName, out var assemblyPath))
{
return Enumerable.Empty<IWrapper>();
}
return LoadDynamicWrapper(assemblyPath);
}
private static bool IsValid(IContextStorageFactory factory)
{
try
{
return factory != null && factory.IsValid;
}
catch (Exception)
{
// REVIEW maybe log at finest?
return false;
}
}
}
}