-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathPrefixOrAPM.cs
More file actions
126 lines (109 loc) · 4.22 KB
/
Copy pathPrefixOrAPM.cs
File metadata and controls
126 lines (109 loc) · 4.22 KB
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
using System;
using System.Collections.Generic;
//using System.Data.SqlTypes;
using System.Diagnostics;
using System.Linq;
using System.Text;
namespace StackifyLib.Utils
{
/*
* Used to identify if the current app is running on the same computer as Stackify Prefix or APM
*/
internal class PrefixOrAPM
{
internal enum ProfilerType
{
None,
Prefix,
APM
}
static DateTime _LastCheck = DateTime.MinValue;
static ProfilerType _LastProfilerType = ProfilerType.None;
private static bool _ScanProcessException = false;
internal static ProfilerType GetProfilerType()
{
if (_LastCheck > DateTime.UtcNow.AddMinutes(-1))
{
return _LastProfilerType;
}
_LastCheck = DateTime.UtcNow;
bool foundProcess = false;
string instanceID = Left(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"), 6);
string siteName = (Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? "").TrimStart('~', '1');
//is azure app service?
if (!string.IsNullOrEmpty(instanceID) && !string.IsNullOrEmpty(siteName))
{
_LastProfilerType = ProfilerType.APM;
return _LastProfilerType;
}
if (!_ScanProcessException)
{
try
{
foreach (var process in Process.GetProcesses().ToList())
{
if (process.Id <= 0)
continue;
try
{
switch (process?.ProcessName?.ToLower().Replace(".vshost", ""))
{
case "devdashservice":
case "stackifytracerservice":
case "stackifytracernotifier":
case "devdashtestconsole":
_LastProfilerType = ProfilerType.Prefix;
foundProcess = true;
break;
case "stackifymonitoringservice":
case "monitortestconsole":
if(_LastProfilerType != ProfilerType.Prefix)
_LastProfilerType = ProfilerType.APM;
foundProcess = true;
break;
}
}
catch
{
_ScanProcessException = true;
}
}
}
catch
{
_ScanProcessException = true;
}
}
//fall back to see if this has been set
if (!foundProcess && _ScanProcessException)
{
var stackifyPath = Environment.GetEnvironmentVariable("StackifyPath");
if (!string.IsNullOrEmpty(stackifyPath) && (stackifyPath.IndexOf("prefix", StringComparison.CurrentCultureIgnoreCase) > -1 || stackifyPath.IndexOf("devdash", StringComparison.CurrentCultureIgnoreCase) > -1))
{
_LastProfilerType = ProfilerType.Prefix;
}
else if (!string.IsNullOrEmpty(stackifyPath))
{
_LastProfilerType = ProfilerType.APM;
}
}
return _LastProfilerType;
}
private static string Left(string sValue, int iMaxLength)
{
//Check if the value is valid
if (string.IsNullOrEmpty(sValue))
{
//Set valid empty string as string could be null
sValue = string.Empty;
}
else if (sValue.Length > iMaxLength)
{
//Make the string no longer than the max length
sValue = sValue.Substring(0, iMaxLength);
}
//Return the string
return sValue;
}
}
}