Skip to content

Commit

Permalink
Preview TraceWriter: Fixes Argument Null Exception / Null Reference E…
Browse files Browse the repository at this point in the history
…xception (#2246)

* fixes NRE

* opted for json null
  • Loading branch information
bchong95 authored Feb 23, 2021
1 parent 575d9f6 commit df040f3
Show file tree
Hide file tree
Showing 4 changed files with 463 additions and 161 deletions.
64 changes: 43 additions & 21 deletions Microsoft.Azure.Cosmos/src/Tracing/TraceWriter.TraceJsonWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ public void Visit(PointOperationStatisticsTraceDatum pointOperationStatisticsTra
this.jsonWriter.WriteStringValue("PointOperationStatistics");

this.jsonWriter.WriteFieldName("ActivityId");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.ActivityId);
this.WriteStringValueOrNull(pointOperationStatisticsTraceDatum.ActivityId);

this.jsonWriter.WriteFieldName("ResponseTimeUtc");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.ResponseTimeUtc.ToString("o", CultureInfo.InvariantCulture));
Expand All @@ -173,25 +173,16 @@ public void Visit(PointOperationStatisticsTraceDatum pointOperationStatisticsTra
this.jsonWriter.WriteNumber64Value(pointOperationStatisticsTraceDatum.RequestCharge);

this.jsonWriter.WriteFieldName("RequestUri");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.RequestUri);
this.WriteStringValueOrNull(pointOperationStatisticsTraceDatum.RequestUri);

if (!string.IsNullOrEmpty(pointOperationStatisticsTraceDatum.ErrorMessage))
{
this.jsonWriter.WriteFieldName("ErrorMessage");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.ErrorMessage);
}
this.jsonWriter.WriteFieldName("ErrorMessage");
this.WriteStringValueOrNull(pointOperationStatisticsTraceDatum.ErrorMessage);

if (pointOperationStatisticsTraceDatum.RequestSessionToken != null)
{
this.jsonWriter.WriteFieldName("RequestSessionToken");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.RequestSessionToken);
}
this.jsonWriter.WriteFieldName("RequestSessionToken");
this.WriteStringValueOrNull(pointOperationStatisticsTraceDatum.RequestSessionToken);

if (pointOperationStatisticsTraceDatum.ResponseSessionToken != null)
{
this.jsonWriter.WriteFieldName("ResponseSessionToken");
this.jsonWriter.WriteStringValue(pointOperationStatisticsTraceDatum.ResponseSessionToken);
}
this.jsonWriter.WriteFieldName("ResponseSessionToken");
this.WriteStringValueOrNull(pointOperationStatisticsTraceDatum.ResponseSessionToken);

this.jsonWriter.WriteObjectEnd();
}
Expand Down Expand Up @@ -250,7 +241,14 @@ private static void VisitAddressResolutionStatistics(
}

jsonWriter.WriteFieldName("TargetEndpoint");
jsonWriter.WriteStringValue(addressResolutionStatistics.TargetEndpoint);
if (addressResolutionStatistics.TargetEndpoint == null)
{
jsonWriter.WriteNullValue();
}
else
{
jsonWriter.WriteStringValue(addressResolutionStatistics.TargetEndpoint);
}

