Skip to content

Commit

Permalink
Merge branch 'users/akotalwar/AddODEAndRDHeaders' of https://github.c…
Browse files Browse the repository at this point in the history
…om/Azure/azure-cosmos-dotnet-v3 into users/akotalwar/AddODEAndRDHeaders
  • Loading branch information
akotalwar committed Jun 2, 2023
2 parents e33d967 + b88eecb commit d13abdf
Show file tree
Hide file tree
Showing 18 changed files with 147 additions and 93 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"Logging": {
"LogLevel": {
"Azure-Cosmos-Operation-Request-Diagnostics": "Information"
"OpenTelemetry": {
"LogLevel": {
"Azure.Cosmos.Operation.Request.Diagnostics": "Warning"
}
}
},
"CosmosDBEndPointUrl": "https://localhost:8081",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.10" />
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.33.0-preview" />
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
<PackageReference Include="OpenTelemetry" Version="1.4.0" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
</ItemGroup>

Expand Down
31 changes: 22 additions & 9 deletions Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ static async Task Main()
try
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("AppSettings.json")
.Build();
.AddJsonFile("AppSettings.json")
.Build();

string endpoint = configuration["CosmosDBEndPointUrl"];
if (string.IsNullOrEmpty(endpoint))
Expand All @@ -52,12 +52,16 @@ static async Task Main()
serviceVersion: "1.0.0");

// Set up logging to forward logs to chosen exporter
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddOpenTelemetry(options =>
{
options.IncludeFormattedMessage = true;
options.SetResourceBuilder(resource);
options.AddAzureMonitorLogExporter(o => o.ConnectionString = aiConnectionString); // Set up exporter of your choice
}));
using ILoggerFactory loggerFactory
= LoggerFactory.Create(builder => builder
.AddConfiguration(configuration.GetSection("Logging"))
.AddOpenTelemetry(options =>
{
options.IncludeFormattedMessage = true;
options.SetResourceBuilder(resource);
options.AddAzureMonitorLogExporter(o => o.ConnectionString = aiConnectionString); // Set up exporter of your choice
}));
/*.AddFilter(level => level == LogLevel.Error) // Filter is irrespective of event type or event name*/

AzureEventSourceLogForwarder logforwader = new AzureEventSourceLogForwarder(loggerFactory);
logforwader.Start();
Expand All @@ -76,6 +80,7 @@ static async Task Main()
{
IsDistributedTracingEnabled = true // Defaults to true, set to false to disable
};

// </EnableDistributedTracing>
using (CosmosClient client = new CosmosClient(endpoint, authKey, options))
{
Expand All @@ -88,7 +93,6 @@ static async Task Main()

await Program.RunCrudDemo(container);
}

}
finally
{
Expand Down Expand Up @@ -116,6 +120,15 @@ public static async Task RunCrudDemo(Container container)
Console.WriteLine($"Read document with id: {i}");
}

try
{
await container.ReadItemAsync<Item>($"random key", new PartitionKey($"random partition"));
}
catch(Exception)
{
Console.WriteLine("Generate exception by reading an invalid key");
}

