-
Notifications
You must be signed in to change notification settings - Fork 533
Expand file tree
/
Copy pathCosmosTraceDiagnostics.cs
More file actions
180 lines (150 loc) · 6.32 KB
/
CosmosTraceDiagnostics.cs
File metadata and controls
180 lines (150 loc) · 6.32 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
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.Diagnostics
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Azure.Cosmos.Json;
using Microsoft.Azure.Cosmos.Query.Core.Metrics;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using static Microsoft.Azure.Cosmos.Tracing.TraceData.ClientSideRequestStatisticsTraceDatum;
internal sealed class CosmosTraceDiagnostics : CosmosDiagnostics
{
private readonly Lazy<ServerSideCumulativeMetrics> accumulatedMetrics;
private readonly Lazy<string> cachedSummaryJson;
public CosmosTraceDiagnostics(ITrace trace)
: this(trace, CosmosClientOptions.DefaultMaxDiagnosticsSummarySizeBytes)
{
}
internal CosmosTraceDiagnostics(ITrace trace, int maxDiagnosticsSummarySizeBytes)
{
if (trace == null)
{
throw new ArgumentNullException(nameof(trace));
}
// Need to set to the root trace, since we don't know which layer of the stack the response message was returned from.
ITrace rootTrace = trace;
while (rootTrace.Parent != null)
{
rootTrace = rootTrace.Parent;
}
this.Value = rootTrace;
this.accumulatedMetrics = new Lazy<ServerSideCumulativeMetrics>(() => PopulateServerSideCumulativeMetrics(this.Value));
this.cachedSummaryJson = new Lazy<string>(() =>
DiagnosticsSummaryWriter.WriteSummary(this.Value, maxDiagnosticsSummarySizeBytes));
}
public ITrace Value { get; }
public override string ToString()
{
if (this.Value is Tracing.Trace rootConcreteTrace)
{
rootConcreteTrace.SetWalkingStateRecursively();
}
return this.ToJsonString();
}
public override string ToString(DiagnosticsVerbosity verbosity)
{
return verbosity switch
{
DiagnosticsVerbosity.Summary => this.cachedSummaryJson.Value,
DiagnosticsVerbosity.Detailed => this.ToString(),
_ => this.ToString(),
};
}
public override TimeSpan GetClientElapsedTime()
{
return this.Value.Duration;
}
public override IReadOnlyList<(string regionName, Uri uri)> GetContactedRegions()
{
if (this.Value is Tracing.Trace rootConcreteTrace)
{
rootConcreteTrace.SetWalkingStateRecursively();
}
return this.Value?.Summary?.RegionsContacted;
}
public override ServerSideCumulativeMetrics GetQueryMetrics()
{
if (this.Value is Tracing.Trace rootConcreteTrace)
{
rootConcreteTrace.SetWalkingStateRecursively();
}
return this.accumulatedMetrics.Value;
}
internal bool IsGoneExceptionHit()
{
if (this.Value is Tracing.Trace rootConcreteTrace)
{
rootConcreteTrace.SetWalkingStateRecursively();
}
return this.WalkTraceTreeForGoneException(this.Value);
}
private bool WalkTraceTreeForGoneException(ITrace currentTrace)
{
if (currentTrace == null)
{
return false;
}
foreach (object datum in currentTrace.Data.Values)
{
if (datum is ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum)
{
foreach (StoreResponseStatistics responseStatistics in clientSideRequestStatisticsTraceDatum.StoreResponseStatisticsList)
{
if (responseStatistics.StoreResult != null && responseStatistics.StoreResult.StatusCode == Documents.StatusCodes.Gone)
{
return true;
}
}
}
}
foreach (ITrace childTrace in currentTrace.Children)
{
if (this.WalkTraceTreeForGoneException(childTrace))
{
return true;
}
}
return false;
}
private string ToJsonString()
{
ReadOnlyMemory<byte> utf8String = this.WriteTraceToJsonWriter(JsonSerializationFormat.Text);
return Encoding.UTF8.GetString(utf8String.Span);
}
private ReadOnlyMemory<byte> WriteTraceToJsonWriter(JsonSerializationFormat jsonSerializationFormat)
{
IJsonWriter jsonTextWriter = JsonWriter.Create(jsonSerializationFormat);
TraceWriter.WriteTrace(jsonTextWriter, this.Value);
return jsonTextWriter.GetResult();
}
private static ServerSideCumulativeMetrics PopulateServerSideCumulativeMetrics(ITrace trace)
{
ServerSideMetricsInternalAccumulator accumulator = new ServerSideMetricsInternalAccumulator();
ServerSideMetricsTraceExtractor.WalkTraceTreeForQueryMetrics(trace, accumulator);
IReadOnlyList<ServerSidePartitionedMetricsInternal> serverSideMetricsList = accumulator.GetPartitionedServerSideMetrics().Select(metrics => new ServerSidePartitionedMetricsInternal(metrics)).ToList();
ServerSideCumulativeMetrics accumulatedMetrics = new ServerSideCumulativeMetricsInternal(serverSideMetricsList);
return accumulatedMetrics.PartitionedMetrics.Count != 0 ? accumulatedMetrics : null;
}
public override DateTime? GetStartTimeUtc()
{
if (this.Value == null || this.Value.StartTime == null)
{
return null;
}
return this.Value.StartTime;
}
public override int GetFailedRequestCount()
{
if (this.Value == null || this.Value.Summary == null)
{
return 0;
}
return this.Value.Summary.GetFailedCount();
}
}
}