-
Notifications
You must be signed in to change notification settings - Fork 328
Expand file tree
/
Copy pathTraceContextBase.cs
More file actions
220 lines (190 loc) · 7.81 KB
/
Copy pathTraceContextBase.cs
File metadata and controls
220 lines (190 loc) · 7.81 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
// ----------------------------------------------------------------------------------
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------
namespace DurableTask.Core
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using DurableTask.Core.Common;
using DurableTask.Core.Serializing;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
/// <summary>
/// TraceContext keep the correlation value.
/// </summary>
public abstract class TraceContextBase
{
private static readonly JsonSerializer serializer;
/// <summary>
/// Default constructor
/// </summary>
protected TraceContextBase()
{
OrchestrationTraceContexts = new Stack<TraceContextBase>();
}
static TraceContextBase()
{
CustomJsonSerializerSettings = new JsonSerializerSettings()
{
TypeNameHandling = TypeNameHandling.Objects,
PreserveReferencesHandling = PreserveReferencesHandling.Objects,
ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
SerializationBinder = new TraceContextSerializationBinder(),
};
serializer = JsonSerializer.Create(CustomJsonSerializerSettings);
}
/// <summary>
/// Start time of this telemetry
/// </summary>
public DateTimeOffset StartTime { get; set; }
/// <summary>
/// Type of this telemetry.
/// Request Telemetry or Dependency Telemetry.
/// Use
/// <see cref="TelemetryType"/>
/// </summary>
public TelemetryType TelemetryType { get; set; }
/// <summary>
/// OrchestrationState save the state of the
/// </summary>
public Stack<TraceContextBase> OrchestrationTraceContexts { get; set; }
/// <summary>
/// Keep OperationName in case, don't have an Activity in this context
/// </summary>
public string OperationName { get; set; }
/// <summary>
/// Current Activity only managed by this concrete class.
/// This property is not serialized.
/// </summary>
[JsonIgnore]
internal Activity CurrentActivity { get; set; }
/// <summary>
/// Return if the orchestration is on replay
/// </summary>
/// <returns></returns>
[JsonIgnore]
public bool IsReplay { get; set; } = false;
/// <summary>
/// Duration of this context. Valid after call Stop() method.
/// </summary>
[JsonIgnore]
public abstract TimeSpan Duration { get; }
[JsonIgnore]
static JsonSerializerSettings CustomJsonSerializerSettings { get; }
/// <summary>
/// Serializable Json string of TraceContext
/// </summary>
[JsonIgnore]
public string SerializableTraceContext =>
Utils.SerializeToJson(serializer, this);
/// <summary>
/// Telemetry.Id Used for sending telemetry. refer this URL
/// https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.extensibility.implementation.operationtelemetry?view=azure-dotnet
/// </summary>
[JsonIgnore]
public abstract string TelemetryId { get; }
/// <summary>
/// Telemetry.Context.Operation.Id Used for sending telemetry refer this URL
/// https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.extensibility.implementation.operationtelemetry?view=azure-dotnet
/// </summary>
[JsonIgnore]
public abstract string TelemetryContextOperationId { get; }
/// <summary>
/// Get RequestTraceContext of Current Orchestration
/// </summary>
/// <returns></returns>
public TraceContextBase GetCurrentOrchestrationRequestTraceContext()
{
foreach(TraceContextBase element in OrchestrationTraceContexts)
{
if (TelemetryType.Request == element.TelemetryType) return element;
}
throw new InvalidOperationException("Can not find RequestTraceContext");
}
/// <summary>
/// Telemetry.Context.Operation.ParentId Used for sending telemetry refer this URL
/// https://docs.microsoft.com/en-us/dotnet/api/microsoft.applicationinsights.extensibility.implementation.operationtelemetry?view=azure-dotnet
/// </summary>
[JsonIgnore]
public abstract string TelemetryContextOperationParentId { get; }
/// <summary>
/// Set Parent TraceContext and Start the context
/// </summary>
/// <param name="parentTraceContext"> Parent Trace</param>
public abstract void SetParentAndStart(TraceContextBase parentTraceContext);
/// <summary>
/// Start TraceContext as new
/// </summary>
public abstract void StartAsNew();
/// <summary>
/// Stop TraceContext
/// </summary>
public void Stop() => CurrentActivity?.Stop();
/// <summary>
/// Set Activity.Current to CurrentActivity
/// </summary>
public void SetActivityToCurrent()
{
Activity.Current = CurrentActivity;
}
/// <summary>
/// Restore TraceContext sub class
/// </summary>
/// <param name="json">Serialized json of TraceContext sub classes</param>
/// <returns></returns>
public static TraceContextBase Restore(string json)
{
// If the JSON is empty, we assume to have an empty context
if (string.IsNullOrEmpty(json))
{
return TraceContextFactory.Empty;
}
// Obtain typename and validate that it is a subclass of `TraceContextBase`.
// If it's not, we throw an exception.
Type traceContextType = null;
Type traceContextBasetype = typeof(TraceContextBase);
JToken typeName = JObject.Parse(json)["$type"];
traceContextType = Type.GetType(typeName.Value<string>());
if (!traceContextType.IsSubclassOf(traceContextBasetype))
{
string typeNameStr = typeName.ToString();
string baseNameStr = traceContextBasetype.ToString();
throw new Exception($"Serialized TraceContext type ${typeNameStr} is not a subclass of ${baseNameStr}." +
"This probably means something went wrong in serializing the TraceContext.");
}
// De-serialize the object now that we now it's safe
var restored = Utils.DeserializeFromJson(
serializer,
json,
traceContextType) as TraceContextBase;
restored.OrchestrationTraceContexts = new Stack<TraceContextBase>(restored.OrchestrationTraceContexts);
return restored;
}
}
/// <summary>
/// Telemetry Type
/// </summary>
public enum TelemetryType
{
/// <summary>
/// Request Telemetry
/// </summary>
Request,
/// <summary>
/// Dependency Telemetry
/// </summary>
Dependency,
}
}