for (int i = 1; i <= 5; i++)
{
await container.ReplaceItemAsync(new Item { Id = $"{i}", Status = "updated" }, $"{i}", new PartitionKey($"{i}"));
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/GatewayAccountReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ await this.cosmosAuthorization.AddAuthorizationHeaderAsync(

using (ITrace trace = Trace.GetRootTrace("Account Read", TraceComponent.Transport, TraceLevel.Info))
{
IClientSideRequestStatistics stats = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace.Summary);
IClientSideRequestStatistics stats = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace);

try
{
Expand Down
2 changes: 1 addition & 1 deletion Microsoft.Azure.Cosmos/src/Handler/TransportHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ internal async Task<ResponseMessage> ProcessMessageAsync(

DocumentServiceRequest serviceRequest = request.ToDocumentServiceRequest();

ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, request.Trace.Summary);
ClientSideRequestStatisticsTraceDatum clientSideRequestStatisticsTraceDatum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, request.Trace);
serviceRequest.RequestContext.ClientRequestStatistics = clientSideRequestStatisticsTraceDatum;

//TODO: extrace auth into a separate handler
Expand Down
4 changes: 4 additions & 0 deletions Microsoft.Azure.Cosmos/src/HttpClient/CosmosHttpClientCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ namespace Microsoft.Azure.Cosmos
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.Core.Trace;
using Microsoft.Azure.Cosmos.Resource.CosmosExceptions;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.Azure.Cosmos.Tracing.TraceData;
using Microsoft.Azure.Documents;
using Microsoft.Azure.Documents.Collections;
Expand Down Expand Up @@ -361,9 +362,11 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(
}
catch (Exception e)
{
ITrace trace = NoOpTrace.Singleton;
if (clientSideRequestStatistics is ClientSideRequestStatisticsTraceDatum datum)
{
datum.RecordHttpException(requestMessage, e, resourceType, requestStartTime);
trace = datum.Trace;
}
bool isOutOfRetries = CosmosHttpClientCore.IsOutOfRetries(timeoutPolicy, startDateTimeUtc, timeoutEnumerator);

Expand Down Expand Up @@ -394,6 +397,7 @@ private async Task<HttpResponseMessage> SendHttpHelperAsync(
ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString(),
SubStatusCode = SubStatusCodes.TransportGenerated503
},
trace: trace,
innerException: e);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private async Task<ContainerProperties> ReadCollectionAsync(
{
headers.XDate = Rfc1123DateTimeCache.UtcNow();

request.RequestContext.ClientRequestStatistics = clientSideRequestStatistics ?? new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace.Summary);
request.RequestContext.ClientRequestStatistics = clientSideRequestStatistics ?? new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace);
if (clientSideRequestStatistics == null)
{
childTrace.AddDatum(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ private async Task<DocumentServiceResponse> ExecutePartitionKeyRangeReadChangeFe
}

request.Headers[HttpConstants.HttpHeaders.Authorization] = authorizationToken;
request.RequestContext.ClientRequestStatistics = clientSideRequestStatistics ?? new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace.Summary);
request.RequestContext.ClientRequestStatistics = clientSideRequestStatistics ?? new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, trace);
if (clientSideRequestStatistics == null)
{
childTrace.AddDatum("Client Side Request Stats", request.RequestContext.ClientRequestStatistics);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,8 @@ internal sealed class ClientSideRequestStatisticsTraceDatum : TraceDatum, IClien
private IReadOnlyList<StoreResponseStatistics> shallowCopyOfStoreResponseStatistics = null;
private IReadOnlyList<HttpResponseStatistics> shallowCopyOfHttpResponseStatistics = null;
private SystemUsageHistory systemUsageHistory = null;
public TraceSummary TraceSummary = null;

public ClientSideRequestStatisticsTraceDatum(DateTime startTime, TraceSummary summary)
public ClientSideRequestStatisticsTraceDatum(DateTime startTime, ITrace trace)
{
this.RequestStartTimeUtc = startTime;
this.RequestEndTimeUtc = null;
Expand All @@ -45,7 +44,7 @@ public ClientSideRequestStatisticsTraceDatum(DateTime startTime, TraceSummary su
this.FailedReplicas = new HashSet<TransportAddressUri>();
this.RegionsContacted = new HashSet<(string, Uri)>();
this.httpResponseStatistics = new List<HttpResponseStatistics>();
this.TraceSummary = summary;
this.Trace = trace;
}

public DateTime RequestStartTimeUtc { get; }
Expand Down Expand Up @@ -75,6 +74,10 @@ public IReadOnlyDictionary<string, AddressResolutionStatistics> EndpointToAddres

public HashSet<(string, Uri)> RegionsContacted { get; }

public ITrace Trace { get; private set; }

public TraceSummary TraceSummary => this.Trace?.Summary;

public IReadOnlyList<StoreResponseStatistics> StoreResponseStatisticsList
{
get
Expand Down Expand Up @@ -502,11 +505,11 @@ public HttpResponseStatistics(
if (responseMessage != null)
{
Headers headers = new Headers(GatewayStoreClient.ExtractResponseHeaders(responseMessage));
this.ActivityId = headers.ActivityId ?? Trace.CorrelationManager.ActivityId.ToString();
this.ActivityId = headers.ActivityId ?? System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString();
}
else
{
this.ActivityId = Trace.CorrelationManager.ActivityId.ToString();
this.ActivityId = System.Diagnostics.Trace.CorrelationManager.ActivityId.ToString();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private ITrace CreateTestTraceTree()

private TraceDatum GetDatumObject(string regionName1, string regionName2 = null)
{
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, new TraceSummary());
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.UtcNow, Trace.GetRootTrace(nameof(RegionContactedInDiagnosticsBenchmark)));
Uri uri1 = new Uri("http://someUri1.com");
datum.RegionsContacted.Add((regionName1, uri1));
if (regionName2 != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
TraceForBaselineTesting rootTrace;
using (rootTrace = TraceForBaselineTesting.GetRootTrace())
{
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, new TraceSummary());
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, rootTrace);
TransportAddressUri uri1 = new TransportAddressUri(new Uri("http://someUri1.com"));
TransportAddressUri uri2 = new TransportAddressUri(new Uri("http://someUri2.com"));
Expand Down Expand Up @@ -405,7 +405,7 @@
TraceForBaselineTesting rootTrace;
using (rootTrace = TraceForBaselineTesting.GetRootTrace())
{
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, new TraceSummary());
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, rootTrace);
datum.ContactedReplicas.Add(default);
TraceWriterBaselineTests.GetPrivateField<Dictionary<string, AddressResolutionStatistics>>(datum, "endpointToAddressResolutionStats").Add("asdf", default);
Expand Down Expand Up @@ -546,7 +546,7 @@
TraceForBaselineTesting rootTrace;
using (rootTrace = TraceForBaselineTesting.GetRootTrace())
{
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, new TraceSummary());
ClientSideRequestStatisticsTraceDatum datum = new ClientSideRequestStatisticsTraceDatum(DateTime.MinValue, rootTrace);
TraceWriterBaselineTests.SetEndRequestTime(datum,DateTime.MaxValue);
HttpResponseStatistics httpResponseStatistics = new HttpResponseStatistics(
Expand Down
Loading

0 comments on commit d13abdf

Please sign in to comment.