Skip to content

Commit 387ab62

Browse files
authored
AI Integration: Fixes Open Telemetry Example (#3868)
* first draft * add filter * revert csproj * fix sample * changed log message * remove unused library
1 parent 3e3bbe5 commit 387ab62

File tree

3 files changed

+26
-15
lines changed

3 files changed

+26
-15
lines changed

Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/AppSettings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
{
22
"Logging": {
3-
"LogLevel": {
4-
"Azure-Cosmos-Operation-Request-Diagnostics": "Information"
3+
"OpenTelemetry": {
4+
"LogLevel": {
5+
"Azure.Cosmos.Operation.Request.Diagnostics": "Warning"
6+
}
57
}
68
},
79
"CosmosDBEndPointUrl": "https://localhost:8081",

Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/OpenTelemetry.csproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@
1212
<PackageReference Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.0.0-beta.10" />
1313
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.33.0-preview" />
1414
<PackageReference Include="Microsoft.Extensions.Azure" Version="1.6.3" />
15-
<PackageReference Include="OpenTelemetry" Version="1.4.0" />
16-
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="7.0.2" />
17-
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
18-
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0" />
1915
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0" />
2016
</ItemGroup>
2117

Microsoft.Azure.Cosmos.Samples/Usage/OpenTelemetry/Program.cs

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ static async Task Main()
2525
try
2626
{
2727
IConfigurationRoot configuration = new ConfigurationBuilder()
28-
.AddJsonFile("AppSettings.json")
29-
.Build();
28+
.AddJsonFile("AppSettings.json")
29+
.Build();
3030

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

5454
// Set up logging to forward logs to chosen exporter
55-
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddOpenTelemetry(options =>
56-
{
57-
options.IncludeFormattedMessage = true;
58-
options.SetResourceBuilder(resource);
59-
options.AddAzureMonitorLogExporter(o => o.ConnectionString = aiConnectionString); // Set up exporter of your choice
60-
}));
55+
using ILoggerFactory loggerFactory
56+
= LoggerFactory.Create(builder => builder
57+
.AddConfiguration(configuration.GetSection("Logging"))
58+
.AddOpenTelemetry(options =>
59+
{
60+
options.IncludeFormattedMessage = true;
61+
options.SetResourceBuilder(resource);
62+
options.AddAzureMonitorLogExporter(o => o.ConnectionString = aiConnectionString); // Set up exporter of your choice
63+
}));
64+
/*.AddFilter(level => level == LogLevel.Error) // Filter is irrespective of event type or event name*/
6165

6266
AzureEventSourceLogForwarder logforwader = new AzureEventSourceLogForwarder(loggerFactory);
6367
logforwader.Start();
@@ -76,6 +80,7 @@ static async Task Main()
7680
{
7781
IsDistributedTracingEnabled = true // Defaults to true, set to false to disable
7882
};
83+
7984
// </EnableDistributedTracing>
8085
using (CosmosClient client = new CosmosClient(endpoint, authKey, options))
8186
{
@@ -88,7 +93,6 @@ static async Task Main()
8893

8994
await Program.RunCrudDemo(container);
9095
}
91-
9296
}
9397
finally
9498
{
@@ -116,6 +120,15 @@ public static async Task RunCrudDemo(Container container)
116120
Console.WriteLine($"Read document with id: {i}");
117121
}
118122

123+
try
124+
{
125+
await container.ReadItemAsync<Item>($"random key", new PartitionKey($"random partition"));
126+
}
127+
catch(Exception)
128+
{
129+
Console.WriteLine("Generate exception by reading an invalid key");
130+
}
131+
119132
for (int i = 1; i <= 5; i++)
120133
{
121134
await container.ReplaceItemAsync(new Item { Id = $"{i}", Status = "updated" }, $"{i}", new PartitionKey($"{i}"));

0 commit comments

Comments
 (0)