jsonWriter.WriteObjectEnd();
}
Expand All @@ -271,7 +269,14 @@ private static void VisitStoreResponseStatistics(
jsonWriter.WriteStringValue(storeResponseStatistics.RequestOperationType.ToString());

jsonWriter.WriteFieldName("LocationEndpoint");
jsonWriter.WriteStringValue(storeResponseStatistics.LocationEndpoint.ToString());
if (storeResponseStatistics.LocationEndpoint == null)
{
jsonWriter.WriteNullValue();
}
else
{
jsonWriter.WriteStringValue(storeResponseStatistics.LocationEndpoint.ToString());
}

if (storeResponseStatistics.StoreResult != null)
{
Expand Down Expand Up @@ -301,7 +306,7 @@ private void WriteJsonUriArray(string propertyName, IEnumerable<Uri> uris)
{
foreach (Uri contactedReplica in uris)
{
this.jsonWriter.WriteStringValue(contactedReplica.ToString());
this.WriteStringValueOrNull(contactedReplica?.ToString());
}
}

Expand All @@ -326,6 +331,11 @@ private void WriteJsonUriArrayWithDuplicatesCounted(string propertyName, IReadOn
for (int i = 0; i < totalCount; i++)
{
Uri contactedReplica = uris[i];
if (contactedReplica == null)
{
continue;
}

if (contactedReplica.Equals(previous))
{
duplicateCount++;
Expand All @@ -345,7 +355,7 @@ private void WriteJsonUriArrayWithDuplicatesCounted(string propertyName, IReadOn
this.jsonWriter.WriteFieldName("Count");
this.jsonWriter.WriteNumber64Value(duplicateCount);
this.jsonWriter.WriteFieldName("Uri");
this.jsonWriter.WriteStringValue(contactedReplica.ToString());
this.WriteStringValueOrNull(contactedReplica?.ToString());
this.jsonWriter.WriteObjectEnd();
}

Expand All @@ -356,6 +366,18 @@ private void WriteJsonUriArrayWithDuplicatesCounted(string propertyName, IReadOn

this.jsonWriter.WriteArrayEnd();
}

private void WriteStringValueOrNull(string value)
{
if (value == null)
{
this.jsonWriter.WriteNullValue();
}
else
{
this.jsonWriter.WriteStringValue(value);
}
}
}
}
}
23 changes: 14 additions & 9 deletions Microsoft.Azure.Cosmos/src/Tracing/TraceWriter.TraceTextWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ public void Visit(QueryMetricsTraceDatum queryMetricsTraceDatum)
public void Visit(PointOperationStatisticsTraceDatum pointOperationStatisticsTraceDatum)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendLine($"Activity ID: {pointOperationStatisticsTraceDatum.ActivityId}");
stringBuilder.AppendLine($"Activity ID: {pointOperationStatisticsTraceDatum.ActivityId ?? "<null>"}");
stringBuilder.AppendLine($"Status Code: {pointOperationStatisticsTraceDatum.StatusCode}/{pointOperationStatisticsTraceDatum.SubStatusCode}");
stringBuilder.AppendLine($"Response Time: {pointOperationStatisticsTraceDatum.ResponseTimeUtc.ToString("hh:mm:ss:fff", CultureInfo.InvariantCulture)}");
stringBuilder.AppendLine($"Request Charge: {pointOperationStatisticsTraceDatum.RequestCharge}");
stringBuilder.AppendLine($"Request URI: {pointOperationStatisticsTraceDatum.RequestUri}");
stringBuilder.AppendLine($"Session Tokens: {pointOperationStatisticsTraceDatum.RequestSessionToken} / {pointOperationStatisticsTraceDatum.ResponseSessionToken}");
stringBuilder.AppendLine($"Request URI: {pointOperationStatisticsTraceDatum.RequestUri ?? "<null>"}");
stringBuilder.AppendLine($"Session Tokens: {pointOperationStatisticsTraceDatum.RequestSessionToken ?? "<null>"} / {pointOperationStatisticsTraceDatum.ResponseSessionToken ?? "<null>"}");
if (pointOperationStatisticsTraceDatum.ErrorMessage != null)
{
stringBuilder.AppendLine($"Error Message: {pointOperationStatisticsTraceDatum.ErrorMessage}");
Expand All @@ -349,6 +349,11 @@ public void Visit(ClientSideRequestStatisticsTraceDatum clientSideRequestStatist
Dictionary<Uri, int> uriAndCounts = new Dictionary<Uri, int>();
foreach (Uri uri in clientSideRequestStatisticsTraceDatum.ContactedReplicas)
{
if (uri == null)
{
continue;
}

if (!uriAndCounts.TryGetValue(uri, out int count))
{
count = 0;
Expand All @@ -359,19 +364,19 @@ public void Visit(ClientSideRequestStatisticsTraceDatum clientSideRequestStatist

foreach (KeyValuePair<Uri, int> uriAndCount in uriAndCounts)
{
stringBuilder.AppendLine($"{space}{uriAndCount.Key}: {uriAndCount.Value}");
stringBuilder.AppendLine($"{space}{uriAndCount.Key?.ToString() ?? "<null>"}: {uriAndCount.Value}");
}

stringBuilder.AppendLine("Failed to Contact Replicas");
foreach (Uri failedToContactReplica in clientSideRequestStatisticsTraceDatum.FailedReplicas)
{
stringBuilder.AppendLine($"{space}{failedToContactReplica}");
stringBuilder.AppendLine($"{space}{failedToContactReplica?.ToString() ?? "<null>"}");
}

stringBuilder.AppendLine("Regions Contacted");
foreach (Uri regionContacted in clientSideRequestStatisticsTraceDatum.ContactedReplicas)
{
stringBuilder.AppendLine($"{space}{regionContacted}");
stringBuilder.AppendLine($"{space}{regionContacted?.ToString() ?? "<null>"}");
}

stringBuilder.AppendLine("Address Resolution Statistics");
Expand Down Expand Up @@ -409,8 +414,8 @@ public void Visit(ClientSideRequestStatisticsTraceDatum clientSideRequestStatist
if (stat.StoreResult != null)
{
stringBuilder.AppendLine($"{space}Store Result");
stringBuilder.AppendLine($"{space}{space}Activity Id: {stat.StoreResult.ActivityId}");
stringBuilder.AppendLine($"{space}{space}Store Physical Address: {stat.StoreResult.StorePhysicalAddress}");
stringBuilder.AppendLine($"{space}{space}Activity Id: {stat.StoreResult.ActivityId ?? "<null>"}");
stringBuilder.AppendLine($"{space}{space}Store Physical Address: {stat.StoreResult.StorePhysicalAddress?.ToString() ?? "<null>"}");
stringBuilder.AppendLine($"{space}{space}Status Code: {stat.StoreResult.StatusCode}/{stat.StoreResult.SubStatusCode}");
stringBuilder.AppendLine($"{space}{space}Is Valid: {stat.StoreResult.IsValid}");
stringBuilder.AppendLine($"{space}{space}LSN Info");
Expand All @@ -419,7 +424,7 @@ public void Visit(ClientSideRequestStatisticsTraceDatum clientSideRequestStatist
stringBuilder.AppendLine($"{space}{space}{space}Global LSN: {stat.StoreResult.GlobalCommittedLSN}");
stringBuilder.AppendLine($"{space}{space}{space}Quorum Acked LSN: {stat.StoreResult.QuorumAckedLSN}");
stringBuilder.AppendLine($"{space}{space}{space}Using LSN: {stat.StoreResult.UsingLocalLSN}");
stringBuilder.AppendLine($"{space}{space}Session Token: {stat.StoreResult.SessionToken.ConvertToString()}");
stringBuilder.AppendLine($"{space}{space}Session Token: {stat.StoreResult.SessionToken?.ConvertToString() ?? "<null>"}");
stringBuilder.AppendLine($"{space}{space}Quorum Info");
stringBuilder.AppendLine($"{space}{space}{space}Current Replica Set Size: {stat.StoreResult.CurrentReplicaSetSize}");
stringBuilder.AppendLine($"{space}{space}{space}Current Write Quorum: {stat.StoreResult.CurrentWriteQuorum}");
Expand Down
Loading

0 comments on commit df040f3

Please sign in to comment.