Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Добавли возможность настройки для логирования в трассы и в консоль #41

Open
wants to merge 42 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
f4dfcf0
Небольшой рефакторинг
Rinatich Dec 7, 2023
3a1fbfe
Добавил опции трейсинга
Rinatich Dec 7, 2023
867bf46
Добавил DiagnosticListener
rgaynetdinov Dec 8, 2023
cdc1c4f
Добавлен слушатель событий для логирования
Rinatich Dec 11, 2023
9c90357
Добавли регистрацию для инструментации
Rinatich Dec 11, 2023
64098a1
Избавился от ворнингов
Rinatich Dec 11, 2023
7b32ba8
Добавил отправку событий
Rinatich Dec 11, 2023
95f6cb4
Перенес обработку событий в отдельную инструментацию
Rinatich Dec 12, 2023
a4e970f
Удалил использование activity во всех событиях
Rinatich Dec 12, 2023
fb88fb0
Переделал настройку для проекта-примера
Rinatich Dec 12, 2023
ab07777
Перенес логику в один файл
Rinatich Dec 12, 2023
188773b
Удалил EventItemBuilder
Rinatich Dec 12, 2023
4c5494f
Перенес все нужное в проект с OpenTelemetry + удалил SupportsNullActi…
Rinatich Dec 12, 2023
cda5b0a
Вынес в отдельную настройку запись исключений
Rinatich Dec 12, 2023
1954fc0
Тестирование
Rinatich Dec 12, 2023
c314fa3
Форматирование csproj файла
rgaynetdinov Dec 13, 2023
ba4e0cb
Вынес отправку событий в отдельный класс
rgaynetdinov Dec 25, 2023
5283bdc
Перенес отправку событий в проект с абстракциями
rgaynetdinov Dec 25, 2023
1d7114e
Добавил отправку события, связанную с чтением модели
rgaynetdinov Dec 25, 2023
4b083c0
Добавил обработку события получения модели
rgaynetdinov Dec 25, 2023
e20dcbf
Добавил информацию об очереди
rgaynetdinov Dec 25, 2023
432a666
Увеличил версию
rgaynetdinov Dec 25, 2023
3a7c71a
Удалил неиспользуемый Source
rgaynetdinov Dec 25, 2023
8649320
Увеличил версию
rgaynetdinov Dec 25, 2023
380a755
Добавил маскирование в телеметрии
rgaynetdinov Jan 26, 2024
cb1bf80
Добавил логирование модели вместо сырого текста для маскирования данных
rgaynetdinov Jan 26, 2024
ab1b9db
Добавил пример для проверки
rgaynetdinov Jan 26, 2024
e7fef20
Избавился от ворнинга
rgaynetdinov Jan 26, 2024
c94f953
Обновил пакет
rgaynetdinov Jan 26, 2024
0f49a8d
Поправил заголово для контента для рендеринга логов
rgaynetdinov Jan 26, 2024
981c069
Добавил структурное логирование
rgaynetdinov Jan 30, 2024
31a8b3e
Добавил извлечение данных
rgaynetdinov Apr 4, 2024
bb83f5c
Убрал ошибки, связанные с 3-м аргументом
rgaynetdinov May 23, 2024
d278902
Merge branch 'master' into feature/145-instrumentation
rgaynetdinov May 23, 2024
7c60ca2
Исправил ошибки после слияния
rgaynetdinov May 23, 2024
2054363
Избавился от ворнингов
rgaynetdinov May 23, 2024
e81e554
Обновил пакеты
rgaynetdinov May 23, 2024
bad517f
Обновил пакеты с open telemetry
rgaynetdinov May 23, 2024
9b059a6
Правки после тестирования
rgaynetdinov May 24, 2024
4033fa2
Добавил документацию
Rinatich May 24, 2024
31b1b6e
Поправил названия тэга для очереди
rgaynetdinov May 24, 2024
9650453
Merge branch 'feature/145-instrumentation' of https://github.com/Bynd…
rgaynetdinov May 24, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Byndyusoft.Net.RabbitMq.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Byndyusoft/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Liveness/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=middlewares/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<Project>
<PropertyGroup>
<Version>0.8.0</Version>
<Version>0.9.0</Version>
<Authors>Byndyusoft</Authors>
<PackageTags>Byndyusoft;RabbitMq</PackageTags>
<RepositoryUrl>https://github.com/Byndyusoft/Byndyusoft.Net.RabbitMq</RepositoryUrl>
Expand Down
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,51 @@ public class QueueHostedService : BackgroundService
}
```

## Конфигурация

```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOpenTelemetry()
.WithTracing(builder => builder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Byndyusoft.Net.RabbitMq"))
.AddJaegerExporter(jaeger =>
{
jaeger.AgentHost = "localhost";
jaeger.AgentPort = 6831;
})
.AddRabbitMqClientInstrumentation(o =>
{
options.LogEventsInTrace = false;
options.LogEventsInLogs = true;
options.TagRequestParamsInTrace = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

RequestParams странно смотрится в реббите

options.EnrichLogsWithParams = true;
options.EnrichLogsWithQueueInfo = true;
options.RecordExceptions = true;
options.LogContentType = LogContentType.ReadModel;
}));
services.AddRabbitMqClient("host=localhost;username=guest;password=guest");
...
}
}
```

Параметры конфигурации инструментации трассировки:
1. LogEventsInTrace — создавать события в трассировке. По умолчанию: false.
2. LogEventsInLogs — логировать события. По умолчанию: true.
3. TagRequestParamsInTrace — добавлять параметры сообщения в тэги трассировки. По умолчанию: true. Чтобы узнать, как извлекаются данные, см. [здесь](https://github.com/Byndyusoft/Byndyusoft.Telemetry#object-telemetry-item-collector).
4. EnrichLogsWithParams — обогатить каждый лог параметрами сообщения в контексте обработки этого сообщения. По умолчанию: true. Чтобы узнать, как извлекаются данные, см. [здесь](https://github.com/Byndyusoft/Byndyusoft.Telemetry#object-telemetry-item-collector).
5. EnrichLogsWithQueueInfo — обогатить каждый лог информацией об очереди в контексте обработки этого сообщения. Default: true.
6. RecordExceptions — помечать ошибки в трассировке. По умолчанию: true.
7. LogContentType — способ извлечения содержимого сообщения при его обработке. По умолчанию: ReadModel.
* ReadModel - будет логироваться десериализованная модель в отдельном событии трассировки и/или в отдельной записи лога. В этом случае можно маскировать данные. См. [здесь](https://github.com/Byndyusoft/Byndyusoft.MaskedSerialization)
* Skip - не будет логироваться содержимое сообщения.
* RawString - будет логироваться содержимое сообщения как есть (строка). Маскировка данных работать не будет.

<br/>

[![(License)](https://img.shields.io/github/license/Byndyusoft/Byndyusoft.Net.RabbitMq.svg)](LICENSE)

| Package name | Nuget | Downloads |
Expand Down
7 changes: 5 additions & 2 deletions example/Example.ConsoleApp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,15 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Byndyusoft.Logging.OpenTelemetry" Version="3.0.1" />
<PackageReference Include="Byndyusoft.Net.Http.MessagePack" Version="0.5.3" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageReference Include="OpenTelemetry" Version="1.8.0" />
<PackageReference Include="OpenTelemetry" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Exporter.Jaeger" Version="1.5.1" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.0" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.8.1" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.6" />
<PackageReference Include="OpenTelemetry.Extensions.Hosting" Version="1.8.1" />
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.8.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions example/HostedServices/PullingExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Byndyusoft.Messaging.RabbitMq;
using Byndyusoft.Messaging.RabbitMq.Diagnostics;
using Microsoft.Extensions.Hosting;

namespace Byndyusoft.Net.RabbitMq.HostedServices
Expand Down Expand Up @@ -37,6 +38,7 @@ await _rabbitMqClient.CreateQueueAsync(_rabbitMqClient.Options.NamingConventions
{
var model = await message.Content.ReadFromJsonAsync<Message>(
cancellationToken: stoppingToken);
RabbitMqClientEvents.OnMessageModelRead(model);
Console.WriteLine(JsonSerializer.Serialize(model));
await _rabbitMqClient.CompleteMessageAsync(message, ConsumeResult.Ack, stoppingToken);
continue;
Expand Down
2 changes: 2 additions & 0 deletions example/HostedServices/QueueInstallerHostedService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
Console.WriteLine($"Retry count {message.RetryCount}");

// TODO Expression is alway true. Возможно, проверка не нужна
// ReSharper disable once ConditionIsAlwaysTrueOrFalse
if (message.RetryCount >= 0)
return Task.FromResult(ConsumeResult.Retry);

Expand Down
4 changes: 3 additions & 1 deletion example/HostedServices/SubscribeAsMessagePackExample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using Byndyusoft.Messaging.RabbitMq;
using Byndyusoft.Messaging.RabbitMq.Diagnostics;
using Byndyusoft.Messaging.RabbitMq.Utils;
using MessagePack;
using Microsoft.Extensions.Hosting;
Expand All @@ -25,6 +26,7 @@ async Task<ConsumeResult> OnMessage(ReceivedRabbitMqMessage message, Cancellatio
{
var model = await message.Content.ReadFromMessagePackAsync<T>(options, token)
.ConfigureAwait(false);
RabbitMqClientEvents.OnMessageModelRead(model);
var result = await onMessage(model, token).ConfigureAwait(false);
return result;
}
Expand Down Expand Up @@ -83,7 +85,7 @@ await Task.Run(async () =>
var rand = new Random();
while (stoppingToken.IsCancellationRequested == false)
{
var message = new Message { Property = "messagepack-example" };
var message = new Message { Property = "messagepack-example", Secret = "pwd" };
await _rabbitMqClient.PublishAsMessagePackAsync(null, queueName, message, null, stoppingToken);
await Task.Delay(TimeSpan.FromSeconds(rand.NextDouble()), stoppingToken);
}
Expand Down
7 changes: 6 additions & 1 deletion example/Message.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using Byndyusoft.MaskedSerialization.Annotations.Attributes;
using Byndyusoft.Telemetry.Abstraction.Attributes;

namespace Byndyusoft.Net.RabbitMq
{
public class Message
{
public string Property { get; set; } = default!;
[TelemetryItem] public string Property { get; set; } = default!;

[Masked] public string Secret { get; set; } = default!;
}
}
103 changes: 56 additions & 47 deletions example/Program.cs
Original file line number Diff line number Diff line change
@@ -1,78 +1,87 @@
using System.Diagnostics;
using System.Threading.Tasks;
using Byndyusoft.Net.RabbitMq.HostedServices;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using OpenTelemetry;
using OpenTelemetry.Resources;
using OpenTelemetry.Trace;

namespace Byndyusoft.Net.RabbitMq
{
public static class Program
{
private static readonly ActivitySource ActivitySource = new(nameof(Program));

public static async Task Main(string[] args)
{
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.SetResourceBuilder(ResourceBuilder.CreateDefault()
.AddService("Byndyusoft.Net.RabbitMq"))
.SetSampler(new AlwaysOnSampler())
.AddSource(ActivitySource.Name)
.AddJaegerExporter(jaeger =>
{
jaeger.AgentHost = "localhost";
jaeger.AgentPort = 6831;
})
.AddRabbitMqClientInstrumentation()
.Build();

await CreateHostBuilder(args).RunConsoleAsync();
}

private static IHostBuilder CreateHostBuilder(string[] args)
{
return Host.CreateDefaultBuilder(args)
.ConfigureHostOptions(options =>
{
options.BackgroundServiceExceptionBehavior = BackgroundServiceExceptionBehavior.Ignore;
})
.ConfigureLogging(log => log.AddConsole())
.ConfigureAppConfiguration(configuration => { configuration.AddJsonFile("appsettings.json", true); })
.ConfigureServices((_, services) =>
.ConfigureWebHostDefaults(webBuilder =>
{
services.AddRabbitMqRpc();
services.AddSingleton<MathRpcServiceClient>();
services.AddRpcService<MathRpcService>();
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureLogging(i => i.AddConsole());
//webBuilder.UseSerilog((context, configuration) => configuration
// .UseDefaultSettings(context.Configuration)
// .UseOpenTelemetryTraces()
// .WriteToOpenTelemetry(activityEventBuilder: StructuredActivityEventBuilder.Instance));
});
}
}

//services.AddHostedService<PullingExample>();
//services.AddHostedService<RetryAndErrorExample>();
services.AddHostedService<RpcExample>();
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddOpenTelemetry()
.WithTracing(builder => builder
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("Byndyusoft.Net.RabbitMq"))
.AddJaegerExporter(jaeger =>
{
jaeger.AgentHost = "localhost";
jaeger.AgentPort = 6831;
})
.AddRabbitMqClientInstrumentation(o =>
{
o.LogEventsInTrace = true;
o.LogEventsInLogs = true;
o.RecordExceptions = true;
}));
services.AddRabbitMqRpc();
services.AddSingleton<MathRpcServiceClient>();
services.AddRpcService<MathRpcService>();

//services.AddHostedService<SubscribeAsMessagePackExample>();
//services.AddHostedService<PullingExample>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Зачем эти участки? Может их удалить или описать в README, как пользоваться примерами?

//services.AddHostedService<RetryAndErrorExample>();
//services.AddHostedService<RpcExample>();

//services.AddHostedService<RpcServerExample>();
//services.AddHostedService<SubscribeAsExample>();
//services.AddHostedService<SubscribeAsJsonExample>();
//services.AddHostedService<SubscribeExchangeExample>();
//services.AddHostedService<ClientFactoryExample>();
services.AddHostedService<SubscribeAsMessagePackExample>();

//services.AddHostedService<QueueInstallerHostedService>();
//services.AddHostedService<RpcServerExample>();
//services.AddHostedService<SubscribeAsExample>();
//services.AddHostedService<SubscribeAsJsonExample>();
//services.AddHostedService<SubscribeExchangeExample>();
//services.AddHostedService<ClientFactoryExample>();

//services.AddRabbitMqClient("host=localhost;username=guest;password=guest");
//services.AddHostedService<QueueInstallerHostedService>();

//services.AddRabbitMqClient("client-factory", "host=localhost;username=guest;password=guest");
services.AddInMemoryRabbitMqClient();
services.AddRabbitMqClient("host=localhost;username=guest;password=guest");

services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
});
//services.AddRabbitMqClient("client-factory", "host=localhost;username=guest;password=guest");
//services.AddInMemoryRabbitMqClient();

services.BuildServiceProvider(new ServiceProviderOptions
{
ValidateOnBuild = true,
ValidateScopes = true
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
}
}
}
10 changes: 10 additions & 0 deletions example/appsettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
12 changes: 0 additions & 12 deletions nuget.config

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<ItemGroup>
<PackageReference Include="System.Text.Json" Version="7.0.0" />
<PackageReference Include="Byndyusoft.Net.Http.Json" Version="0.3.1" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" Version="6.0.0" />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Исправить форматирование

</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Net.Http;

namespace Byndyusoft.Messaging.RabbitMq
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Byndyusoft.Messaging.RabbitMq.Diagnostics.Consts
{
public class DiagnosticNames
{
public static string RabbitMq = "Byndyusoft.RabbitMq";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace Byndyusoft.Messaging.RabbitMq.Diagnostics.Consts
{
public class EventNames
{
public static string MessagePublishing = "Byndyusoft.RabbitMq.Message.Publishing";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$"{DiagnosticNames}.Message.Publishing" ?

public static string MessageReturned = "Byndyusoft.RabbitMq.Message.Returned";
public static string MessageGot = "Byndyusoft.RabbitMq.Message.Got";
public static string MessageModelRead = "Byndyusoft.RabbitMq.Message.ModelRead";
public static string MessageReplied = "Byndyusoft.RabbitMq.Message.Replied";
public static string MessageConsumed = "Byndyusoft.RabbitMq.Message.Consumed";
public static string UnhandledException = "Byndyusoft.RabbitMq.UnhandledException";
}
}
Loading