diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..eebc8cb --- /dev/null +++ b/.gitattributes @@ -0,0 +1,4 @@ +# Auto detect text files and perform LF normalization + +* text=auto +*.sh text eol=lf diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..b15d368 --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,40 @@ +# If this file is renamed, the incrementing run attempt number will be reset. + +name: CI + +on: + push: + branches: [ "dev", "main" ] + pull_request: + branches: [ "dev", "main" ] + +env: + CI_BUILD_NUMBER_BASE: ${{ github.run_number }} + CI_TARGET_BRANCH: ${{ github.head_ref || github.ref_name }} + +jobs: + build: + + runs-on: ubuntu-latest + + permissions: + contents: write + + steps: + - uses: actions/checkout@v4 + - name: Setup + uses: actions/setup-dotnet@v4 + with: + dotnet-version: 9.0.x + - name: Compute build number + shell: bash + run: | + echo "CI_BUILD_NUMBER=$(($CI_BUILD_NUMBER_BASE+2300))" >> $GITHUB_ENV + - name: Build and Publish + env: + DOTNET_CLI_TELEMETRY_OPTOUT: true + NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + shell: pwsh + run: | + ./Build.ps1 diff --git a/Build.ps1 b/Build.ps1 new file mode 100644 index 0000000..7952809 --- /dev/null +++ b/Build.ps1 @@ -0,0 +1,80 @@ +Write-Output "build: Tool versions follow" + +dotnet --version +dotnet --list-sdks + +Write-Output "build: Build started" + +Push-Location $PSScriptRoot +try { + if(Test-Path .\artifacts) { + Write-Output "build: Cleaning ./artifacts" + Remove-Item ./artifacts -Force -Recurse + } + + & dotnet restore --no-cache + + $dbp = [Xml] (Get-Content .\Directory.Version.props) + $versionPrefix = $dbp.Project.PropertyGroup.VersionPrefix + + Write-Output "build: Package version prefix is $versionPrefix" + + $branch = @{ $true = $env:CI_TARGET_BRANCH; $false = $(git symbolic-ref --short -q HEAD) }[$NULL -ne $env:CI_TARGET_BRANCH]; + $revision = @{ $true = "{0:00000}" -f [convert]::ToInt32("0" + $env:CI_BUILD_NUMBER, 10); $false = "local" }[$NULL -ne $env:CI_BUILD_NUMBER]; + $suffix = @{ $true = ""; $false = "$($branch.Substring(0, [math]::Min(10,$branch.Length)) -replace '([^a-zA-Z0-9\-]*)', '')-$revision"}[$branch -eq "main" -and $revision -ne "local"] + $commitHash = $(git rev-parse --short HEAD) + $buildSuffix = @{ $true = "$($suffix)-$($commitHash)"; $false = "$($branch)-$($commitHash)" }[$suffix -ne ""] + + Write-Output "build: Package version suffix is $suffix" + Write-Output "build: Build version suffix is $buildSuffix" + + foreach ($src in Get-ChildItem src/*) { + Push-Location $src + + Write-Output "build: Packaging project in $src" + + if ($suffix) { + & dotnet publish -c Release -o ./obj/publish --version-suffix=$buildSuffix /p:ContinuousIntegrationBuild=true + & dotnet pack -c Release -o ../../artifacts --no-build --version-suffix=$suffix + } else { + & dotnet publish -c Release -o ./obj/publish /p:ContinuousIntegrationBuild=true + & dotnet pack -c Release -o ../../artifacts --no-build + } + if($LASTEXITCODE -ne 0) { throw "Packaging failed" } + + Pop-Location + } + + if(Test-Path .\test) { + foreach ($test in Get-ChildItem test/*.Tests) { + Push-Location $test + + Write-Output "build: Testing project in $test" + + & dotnet test -c Release --no-build --no-restore + if($LASTEXITCODE -ne 0) { throw "Testing failed" } + + Pop-Location + } + } + + if ($env:NUGET_API_KEY) { + # GitHub Actions will only supply this to branch builds and not PRs. We publish + # builds from any branch this action targets (i.e. main and dev). + + Write-Output "build: Publishing NuGet packages" + + foreach ($nupkg in Get-ChildItem artifacts/*.nupkg) { + & dotnet nuget push -k $env:NUGET_API_KEY -s https://api.nuget.org/v3/index.json "$nupkg" + if($LASTEXITCODE -ne 0) { throw "Publishing failed" } + } + + if (!($suffix)) { + Write-Output "build: Creating release for version $versionPrefix" + + iex "gh release create v$versionPrefix --title v$versionPrefix --generate-notes $(get-item ./artifacts/*.nupkg) $(get-item ./artifacts/*.snupkg)" + } + } +} finally { + Pop-Location +} diff --git a/Directory.Version.props b/Directory.Version.props new file mode 100644 index 0000000..e7bbeca --- /dev/null +++ b/Directory.Version.props @@ -0,0 +1,5 @@ + + + 1.0.0 + + diff --git a/LICENSE b/LICENSE index 261eeb9..d4165e1 100644 --- a/LICENSE +++ b/LICENSE @@ -1,3 +1,20 @@ +Seq.Input.RabbitMQ is Copyright (c) 2019 Datalust Pty Ltd and Contributors, +provided subject to the Apache License, Version 2.0. + +The Seq.Input.RabbitMQ NuGet package includes binary components licensed by +their respective copyright owners, including but not limited to: + + * .NET CoreFX components (System.*.dll) - MIT License, + https://github.com/dotnet/corefx/blob/master/LICENSE.TXT + + * Microsoft .NET libraries (System.*.dll) - MS-.NET-Library License, + https://dotnet.microsoft.com/dotnet_library_license.htm + + * RabbitMQ.Client.dll - Apache 2.0 license, + http://www.rabbitmq.com/dotnet.html + +--- + Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ diff --git a/README.md b/README.md index b50a39d..37c5bff 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,15 @@ -# seq-input-rabbitmq -An example Seq custom input that pulls events from RabbitMQ +# Seq.Input.RabbitMQ [![CI](https://github.com/datalust/seq-input-rabbitmq/actions/workflows/ci.yml/badge.svg)](https://github.com/datalust/seq-input-rabbitmq/actions/workflows/ci.yml) + +A Seq custom input that pulls events from RabbitMQ. **Requires Seq 2025.2+.** + +### Getting started + +The app is published to NuGet as [_Seq.Input.RabbitMQ_](https://nuget.org/packages/seq.input.rabbitmq). Follow the instructions for [installing a Seq App](https://docs.getseq.net/docs/installing-seq-apps) and start an instance of the app, providing your RabbitMQ server details. + +### Sending events to the input + +The input accepts events in [compact JSON format](https://github.com/serilog/serilog-formatting-compact#format-details), encoded as UTF-8 text. + +The [_Serilog.Sinks.RabbitMQ_ sink](https://github.com/sonicjolt/serilog-sinks-rabbitmq), along with the [_Serilog.Formatting.Compact_ formatter](https://github.com/serilog/serilog-formatting-compact), can be used for this. + +See the _Demo_ project included in the repository for an example of client configuration that works with the default input configuration. diff --git a/asset/seq-input-rabbitmq.png b/asset/seq-input-rabbitmq.png new file mode 100644 index 0000000..5db3771 Binary files /dev/null and b/asset/seq-input-rabbitmq.png differ diff --git a/example/Demo/Demo.csproj b/example/Demo/Demo.csproj new file mode 100644 index 0000000..90b3739 --- /dev/null +++ b/example/Demo/Demo.csproj @@ -0,0 +1,14 @@ + + + + net9.0 + Exe + + + + + + + + + diff --git a/example/Demo/Program.cs b/example/Demo/Program.cs new file mode 100644 index 0000000..e2d9bb0 --- /dev/null +++ b/example/Demo/Program.cs @@ -0,0 +1,22 @@ +using System.Threading; +using Serilog; +using Serilog.Formatting.Compact; + +Log.Logger = new LoggerConfiguration() + .Enrich.WithProperty("Application", "Demo") + .WriteTo.RabbitMQ((client, sink) => + { + client.Hostnames.Add("localhost"); + client.Username = "guest"; + client.Password = "guest"; + client.Exchange = ""; + client.RoutingKey = "logs"; + sink.TextFormatter = new CompactJsonFormatter(); + }) + .CreateLogger(); + +while (true) +{ + Log.Information("Yo, RabbitMQ!"); + Thread.Sleep(1000); +} diff --git a/seq-input-rabbitmq.sln b/seq-input-rabbitmq.sln new file mode 100644 index 0000000..089c208 --- /dev/null +++ b/seq-input-rabbitmq.sln @@ -0,0 +1,46 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.102 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "sln", "sln", "{7D1D14F7-D40D-43EB-8CC0-04B555758178}" + ProjectSection(SolutionItems) = preProject + Build.ps1 = Build.ps1 + LICENSE = LICENSE + README.md = README.md + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{704915B0-6D95-4CF8-ACC2-5ED939A2913C}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Seq.Input.RabbitMQ", "src\Seq.Input.RabbitMQ\Seq.Input.RabbitMQ.csproj", "{E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "example", "example", "{584683E5-0578-42F0-A958-3AAB3661AA9E}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Demo", "example\Demo\Demo.csproj", "{99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0}.Release|Any CPU.Build.0 = Release|Any CPU + {99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {E80E7949-A3AE-4C7C-9083-9FE9EE1F78E0} = {704915B0-6D95-4CF8-ACC2-5ED939A2913C} + {99D4AAE3-35B3-4BE1-AA5F-7CC8E6B49A07} = {584683E5-0578-42F0-A958-3AAB3661AA9E} + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2DD287FB-EAAA-422B-BA1E-B1C91CCC0100} + EndGlobalSection +EndGlobal diff --git a/seq-input-rabbitmq.sln.DotSettings b/seq-input-rabbitmq.sln.DotSettings new file mode 100644 index 0000000..3809fdb --- /dev/null +++ b/seq-input-rabbitmq.sln.DotSettings @@ -0,0 +1,2 @@ + + MQV \ No newline at end of file diff --git a/src/Seq.Input.RabbitMQ/RabbitMQInput.cs b/src/Seq.Input.RabbitMQ/RabbitMQInput.cs new file mode 100644 index 0000000..14079f2 --- /dev/null +++ b/src/Seq.Input.RabbitMQ/RabbitMQInput.cs @@ -0,0 +1,141 @@ +using System; +using System.IO; +using System.Text; +using System.Threading.Tasks; +using Seq.Apps; +// ReSharper disable MemberCanBePrivate.Global, UnusedType.Global, UnusedAutoPropertyAccessor.Global + +namespace Seq.Input.RabbitMQ; + +[SeqApp("RabbitMQ Input", + Description = "Pulls JSON-formatted events from a RabbitMQ queue. For details of the " + + "supported JSON schema, see " + + "https://github.com/serilog/serilog-formatting-compact/#format-details.")] +public sealed class RabbitMQInput : SeqApp, IPublishJson, IDisposable +{ + RabbitMQListener _listener; + + [SeqAppSetting( + DisplayName = "RabbitMQ host", + IsOptional = true, + HelpText = "The hostname on which RabbitMQ is running. The default is `localhost`.")] + public string RabbitMQHost { get; set; } = "localhost"; + + [SeqAppSetting( + DisplayName = "RabbitMQ Virtual Host", + IsOptional = true, + HelpText = "The virtual host in RabbitMQ. The default is `/`.")] + public string RabbitMQVHost { get; set; } = "/"; + + [SeqAppSetting( + DisplayName = "RabbitMQ port", + IsOptional = true, + HelpText = "The port on which the RabbitMQ server is listening. The default is `5672`.")] + public int RabbitMQPort { get; set; } = 5672; + + [SeqAppSetting( + DisplayName = "RabbitMQ user", + IsOptional = true, + HelpText = "The username provided when connecting to RabbitMQ. The default is `guest`.")] + public string RabbitMQUser { get; set; } = "guest"; + + [SeqAppSetting( + DisplayName = "RabbitMQ password", + IsOptional = true, + InputType = SettingInputType.Password, + HelpText = "The password provided when connecting to RabbitMQ. The default is `guest`.")] + public string RabbitMQPassword { get; set; } = "guest"; + + [SeqAppSetting( + DisplayName = "RabbitMQ queue", + IsOptional = true, + HelpText = "The RabbitMQ queue name to receive events from. The default is `Logs`.")] + public string RabbitMQQueue { get; set; } = "logs"; + + [SeqAppSetting( + DisplayName = "Require SSL", + IsOptional = true, + HelpText = "Whether or not the connection is with SSL. The default is false.")] + public bool IsSsl { get; set; } + + [SeqAppSetting( + DisplayName = "Durable", + IsOptional = true, + HelpText = "Whether or not the queue is durable. The default is false.")] + public bool IsQueueDurable { get; set; } + + [SeqAppSetting( + DisplayName = "Exclusive", + IsOptional = true, + HelpText = "Whether or not the queue is exclusive. The default is false.")] + public bool IsQueueExclusive { get; set; } + + [SeqAppSetting( + DisplayName = "Auto-delete", + IsOptional = true, + HelpText = "Whether or not the queue subscription is durable. The default is false.")] + public bool IsQueueAutoDelete { get; set; } + + [SeqAppSetting( + DisplayName = "Auto-ACK", + IsOptional = true, + HelpText = "Whether or not messages should be auto-acknowledged. The default is true.")] + public bool IsReceiveAutoAck { get; set; } = true; + + [SeqAppSetting( + DisplayName = "Dead Letter Exchange", + IsOptional = true, + HelpText = "The name of the dead letter exchange associated with this queue. If specified, the exchange will be used when declaring the queue, otherwise no dead lettering will be configured.")] + public string Dlx { get; set; } + + public void Start(TextWriter inputWriter) + { + var sync = new object(); + Task ReceiveAsync(ReadOnlyMemory body) + { + try + { + lock (sync) + { + var clef = Encoding.UTF8.GetString(body.ToArray()); + inputWriter.WriteLine(clef); + } + } + catch (Exception ex) + { + Log.Error(ex, "A received message could not be decoded"); + } + + return Task.CompletedTask; + } + + // Not a deadlock risk on .NET 8, but ideally we'll introduce `IPublishJsonAsync` and provide + // async start/stop/dispose variants. + _listener = RabbitMQListener.CreateAsync( + ReceiveAsync, + RabbitMQHost, + RabbitMQVHost, + RabbitMQPort, + RabbitMQUser, + RabbitMQPassword, + RabbitMQQueue, + IsSsl, + IsQueueDurable, + IsQueueAutoDelete, + IsQueueExclusive, + IsReceiveAutoAck, + Dlx).Result; + } + + public void Stop() + { + // Not a deadlock risk on .NET 8, but ideally we'll introduce `IPublishJsonAsync` and provide + // async start/stop/dispose variants. + _listener.CloseAsync().Wait(); + } + + public void Dispose() + { + _listener?.Dispose(); + } +} \ No newline at end of file diff --git a/src/Seq.Input.RabbitMQ/RabbitMQListener.cs b/src/Seq.Input.RabbitMQ/RabbitMQListener.cs new file mode 100644 index 0000000..3a9274d --- /dev/null +++ b/src/Seq.Input.RabbitMQ/RabbitMQListener.cs @@ -0,0 +1,71 @@ +using System; +using System.Collections.Generic; +using System.Threading.Tasks; +using RabbitMQ.Client; +using RabbitMQ.Client.Events; + +namespace Seq.Input.RabbitMQ; + +class RabbitMQListener(IConnection connection, IChannel channel) : IDisposable +{ + public static async Task CreateAsync( + Func, Task> receiveAsync, + string rabbitMQHost, + string rabbitMQVHost, + int rabbitMQPort, + string rabbitMQUser, + string rabbitMQPassword, + string rabbitMQQueue, + bool isSsl, + bool isQueueDurable, + bool isQueueAutoDelete, + bool isQueueExclusive, + bool isReceiveAutoAck, + string dlx) + { + var factory = new ConnectionFactory + { + HostName = rabbitMQHost, + VirtualHost = rabbitMQVHost, + Port = rabbitMQPort, + UserName = rabbitMQUser, + Password = rabbitMQPassword, + Ssl = + { + Enabled = isSsl + } + }; + + var connection = await factory.CreateConnectionAsync(); + var channel = await connection.CreateChannelAsync(); + + var arguments = string.IsNullOrWhiteSpace(dlx) + ? null + : new Dictionary { {"x-dead-letter-exchange", dlx} }; + + await channel.QueueDeclareAsync( + rabbitMQQueue, + durable: isQueueDurable, + exclusive: isQueueExclusive, + autoDelete: isQueueAutoDelete, + arguments: arguments); + + var consumer = new AsyncEventingBasicConsumer(channel); + consumer.ReceivedAsync += async (_, ea) => await receiveAsync(ea.Body); + await channel.BasicConsumeAsync(rabbitMQQueue, autoAck: isReceiveAutoAck, consumer: consumer); + + return new RabbitMQListener(connection, channel); + } + + public async Task CloseAsync() + { + await channel.CloseAsync(); + await connection.CloseAsync(); + } + + public void Dispose() + { + channel?.Dispose(); + connection?.Dispose(); + } +} \ No newline at end of file diff --git a/src/Seq.Input.RabbitMQ/Seq.Input.RabbitMQ.csproj b/src/Seq.Input.RabbitMQ/Seq.Input.RabbitMQ.csproj new file mode 100644 index 0000000..62cdd64 --- /dev/null +++ b/src/Seq.Input.RabbitMQ/Seq.Input.RabbitMQ.csproj @@ -0,0 +1,26 @@ + + + net8.0 + latest + Ingest events into Seq directly from RabbitMQ + Datalust and Contributors + seq-app + https://github.com/datalust/seq-input-rabbitmq + https://github.com/datalust/seq-input-rabbitmq + git + True + + LICENSE + + + + + + + + + + + + +