Skip to content

Commit 4e3b12e

Browse files
author
mwatson
committed
Misc bug fixes, ProfilerTracer updates for windows services and new Tracked Functions
1 parent 2036248 commit 4e3b12e

File tree

11 files changed

+4130
-55
lines changed

11 files changed

+4130
-55
lines changed

Src/NLog.Targets.Stackify/project.json

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"version": "1.23.1",
2+
"version": "1.23.3",
33

44
"title": "NLog.Targets.Stackify",
55

@@ -12,24 +12,26 @@
1212
},
1313

1414
"dependencies": {
15-
"StackifyLib": "1.23.1-beta2"
1615
},
1716

1817
"frameworks": {
1918
"netstandard1.3": {
2019
"dependencies": {
21-
"NLog": "4.4.0-*"
20+
"NLog": "4.4.0-*",
21+
"StackifyLib": "1.23.3"
2222
}
2323
},
2424
"net40": {
2525
"dependencies": {
26-
"NLog": "4.2.3"
26+
"NLog": "4.2.3",
27+
"StackifyLib": "1.23.3"
2728
}
2829

2930
},
3031
"net45": {
3132
"dependencies": {
32-
"NLog": "4.2.3"
33+
"NLog": "4.2.3",
34+
"StackifyLib": "1.23.3"
3335
}
3436

3537
}

Src/StackifyLib/Internal/Metrics/MetricClient.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -369,7 +369,7 @@ public static bool UploadMetrics(DateTime currentMinute)
369369
if (!_httpClient.MatchedClientDeviceApp())
370370
{
371371
// purgeOlderThan = DateTime.UtcNow;
372-
StackifyAPILogger.Log("Upload metrics skipped due to being disabled");
372+
StackifyAPILogger.Log("Upload metrics skipped because we were unable to match the app to an app in Stackify");
373373
}
374374
else if (!_httpClient.IsAuthorized())
375375
{

Src/StackifyLib/Logger.cs

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -305,45 +305,9 @@ public static List<TraceFrame> GetCurrentStackTrace(string declaringClassName, i
305305
return frames;
306306
}
307307

308-
private static bool? _PrefixEnabled = null;
309-
310308
public static bool PrefixEnabled()
311309
{
312-
if (_PrefixEnabled != null)
313-
return _PrefixEnabled.Value;
314-
315-
var variable = Environment.GetEnvironmentVariable("StackSquatchUpdated");
316-
317-
318-
if (!string.IsNullOrEmpty(variable))
319-
{
320-
321-
DateTime updated;
322-
323-
if (DateTime.TryParse(variable, out updated))
324-
{
325-
if (updated > DateTime.UtcNow.AddHours(-24))
326-
{
327-
StackifyLib.Utils.StackifyAPILogger.Log("Prefix enabled", true);
328-
_PrefixEnabled = true;
329-
}
330-
else
331-
{
332-
_PrefixEnabled = false;
333-
}
334-
}
335-
else
336-
{
337-
_PrefixEnabled = false;
338-
}
339-
340-
}
341-
else
342-
{
343-
_PrefixEnabled = false;
344-
}
345-
346-
return _PrefixEnabled.Value;
310+
return PrefixOrAPM.GetProfilerType() == PrefixOrAPM.ProfilerType.Prefix;
347311
}
348312
}
349313
}

Src/StackifyLib/ProfileTracer.cs

Lines changed: 74 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class ProfileTracer
2424
private string _RequestID = null;
2525
internal bool IsOperation { get; set; }
2626
#if NET45 || NETSTANDARD1_3
27-
//private static EtwEventListener _etwEventListener = null;
27+
private static EtwEventListener _etwEventListener = null;
2828
#endif
2929
internal ProfileTracer(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory)
3030
{
@@ -114,6 +114,18 @@ public static ProfileTracer CreateAsCodeBlock(string methodDisplayText)
114114
return tracer;
115115
}
116116

