-
-
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.
Add basic telemetry support in core message bus implementation
Adds core Execute/Notify and CreateMapper telemetry. Also provides telemetry when notifying subjects. The messaging "protocol" is specified as the assembly for the message type, and the protocol version, the assembly version. This will allow querying/tracking contracts being used. Operations for Execute methods are `process` and for notify/onnext, `send` (since they are fire&forget notifications). A new console app sample that showscases the features is also provided. Closes #73
- Loading branch information
Showing
15 changed files
with
209 additions
and
15 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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Merq; | ||
|
||
static class Telemetry | ||
{ | ||
static readonly ActivitySource tracer = new(ThisAssembly.Project.AssemblyName, ThisAssembly.Project.Version); | ||
|
||
public static Activity? StartActivity(Type type, string operation = "process", [CallerMemberName] string? member = default, [CallerFilePath] string? file = default, [CallerLineNumber] int? line = default) | ||
=> tracer.StartActivity(ActivityKind.Producer, name: $"{member}/{type.FullName}") | ||
?.SetTag("code.function", member) | ||
?.SetTag("code.filepath", file) | ||
?.SetTag("code.lineno", line) | ||
?.SetTag("messaging.system", "merq") | ||
?.SetTag("messaging.destination", type.FullName) | ||
?.SetTag("messaging.destination_kind", "topic") | ||
?.SetTag("messaging.operation", operation) | ||
?.SetTag("messaging.protocol", type.Assembly.GetName().Name) | ||
?.SetTag("messaging.protocol_version", type.Assembly.GetName().Version?.ToString() ?? "unknown"); | ||
} |
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
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,40 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net6.0</TargetFramework> | ||
<ImplicitUsings>true</ImplicitUsings> | ||
<!-- Allow inspection of generated code under obj --> | ||
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles> | ||
<GenerateDocumentationFile>false</GenerateDocumentationFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Console" /> | ||
<PackageReference Include="OpenTelemetry.Exporter.Zipkin" /> | ||
<PackageReference Include="RxFree"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Spectre.Console" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\Merq.AutoMapper\Merq.AutoMapper.csproj" /> | ||
<ProjectReference Include="..\Library1\Library1.csproj" Aliases="Library1" /> | ||
<ProjectReference Include="..\Library2\Library2.csproj" Aliases="Library2" /> | ||
</ItemGroup> | ||
|
||
<!-- Item fixes to support project references vs package references --> | ||
<ItemGroup Label="All items in this group aren't needed when referencing the nuget packages instead of project references"> | ||
<!-- Analyzers and code fixes otherwise automatically added by Merq package --> | ||
<ProjectReference Include="..\..\Merq.CodeAnalysis\Merq.CodeAnalysis.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" /> | ||
<ProjectReference Include="..\..\Merq.CodeFixes\Merq.CodeFixes.csproj" ReferenceOutputAssembly="false" OutputItemType="Analyzer" /> | ||
<!-- Source included in Merq.DependencyInjection package to register message bus. --> | ||
<Compile Include="..\..\Merq.DependencyInjection\MerqServicesExtension.cs" Link="MerqServicesExtension.cs" Visible="false" /> | ||
<!-- Dependency otherwise brought-in by Merq.DependencyInjection for automated service discovery and registration at compile-time. --> | ||
<PackageReference Include="Devlooped.Extensions.DependencyInjection.Attributed" /> | ||
</ItemGroup> | ||
|
||
</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,53 @@ | ||
extern alias Library1; | ||
extern alias Library2; | ||
using Merq; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using OpenTelemetry; | ||
using OpenTelemetry.Resources; | ||
using OpenTelemetry.Trace; | ||
using static Spectre.Console.AnsiConsole; | ||
|
||
// Initialize services | ||
var collection = new ServiceCollection(); | ||
// Library1 contains [Service]-annotated classes, which will be automatically registered here. | ||
collection.AddMessageBus(addDiscoveredServices: true, enableAutoMapping: true); | ||
|
||
var services = collection.BuildServiceProvider(); | ||
var bus = services.GetRequiredService<IMessageBus>(); | ||
|
||
// Setup OpenTelemetry: https://learn.microsoft.com/en-us/dotnet/core/diagnostics/distributed-tracing-instrumentation-walkthroughs | ||
using var tracer = Sdk | ||
.CreateTracerProviderBuilder() | ||
.SetResourceBuilder(ResourceBuilder.CreateDefault().AddService("ConsoleApp")) | ||
.AddSource("Merq.Core") | ||
.AddSource("Merq.AutoMapper") | ||
.AddConsoleExporter() | ||
.AddZipkinExporter() | ||
.Build(); | ||
|
||
MarkupLine("[yellow]Executing with command from same assembly[/]"); | ||
|
||
// NOTE: we subscribe to an event in Library2, which is (duck)compatible with the one | ||
// notified by the Library1.Echo command handler! | ||
bus.Observe<Library2::Library.OnDidSay>() | ||
.Subscribe(e => MarkupLine($"[red]Received Library2:{e.GetType().Name}.Message={e.Message}[/]")); | ||
|
||
// Also observe the original message, for comparison | ||
bus.Observe<Library1::Library.OnDidSay>() | ||
.Subscribe(e => MarkupLine($"[lime]Received Library1:{e.GetType().Name}.Message={e.Message}[/]")); | ||
|
||
// We can execute passing an object of the same type/assembly as the EchoHandler expects | ||
var message = bus.Execute(new Library1::Library.Echo("Hello World")); | ||
|
||
WriteLine(message); | ||
|
||
MarkupLine("[yellow]Executing with command from different assembly[/]"); | ||
|
||
// But we can also execute passing an object from an entirely different assembly | ||
message = bus.Execute(new Library2::Library.Echo("Hello World")); | ||
|
||
WriteLine(message); | ||
|
||
// Test rapid fire messages | ||
//Parallel.For(0, 10, i | ||
// => bus.Execute(new Library2::Library.Echo($"Hello World ({i})"))); |
Oops, something went wrong.