-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from cloudfy/24-arcturuseventbus
24 arcturuseventbus
- Loading branch information
Showing
30 changed files
with
865 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/Arcturus.EventBus.Abstracts/Arcturus.EventBus.Abstracts.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Arcturus.EventBus.Abstracts</PackageId> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>eventbus,events,arcturus</PackageTags> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IConnection | ||
{ | ||
/// <summary> | ||
/// Gets the name of the application. | ||
/// </summary> | ||
string? ApplicationId { get; } | ||
/// <summary> | ||
/// Gets a value indicating if the connection is connected. | ||
/// </summary> | ||
bool IsConnected { get; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IEventBusFactory | ||
{ | ||
IProcessor CreateProcessor(string? queue = null); | ||
IPublisher CreatePublisher(string? queue = null); | ||
ISubscriber CreateSubscriber(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IEventMessage { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IEventMessageHandler<in TEvent> where TEvent : IEventMessage | ||
{ | ||
Task Handle(TEvent @event, CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IProcessor | ||
{ | ||
/// <summary> | ||
/// Event triggered when a message is processed asynchronously. | ||
/// </summary> | ||
event Func<IEventMessage, OnProcessEventArgs?, Task> OnProcessAsync; | ||
|
||
/// <summary> | ||
/// Waits for events to be processed. | ||
/// </summary> | ||
/// <param name="cancellationToken">Token to monitor for cancellation requests.</param> | ||
/// <returns>A task that represents the asynchronous operation.</returns> | ||
Task WaitForEvents( | ||
CancellationToken cancellationToken = default); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface IPublisher | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <typeparam name="TEvent"></typeparam> | ||
/// <param name="event"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns></returns> | ||
Task Publish<TEvent>( | ||
TEvent @event | ||
, CancellationToken cancellationToken = default) where TEvent : IEventMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public interface ISubscriber | ||
{ | ||
Task Subscribe<TEvent>( | ||
Func<TEvent, Task> handler | ||
, CancellationToken cancellationToken = default) where TEvent : IEventMessage; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
namespace Arcturus.EventBus.Abstracts; | ||
|
||
public sealed class OnProcessEventArgs : EventArgs | ||
{ | ||
public OnProcessEventArgs( | ||
string? messageId | ||
, IDictionary<string, object?>? headers | ||
, CancellationToken cancellationToken = default) | ||
{ | ||
MessageId = messageId; | ||
Headers = headers; | ||
CancellationToken = cancellationToken; | ||
} | ||
/// <summary> | ||
/// Gets a dictionary of headers or null. | ||
/// </summary> | ||
public IDictionary<string, object?>? Headers { get; } | ||
/// <summary> | ||
/// Gets an id of the message or null. | ||
/// </summary> | ||
public string? MessageId { get; } | ||
/// <summary> | ||
/// Gets a cancellation token or null. | ||
/// </summary> | ||
public CancellationToken? CancellationToken { get; } | ||
} |
17 changes: 17 additions & 0 deletions
17
src/Arcturus.EventBus.OpenTelemetry/Arcturus.EventBus.OpenTelemetry.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Arcturus.EventBus.OpenTelemetry</PackageId> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>eventbus,events,arcturus,opentelemetry</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="OpenTelemetry.Api" Version="1.10.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Arcturus.EventBus\Arcturus.EventBus.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
57 changes: 57 additions & 0 deletions
57
src/Arcturus.EventBus.OpenTelemetry/TraceProviderBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using OpenTelemetry.Context.Propagation; | ||
using OpenTelemetry.Trace; | ||
using OpenTelemetry; | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Arcturus.EventBus.Diagnostics; | ||
|
||
namespace Arcturus.EventBus.OpenTelemetry; | ||
|
||
public static class TraceProviderBuilderExtensions | ||
{ | ||
public static TracerProviderBuilder AddEventBusInstrumentation(this TracerProviderBuilder builder) | ||
{ | ||
EventBusActivitySource.ContextExtractor = OpenTelemetryContextExtractor; | ||
EventBusActivitySource.ContextInjector = OpenTelemetryContextInjector; | ||
builder.AddSource(EventBusActivitySource.ActivityName); | ||
return builder; | ||
} | ||
|
||
private static ActivityContext OpenTelemetryContextExtractor(IDiagnosticProperties props) | ||
{ | ||
// Extract the PropagationContext of the upstream parent from the message headers. | ||
var parentContext = Propagators.DefaultTextMapPropagator.Extract(default, props.Headers, OpenTelemetryContextGetter); | ||
Baggage.Current = parentContext.Baggage; | ||
return parentContext.ActivityContext; | ||
} | ||
|
||
private static IEnumerable<string> OpenTelemetryContextGetter(IDictionary<string, object?>? carrier, string key) | ||
{ | ||
try | ||
{ | ||
if (carrier is not null && carrier.TryGetValue(key, out object? value) && value is byte[] bytes) | ||
{ | ||
return [Encoding.UTF8.GetString(bytes)]; | ||
} | ||
} | ||
catch (Exception) | ||
{ | ||
//this.logger.LogError(ex, "Failed to extract trace context."); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private static void OpenTelemetryContextInjector(Activity activity, IDictionary<string, object?>? props) | ||
{ | ||
// Inject the current Activity's context into the message headers. | ||
Propagators.DefaultTextMapPropagator.Inject(new PropagationContext(activity.Context, Baggage.Current), props, OpenTelemetryContextSetter); | ||
} | ||
|
||
private static void OpenTelemetryContextSetter(IDictionary<string, object?>? carrier, string key, string value) | ||
{ | ||
if (carrier is null) | ||
return; | ||
carrier[key] = Encoding.UTF8.GetBytes(value); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
src/Arcturus.EventBus.RabbitMQ/Arcturus.EventBus.RabbitMQ.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<PackageId>Arcturus.EventBus.RabbitMQ</PackageId> | ||
<PackageLicenseExpression>MIT</PackageLicenseExpression> | ||
<PackageTags>eventbus,events,arcturus,rabbitmq</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" /> | ||
<PackageReference Include="RabbitMQ.Client" Version="7.0.0" /> | ||
<PackageReference Include="Polly" Version="8.5.0" /> | ||
<PackageReference Include="OpenTelemetry.Api" Version="1.10.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Arcturus.EventBus\Arcturus.EventBus.csproj" /> | ||
</ItemGroup> | ||
|
||
|
||
</Project> |
34 changes: 34 additions & 0 deletions
34
src/Arcturus.EventBus.RabbitMQ/Internals/DefaultEventMessageTypeResolver.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
namespace Arcturus.EventBus.RabbitMQ.Internals; | ||
|
||
/// <summary> | ||
/// Default type resolver using the AppDomain. | ||
/// </summary> | ||
internal class DefaultEventMessageTypeResolver | ||
{ | ||
private readonly Dictionary<string, Type?> _reflectionCache = []; | ||
|
||
/// <summary> | ||
/// Resolves <paramref name="typeName"/> to a <see cref="Type"/>. | ||
/// </summary> | ||
/// <param name="typeName">Required. Name of type to resolve.</param> | ||
/// <returns><see cref="Type"/> or null.</returns> | ||
internal Type? ResolveType(string typeName) | ||
{ | ||
ArgumentNullException.ThrowIfNull(typeName, nameof(typeName)); | ||
|
||
if (_reflectionCache.TryGetValue(typeName, out var type)) | ||
return type; | ||
|
||
type = AppDomain.CurrentDomain | ||
.GetAssemblies() | ||
.Select(a => a.GetType(typeName)) | ||
.Where(_ => _ is not null) | ||
.FirstOrDefault(); | ||
if (type is not null) | ||
{ | ||
_reflectionCache.Add(typeName, type); | ||
return type; | ||
} | ||
return null; | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/Arcturus.EventBus.RabbitMQ/Internals/EventMessageConverter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using Arcturus.EventBus.Abstracts; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace Arcturus.EventBus.RabbitMQ.Internals; | ||
|
||
internal sealed class EventMessageConverter : JsonConverter<IEventMessage> | ||
{ | ||
private const string DiscriminatorPropertyName = "$eventType"; | ||
private readonly DefaultEventMessageTypeResolver _typeResolver; | ||
|
||
internal EventMessageConverter() | ||
{ | ||
_typeResolver = new DefaultEventMessageTypeResolver(); | ||
} | ||
|
||
public override IEventMessage Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
{ | ||
using var document = JsonDocument.ParseValue(ref reader); | ||
var root = document.RootElement; | ||
|
||
// Get the discriminator | ||
if (!root.TryGetProperty(DiscriminatorPropertyName, out var typeProperty)) | ||
{ | ||
throw new JsonException($"Missing discriminator property '{DiscriminatorPropertyName}'."); | ||
} | ||
|
||
var typeName = typeProperty.GetString() ?? throw new JsonException("Discriminator property value is null."); | ||
var type = _typeResolver.ResolveType(typeName) ?? throw new JsonException($"Cannot resolve type '{typeName}'."); | ||
var json = root.GetRawText(); | ||
return (IEventMessage)JsonSerializer.Deserialize(json, type, options)!; | ||
} | ||
|
||
public override void Write(Utf8JsonWriter writer, IEventMessage value, JsonSerializerOptions options) | ||
{ | ||
if (value == null) | ||
{ | ||
writer.WriteNullValue(); | ||
return; | ||
}// Get the actual type of the object | ||
var type = value.GetType(); | ||
writer.WriteStartObject(); | ||
|
||
// Write the discriminator | ||
writer.WriteString(DiscriminatorPropertyName, type.FullName); | ||
|
||
// Serialize the object properties | ||
var properties = type.GetProperties(); | ||
foreach (var property in properties) | ||
{ | ||
var propertyValue = property.GetValue(value); | ||
if (propertyValue != null) | ||
{ | ||
writer.WritePropertyName(property.Name); | ||
JsonSerializer.Serialize(writer, propertyValue, propertyValue?.GetType() ?? typeof(object), options); | ||
} | ||
else if (propertyValue == null && | ||
options.DefaultIgnoreCondition != JsonIgnoreCondition.WhenWritingNull) | ||
{ | ||
writer.WritePropertyName(property.Name); | ||
writer.WriteNullValue(); | ||
} | ||
} | ||
|
||
writer.WriteEndObject(); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/Arcturus.EventBus.RabbitMQ/Internals/EventMessageSerializer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Arcturus.EventBus.Abstracts; | ||
using System.Text.Json; | ||
|
||
namespace Arcturus.EventBus.RabbitMQ.Internals; | ||
|
||
internal static class EventMessageSerializer | ||
{ | ||
private static JsonSerializerOptions _options = new() | ||
{ | ||
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull | ||
, WriteIndented = false | ||
, Converters = { new EventMessageConverter() } | ||
}; | ||
|
||
internal static string Serialize(IEventMessage @event) | ||
=> JsonSerializer.Serialize<IEventMessage>(@event, _options); | ||
internal static IEventMessage Deserialize(string message) | ||
=> JsonSerializer.Deserialize<IEventMessage>(message, _options)!; | ||
} |
Oops, something went wrong.