Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sourabh1007 committed Dec 2, 2024
1 parent cf4951b commit dc3f71a
Show file tree
Hide file tree
Showing 8 changed files with 255 additions and 226 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public KeyValuePair<string, object>[] PopulateNetworkMeterDimensions(string oper
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.DbName, databaseName),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.ServerAddress, accountName?.Host),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.DbOperation, operationName),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.StatusCode, GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.SubStatusCode, GetSubStatusCode(attributes, ex))
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.StatusCode, CosmosDbMeterUtil.GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.SubStatusCode, CosmosDbMeterUtil.GetSubStatusCode(attributes, ex))
};
}

Expand All @@ -199,33 +199,9 @@ public KeyValuePair<string, object>[] PopulateOperationMeterDimensions(string op
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.DbName, databaseName),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.ServerAddress, accountName?.Host),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.DbOperation, operationName),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.StatusCode, GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.SubStatusCode, GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.ServerAddress, accountName.Host),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.DbOperation, operationName),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.Region, string.Join(",", attributes.Diagnostics.GetContactedRegions()))
};
}

private static int? GetStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.StatusCode,
_ when attributes != null => (int)attributes.StatusCode,
_ => null
};
}

private static int? GetSubStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.SubStatusCode,
_ when attributes != null => (int)attributes.SubStatusCode,
_ => null
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.StatusCode, CosmosDbMeterUtil.GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.SubStatusCode, CosmosDbMeterUtil.GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(AppInsightClassicAttributeKeys.Region, CosmosDbMeterUtil.GetRegions(attributes?.Diagnostics))
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.Linq;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Diagnostics;
using Microsoft.Azure.Cosmos.Telemetry.Models;
Expand All @@ -33,7 +34,7 @@ internal static void RecordHistogramMetric<T>(
}
catch (Exception ex)
{
DefaultTrace.TraceWarning($"Failed to record metric. {ex.StackTrace}");
DefaultTrace.TraceWarning($"Failed to record metric. {ex}");
}
}

Expand Down Expand Up @@ -64,7 +65,7 @@ internal static bool TryGetDiagnostics(OpenTelemetryAttributes attributes,
};

// Ensure diagnostics is not null and cast is valid
if (diagnostics is CosmosTraceDiagnostics traceDiagnostics)
if (diagnostics != null && diagnostics is CosmosTraceDiagnostics traceDiagnostics)
{
traces = traceDiagnostics;
return true;
Expand Down Expand Up @@ -136,5 +137,41 @@ internal static bool TryNetworkMetricsValues(

return true;
}

internal static string[] GetRegions(CosmosDiagnostics diagnostics)
{
if (diagnostics?.GetContactedRegions() is not IReadOnlyList<(string regionName, Uri uri)> contactedRegions)
{
return null;
}

return contactedRegions
.Select(region => region.regionName)
.Distinct()
.ToArray();
}

internal static int? GetStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.StatusCode,
_ when attributes != null => (int)attributes.StatusCode,
_ => null
};
}

internal static int? GetSubStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.SubStatusCode,
_ when attributes != null => (int)attributes.SubStatusCode,
_ => null
};
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal static class CosmosDbNetworkMeter
/// <summary>
/// Populator Used for Dimension Attributes
/// </summary>
private static readonly IActivityAttributePopulator DimensionPopulator = TracesStabilityFactory.GetAttributePopulator();
internal static IActivityAttributePopulator DimensionPopulator = TracesStabilityFactory.GetAttributePopulator();

private static Histogram<double> RequestLatencyHistogram = null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ internal static class CosmosDbOperationMeter
/// <summary>
/// Populator Used for Dimension Attributes
/// </summary>
private static readonly IActivityAttributePopulator DimensionPopulator = TracesStabilityFactory.GetAttributePopulator();
internal static IActivityAttributePopulator DimensionPopulator = TracesStabilityFactory.GetAttributePopulator();

