diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/ApplicationInsights.csproj b/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/ApplicationInsights.csproj index 59162abaca..2f48dc20f1 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/ApplicationInsights.csproj +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/ApplicationInsights.csproj @@ -9,8 +9,8 @@ - - + + diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/Program.cs index 526452beb2..7e1a6e2c4d 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/ApplicationInsights/Program.cs @@ -4,19 +4,18 @@ using System.Threading.Tasks; using Newtonsoft.Json; using Microsoft.Azure.Cosmos; - using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.ApplicationInsights; using Microsoft.ApplicationInsights.WorkerService; - using Microsoft.Extensions.Logging.ApplicationInsights; + using Microsoft.ApplicationInsights.DataContracts; internal class Program { private static readonly string databaseName = "samples"; private static readonly string containerName = "ai-sample"; - private static TelemetryClient _telemetryClient; + private static TelemetryClient telemetryClient; static async Task Main() { @@ -49,35 +48,57 @@ static async Task Main() services.AddApplicationInsightsTelemetryWorkerService((ApplicationInsightsServiceOptions options) => options.ConnectionString = aiConnectionString); IServiceProvider serviceProvider = services.BuildServiceProvider(); - _telemetryClient = serviceProvider.GetRequiredService(); + telemetryClient = serviceProvider.GetRequiredService(); // - CosmosClientOptions options = new CosmosClientOptions() - { - IsDistributedTracingEnabled = true // Defaults to true, set to false to disable - }; - using (CosmosClient client = new CosmosClient(endpoint, authKey, options)) - { - Console.WriteLine($"Getting container reference for {containerName}."); + var infoOperation = telemetryClient.StartOperation(".Net SDK : ApplicationInsights SDK"); // Application level activity to track the entire execution of the application - ContainerProperties properties = new ContainerProperties(containerName, partitionKeyPath: "/id"); + var gops = telemetryClient.StartOperation("GATEWAY MODE"); // Activity to track the execution of the gateway mode + await Program.RunCosmosDbOperation(ConnectionMode.Gateway, endpoint, authKey); + telemetryClient.StopOperation(gops); - await client.CreateDatabaseIfNotExistsAsync(databaseName); - Container container = await client.GetDatabase(databaseName).CreateContainerIfNotExistsAsync(properties); + var dops = telemetryClient.StartOperation("DIRECT MODE"); // Activity to track the execution of the direct mode + await Program.RunCosmosDbOperation(ConnectionMode.Direct, endpoint, authKey); + telemetryClient.StopOperation(dops); - await Program.RunCrudDemo(container); - } + telemetryClient.StopOperation(infoOperation); } finally { // Explicitly calling Flush() followed by sleep is required for Application Insights logging in console apps to ensure that telemetry is sent to the back-end even if application terminates. - _telemetryClient?.Flush(); + telemetryClient?.Flush(); await Task.Delay(5000); Console.WriteLine("End of demo."); } } + private static async Task RunCosmosDbOperation(ConnectionMode connMode, string endpoint, string authKey) + { + // + CosmosClientOptions options = new CosmosClientOptions() + { + CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions() + { + DisableDistributedTracing = false + }, + ConnectionMode = connMode + }; + // + + using (CosmosClient client = new CosmosClient(endpoint, authKey, options)) + { + Console.WriteLine($"Getting container reference for {containerName}."); + + ContainerProperties properties = new ContainerProperties(containerName, partitionKeyPath: "/id"); + + await client.CreateDatabaseIfNotExistsAsync(databaseName); + Container container = await client.GetDatabase(databaseName).CreateContainerIfNotExistsAsync(properties); + + await Program.RunCrudDemo(container); + } + } + public static async Task RunCrudDemo(Container container) { // Any operations will automatically generate telemetry diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/OpenTelemetry.csproj b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/OpenTelemetry.csproj index 83224d4570..42eb521c7a 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/OpenTelemetry.csproj +++ b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/OpenTelemetry.csproj @@ -9,10 +9,11 @@ - - - + + + + diff --git a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs index b4729e41c7..562ccb2902 100644 --- a/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs +++ b/Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs @@ -11,6 +11,7 @@ using Microsoft.Extensions.Logging; using Microsoft.Extensions.Configuration; using Azure.Monitor.OpenTelemetry.Exporter; + using System.Diagnostics; internal class Program { @@ -52,15 +53,15 @@ static async Task Main() serviceVersion: "1.0.0"); // Set up logging to forward logs to chosen exporter - using ILoggerFactory loggerFactory + 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 - })); + { + 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); @@ -69,30 +70,28 @@ static async Task Main() // Configure OpenTelemetry trace provider AppContext.SetSwitch("Azure.Experimental.EnableActivitySource", true); _traceProvider = Sdk.CreateTracerProviderBuilder() - .AddSource("Azure.Cosmos.Operation") // Cosmos DB source for operation level telemetry + .AddSource("Azure.Cosmos.Operation", // Cosmos DB source for operation level telemetry + "Azure.Cosmos.Request", // Cosmos DB source for DIRECT Mode network request level telemetry + "Sample.Application") .AddAzureMonitorTraceExporter(o => o.ConnectionString = aiConnectionString) // Set up exporter of your choice + .AddHttpClientInstrumentation() // Added to capture HTTP telemetry .SetResourceBuilder(resource) .Build(); // - // - CosmosClientOptions options = new CosmosClientOptions() + ActivitySource source = new ActivitySource("Sample.Application"); + using (_ = source.StartActivity(".Net SDK : Azure Monitor : Open Telemetry Sample")) // Application level activity to track the entire execution of the application { - IsDistributedTracingEnabled = true // Defaults to true, set to false to disable - }; - - // - using (CosmosClient client = new CosmosClient(endpoint, authKey, options)) - { - Console.WriteLine($"Getting container reference for {containerName}."); - - ContainerProperties properties = new ContainerProperties(containerName, partitionKeyPath: "/id"); - - await client.CreateDatabaseIfNotExistsAsync(databaseName); - Container container = await client.GetDatabase(databaseName).CreateContainerIfNotExistsAsync(properties); - - await Program.RunCrudDemo(container); + using (_ = source.StartActivity("GATEWAY MODE")) // Activity to track the execution of the gateway mode + { + await Program.RunCosmosDbOperation(ConnectionMode.Gateway, endpoint, authKey); + } + using (_ = source.StartActivity("DIRECT MODE")) // Activity to track the execution of the direct mode + { + await Program.RunCosmosDbOperation(ConnectionMode.Direct, endpoint, authKey); + } } + } finally { @@ -104,6 +103,32 @@ static async Task Main() } } + private static async Task RunCosmosDbOperation(ConnectionMode connMode, string endpoint, string authKey) + { + // + CosmosClientOptions options = new CosmosClientOptions() + { + CosmosClientTelemetryOptions = new CosmosClientTelemetryOptions() + { + DisableDistributedTracing = false + }, + ConnectionMode = connMode + }; + // + + using (CosmosClient client = new CosmosClient(endpoint, authKey, options)) + { + Console.WriteLine($"Getting container reference for {containerName}."); + + ContainerProperties properties = new ContainerProperties(containerName, partitionKeyPath: "/id"); + + await client.CreateDatabaseIfNotExistsAsync(databaseName); + Container container = await client.GetDatabase(databaseName).CreateContainerIfNotExistsAsync(properties); + + await Program.RunCrudDemo(container); + } + } + public static async Task RunCrudDemo(Container container) { // Any operations will automatically generate telemetry