/
AgentInstallConfiguration.cs
219 lines (198 loc) · 8.06 KB
/
AgentInstallConfiguration.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
// Copyright 2020 New Relic, Inc. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
using System;
using System.IO;
using System.Linq;
using NewRelic.Agent.Core.AgentHealth;
using NewRelic.Core;
using NewRelic.SystemInterfaces;
using Newtonsoft.Json;
using System.Reflection;
using System.Diagnostics;
using NewRelic.Core.Logging;
#if NETFRAMEWORK
using System.Web;
using Microsoft.Win32;
#endif
namespace NewRelic.Agent.Core
{
public static partial class AgentInstallConfiguration
{
#if NETSTANDARD2_0
private const string NewRelicHomeEnvironmentVariable = "CORECLR_NEWRELIC_HOME";
private const string RuntimeDirectoryName = "netcore";
#else
private const string NewRelicHomeEnvironmentVariable = "NEWRELIC_HOME";
private const string RuntimeDirectoryName = "netframework";
#endif
private const string NewRelicInstallPathEnvironmentVariable = "NEWRELIC_INSTALL_PATH";
private const string NewRelicLogDirectoryEnvironmentVariable = "NEWRELIC_LOG_DIRECTORY";
private const string NewRelicLogLevelEnvironmentVariable = "NEWRELIC_LOG_LEVEL";
public static bool IsWindows { get; }
#if NETFRAMEWORK
public static DotnetFrameworkVersion DotnetFrameworkVersion { get; }
#else
public static DotnetCoreVersion DotnetCoreVersion { get; }
#endif
public static bool IsNetstandardPresent { get; }
public static bool IsNet46OrAbove { get; }
public static bool IsNetCore30OrAbove { get; }
public static string NewRelicHome { get; }
public static string NewRelicInstallPath { get; }
public static string NewRelicLogDirectory { get; }
public static string NewRelicLogLevel { get; }
public static string HomeExtensionsDirectory { get; }
public static string RuntimeHomeExtensionsDirectory { get; }
public static string InstallPathExtensionsDirectory { get; }
public static int ProcessId { get; }
public static string AppDomainName { get; }
public static string AppDomainAppVirtualPath { get; }
public static AgentInfo AgentInfo { get; }
public static string AgentVersion { get; }
public static long AgentVersionTimestamp { get; }
static AgentInstallConfiguration()
{
#if NETFRAMEWORK
IsWindows = true;
#else
IsWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
#endif
NewRelicHome = GetNewRelicHome();
NewRelicInstallPath = GetNewRelicInstallPath();
NewRelicLogDirectory = GetNewRelicLogDirectory();
NewRelicLogLevel = GetNewRelicLogLevel();
HomeExtensionsDirectory = NewRelicHome != null ? Path.Combine(NewRelicHome, "extensions") : null;
RuntimeHomeExtensionsDirectory = HomeExtensionsDirectory != null ? Path.Combine(HomeExtensionsDirectory, RuntimeDirectoryName) : null;
InstallPathExtensionsDirectory = NewRelicInstallPath != null ? Path.Combine(NewRelicInstallPath, "extensions") : null;
AgentVersion = GetAgentVersion();
AgentVersionTimestamp = GetAgentVersionTimestamp();
IsNetstandardPresent = GetIsNetstandardPresent();
IsNet46OrAbove = GetIsNet46OrAbove();
IsNetCore30OrAbove = GetIsNetCore30OrAbove();
ProcessId = new ProcessStatic().GetCurrentProcess().Id;
AppDomainName = AppDomain.CurrentDomain.FriendlyName;
#if NETFRAMEWORK
if (HttpRuntime.AppDomainAppVirtualPath != null)
{
AppDomainAppVirtualPath = HttpRuntime.AppDomainAppVirtualPath;
}
try
{
DotnetFrameworkVersion = DotnetVersion.GetDotnetFrameworkVersion();
}
catch { }
#else
try
{
DotnetCoreVersion = DotnetVersion.GetDotnetCoreVersion();
}
catch { }
#endif
AgentInfo = GetAgentInfo();
}
//This method and delegate is here to allow dependency injecting the IsWindows logic via a typed delegate
public delegate bool IsWindowsDelegate();
public static bool GetIsWindows()
{
return IsWindows;
}
private static string GetAgentVersion()
{
try
{
return FileVersionInfo.GetVersionInfo(typeof(AgentInstallConfiguration).Assembly.Location).FileVersion;
}
catch (Exception ex)
{
Log.Error(ex, "Failed to determine agent version.");
return "?.?.?.?";
}
}
private static long GetAgentVersionTimestamp()
{
try
{
return Assembly.GetExecutingAssembly().GetCustomAttribute<BuildTimestampAttribute>().BuildTimestamp.Value;
}
catch
{
return 0;
}
}
private static bool GetIsNetstandardPresent()
{
var netstandard20Version = new Version(2, 0, 0, 0);
var netstandardAssembly = AppDomain.CurrentDomain.GetAssemblies().FirstOrDefault(assembly => assembly.GetName().Name == "netstandard");
return netstandardAssembly != null && netstandardAssembly.GetName().Version >= netstandard20Version;
}
private static bool GetIsNet46OrAbove()
{
#if NETFRAMEWORK
var net46Version = new Version(4, 0, 30319, 42000);
return System.Environment.Version >= net46Version;
#else
return true;
#endif
}
private static bool GetIsNetCore30OrAbove()
{
#if NETFRAMEWORK
return false;
#else
var version = System.Environment.Version;
// Prior to .NET Core 3.0 System.Environment.Version returned 4.0.30319.42000.
// Therefore, the next major version beyond 3.0 will be 5.0.
// See: https://docs.microsoft.com/en-us/dotnet/core/whats-new/dotnet-core-3-0#improved-net-core-version-apis
if (version.Major == 4) return false;
return version.Major >= 3;
#endif
}
private static string GetNewRelicHome()
{
var newRelicHome = System.Environment.GetEnvironmentVariable(NewRelicHomeEnvironmentVariable);
if (newRelicHome != null && Directory.Exists(newRelicHome)) return Path.GetFullPath(newRelicHome);
#if NETFRAMEWORK
var key = Registry.LocalMachine.OpenSubKey(@"Software\New Relic\.NET Agent");
if (key != null) newRelicHome = (string)key.GetValue("NewRelicHome");
#endif
return newRelicHome;
}
private static string GetNewRelicInstallPath()
{
var newRelicInstallPath = System.Environment.GetEnvironmentVariable(NewRelicInstallPathEnvironmentVariable);
if (newRelicInstallPath != null)
{
newRelicInstallPath = Path.Combine(newRelicInstallPath, RuntimeDirectoryName);
if (Directory.Exists(newRelicInstallPath)) return newRelicInstallPath;
}
newRelicInstallPath = System.Environment.GetEnvironmentVariable(NewRelicHomeEnvironmentVariable);
return newRelicInstallPath;
}
private static string GetNewRelicLogDirectory()
{
var newRelicLogDirectory = System.Environment.GetEnvironmentVariable(NewRelicLogDirectoryEnvironmentVariable);
if (newRelicLogDirectory != null && Directory.Exists(newRelicLogDirectory)) return Path.GetFullPath(newRelicLogDirectory);
return newRelicLogDirectory;
}
private static string GetNewRelicLogLevel()
{
return System.Environment.GetEnvironmentVariable(NewRelicLogLevelEnvironmentVariable);
}
private static AgentInfo GetAgentInfo()
{
var agentInfoPath = $@"{NewRelicHome}\agentinfo.json";
if (File.Exists(agentInfoPath))
{
try
{
return JsonConvert.DeserializeObject<AgentInfo>(File.ReadAllText(agentInfoPath));
}
catch
{
// Fail silently
}
}
return null;
}
}
}