Skip to content

Commit

Permalink
Update to latest ingest libraries (#371)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mpdreamz authored Apr 10, 2024
1 parent 88cbf7d commit 2034617
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ecs-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "docs", "docs\docs.csproj",
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "aspnetcore-with-extensions-logging", "examples\aspnetcore-with-extensions-logging\aspnetcore-with-extensions-logging.csproj", "{D866F335-BC19-49A8-AF72-4BA66CC7AFFB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "playground", "examples\playground\playground.csproj", "{86AEB76A-C210-4250-8541-B349C26C1683}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -259,6 +261,10 @@ Global
{D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D866F335-BC19-49A8-AF72-4BA66CC7AFFB}.Release|Any CPU.Build.0 = Release|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86AEB76A-C210-4250-8541-B349C26C1683}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -298,6 +304,7 @@ Global
{EC19A9E1-79CC-46A8-94D7-EE66ED22D3BD} = {B268060B-83ED-4944-B135-C362DFCBFC0C}
{1CAEFBD7-B800-41C4-81D3-CB6839FA563D} = {05075402-8669-45BD-913A-BD40A29BBEAB}
{D866F335-BC19-49A8-AF72-4BA66CC7AFFB} = {05075402-8669-45BD-913A-BD40A29BBEAB}
{86AEB76A-C210-4250-8541-B349C26C1683} = {05075402-8669-45BD-913A-BD40A29BBEAB}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7F60C4BB-6216-4E50-B1E4-9C38EB484843}
Expand Down
123 changes: 123 additions & 0 deletions examples/playground/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Elastic.Channels;
using Elastic.CommonSchema;
using Elastic.Elasticsearch.Ephemeral;
using Elastic.Ingest.Elasticsearch;
using Elastic.Ingest.Elasticsearch.CommonSchema;
using Elastic.Ingest.Elasticsearch.DataStreams;
using Elastic.Serilog.Sinks;
using Elastic.Transport;
using Serilog;
using Serilog.Events;
using Log = Serilog.Log;

var testSerilog = true;

var random = new Random();
var ctxs = new CancellationTokenSource();
var parallelOpts = new ParallelOptions { MaxDegreeOfParallelism = Environment.ProcessorCount, CancellationToken = ctxs.Token };
const int numDocs = 1_000_000;
var bufferOptions = new BufferOptions { };
var config = new EphemeralClusterConfiguration("8.13.0");
using var cluster = new EphemeralCluster(config);
using var channel = SetupElasticsearchChannel();

Console.CancelKeyPress += (sender, eventArgs) =>
{
ctxs.Cancel();
cluster.Dispose();
eventArgs.Cancel = true;
};


using var started = cluster.Start();

if (testSerilog)
await PushToSerilog();
else
await PushToChannel(channel);

Console.WriteLine($"Press any key...");
Console.ReadKey();


async Task PushToSerilog()
{
SetupSerilog();

Parallel.For(0, numDocs, parallelOpts, i =>
{
var randomData = $"Logging information {i} - Random value: {random.NextDouble()}";
Log.Information(randomData);
});

/*
foreach (var i in Enumerable.Range(0, numDocs))
{
var randomData = $"Logging information {i} - Random value: {random.NextDouble()}";
Log.Information(randomData);
}
*/

Log.CloseAndFlush();
await Task.Delay(TimeSpan.FromMinutes(1), ctxs.Token);

void SetupSerilog()
{
Serilog.Debugging.SelfLog.Enable(s => Console.WriteLine(s));
Log.Logger = new LoggerConfiguration()
.WriteTo.Elasticsearch(new[] { new Uri("http://localhost:9200") }, o =>
{
o.ConfigureChannel = c =>
{
c.BufferOptions = bufferOptions;
};
o.BootstrapMethod = BootstrapMethod.Failure;
o.MinimumLevel = LogEventLevel.Verbose;
o.DataStream = new DataStreamName("logs");
})
.CreateLogger();
}
}


async Task PushToChannel(EcsDataStreamChannel<EcsDocument> c)
{
if (c == null) throw new ArgumentNullException(nameof(c));

await c.BootstrapElasticsearchAsync(BootstrapMethod.Failure);

foreach (var i in Enumerable.Range(0, numDocs))
await DoChannelWrite(i, ctxs.Token);

/*
await Parallel.ForEachAsync(Enumerable.Range(0, numDocs), parallelOpts, async (i, ctx) =>
{
await DoChannelWrite(i, ctx);
});
*/


async Task DoChannelWrite(int i, CancellationToken cancellationToken)
{
var message = $"Logging information {i} - Random value: {random.NextDouble()}";
var doc = EcsDocument.CreateNewWithDefaults<EcsDocument>();
doc.Message = message;
if (await c.WaitToWriteAsync(cancellationToken) && c.TryWrite(doc))
return;

Console.WriteLine("Failed To write");
await Task.Delay(TimeSpan.FromSeconds(5), cancellationToken);
}
}

EcsDataStreamChannel<EcsDocument> SetupElasticsearchChannel()
{
var transportConfiguration = new TransportConfiguration(new Uri("http://localhost:9200"));
var c = new EcsDataStreamChannel<EcsDocument>(
new DataStreamChannelOptions<EcsDocument>(new DistributedTransport(transportConfiguration))
{
BufferOptions = bufferOptions
});

return c;
}
18 changes: 18 additions & 0 deletions examples/playground/playground.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\Elastic.Serilog.Sinks\Elastic.Serilog.Sinks.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elastic.Elasticsearch.Ephemeral" Version="0.5.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.6.0" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.7.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<ItemGroup>
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.12.1" />
<PackageReference Include="Elastic.Elasticsearch.Xunit" Version="0.4.3" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.6.0" />
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.7.0" />

</ItemGroup>

Expand Down

0 comments on commit 2034617

Please sign in to comment.