AI Integration: Fixes Open Telemetry Example (#3868)

* first draft

* add filter

* revert csproj

* fix sample

* changed log message

* remove unused library
This commit is contained in:
Sourabh Jain 2023-06-02 22:09:59 +05:30 коммит произвёл GitHub
Родитель 3e3bbe5d53
Коммит 387ab62dbb
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
3 изменённых файлов: 26 добавлений и 15 удалений

Просмотреть файл

@ -1,7 +1,9 @@
{
"Logging": {
"LogLevel": {
"Azure-Cosmos-Operation-Request-Diagnostics": "Information"
"OpenTelemetry": {
"LogLevel": {
"Azure.Cosmos.Operation.Request.Diagnostics": "Warning"
}
}
},
"CosmosDBEndPointUrl": "https://localhost:8081",

Просмотреть файл

@ -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>

Просмотреть файл

@ -25,8 +25,8 @@
try
{
IConfigurationRoot configuration = new ConfigurationBuilder()
.AddJsonFile("AppSettings.json")
.Build();
.AddJsonFile("AppSettings.json")
.Build();
string endpoint = configuration["CosmosDBEndPointUrl"];
if (string.IsNullOrEmpty(endpoint))
@ -52,12 +52,16 @@
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();
@ -76,6 +80,7 @@
{
IsDistributedTracingEnabled = true // Defaults to true, set to false to disable
};
// </EnableDistributedTracing>
using (CosmosClient client = new CosmosClient(endpoint, authKey, options))
{
@ -88,7 +93,6 @@
await Program.RunCrudDemo(container);
}
}
finally
{
@ -116,6 +120,15 @@
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}"));