-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathProfileTracer.cs
More file actions
394 lines (321 loc) · 13.7 KB
/
ProfileTracer.cs
File metadata and controls
394 lines (321 loc) · 13.7 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
using System;
using System.Collections.Specialized;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using StackifyLib.Utils;
namespace StackifyLib
{
public class ProfileTracer
{
private readonly string _methodDisplayText = null;
private bool _ignoreChildFrames = false;
private readonly string _requestReportingCategory = null;
private readonly string _appReportingCategory = null;
private bool _customMetricCount = false;
private bool _customMetricTime = false;
private bool _autoReportZeroIfNothingReported = false;
private string _customMetricCategory = null;
private string _customMetricName = null;
private readonly string _transactionId = Guid.NewGuid().ToString();
private string _requestId = null;
#if NETFULL
#if !NET40
private static EtwEventListener _etwEventListener = null;
#endif
#endif
internal bool IsOperation { get; set; }
internal ProfileTracer(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory)
{
_methodDisplayText = methodDisplayText;
_requestReportingCategory = requestLevelReportingCategory;
_appReportingCategory = appLevelReportingCategory;
#if NETFULL
try
{
if (System.Web.HttpContext.Current != null)
{
var id = System.Web.HttpContext.Current.Items["Stackify-RequestID"];
if (id != null && string.IsNullOrEmpty(id.ToString()) == false)
{
_requestId = id.ToString();
}
}
if (string.IsNullOrEmpty(_requestId))
{
var correltionManagerId = System.Runtime.Remoting.Messaging.CallContext.LogicalGetData("Stackify-RequestID");
if (correltionManagerId != null)
{
_requestId = correltionManagerId.ToString();
}
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#ProfileTracer ctor", ex);
}
#endif
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void SetReportingUrl(string reportingUrl)
{
#if NETFULL
try
{
if (System.Web.HttpContext.Current != null)
{
System.Web.HttpContext.Current.Items["Stackify.ReportingUrl"] = reportingUrl;
}
}
catch (Exception ex)
{
StackifyAPILogger.Log("#ProfileTracer #SetReportingUrl", ex);
}
#endif
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void SetOperationName(string operationName)
{
}
[Obsolete("Just used for testing", false)]
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void NoOp()
{
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void TraceString(string logMsg)
{
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void TraceMongoCommand(string logMsg)
{
}
/// <summary>
/// Used to logically group a section of code
/// </summary>
/// <param name="methodDisplayText"></param>
/// <returns></returns>
public static ProfileTracer CreateAsCodeBlock(string methodDisplayText)
{
var tracer = new ProfileTracer(methodDisplayText, null, null);
return tracer;
}
/// <summary>
/// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
/// </summary>
/// <param name="functionName"></param>
/// <returns></returns>
public static ProfileTracer CreateAsTrackedFunction(string functionName)
{
var tracer = new ProfileTracer(functionName, "Tracked Function", null);
return tracer;
}
/// <summary>
/// Used by non web apps to define transactions in code that are turned in to operations to be tracked in Stackify APM or Prefix
/// </summary>
/// <param name="operationName"></param>
/// <param name="uniqueOperationID"></param>
/// <returns></returns>
public static ProfileTracer CreateAsOperation(string operationName, string uniqueOperationID = null)
{
#if NETFULL
#if !NET40
if (_etwEventListener == null)
{
_etwEventListener = new EtwEventListener();
}
#endif
#endif
var tracer = new ProfileTracer(operationName, null, null);
tracer.IsOperation = true;
if (string.IsNullOrEmpty(uniqueOperationID) == false)
{
tracer._requestId = uniqueOperationID;
}
return tracer;
}
/// <summary>
/// Used to logically group a section of code
/// </summary>
/// <param name="methodDisplayText"></param>
/// <param name="requestLevelReportingCategory"></param>
/// <param name="appLevelReportingCategory"></param>
/// <returns></returns>
public static ProfileTracer CreateAsDependency(string methodDisplayText, string requestLevelReportingCategory, string appLevelReportingCategory = null)
{
var tracer = new ProfileTracer(methodDisplayText, requestLevelReportingCategory, appLevelReportingCategory);
return tracer;
}
public ProfileTracer SetUniqueOperationID(string uniqueOperationID)
{
if (string.IsNullOrEmpty(uniqueOperationID) == false)
{
this._requestId = uniqueOperationID;
}
return this;
}
public ProfileTracer CreateMetric(string categoryName, string metricName, bool trackCount = true, bool trackTime = true, bool autoReportZeroIfNothingReported = false)
{
_customMetricCategory = categoryName;
_customMetricName = metricName;
_customMetricCount = trackCount;
_customMetricTime = trackTime;
_autoReportZeroIfNothingReported = autoReportZeroIfNothingReported;
return this;
}
public ProfileTracer IgnoreChildFrames(bool value = true)
{
_ignoreChildFrames = value;
return this;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public void ApplyDistributedHeaders(string traceparent,string tracestate)
{
}
//Method the profiler looks for
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ExecInternal2(string values, Action action)
{
try
{
action();
}
finally
{
ExecInternalComplete2(_transactionId + "|" + _requestId + "|0|" + IsOperation);
}
}
//Method the profiler looks for
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ExecInternalOperation(string values, Action action)
{
try
{
action();
}
finally
{
ExecInternalComplete2(_transactionId + "|" + _requestId + "|0|" + IsOperation);
}
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private Task ExecInternal2(string values, Func<Task> action)
{
var t = action();
ExecInternalTaskStarted2(_transactionId + "|" + t.Id + "|" + IsOperation);
return t;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private Task<T> ExecInternal2<T>(string values, Func<Task<T>> action)
{
var t = action();
ExecInternalTaskStarted2(_transactionId + "|" + t.Id + "|" + IsOperation);
return t;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private Task ExecInternalOperation(string values, Func<Task> action)
{
var t = action();
ExecInternalTaskStarted2(_transactionId + "|" + t.Id + "|" + IsOperation);
return t;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private Task<T> ExecInternalOperation<T>(string values, Func<Task<T>> action)
{
var t = action();
ExecInternalTaskStarted2(_transactionId + "|" + t.Id + "|" + IsOperation);
return t;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ExecInternalTaskStarted2(string values)
{
}
public void Exec(Action action)
{
if (action == null)
{
return;
}
var now = DateTimeOffset.UtcNow;
if (this.IsOperation)
{
ExecInternalOperation(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, action);
}
else
{
ExecInternal2(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, action);
}
if (_customMetricTime)
{
Metrics.Time(_customMetricCategory, _customMetricName + " Time", now);
}
if (_customMetricCount)
{
Metrics.Count(_customMetricCategory, _customMetricName, 1, _autoReportZeroIfNothingReported);
}
}
public Task<T> ExecAsync<T>(Func<Task<T>> task)
{
var now = DateTimeOffset.UtcNow;
Task<T> t;
if (this.IsOperation)
{
t = ExecInternalOperation<T>(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, task);
}
else
{
t = ExecInternal2<T>(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, task);
}
t.ContinueWith((tend) =>
{
if (_customMetricTime)
{
Metrics.Time(_customMetricCategory, _customMetricName + " Time", now);
}
ExecInternalComplete2(_transactionId + "|" + _requestId + "|" + tend.Id + "|" + IsOperation);
});
if (_customMetricCount)
{
Metrics.Count(_customMetricCategory, _customMetricName, 1, _autoReportZeroIfNothingReported);
}
return t;
}
public Task ExecAsync(Func<Task> task)
{
var now = DateTimeOffset.UtcNow;
Task t;
if (this.IsOperation)
{
t = ExecInternalOperation(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, task);
}
else
{
t = ExecInternal2(_methodDisplayText + "|" + (_ignoreChildFrames ? 1 : 0) + "|" + _requestReportingCategory + "|" + _appReportingCategory + "|" + _transactionId + "|" + _requestId + "|" + IsOperation, task);
}
t.ContinueWith((tend) =>
{
if (_customMetricTime)
{
Metrics.Time(_customMetricCategory, _customMetricName + " Time", now);
}
ExecInternalComplete2(_transactionId + "|" + _requestId + "|" + tend.Id + "|" + IsOperation);
});
if (_customMetricCount)
{
Metrics.Count(_customMetricCategory, _customMetricName, 1, _autoReportZeroIfNothingReported);
}
return t;
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
private void ExecInternalComplete2(string values)
{
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void SetEnvironmentName(string environmentName)
{
}
[MethodImpl(MethodImplOptions.PreserveSig | MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
public static void SetApplicationName(string applicationName)
{
}
}
}