Skip to content

Commit 4d6a11a

Browse files
author
mwatson
committed
Change how we evaluate if prefix or apm
1 parent 9780e5a commit 4d6a11a

File tree

3 files changed

+100
-36
lines changed

3 files changed

+100
-36
lines changed

Src/StackifyLib/Logger.cs

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -304,45 +304,16 @@ public static List<TraceFrame> GetCurrentStackTrace(string declaringClassName, i
304304
return frames;
305305
}
306306

307-
private static bool? _PrefixEnabled = null;
308307

309308
public static bool PrefixEnabled()
310309
{
311-
if (_PrefixEnabled != null)
312-
return _PrefixEnabled.Value;
313-
314-
var variable = Environment.GetEnvironmentVariable("StackSquatchUpdated", EnvironmentVariableTarget.Machine);
315-
316-
317-
if (!string.IsNullOrEmpty(variable))
318-
{
319-
320-
DateTime updated;
321-
322-
if (DateTime.TryParse(variable, out updated))
323-
{
324-
if (updated > DateTime.UtcNow.AddHours(-24))
325-
{
326-
StackifyLib.Utils.StackifyAPILogger.Log("Prefix enabled", true);
327-
_PrefixEnabled = true;
328-
}
329-
else
330-
{
331-
_PrefixEnabled = false;
332-
}
333-
}
334-
else
335-
{
336-
_PrefixEnabled = false;
337-
}
338-
339-
}
340-
else
341-
{
342-
_PrefixEnabled = false;
343-
}
344-
345-
return _PrefixEnabled.Value;
310+
return StackifyLib.Utils.PrefixOrAPM.GetProfilerType() == PrefixOrAPM.ProfilerType.Prefix;
346311
}
312+
313+
/// <summary>
314+
/// Scans to see if Prefix is running
315+
/// </summary>
316+
/// <returns></returns>
317+
347318
}
348319
}

Src/StackifyLib/StackifyLib.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Compile Include="Utils\ErrorGovernor.cs" />
7878
<Compile Include="Utils\HelperFunctions.cs" />
7979
<Compile Include="Utils\HttpClient.cs" />
80+
<Compile Include="Utils\PrefixOrAPM.cs" />
8081
<Compile Include="Utils\RegistryHelper.cs" />
8182
<Compile Include="Utils\SequentialGuid.cs" />
8283
<Compile Include="Utils\StackifyAPILogger.cs" />
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data.SqlTypes;
4+
using System.Diagnostics;
5+
using System.Linq;
6+
using System.Text;
7+
8+
namespace StackifyLib.Utils
9+
{
10+
internal class PrefixOrAPM
11+
{
12+
internal enum ProfilerType
13+
{
14+
None,
15+
Prefix,
16+
APM
17+
}
18+
19+
static DateTime _LastCheck = DateTime.MinValue;
20+
static ProfilerType _LastProfilerType = ProfilerType.None;
21+
private static bool _ScanProcessException = false;
22+
internal static ProfilerType GetProfilerType()
23+
{
24+
if (_LastCheck > DateTime.UtcNow.AddMinutes(-1))
25+
{
26+
return _LastProfilerType;
27+
}
28+
_LastCheck = DateTime.UtcNow;
29+
bool foundProcess = false;
30+
31+
if (!_ScanProcessException)
32+
{
33+
try
34+
{
35+
foreach (var process in Process.GetProcesses().ToList())
36+
{
37+
if (process.Id <= 0)
38+
continue;
39+
40+
try
41+
{
42+
switch (process?.ProcessName?.ToLower().Replace(".vshost", ""))
43+
{
44+
case "devdashservice":
45+
case "stackifytracernotifier":
46+
case "devdashtestconsole":
47+
_LastProfilerType = ProfilerType.Prefix;
48+
foundProcess = true;
49+
break;
50+
case "stackifymonitoringservice":
51+
case "monitortestconsole":
52+
_LastProfilerType = ProfilerType.APM;
53+
foundProcess = true;
54+
break;
55+
}
56+
57+
if (foundProcess) break;
58+
}
59+
catch
60+
{
61+
_ScanProcessException = true;
62+
}
63+
64+
}
65+
}
66+
catch
67+
{
68+
_ScanProcessException = true;
69+
}
70+
}
71+
72+
73+
//fall back to see if this has been set
74+
if (!foundProcess && _ScanProcessException)
75+
{
76+
var stackifyPath = Environment.GetEnvironmentVariable("StackifyPath", EnvironmentVariableTarget.Machine);
77+
78+
if (!string.IsNullOrEmpty(stackifyPath) && (stackifyPath.IndexOf("prefix", StringComparison.CurrentCultureIgnoreCase) > -1 || stackifyPath.IndexOf("devdash", StringComparison.CurrentCultureIgnoreCase) > -1))
79+
{
80+
_LastProfilerType = ProfilerType.Prefix;
81+
}
82+
else
83+
{
84+
_LastProfilerType = ProfilerType.APM;
85+
}
86+
87+
}
88+
89+
return _LastProfilerType;
90+
}
91+
}
92+
}

0 commit comments

Comments
 (0)