forked from elastic/apm-agent-dotnet
/
HostBuilderExtensions.cs
106 lines (90 loc) · 3.99 KB
/
HostBuilderExtensions.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
using System;
using System.Linq;
using System.Reflection;
using Elastic.Apm.Api;
using Elastic.Apm.Config;
using Elastic.Apm.DiagnosticSource;
using Elastic.Apm.Extensions.Hosting.Config;
using Elastic.Apm.Logging;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace Elastic.Apm.Extensions.Hosting
{
public static class HostBuilderExtensions
{
/// <summary>
/// Register Elastic APM .NET Agent with components in the container.
/// You can customize the agent by passing additional IDiagnosticsSubscriber components to this method.
/// Use this method if you want to control what tracing capability of the agent you would like to use
/// or in case you want to minimize the number of dependencies added to your application.
/// If you want to simply enable every tracing component without configuration please use the
/// UseAllElasticApm extension method from the Elastic.Apm.NetCoreAll package.
/// </summary>
/// <param name="builder">Builder.</param>
/// <param name="subscribers">Specify which diagnostic source subscribers you want to connect.</param>
public static IHostBuilder UseElasticApm(this IHostBuilder builder, params IDiagnosticsSubscriber[] subscribers)
{
builder.ConfigureServices((ctx, services) =>
{
//If the static agent doesn't exist, we create one here. If there is already 1 agent created, we reuse it.
if (!Agent.IsConfigured)
{
services.AddSingleton<IApmLogger, NetCoreLogger>();
services.AddSingleton<IConfigurationReader>(sp =>
new MicrosoftExtensionsConfig(ctx.Configuration, sp.GetService<IApmLogger>(), ctx.HostingEnvironment.EnvironmentName));
}
else
{
services.AddSingleton(Agent.Instance.Logger);
services.AddSingleton(Agent.Instance.ConfigurationReader);
}
services.AddSingleton(sp =>
{
if (Agent.IsConfigured) return Agent.Components;
var logger = sp.GetService<IApmLogger>();
var configReader = sp.GetService<IConfigurationReader>();
var components = new AgentComponents(logger, configReader);
UpdateServiceInformation(components.Service);
return components;
});
services.AddSingleton<IApmAgent, ApmAgent>(sp =>
{
if (Agent.IsConfigured) return Agent.Instance;
var components = sp.GetService<AgentComponents>();
var newAgentInstance = new ApmAgent(components);
Agent.Setup(components);
return newAgentInstance;
});
services.AddSingleton(sp => sp.GetRequiredService<IApmAgent>().Tracer);
var serviceProvider = services.BuildServiceProvider();
var agent = serviceProvider.GetService<IApmAgent>();
if (!(agent is ApmAgent apmAgent)) return;
if (!Agent.IsConfigured || !apmAgent.ConfigurationReader.Enabled) return;
if (subscribers != null && subscribers.Any() && Agent.IsConfigured)
apmAgent.Subscribe(subscribers);
});
return builder;
}
internal static void UpdateServiceInformation(Service service)
{
var aspNetCoreVersion = GetAssemblyVersion("Microsoft.AspNetCore");
var hostingVersion = GetAssemblyVersion("Microsoft.Extensions.Hosting");
var version = aspNetCoreVersion ?? hostingVersion ?? "n/a";
service.Framework = new Framework { Name = aspNetCoreVersion != null ? "ASP.NET Core" : ".NET Core", Version = version };
service.Language = new Language { Name = "C#" }; //TODO
}
private static string GetAssemblyVersion(string assemblyName)
{
var versionQuery = AppDomain.CurrentDomain.GetAssemblies().Where(n => n.GetName().Name == assemblyName);
var assemblies = versionQuery as Assembly[] ?? versionQuery.ToArray();
if (assemblies.Any()) return assemblies.First().GetName().Version?.ToString();
versionQuery = AppDomain.CurrentDomain.GetAssemblies().Where(n =>
{
var name = n.GetName().Name;
return name != null && name.Contains(assemblyName);
});
var enumerable = versionQuery as Assembly[] ?? versionQuery.ToArray();
return enumerable.Any() ? enumerable.FirstOrDefault()?.GetName().Version?.ToString() : null;
}
}
}