-
-
Notifications
You must be signed in to change notification settings - Fork 507
Expand file tree
/
Copy pathExceptionlessElasticConfiguration.cs
More file actions
114 lines (99 loc) · 4.79 KB
/
ExceptionlessElasticConfiguration.cs
File metadata and controls
114 lines (99 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using Elasticsearch.Net;
using Exceptionless.Core.Configuration;
using Exceptionless.Core.Extensions;
using Exceptionless.Core.Repositories.Queries;
using Exceptionless.Core.Serialization;
using Foundatio.Caching;
using Foundatio.Extensions.Hosting.Startup;
using Foundatio.Jobs;
using Foundatio.Messaging;
using Foundatio.Queues;
using Foundatio.Repositories.Elasticsearch;
using Foundatio.Repositories.Elasticsearch.Configuration;
using Foundatio.Repositories.Elasticsearch.Queries.Builders;
using Foundatio.Resilience;
using Microsoft.Extensions.Logging;
using Nest;
using Newtonsoft.Json;
namespace Exceptionless.Core.Repositories.Configuration;
public sealed class ExceptionlessElasticConfiguration : ElasticConfiguration, IStartupAction
{
private readonly AppOptions _appOptions;
private readonly JsonSerializerSettings _serializerSettings;
public ExceptionlessElasticConfiguration(
AppOptions appOptions,
IQueue<WorkItemData> workItemQueue,
JsonSerializerSettings serializerSettings,
ICacheClient cacheClient,
IMessageBus messageBus,
IServiceProvider serviceProvider,
TimeProvider timeProvider,
IResiliencePolicyProvider resiliencePolicyProvider,
ILoggerFactory loggerFactory
) : base(workItemQueue, cacheClient, messageBus, timeProvider, resiliencePolicyProvider, loggerFactory)
{
_appOptions = appOptions;
_serializerSettings = serializerSettings;
_logger.LogInformation("All new indexes will be created with {ElasticsearchNumberOfShards} Shards and {ElasticsearchNumberOfReplicas} Replicas", _appOptions.ElasticsearchOptions.NumberOfShards, _appOptions.ElasticsearchOptions.NumberOfReplicas);
AddIndex(Stacks = new StackIndex(this));
AddIndex(Events = new EventIndex(this, serviceProvider, appOptions));
AddIndex(Migrations = new MigrationIndex(this, _appOptions.ElasticsearchOptions.ScopePrefix + "migrations", appOptions.ElasticsearchOptions.NumberOfReplicas));
AddIndex(Organizations = new OrganizationIndex(this));
AddIndex(Projects = new ProjectIndex(this));
AddIndex(SavedViews = new SavedViewIndex(this));
AddIndex(Tokens = new TokenIndex(this));
AddIndex(Users = new UserIndex(this));
AddIndex(WebHooks = new WebHookIndex(this));
}
public Task RunAsync(CancellationToken shutdownToken = default)
{
if (_appOptions.ElasticsearchOptions.DisableIndexConfiguration)
return Task.CompletedTask;
return ConfigureIndexesAsync();
}
public override void ConfigureGlobalQueryBuilders(ElasticQueryBuilder builder)
{
builder.Register(new AppFilterQueryBuilder(_appOptions, TimeProvider));
builder.Register(new OrganizationQueryBuilder());
builder.Register(new ProjectQueryBuilder());
builder.Register(new StackQueryBuilder());
}
public ElasticsearchOptions Options => _appOptions.ElasticsearchOptions;
public StackIndex Stacks { get; }
public EventIndex Events { get; }
public MigrationIndex Migrations { get; }
public OrganizationIndex Organizations { get; }
public ProjectIndex Projects { get; }
public SavedViewIndex SavedViews { get; }
public TokenIndex Tokens { get; }
public UserIndex Users { get; }
public WebHookIndex WebHooks { get; }
protected override IElasticClient CreateElasticClient()
{
var connectionPool = CreateConnectionPool();
var settings = new ConnectionSettings(connectionPool, (serializer, values) => new ElasticJsonNetSerializer(serializer, values, _serializerSettings));
ConfigureSettings(settings);
foreach (var index in Indexes)
index.ConfigureSettings(settings);
if (!String.IsNullOrEmpty(_appOptions.ElasticsearchOptions.UserName) && !String.IsNullOrEmpty(_appOptions.ElasticsearchOptions.Password))
settings.BasicAuthentication(_appOptions.ElasticsearchOptions.UserName, _appOptions.ElasticsearchOptions.Password);
var client = new ElasticClient(settings);
return client;
}
protected override IConnectionPool CreateConnectionPool()
{
var serverUris = Options?.ServerUrl.Split(',').Select(url => new Uri(url));
return new StaticConnectionPool(serverUris);
}
protected override void ConfigureSettings(ConnectionSettings settings)
{
if (_appOptions.AppMode == AppMode.Development)
settings.EnableDebugMode();
settings.ServerCertificateValidationCallback(CertificateValidations.AllowAll);
settings.EnableApiVersioningHeader();
settings.DisableDirectStreaming();
settings.EnableTcpKeepAlive(TimeSpan.FromSeconds(30), TimeSpan.FromSeconds(2));
settings.DefaultFieldNameInferrer(p => p.ToLowerUnderscoredWords());
settings.MaximumRetries(5);
}
}