-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathProgram.cs
115 lines (95 loc) · 3.82 KB
/
Program.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
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
namespace AspNetCoreSmokeTest
{
public class Program
{
public static volatile int ExitCode = 0;
public static async Task<int> Main(string[] args)
{
var profilerNotRequired = Environment.GetEnvironmentVariable("PROFILER_IS_NOT_REQUIRED");
if (!IsProfilerAttached() && profilerNotRequired != "True")
{
Console.WriteLine($"Error: Profiler is required and is not loaded. PROFILER_IS_NOT_REQUIRED={profilerNotRequired}.");
return 1;
}
if(Environment.GetEnvironmentVariable("CRASH_APP_ON_STARTUP") == "1")
{
throw new BadImageFormatException("Expected");
}
Console.WriteLine("Process details: ");
Console.WriteLine($"Framework: {RuntimeInformation.FrameworkDescription}");
Console.WriteLine($"Process arch: {RuntimeInformation.ProcessArchitecture}");
Console.WriteLine($"OS arch: {RuntimeInformation.OSArchitecture}");
Console.WriteLine($"OS description: {RuntimeInformation.OSDescription}");
await CreateHostBuilder(args).Build().RunAsync();
var completedPath = Path.Combine(
Environment.GetEnvironmentVariable("DD_TRACE_LOG_DIRECTORY") ?? ".",
"completed.txt");
File.WriteAllText(completedPath, DateTimeOffset.UtcNow.ToString());
return ExitCode;
}
#if NETCOREAPP3_0_OR_GREATER
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.ConfigureWebHostDefaults(webBuilder => webBuilder.Configure(Configure));
private static void ConfigureServices(IServiceCollection services)
{
services.AddControllers()
.AddApplicationPart(typeof(ValuesController).Assembly);
services.AddHostedService<Worker>();
}
private static void Configure(IApplicationBuilder app)
{
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
}
#else
public static IWebHostBuilder CreateHostBuilder(string[] args) =>
Microsoft.AspNetCore.WebHost.CreateDefaultBuilder(args)
.ConfigureServices(ConfigureServices)
.Configure(Configure);
private static void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
services.AddHostedService<Worker>();
}
private static void Configure(IApplicationBuilder app)
{
app.UseMvc();
}
#endif
private static readonly Type NativeMethodsType = Type.GetType("Datadog.Trace.ClrProfiler.NativeMethods, Datadog.Trace");
public static bool IsProfilerAttached()
{
if(NativeMethodsType is null)
{
return false;
}
try
{
MethodInfo profilerAttachedMethodInfo = NativeMethodsType.GetMethod("IsProfilerAttached");
return (bool)profilerAttachedMethodInfo.Invoke(null, null);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
return false;
}
public static string GetTracerAssemblyLocation()
{
return NativeMethodsType?.Assembly.Location ?? "(none)";
}
}
}