Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diagnostics: Adds Summary for Diagnostic message #2732

Merged
merged 15 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 144 additions & 0 deletions Microsoft.Azure.Cosmos/src/Tracing/TraceData/SummaryDiagnostics.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// ------------------------------------------------------------

namespace Microsoft.Azure.Cosmos.Tracing.TraceData
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Microsoft.Azure.Cosmos.Json;

internal struct SummaryDiagnostics
{
public SummaryDiagnostics(ITrace trace)
: this()
{
this.DirectRequestsSummary = new Lazy<Dictionary<(int, int), int>>(() =>
new Dictionary<(int, int), int>());
this.GatewayRequestsSummary = new Lazy<Dictionary<(int, int), int>>(() =>
new Dictionary<(int, int), int>());
this.AllRegionsContacted = new Lazy<HashSet<Uri>>(() => new HashSet<Uri>());
this.CollectSummaryFromTraceTree(trace);
}

public Lazy<HashSet<Uri>> AllRegionsContacted { get; private set; }

// Count of (StatusCode, SubStatusCode) tuples
public Lazy<Dictionary<(int statusCode, int subStatusCode), int>> DirectRequestsSummary { get; private set; }

public Lazy<Dictionary<(int statusCode, int subStatusCode), int>> GatewayRequestsSummary { get; private set; }

private void CollectSummaryFromTraceTree(ITrace currentTrace)
{
foreach (object datums in currentTrace.Data.Values)
{
if (datums is ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum)
{
this.AggregateStatsFromStoreResults(clientSideRequestStatisticsTraceDatum.StoreResponseStatisticsList);
this.AggregateGatewayStatistics(clientSideRequestStatisticsTraceDatum.HttpResponseStatisticsList);
this.AggregateRegionsContacted(clientSideRequestStatisticsTraceDatum.RegionsContacted);
}
}

foreach (ITrace childTrace in currentTrace.Children)
{
this.CollectSummaryFromTraceTree(childTrace);
}
}

private void AggregateRegionsContacted(HashSet<(string, Uri)> regionsContacted)
{
foreach ((string _, Uri uri) in regionsContacted)
{
this.AllRegionsContacted.Value.Add(uri);
}
}

private void AggregateGatewayStatistics(IReadOnlyList<ClientSideRequestStatisticsTraceDatum.HttpResponseStatistics> httpResponseStatisticsList)
{
foreach (ClientSideRequestStatisticsTraceDatum.HttpResponseStatistics httpResponseStatistics in httpResponseStatisticsList)
{
int statusCode = 0;
int substatusCode = 0;
if (httpResponseStatistics.HttpResponseMessage != null)
{
statusCode = (int)httpResponseStatistics.HttpResponseMessage.StatusCode;
HttpResponseHeadersWrapper gatewayHeaders = new HttpResponseHeadersWrapper(
httpResponseStatistics.HttpResponseMessage.Headers,
httpResponseStatistics.HttpResponseMessage.Content?.Headers);
if (!int.TryParse(gatewayHeaders.SubStatus,
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
NumberStyles.Integer,
CultureInfo.InvariantCulture,
out substatusCode))
{
substatusCode = 0;
}
}

if (!this.GatewayRequestsSummary.Value.ContainsKey((statusCode, substatusCode)))
{
this.GatewayRequestsSummary.Value[(statusCode, substatusCode)] = 1;
}
else
{
this.GatewayRequestsSummary.Value[(statusCode, substatusCode)]++;
}
}
}

private void AggregateStatsFromStoreResults(IReadOnlyList<ClientSideRequestStatisticsTraceDatum.StoreResponseStatistics> storeResponseStatisticsList)
{
foreach (ClientSideRequestStatisticsTraceDatum.StoreResponseStatistics storeResponseStatistics in storeResponseStatisticsList)
{
int statusCode = (int)storeResponseStatistics.StoreResult.StatusCode;
int subStatusCode = (int)storeResponseStatistics.StoreResult.SubStatusCode;
if (!this.DirectRequestsSummary.Value.ContainsKey((statusCode, subStatusCode)))
{
this.DirectRequestsSummary.Value[(statusCode, subStatusCode)] = 1;
}
else
{
this.DirectRequestsSummary.Value[(statusCode, subStatusCode)]++;
}
}
}

public void WriteSummaryDiagnostics(IJsonWriter jsonWriter)
{
jsonWriter.WriteObjectStart();

if (this.DirectRequestsSummary.IsValueCreated)
{
jsonWriter.WriteFieldName("DirectCalls");
jsonWriter.WriteObjectStart();
foreach (KeyValuePair<(int, int), int> kvp in this.DirectRequestsSummary.Value)
{
jsonWriter.WriteFieldName(kvp.Key.ToString());
jsonWriter.WriteNumber64Value(kvp.Value);
}
jsonWriter.WriteObjectEnd();
}

if (this.AllRegionsContacted.IsValueCreated)
asketagarwal marked this conversation as resolved.
Show resolved Hide resolved
{
jsonWriter.WriteFieldName("RegionsContacted");
jsonWriter.WriteNumber64Value(this.AllRegionsContacted.Value.Count);
j82w marked this conversation as resolved.
Show resolved Hide resolved
}

if (this.GatewayRequestsSummary.IsValueCreated)
{
jsonWriter.WriteFieldName("GatewayCalls");
jsonWriter.WriteObjectStart();
foreach (KeyValuePair<(int, int), int> kvp in this.GatewayRequestsSummary.Value)
{
jsonWriter.WriteFieldName(kvp.Key.ToString());
jsonWriter.WriteNumber64Value(kvp.Value);
}
jsonWriter.WriteObjectEnd();
}

jsonWriter.WriteObjectEnd();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ private static class TraceJsonWriter
{
public static void WriteTrace(
IJsonWriter writer,
ITrace trace)
ITrace trace,
bool isRootTrace)
{
if (writer == null)
{
Expand All @@ -34,6 +35,13 @@ public static void WriteTrace(

writer.WriteObjectStart();

if (isRootTrace)
{
writer.WriteFieldName("Summary");
SummaryDiagnostics summaryDiagnostics = new SummaryDiagnostics(trace);
summaryDiagnostics.WriteSummaryDiagnostics(writer);
}

writer.WriteFieldName("name");
writer.WriteStringValue(trace.Name);

Expand Down Expand Up @@ -90,7 +98,9 @@ public static void WriteTrace(

foreach (ITrace child in trace.Children)
{
WriteTrace(writer, child);
WriteTrace(writer,
child,
isRootTrace: false);
}

writer.WriteArrayEnd();
Expand Down
4 changes: 3 additions & 1 deletion Microsoft.Azure.Cosmos/src/Tracing/TraceWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ public static void WriteTrace(
IJsonWriter writer,
ITrace trace)
{
TraceJsonWriter.WriteTrace(writer, trace);
TraceJsonWriter.WriteTrace(writer,
trace,
isRootTrace: true);
}

public static string TraceToText(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "ExecuteAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -242,6 +243,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -429,6 +431,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -616,6 +619,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -803,6 +807,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -990,6 +995,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1177,6 +1183,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1364,6 +1371,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1551,6 +1559,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1738,6 +1747,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -2079,6 +2089,7 @@
└── Create Trace(00000000-0000-0000-0000-000000000000) Batch-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "Trace Forest",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1779,6 +1780,7 @@
└── POCO Materialization(00000000-0000-0000-0000-000000000000) Poco-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "Trace Forest",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -2835,6 +2837,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "Trace Forest",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -3851,6 +3854,7 @@
└── POCO Materialization(00000000-0000-0000-0000-000000000000) Poco-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "Trace Forest",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
)
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateDatabaseAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -176,6 +177,7 @@
)
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateDatabaseAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -321,6 +322,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -731,6 +733,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1032,6 +1035,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down Expand Up @@ -1382,6 +1386,7 @@
└── Get Collection Cache(00000000-0000-0000-0000-000000000000) Routing-Component MemberName@FilePath:42 12:00:00:000 0.00 milliseconds
]]></Text>
<Json><![CDATA[{
"Summary": {},
"name": "CreateItemAsync",
"id": "00000000-0000-0000-0000-000000000000",
"caller info": {
Expand Down
Loading