117+
/// <summary>
118+
/// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
119+
/// </summary>
120+
/// <param name="operationName"></param>
121+
/// <param name="uniqueOperationID"></param>
122+
/// <returns></returns>
123+
public static ProfileTracer CreateAsTrackedFunction(string functionName)
124+
{
125+
ProfileTracer tracer = new ProfileTracer(functionName, "Tracked Function", null);
126+
return tracer;
127+
}
128+
117129
/// <summary>
118130
/// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
119131
/// </summary>
@@ -123,15 +135,15 @@ public static ProfileTracer CreateAsCodeBlock(string methodDisplayText)
123135
public static ProfileTracer CreateAsOperation(string operationName, string uniqueOperationID = null)
124136
{
125137
#if NET45 || NETSTANDARD1_3
126-
//if(_etwEventListener == null)
127-
// _etwEventListener = new EtwEventListener();
138+
if (_etwEventListener == null)
139+
_etwEventListener = new EtwEventListener();
128140
#endif
129141
ProfileTracer tracer = new ProfileTracer(operationName, null, null);
130142
tracer.IsOperation = true;
131143

132144
if (!string.IsNullOrEmpty(uniqueOperationID))
133145
{
134-
tracer._transactionID = uniqueOperationID;
146+
tracer._RequestID = uniqueOperationID;
135147
}
136148

137149
return tracer;
@@ -143,13 +155,23 @@ public static ProfileTracer CreateAsOperation(string operationName, string uniqu
143155
/// <param name="methodDisplayText"></param>
144156
/// <returns></returns>
145157

146-
public static ProfileTracer CreateAsCodeBlock(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory = null)
158+
public static ProfileTracer CreateAsDependency(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory = null)
147159
{
148160
ProfileTracer tracer = new ProfileTracer(methodDisplayText, requestLevelReportingCategory, appLevelReportingCategory);
149161
return tracer;
150162
}
151163

152164

165+
public ProfileTracer SetUniqueOperationID(string uniqueOperationID)
166+
{
167+
if (!string.IsNullOrEmpty(uniqueOperationID))
168+
{
169+
this._RequestID = uniqueOperationID;
170+
}
171+
172+
return this;
173+
}
174+
153175
public ProfileTracer CreateMetric(string categoryName, string metricName, bool trackCount = true, bool trackTime = true, bool autoReportZeroIfNothingReported = false)
154176
{
155177
_customMetricCategory = categoryName;
@@ -206,6 +228,14 @@ private Task ExecInternal2(string values, Func<Task> action)
206228
return t;
207229
}
208230

231+
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
232+
private Task<T> ExecInternal2<T>(string values, Func<Task<T>> action)
233+
{
234+
Task<T> t = action();
235+
ExecInternalTaskStarted2(_transactionID + "|" + t.Id + "|" + IsOperation);
236+
return t;
237+
}
238+
209239

210240
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
211241
private Task ExecInternalOperation(string values, Func<Task> action)
@@ -215,6 +245,14 @@ private Task ExecInternalOperation(string values, Func<Task> action)
215245
return t;
216246
}
217247

248+
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
249+
private Task<T> ExecInternalOperation<T>(string values, Func<Task<T>> action)
250+
{
251+
Task<T> t = action();
252+
ExecInternalTaskStarted2(_transactionID + "|" + t.Id + "|" + IsOperation);
253+
return t;
254+
}
255+
218256

219257
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
220258
private void ExecInternalTaskStarted2(string values)
@@ -253,7 +291,38 @@ public void Exec(Action action)
253291

254292
}
255293

294+
public Task<T> ExecAsync<T>(Func<Task<T>> task)
295+
{
296+
DateTimeOffset now = DateTimeOffset.UtcNow;
297+
298+
Task<T> t;
299+
300+
301+
if (this.IsOperation)
302+
{
303+
t = ExecInternalOperation<T>(_methodDisplayText + "|" + (ignoreChildFrames ? 1 : 0).ToString() + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionID + "|" + _RequestID + "|" + IsOperation, task);
304+
}
305+
else
306+
{
307+
t = ExecInternal2<T>(_methodDisplayText + "|" + (ignoreChildFrames ? 1 : 0).ToString() + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionID + "|" + _RequestID + "|" + IsOperation, task);
308+
}
309+
310+
t.ContinueWith((tend) =>
311+
{
312+
if (_customMetricTime)
313+
{
314+
Metrics.Time(_customMetricCategory, _customMetricName + " Time", now);
315+
}
316+
ExecInternalComplete2(_transactionID + "|" + _RequestID + "|" + tend.Id + "|" + IsOperation);
317+
});
318+
319+
if (_customMetricCount)
320+
{
321+
Metrics.Count(_customMetricCategory, _customMetricName, 1, _autoReportZeroIfNothingReported);
322+
}
256323

324+
return t;
325+
}
257326

