Skip to content

Commit 2f97b9d

Browse files
authored
Update Elastic packages to avoid transport failures (#464)
- Updated Elastic.Clients.Elasticsearch - Updated Elastic.Ingest.Elasticsearch - Updated Microsoft.Extensions.Hosting in examples Closes #463
1 parent b173202 commit 2f97b9d

File tree

24 files changed

+104
-107
lines changed

24 files changed

+104
-107
lines changed

examples/Elastic.Serilog.Sinks.Example/Elastic.Serilog.Sinks.Example.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
<ItemGroup>
1111
<PackageReference Include="Elastic.Apm" Version="1.22.0" />
1212
<PackageReference Include="Serilog" Version="2.10.0" />
13-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
13+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
1414
<PackageReference Include="Elastic.Elasticsearch.Ephemeral" Version="0.4.3" />
15-
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.12.1" />
15+
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.16.2" />
1616
<PackageReference Include="Serilog.Extensions.Hosting" Version="5.0.1" />
1717
<PackageReference Include="Serilog.Sinks.Console" Version="4.1.0" />
1818
</ItemGroup>

examples/aspnetcore-with-extensions-logging/aspnetcore-with-extensions-logging.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
<ItemGroup>
1111
<PackageReference Include="Elastic.Elasticsearch.Ephemeral" Version="0.4.3" />
12-
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.12.1" />
12+
<PackageReference Include="Elastic.Clients.Elasticsearch" Version="8.16.2" />
1313
</ItemGroup>
1414

1515
<ItemGroup>

examples/aspnetcore-with-serilog/AspnetCoreExample.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
1212
<PackageReference Include="Serilog.Sinks.Async" Version="1.5.0" />
1313
<PackageReference Include="Serilog.Sinks.Console" Version="3.1.1" />
14-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0"/>
14+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.1" />
1515
</ItemGroup>
1616
<ItemGroup>
1717
<ProjectReference Include="..\..\src\Elastic.Apm.SerilogEnricher\Elastic.Apm.SerilogEnricher.csproj" />

src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporter.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,18 @@ public ElasticsearchBenchmarkExporter(ElasticsearchBenchmarkExporterOptions opti
3333
{
3434
Options = options;
3535
var config = Options.CreateTransportConfiguration();
36-
Transport = new DistributedTransport<TransportConfiguration>(config);
36+
Transport = new DistributedTransport<ITransportConfiguration>(config);
3737
}
3838

3939
// ReSharper disable once UnusedMember.Global
4040
/// <summary> Exports benchmark results to Elasticsearch </summary>
4141
public ElasticsearchBenchmarkExporter(ElasticsearchBenchmarkExporterOptions options, Func<ElasticsearchBenchmarkExporterOptions, TransportConfiguration> configure)
4242
{
4343
Options = options;
44-
Transport = new DistributedTransport<TransportConfiguration>(configure(Options));
44+
Transport = new DistributedTransport<ITransportConfiguration>(configure(Options));
4545
}
4646

47-
48-
private ITransport<TransportConfiguration> Transport { get; }
47+
private ITransport<ITransportConfiguration> Transport { get; }
4948
private ElasticsearchBenchmarkExporterOptions Options { get; }
5049

5150
// We only log when we cannot write to Elasticsearch

src/Elastic.CommonSchema.BenchmarkDotNetExporter/ElasticsearchBenchmarkExporterOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Licensed to Elasticsearch B.V under one or more agreements.
1+
// Licensed to Elasticsearch B.V under one or more agreements.
22
// Elasticsearch B.V licenses this file to you under the Apache 2.0 License.
33
// See the LICENSE file in the project root for more information
44

@@ -145,9 +145,9 @@ private NodePool CreateNodePool()
145145
}
146146

147147

148-
internal TransportConfiguration CreateTransportConfiguration()
148+
internal TransportConfigurationDescriptor CreateTransportConfiguration()
149149
{
150-
var settings = new TransportConfiguration(CreateNodePool(), productRegistration: ElasticsearchProductRegistration.Default);
150+
var settings = new TransportConfigurationDescriptor(CreateNodePool(), productRegistration: ElasticsearchProductRegistration.Default);
151151
if (EnableDebugMode)
152152
settings.EnableDebugMode();
153153
return settings;

src/Elastic.Extensions.Logging/ElasticsearchLoggerProvider.cs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
using System;
66
using System.Collections.Generic;
7+
using System.IO;
78
using System.Linq;
89
using System.Threading;
10+
using System.Threading.Tasks;
911
using Elastic.Channels;
1012
using Elastic.Channels.Diagnostics;
1113
using Elastic.Extensions.Logging.Options;
@@ -18,6 +20,10 @@
1820
using Microsoft.Extensions.Logging;
1921
using Microsoft.Extensions.Options;
2022

23+
#if NETSTANDARD2_1_OR_GREATER
24+
using System.Buffers;
25+
#endif
26+
2127
namespace Elastic.Extensions.Logging
2228
{
2329
/// <summary>
@@ -33,6 +39,20 @@ public class ElasticsearchLoggerProvider : ILoggerProvider, ISupportExternalScop
3339
private IExternalScopeProvider? _scopeProvider;
3440
private IBufferedChannel<LogEvent> _shipper;
3541

42+
private static readonly LogEventWriter LogEventWriterInstance = new()
43+
{
44+
WriteToStreamAsync = static async (stream, logEvent, ctx) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false),
45+
#if NETSTANDARD2_1_OR_GREATER
46+
WriteToArrayBuffer = static (arrayBufferWriter, logEvent) =>
47+
{
48+
var serialized = logEvent.SerializeToUtf8Bytes(); // TODO - Performance optimisation to avoid array allocation
49+
var span = arrayBufferWriter.GetSpan(serialized.Length);
50+
serialized.AsSpan().CopyTo(span);
51+
arrayBufferWriter.Advance(serialized.Length);
52+
}
53+
#endif
54+
};
55+
3656
/// <inheritdoc cref="IChannelDiagnosticsListener"/>
3757
public IChannelDiagnosticsListener? DiagnosticsListener { get; }
3858

@@ -132,16 +152,16 @@ private static ITransport CreateTransport(ElasticsearchLoggerOptions loggerOptio
132152
if (loggerOptions.Transport != null) return loggerOptions.Transport;
133153

134154
var connectionPool = CreateNodePool(loggerOptions);
135-
var config = new TransportConfiguration(connectionPool, productRegistration: ElasticsearchProductRegistration.Default);
155+
var config = new TransportConfigurationDescriptor(connectionPool, productRegistration: ElasticsearchProductRegistration.Default);
136156
// Cloud sets authentication as required parameter in the constructor
137157
if (loggerOptions.ShipTo.NodePoolType != NodePoolType.Cloud)
138158
config = SetAuthenticationOnTransport(loggerOptions, config);
139159

140-
var transport = new DistributedTransport<TransportConfiguration>(config);
160+
var transport = new DistributedTransport<ITransportConfiguration>(config);
141161
return transport;
142162
}
143163

144-
private static TransportConfiguration SetAuthenticationOnTransport(ElasticsearchLoggerOptions loggerOptions, TransportConfiguration config)
164+
private static TransportConfigurationDescriptor SetAuthenticationOnTransport(ElasticsearchLoggerOptions loggerOptions, TransportConfigurationDescriptor config)
145165
{
146166
var apiKey = loggerOptions.ShipTo.ApiKey;
147167
var username = loggerOptions.ShipTo.Username;
@@ -177,11 +197,13 @@ private IBufferedChannel<LogEvent> CreatIngestChannel(ElasticsearchLoggerOptions
177197
else
178198
{
179199
var dataStreamNameOptions = loggerOptions.DataStream ?? new DataStreamNameOptions();
200+
180201
var indexChannelOptions = new DataStreamChannelOptions<LogEvent>(transport)
181202
{
182203
DataStream = new DataStreamName(dataStreamNameOptions.Type, dataStreamNameOptions.DataSet, dataStreamNameOptions.Namespace),
183-
WriteEvent = async (stream, ctx, logEvent) => await logEvent.SerializeAsync(stream, ctx).ConfigureAwait(false),
204+
EventWriter = LogEventWriterInstance
184205
};
206+
185207
SetupChannelOptions(_channelConfigurations, indexChannelOptions);
186208
var channel = new EcsDataStreamChannel<LogEvent>(indexChannelOptions);
187209
channel.BootstrapElasticsearch(loggerOptions.BootstrapMethod, loggerOptions.IlmPolicy);
@@ -191,5 +213,14 @@ private IBufferedChannel<LogEvent> CreatIngestChannel(ElasticsearchLoggerOptions
191213

192214
/// <inheritdoc cref="IChannelProvider.GetChannel"/>
193215
public IBufferedChannel<LogEvent> GetChannel() => _shipper;
216+
217+
private sealed class LogEventWriter : IElasticsearchEventWriter<LogEvent>
218+
{
219+
#if NETSTANDARD2_1_OR_GREATER
220+
public Action<ArrayBufferWriter<byte>, LogEvent>? WriteToArrayBuffer { get; set; }
221+
#endif
222+
223+
public Func<Stream, LogEvent, CancellationToken, Task>? WriteToStreamAsync { get; set; }
224+
}
194225
}
195226
}

src/Elastic.Ingest.Elasticsearch.CommonSchema/Elastic.Ingest.Elasticsearch.CommonSchema.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</ItemGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.7.2" />
13+
<PackageReference Include="Elastic.Ingest.Elasticsearch" Version="0.7.5" />
1414
</ItemGroup>
1515

1616
</Project>

src/Elastic.NLog.Targets/ElasticsearchTarget.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,12 @@ protected override void InitializeTarget()
174174
var indexOffset = string.IsNullOrEmpty(indexOffsetHours) ? default(TimeSpan?) : TimeSpan.FromHours(int.Parse(indexOffsetHours));
175175

176176
var connectionPool = CreateNodePool();
177-
var config = new TransportConfiguration(connectionPool, productRegistration: ElasticsearchProductRegistration.Default);
177+
var config = new TransportConfigurationDescriptor(connectionPool, productRegistration: ElasticsearchProductRegistration.Default);
178178
// Cloud sets authentication as required parameter in the constructor
179179
if (NodePoolType != ElasticPoolType.Cloud)
180180
config = SetAuthenticationOnTransport(config);
181181

182-
var transport = new DistributedTransport<TransportConfiguration>(config);
182+
var transport = new DistributedTransport<ITransportConfiguration>(config);
183183
if (!string.IsNullOrEmpty(indexFormat))
184184
{
185185
_channel = CreateIndexChannel(transport, indexFormat, indexOffset, IndexOperation);
@@ -205,7 +205,7 @@ private void SetupChannelOptions(ElasticsearchChannelOptionsBase<NLogEcsDocument
205205
ConfigureChannel?.Invoke(channelOptions);
206206
}
207207

208-
private EcsDataStreamChannel<NLogEcsDocument> CreateDataStreamChannel(DistributedTransport<TransportConfiguration> transport)
208+
private EcsDataStreamChannel<NLogEcsDocument> CreateDataStreamChannel(DistributedTransport<ITransportConfiguration> transport)
209209
{
210210
var ilmPolicy = IlmPolicy?.Render(LogEventInfo.CreateNullEvent());
211211
var dataStreamType = DataStreamType?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
@@ -221,7 +221,7 @@ private EcsDataStreamChannel<NLogEcsDocument> CreateDataStreamChannel(Distribute
221221
return channel;
222222
}
223223

224-
private EcsIndexChannel<NLogEcsDocument> CreateIndexChannel(DistributedTransport<TransportConfiguration> transport, string indexFormat, TimeSpan? indexOffset, OperationMode indexOperation)
224+
private EcsIndexChannel<NLogEcsDocument> CreateIndexChannel(DistributedTransport<ITransportConfiguration> transport, string indexFormat, TimeSpan? indexOffset, OperationMode indexOperation)
225225
{
226226
var indexChannelOptions = new IndexChannelOptions<NLogEcsDocument>(transport)
227227
{
@@ -300,7 +300,7 @@ private NodePool CreateNodePool()
300300
}
301301
}
302302

303-
private TransportConfiguration SetAuthenticationOnTransport(TransportConfiguration config)
303+
private TransportConfigurationDescriptor SetAuthenticationOnTransport(TransportConfigurationDescriptor config)
304304
{
305305
var apiKey = ApiKey?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;
306306
var username = Username?.Render(LogEventInfo.CreateNullEvent()) ?? string.Empty;

src/Elastic.Serilog.Sinks/ConfigSinkExtensions.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ private static void SetBufferOptions(ElasticsearchSinkOptions sinkOptions, int?
151151
};
152152

153153
private static ElasticsearchSinkOptions CreateSinkOptions(
154-
TransportConfiguration transportConfig,
154+
TransportConfigurationDescriptor transportConfig,
155155
BootstrapMethod bootstrapMethod, string? dataStream, string? ilmPolicy, bool? includeHost,
156156
bool? includeActivity, bool? includeProcess, bool? includeUser, ICollection<string>? filterProperties
157157
)
@@ -187,7 +187,7 @@ private static ElasticsearchSinkOptions CreateSinkOptions(
187187
return sinkOptions;
188188
}
189189

190-
private static void SetTransportConfig(TransportConfiguration transportConfig,
190+
private static void SetTransportConfig(TransportConfigurationDescriptor transportConfig,
191191
string? apiKey, string? username, string? password,
192192
Uri? proxy, string? proxyUsername, string? proxyPassword, string? fingerprint, bool debugMode
193193
)
@@ -209,7 +209,6 @@ private static void SetTransportConfig(TransportConfiguration transportConfig,
209209
transportConfig.Authentication(new ApiKey(apiKey));
210210
}
211211

212-
213212
/// <summary>
214213
/// Write logs directly to Elastic Cloud ( https://cloud.elastic.co/ ).
215214
/// <para><paramref name="cloudId"/> describes your deployments endpoints (can be found in the Admin Console)</para>
@@ -222,7 +221,7 @@ public static LoggerConfiguration ElasticCloud(
222221
string username,
223222
string password,
224223
Action<ElasticsearchSinkOptions>? configureOptions = null,
225-
Action<TransportConfiguration>? configureTransport = null,
224+
Action<TransportConfigurationDescriptor>? configureTransport = null,
226225
LoggingLevelSwitch? levelSwitch = null,
227226
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
228227
)

src/Elastic.Serilog.Sinks/ElasticsearchSinkExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public static LoggerConfiguration Elasticsearch(
4747
this LoggerSinkConfiguration loggerConfiguration,
4848
ICollection<Uri> nodes,
4949
Action<ElasticsearchSinkOptions>? configureOptions = null,
50-
Action<TransportConfiguration>? configureTransport = null,
50+
Action<TransportConfigurationDescriptor>? configureTransport = null,
5151
bool useSniffing = false,
5252
LoggingLevelSwitch? levelSwitch = null,
5353
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
@@ -72,7 +72,7 @@ public static LoggerConfiguration Elasticsearch<TEcsDocument>(
7272
this LoggerSinkConfiguration loggerConfiguration,
7373
ICollection<Uri> nodes,
7474
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
75-
Action<TransportConfiguration>? configureTransport = null,
75+
Action<TransportConfigurationDescriptor>? configureTransport = null,
7676
bool useSniffing = false,
7777
LoggingLevelSwitch? levelSwitch = null,
7878
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
@@ -97,7 +97,7 @@ public static LoggerConfiguration ElasticCloud(
9797
string cloudId,
9898
string apiKey,
9999
Action<ElasticsearchSinkOptions>? configureOptions = null,
100-
Action<TransportConfiguration>? configureTransport = null,
100+
Action<TransportConfigurationDescriptor>? configureTransport = null,
101101
LoggingLevelSwitch? levelSwitch = null,
102102
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
103103
)
@@ -122,7 +122,7 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
122122
string cloudId,
123123
string apiKey,
124124
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
125-
Action<TransportConfiguration>? configureTransport = null,
125+
Action<TransportConfigurationDescriptor>? configureTransport = null,
126126
LoggingLevelSwitch? levelSwitch = null,
127127
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
128128
) where TEcsDocument : EcsDocument, new()
@@ -147,7 +147,7 @@ public static LoggerConfiguration ElasticCloud(
147147
string username,
148148
string password,
149149
Action<ElasticsearchSinkOptions>? configureOptions = null,
150-
Action<TransportConfiguration>? configureTransport = null,
150+
Action<TransportConfigurationDescriptor>? configureTransport = null,
151151
LoggingLevelSwitch? levelSwitch = null,
152152
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
153153
)
@@ -173,7 +173,7 @@ public static LoggerConfiguration ElasticCloud<TEcsDocument>(
173173
string username,
174174
string password,
175175
Action<ElasticsearchSinkOptions<TEcsDocument>>? configureOptions = null,
176-
Action<TransportConfiguration>? configureTransport = null,
176+
Action<TransportConfigurationDescriptor>? configureTransport = null,
177177
LoggingLevelSwitch? levelSwitch = null,
178178
LogEventLevel restrictedToMinimumLevel = LevelAlias.Minimum
179179
) where TEcsDocument : EcsDocument, new()

0 commit comments

Comments
 (0)