Skip to content

Commit

Permalink
Add new examples
Browse files Browse the repository at this point in the history
Fixes #8
Part of #9

* Add NuGet.config allow `dotnet tool restore`.
* Add example for a MultiOutputService
* Add example for a OpenTelemetry

Signed-off-by: jan.jansen <[email protected]>
  • Loading branch information
farodin91 committed Jan 21, 2021
1 parent 1dbe7f2 commit 2e7a0d5
Show file tree
Hide file tree
Showing 30 changed files with 512 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .config/dotnet-tools.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"isRoot": true,
"tools": {
"dotnet-format": {
"version": "5.0.142902",
"version": "5.0.206801",
"commands": [
"dotnet-format"
]
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/dotnet.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
with:
dotnet-version: 5.0.x
- name: restore format
run: dotnet tool install dotnet-format --version 5.0.142902+8012f4ede1bb9a4e90ac01efd204b0f1ee428f10 --add-source https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json
run: dotnet tool restore --configfile NuGet.config
- name: check format
run: dotnet format --check ./Motor.NET.sln

Expand Down
32 changes: 31 additions & 1 deletion Motor.NET.sln
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,14 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "examples", "examples", "{3D
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeAndPublishWithRabbitMQ", "examples\ConsumeAndPublishWithRabbitMQ\ConsumeAndPublishWithRabbitMQ.csproj", "{946D0F5D-EC69-49A7-A7A9-16B4A0648247}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Metrics", "examples\Metrics\Metrics.csproj", "{66983F53-AA09-46B7-839C-F23BAE73433C}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MetricsExample", "examples\MetricsExample\MetricsExample.csproj", "{66983F53-AA09-46B7-839C-F23BAE73433C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeAndPublishWithKafka", "examples\ConsumeAndPublishWithKafka\ConsumeAndPublishWithKafka.csproj", "{FA64170E-BF10-4670-84C6-1EF36E143E08}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ConsumeAndMultiOutputPublisherWithRabbitMQ", "examples\ConsumeAndMultiOutputPublisherWithRabbitMQ\ConsumeAndMultiOutputPublisherWithRabbitMQ.csproj", "{F8386DEA-8905-4157-B949-CC151F3FB8D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenTelemetryExample", "examples\OpenTelemetryExample\OpenTelemetryExample.csproj", "{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -558,6 +562,30 @@ Global
{FA64170E-BF10-4670-84C6-1EF36E143E08}.Release|x64.Build.0 = Release|Any CPU
{FA64170E-BF10-4670-84C6-1EF36E143E08}.Release|x86.ActiveCfg = Release|Any CPU
{FA64170E-BF10-4670-84C6-1EF36E143E08}.Release|x86.Build.0 = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|x64.ActiveCfg = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|x64.Build.0 = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|x86.ActiveCfg = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Debug|x86.Build.0 = Debug|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|Any CPU.Build.0 = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|x64.ActiveCfg = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|x64.Build.0 = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|x86.ActiveCfg = Release|Any CPU
{F8386DEA-8905-4157-B949-CC151F3FB8D2}.Release|x86.Build.0 = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|x64.ActiveCfg = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|x64.Build.0 = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|x86.ActiveCfg = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Debug|x86.Build.0 = Debug|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|Any CPU.Build.0 = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x64.ActiveCfg = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x64.Build.0 = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x86.ActiveCfg = Release|Any CPU
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -601,6 +629,8 @@ Global
{946D0F5D-EC69-49A7-A7A9-16B4A0648247} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
{66983F53-AA09-46B7-839C-F23BAE73433C} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
{FA64170E-BF10-4670-84C6-1EF36E143E08} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
{F8386DEA-8905-4157-B949-CC151F3FB8D2} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
{E7F2E446-E2F5-4AD9-8328-42F1B5B4E245} = {3DC7D216-6908-4759-B86F-759FDAE393D9}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5E91C34C-3AEC-4084-BA02-753C9236AA34}
Expand Down
10 changes: 10 additions & 0 deletions NuGet.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="dotnet-tools" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet-tools/nuget/v3/index.json" />
<add key="dotnet5" value="https://pkgs.dev.azure.com/dnceng/public/_packaging/dotnet5/nuget/v3/index.json" />
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
</packageSources>
<disabledPackageSources />
</configuration>
4 changes: 3 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ You find working examples for different use-cases under the [examples](./example

- [Consume and publish to RabbitMQ](./examples/ConsumeAndPublishWithRabbitMQ)
- [Consume and publish to Kafka](./examples/ConsumeAndPublishWithKafka)
- [Create a service with metrics](./examples/Metrics)
- [Consume and publish multiple messages at once to RabbitMQ](./examples/ConsumeAndMultiOutputPublishWithRabbitMQ)
- [Create a service with metrics](./examples/MetricsExample)
- [Create a service with custom traces](./examples/OpenTelemetryExample)

## Support Matrix

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<Product>Motor.NET</Product>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Motor.Extensions.Conversion.SystemJson\Motor.Extensions.Conversion.SystemJson.csproj" />
<ProjectReference Include="..\..\src\Motor.Extensions.Hosting.RabbitMQ\Motor.Extensions.Hosting.RabbitMQ.csproj" />
<ProjectReference Include="..\..\src\Motor.Extensions.Utilities\Motor.Extensions.Utilities.csproj" />
</ItemGroup>

<ItemGroup>
<None Update="appsettings.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="appsettings.Production.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace ConsumeAndMultiOutputPublisherWithRabbitMQ.Model
{
public record OutputMessage
{
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using ConsumeAndMultiOutputPublisherWithRabbitMQ.Model;
using Motor.Extensions.Hosting.Abstractions;

namespace ConsumeAndMultiOutputPublisherWithRabbitMQ
{
public class MultiOutputService : IMultiOutputService<InputMessage, OutputMessage>
{
// Handle incoming messages
public Task<IEnumerable<MotorCloudEvent<OutputMessage>>> ConvertMessageAsync(
MotorCloudEvent<InputMessage> inputEvent,
CancellationToken token = default)
{
// Get the input message from the cloud event
var input = inputEvent.TypedData;

// Do your magic here .....
var output = MagicFunc(input);

// Create a new cloud event from your output message which is automatically published and return a new task.
var outputEvent = output.Select(singleEvent => inputEvent.CreateNew(singleEvent));
return Task.FromResult(outputEvent);
}

private static IEnumerable<OutputMessage> MagicFunc(InputMessage input)
{
if (string.IsNullOrEmpty(input.FancyText))
{
// Reject message in RabbitMQ queue (Any ArgumentException can be used to reject to messages.).
throw new ArgumentNullException("FancyText is empty");
}

return new List<OutputMessage>
{
new()
{
NotSoFancyText = input.FancyText.Reverse().ToString(),
NotSoFancyNumber = input.FancyNumber * -1,
},
new()
{
NotSoFancyText = input.FancyText,
NotSoFancyNumber = input.FancyNumber * -2,
},
};
}
}
}
37 changes: 37 additions & 0 deletions examples/ConsumeAndMultiOutputPublisherWithRabbitMQ/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using ConsumeAndMultiOutputPublisherWithRabbitMQ;
using ConsumeAndMultiOutputPublisherWithRabbitMQ.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Motor.Extensions.Conversion.SystemJson;
using Motor.Extensions.Hosting.Abstractions;
using Motor.Extensions.Hosting.Consumer;
using Motor.Extensions.Hosting.Publisher;
using Motor.Extensions.Hosting.RabbitMQ;
using Motor.Extensions.Utilities;

await MotorHost.CreateDefaultBuilder()
// Configure the types of the input and output messages
.ConfigureMultiOutputService<InputMessage, OutputMessage>()
.ConfigureServices((_, services) =>
{
// Add a handler for the input message which returns an output message
// This handler is called for every new incoming message
services.AddTransient<IMultiOutputService<InputMessage, OutputMessage>, MultiOutputService>();
})
// Add the incomming communication module.
.ConfigureConsumer<InputMessage>((_, builder) =>
{
// In this case the messages are received from RabbitMQ
builder.AddRabbitMQ();
// The encoding of the incoming message, such that the handler is able to deserialize the message
builder.AddSystemJson();
})
// Add the outgoing communication module.
.ConfigurePublisher<OutputMessage>((_, builder) =>
{
// In this case the messages are send to RabbitMQ
builder.AddRabbitMQ();
// The encoding of the outgoing message, such that the handler is able to serialize the message
builder.AddSystemJson();
})
.RunConsoleAsync();
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:58904/",
"sslPort": 44350
}
},
"profiles": {
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"ConsumeAndPublishWithRabbitMQ": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:5001;http://localhost:5000"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Information"
}
},
"RabbitMQConsumer": {
"Queue": {
"Name": "ExampleProductionQueue"
}
},
"RabbitMQPublisher": {
"PublishingTarget": {
"RoutingKey": "production"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"Serilog": {
"MinimumLevel": {
"Default": "Debug",
"Override": {
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information",
"System": "Warning"
}
}
},
"RabbitMQConsumer": {
"Host": "localhost",
"VirtualHost": "/",
"User": "guest",
"Password": "guest",
"Queue": {
"Name": "ExampleQueue",
"Bindings": [
{
"Exchange": "amq.topic",
"RoutingKey": "input"
}
]
},
"PrefetchCount": 10
},
"RabbitMQPublisher": {
"Host": "localhost",
"VirtualHost": "/",
"User": "guest",
"Password": "guest",
"PublishingTarget": {
"Exchange": "amq.topic",
"RoutingKey": "ouput"
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Motor.Extensions.Diagnostics.Metrics.Abstractions;
using Prometheus.Client.Abstractions;

namespace Metrics.DifferentNamespace
namespace MetricsExample.DifferentNamespace
{
public interface IServiceInDifferentNamespace
{
Expand All @@ -14,7 +14,7 @@ public class ServiceInDifferentNamespace : IServiceInDifferentNamespace

public ServiceInDifferentNamespace(IMetricsFactory<ServiceInDifferentNamespace>? metricsFactory)
{
// Resulting label in Prometheus: metrics_differentnamespace_counter_in_different_namespace
// Resulting label in Prometheus: metricsexample_differentnamespace_counter_in_different_namespace
_counter = metricsFactory?.CreateCounter("counter_in_different_namespace", "This counts something else.");
}

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Metrics.Model
namespace MetricsExample.Model
{
public record InputMessage
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Metrics;
using Metrics.DifferentNamespace;
using Metrics.Model;
using MetricsExample;
using MetricsExample.DifferentNamespace;
using MetricsExample.Model;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Motor.Extensions.Conversion.SystemJson;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
using System.Threading;
using System.Threading.Tasks;
using Metrics.DifferentNamespace;
using Metrics.Model;
using MetricsExample.DifferentNamespace;
using MetricsExample.Model;
using Motor.Extensions.Diagnostics.Metrics.Abstractions;
using Motor.Extensions.Hosting.Abstractions;
using Prometheus.Client.Abstractions;

namespace Metrics
namespace MetricsExample
{
public class ServiceWithMetrics : INoOutputService<InputMessage>
{
Expand All @@ -19,11 +19,11 @@ public ServiceWithMetrics(IMetricsFactory<ServiceWithMetrics> metricFactory,
{
_serviceInDifferentNamespace = serviceInDifferentNamespace;

// Resulting label in Prometheus: metrics_emtpy_string_total
// Resulting label in Prometheus: metricsexample_emtpy_string_total
_counter = metricFactory?.CreateCounter("empty_string_total",
"Counts the total number of recieved empty strings.");

// Resulting label in Prometheus: metrics_fancy_number
// Resulting label in Prometheus: metricsexample_fancy_number
_summary = metricFactory?.CreateSummary("fancy_number",
"Shows the distribution of fancy numbers.");
}
Expand Down
File renamed without changes.
8 changes: 8 additions & 0 deletions examples/OpenTelemetryExample/Model/InputMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OpenTelemetryExample.Model
{
public record InputMessage
{
public string FancyText { get; set; } = "FooBar";
public int FancyNumber { get; set; } = 42;
}
}
8 changes: 8 additions & 0 deletions examples/OpenTelemetryExample/Model/OutputMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace OpenTelemetryExample.Model
{
public record OutputMessage
{
public string NotSoFancyText { get; set; }
public int NotSoFancyNumber { get; set; }
}
}
Loading

0 comments on commit 2e7a0d5

Please sign in to comment.