258327
public Task ExecAsync(Func<Task> task)
259328
{

Src/StackifyLib/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@
3434
// You can specify all the values or you can default the Build and Revision Numbers
3535
// by using the '*' as shown below:
3636
// [assembly: AssemblyVersion("1.0.*")]
37-
[assembly: AssemblyVersion("1.22.*")]
38-
[assembly: AssemblyFileVersion("1.22.*")]
37+
[assembly: AssemblyVersion("1.24.*")]
38+
[assembly: AssemblyFileVersion("1.24.*")]
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#if NET45 || NETSTANDARD1_3
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Diagnostics;
5+
using System.Diagnostics.Tracing;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace StackifyLib.Utils
11+
{
12+
public class EtwEventListener : System.Diagnostics.Tracing.EventListener
13+
{
14+
private static readonly Guid tplGuid = new Guid("2e5dba47-a3d2-4d16-8ee0-6671ffdcd7b5");
15+
// private static readonly Guid aspnetGuid = new Guid("aff081fe-0247-4275-9c4e-021f3dc1da35");
16+
17+
protected override void OnEventSourceCreated(EventSource eventSource)
18+
{
19+
if (eventSource.Guid == tplGuid)
20+
{
21+
EnableEvents(eventSource, EventLevel.Informational);
22+
}
23+
}
24+
25+
protected override void OnEventWritten(EventWrittenEventArgs eventData)
26+
{
27+
28+
}
29+
}
30+
}
31+
#endif

Src/StackifyLib/Utils/HttpClient.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -623,11 +623,12 @@ private HttpWebRequest BuildJsonRequest(string url, string jsonData, bool compre
623623
request.Method = "POST";
624624

625625
byte[] payload = Encoding.UTF8.GetBytes(jsonData);
626-
request.Headers[HttpRequestHeader.ContentLength] = payload.Length.ToString();
627626

628627
#if NET45 || NET40
628+
request.ContentLength= payload.Length;
629629
using (Stream stream = request.GetRequestStream())
630630
#else
631+
request.Headers[HttpRequestHeader.ContentLength] = payload.Length.ToString();
631632
using (Stream stream = request.GetRequestStreamAsync().GetAwaiter().GetResult())
632633
#endif
633634
{
@@ -665,8 +666,10 @@ private HttpWebRequest BuildPOSTRequest(string url, string postdata)
665666

666667
#if NET45 || NET40
667668
request.UserAgent = "StackifyLib-" + _version;
669+
request.ContentLength = 0;
668670
#else
669671
request.Headers[HttpRequestHeader.UserAgent] = "StackifyLib-" + _version;
672+
request.Headers[HttpRequestHeader.ContentLength] = "0";
670673
#endif
671674

672675
//if (HttpClient.CustomWebProxy != null)
@@ -676,7 +679,7 @@ private HttpWebRequest BuildPOSTRequest(string url, string postdata)
676679

677680

678681
request.Method = "POST";
679-
request.Headers[HttpRequestHeader.ContentLength] = "0";
682+
680683
if (!String.IsNullOrEmpty(postdata))
681684
{
682685
byte[] payload = Encoding.UTF8.GetBytes(postdata);

Src/StackifyLib/Utils/PrefixOrAPM.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ internal static ProfilerType GetProfilerType()
3333
_LastCheck = DateTime.UtcNow;
3434
bool foundProcess = false;
3535

36+
string instanceID = Left(Environment.GetEnvironmentVariable("WEBSITE_INSTANCE_ID"), 6);
37+
string siteName = (Environment.GetEnvironmentVariable("WEBSITE_IIS_SITE_NAME") ?? "").TrimStart('~', '1');
38+
39+
//is azure app service?
40+
if (!string.IsNullOrEmpty(instanceID) && !string.IsNullOrEmpty(siteName))
41+
{
42+
_LastProfilerType = ProfilerType.APM;
43+
return _LastProfilerType;
44+
}
45+
3646
if (!_ScanProcessException)
3747
{
3848
try
@@ -93,5 +103,23 @@ internal static ProfilerType GetProfilerType()
93103

94104
return _LastProfilerType;
95105
}
106+
107+
private static string Left(string sValue, int iMaxLength)
108+
{
109+
//Check if the value is valid
110+
if (string.IsNullOrEmpty(sValue))
111+
{
112+
//Set valid empty string as string could be null
113+
sValue = string.Empty;
114+
}
115+
else if (sValue.Length > iMaxLength)
116+
{
117+
//Make the string no longer than the max length
118+
sValue = sValue.Substring(0, iMaxLength);
119+
}
120+
121+
//Return the string
122+
return sValue;
123+
}
96124
}
97125
}

Src/StackifyLib/project.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
{
2-
"version": "1.23.1-beta3",
2+
"version": "1.24.0",
33

44
"title": "Stackify API",
55

66
"packOptions": {
77
"owners": [ "stackify_matt", "jtaylorstackify" ],
88
"tags": [ "stackify", "metrics", "errors", "logs" ],
9-
"summary": "Library for accessing the Stackify API",
10-
9+
"summary": "Library for accessing the Stackify API"
1110
},
1211

1312
"dependencies": {

0 commit comments

Comments
 (0)