blob: 4fc17b4ebdd50149d77be24bd6157d7063214116 (
plain)
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
|
//------------------------------------------------------------------------------
// <copyright file="ActivityCorrelator.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
// <owner current="true" primary="true">[....]</owner>
//------------------------------------------------------------------------------
namespace System.Data.Common
{
using System;
using System.Data;
using System.Threading;
using System.Diagnostics;
using System.Globalization;
/// <summary>
/// This class defines the data strucutre for ActvitiyId used for correlated tracing between client (bid trace event) and server (XEvent).
/// It also includes all the APIs used to access the ActivityId. Note: ActivityId is thread based which is stored in TLS.
/// </summary>
internal static class ActivityCorrelator
{
internal const Bid.ApiGroup CorrelationTracePoints = Bid.ApiGroup.Correlation;
internal class ActivityId
{
internal Guid Id { get; private set; }
internal UInt32 Sequence { get; private set; }
internal ActivityId()
{
this.Id = Guid.NewGuid();
this.Sequence = 0; // the first event will start 1
}
// copy-constructor
internal ActivityId(ActivityId activity)
{
this.Id = activity.Id;
this.Sequence = activity.Sequence;
}
internal void Increment()
{
unchecked
{
++this.Sequence;
}
}
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0}:{1}", this.Id, this.Sequence);
}
}
// Declare the ActivityId which will be stored in TLS. The Id is unique for each thread.
// The Sequence number will be incremented when each event happens.
// Correlation along threads is consistent with the current XEvent mechanisam at server.
[ThreadStaticAttribute]
static ActivityId tlsActivity;
/// <summary>
/// Get the current ActivityId
/// </summary>
internal static ActivityId Current
{
get
{
if (tlsActivity == null)
{
tlsActivity = new ActivityId();
}
return new ActivityId(tlsActivity);
}
}
/// <summary>
/// Increment the sequence number and generate the new ActviityId
/// </summary>
/// <returns>ActivityId</returns>
internal static ActivityId Next()
{
if (tlsActivity == null)
{
tlsActivity = new ActivityId();
}
tlsActivity.Increment();
return new ActivityId(tlsActivity);
}
}
}
|