/// <summary>
/// Histogram to record request latency (in seconds) for Cosmos DB operations.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ namespace Microsoft.Azure.Cosmos.Telemetry
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Xml.Linq;
using global::Azure.Core;
using Microsoft.Azure.Cosmos.Tracing.TraceData;

Expand All @@ -18,7 +16,7 @@ internal class DatabaseDupAttributeKeys : IActivityAttributePopulator
private readonly IActivityAttributePopulator otelPopulator;

public DatabaseDupAttributeKeys()
{
{
this.otelPopulator = new OpenTelemetryAttributeKeys();
this.appInsightPopulator = new AppInsightClassicAttributeKeys();
}
Expand Down Expand Up @@ -54,19 +52,9 @@ public void PopulateAttributes(DiagnosticScope scope,

public KeyValuePair<string, object>[] PopulateInstanceCountDimensions(Uri accountName)
{
KeyValuePair<string, object>[] appInsightDimensions = this.appInsightPopulator
.PopulateInstanceCountDimensions(accountName)
.ToArray();
KeyValuePair<string, object>[] otelDimensions = this.otelPopulator
.PopulateInstanceCountDimensions(accountName)
.ToArray();

KeyValuePair<string, object>[] dimensions
= new KeyValuePair<string, object>[appInsightDimensions.Length + otelDimensions.Length];
dimensions
.Concat(appInsightDimensions)
.Concat(otelDimensions);
return dimensions;
return this.MergeDimensions(
() => this.appInsightPopulator.PopulateInstanceCountDimensions(accountName),
() => this.otelPopulator.PopulateInstanceCountDimensions(accountName));
}

public KeyValuePair<string, object>[] PopulateNetworkMeterDimensions(string operationName,
Expand All @@ -78,19 +66,9 @@ public KeyValuePair<string, object>[] PopulateNetworkMeterDimensions(string oper
ClientSideRequestStatisticsTraceDatum.StoreResponseStatistics tcpStats = null,
ClientSideRequestStatisticsTraceDatum.HttpResponseStatistics? httpStats = null)
{
KeyValuePair<string, object>[] appInsightDimensions = this.appInsightPopulator
.PopulateNetworkMeterDimensions(operationName, accountName, containerName, databaseName, attributes, ex, tcpStats, httpStats)
.ToArray();
KeyValuePair<string, object>[] otelDimensions = this.otelPopulator
.PopulateNetworkMeterDimensions(operationName, accountName, containerName, databaseName, attributes, ex, tcpStats, httpStats)
.ToArray();

KeyValuePair<string, object>[] dimensions
= new KeyValuePair<string, object>[appInsightDimensions.Length + otelDimensions.Length];
dimensions
.Concat(appInsightDimensions)
.Concat(otelDimensions);
return dimensions;
return this.MergeDimensions(
() => this.appInsightPopulator.PopulateNetworkMeterDimensions(operationName, accountName, containerName, databaseName, attributes, ex, tcpStats, httpStats),
() => this.otelPopulator.PopulateNetworkMeterDimensions(operationName, accountName, containerName, databaseName, attributes, ex, tcpStats, httpStats));
}

public KeyValuePair<string, object>[] PopulateOperationMeterDimensions(string operationName,
Expand All @@ -100,18 +78,23 @@ public KeyValuePair<string, object>[] PopulateOperationMeterDimensions(string op
OpenTelemetryAttributes attributes,
Exception ex)
{
KeyValuePair<string, object>[] appInsightDimensions = this.appInsightPopulator
.PopulateOperationMeterDimensions(operationName, containerName, databaseName, accountName, attributes, ex)
.ToArray();
KeyValuePair<string, object>[] otelDimensions = this.otelPopulator
.PopulateOperationMeterDimensions(operationName, containerName, databaseName, accountName, attributes, ex)
.ToArray();
return this.MergeDimensions(
() => this.appInsightPopulator.PopulateOperationMeterDimensions(operationName, containerName, databaseName, accountName, attributes, ex),
() => this.otelPopulator.PopulateOperationMeterDimensions(operationName, containerName, databaseName, accountName, attributes, ex));
}

private KeyValuePair<string, object>[] MergeDimensions(
Func<IEnumerable<KeyValuePair<string, object>>> appInsightDimensionsProvider,
Func<IEnumerable<KeyValuePair<string, object>>> otelDimensionsProvider)
{
KeyValuePair<string, object>[] appInsightDimensions = appInsightDimensionsProvider().ToArray();
KeyValuePair<string, object>[] otelDimensions = otelDimensionsProvider().ToArray();

KeyValuePair<string, object>[] dimensions = new KeyValuePair<string, object>[appInsightDimensions.Length + otelDimensions.Length];

Array.Copy(appInsightDimensions, dimensions, appInsightDimensions.Length);
Array.Copy(otelDimensions, 0, dimensions, appInsightDimensions.Length, otelDimensions.Length);

KeyValuePair<string, object>[] dimensions
= new KeyValuePair<string, object>[appInsightDimensions.Length + otelDimensions.Length];
dimensions
.Concat(appInsightDimensions)
.Concat(otelDimensions);
return dimensions;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public void PopulateAttributes(DiagnosticScope scope,
{
scope.AddAttribute<string[]>(
OpenTelemetryAttributeKeys.Region,
GetRegions(response.Diagnostics), (input) => string.Join(",", input));
CosmosDbMeterUtil.GetRegions(response.Diagnostics), (input) => string.Join(",", input));
}

}
Expand All @@ -284,8 +284,8 @@ public KeyValuePair<string, object>[] PopulateNetworkMeterDimensions(string oper
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ServerAddress, accountName?.Host),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ServerPort, accountName?.Port),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.DbOperation, operationName),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.StatusCode, GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.SubStatusCode, GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.StatusCode, CosmosDbMeterUtil.GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.SubStatusCode, CosmosDbMeterUtil.GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ConsistencyLevel, GetConsistencyLevel(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.NetworkProtocolName, GetEndpoint(tcpStats, httpStats).Scheme),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ServiceEndpointHost, GetEndpoint(tcpStats, httpStats).Host),
Expand Down Expand Up @@ -313,54 +313,19 @@ public KeyValuePair<string, object>[] PopulateOperationMeterDimensions(string op
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ServerAddress, accountName?.Host),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ServerPort, accountName?.Port),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.DbOperation, operationName),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.StatusCode, GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.SubStatusCode, GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.StatusCode, CosmosDbMeterUtil.GetStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.SubStatusCode, CosmosDbMeterUtil.GetSubStatusCode(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ConsistencyLevel, GetConsistencyLevel(attributes, ex)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.Region, GetRegions(attributes?.Diagnostics)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.Region, CosmosDbMeterUtil.GetRegions(attributes?.Diagnostics)),
new KeyValuePair<string, object>(OpenTelemetryAttributeKeys.ErrorType, ex?.Message)
};
}

private static string[] GetRegions(CosmosDiagnostics diagnostics)
{
if (diagnostics?.GetContactedRegions() is not IReadOnlyList<(string regionName, Uri uri)> contactedRegions)
{
return null;
}

return contactedRegions
.Select(region => region.regionName)
.Distinct()
.ToArray();
}

private static int? GetStatusCode(ClientSideRequestStatisticsTraceDatum.StoreResponseStatistics tcpStats, ClientSideRequestStatisticsTraceDatum.HttpResponseStatistics? httpStats)
{
return (int?)httpStats?.HttpResponseMessage?.StatusCode ?? (int?)tcpStats?.StoreResult?.StatusCode;
}

private static int? GetStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.StatusCode,
_ when attributes != null => (int)attributes.StatusCode,
_ => null
};
}

private static int? GetSubStatusCode(OpenTelemetryAttributes attributes,
Exception ex)
{
return ex switch
{
CosmosException cosmosException => (int)cosmosException.SubStatusCode,
_ when attributes != null => (int)attributes.SubStatusCode,
_ => null
};
}

private static string GetConsistencyLevel(OpenTelemetryAttributes attributes,
Exception ex)
{
Expand Down
Loading

0 comments on commit dc3f71a

Please sign in to comment.