-
-
Notifications
You must be signed in to change notification settings - Fork 508
Include ref.parent matches in events by-reference navigation
#2280
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
f09562e
8f5e641
ae3cc52
311c5d5
6f152ed
775c14f
0e94773
5f0dd46
6942014
a507d66
85e61ce
3e5f263
d7a0b37
2991ef0
5d986d8
11f79ad
d4b3e97
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| using System.Diagnostics; | ||
| using System.Text.Json; | ||
| using Elastic.Clients.Elasticsearch; | ||
| using Elastic.Clients.Elasticsearch.Tasks; | ||
| using Exceptionless.Core.Models; | ||
| using Exceptionless.Core.Repositories.Configuration; | ||
| using Foundatio.Repositories.Elasticsearch.Extensions; | ||
| using Foundatio.Repositories.Migrations; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Exceptionless.Core.Migrations; | ||
|
|
||
| public sealed class BackfillParentReferences : MigrationBase | ||
| { | ||
| private readonly ElasticsearchClient _client; | ||
| private readonly ExceptionlessElasticConfiguration _config; | ||
| private readonly TimeProvider _timeProvider; | ||
|
|
||
| public BackfillParentReferences(ExceptionlessElasticConfiguration configuration, TimeProvider timeProvider, ILoggerFactory loggerFactory) : base(loggerFactory) | ||
| { | ||
| _config = configuration; | ||
| _client = configuration.Client; | ||
| _timeProvider = timeProvider; | ||
|
|
||
| MigrationType = MigrationType.VersionedAndResumable; | ||
| Version = 3; | ||
| } | ||
|
|
||
| public override async Task RunAsync(MigrationContext context) | ||
| { | ||
| string referenceKey = $"@ref:{Event.KnownReferenceNames.Parent}"; | ||
| string indexKey = $"{Event.KnownReferenceNames.Parent}-r"; | ||
| string script = $$""" | ||
| def parentReference = null; | ||
| if (ctx._source.data != null) { | ||
| parentReference = ctx._source.data['{{referenceKey}}']; | ||
| if (parentReference == null) { | ||
| for (def entry : ctx._source.data.entrySet()) { | ||
| if (entry.getKey().equalsIgnoreCase('{{referenceKey}}')) { | ||
| parentReference = entry.getValue(); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if (parentReference != null) { | ||
| if (ctx._source.idx == null) ctx._source.idx = [:]; | ||
| ctx._source.idx['{{indexKey}}'] = parentReference.toString(); | ||
| } else { | ||
| ctx.op = 'noop'; | ||
| } | ||
| """; | ||
|
|
||
| _logger.LogInformation("Backfilling retained event parent references"); | ||
| var stopwatch = Stopwatch.StartNew(); | ||
| var response = await _client.UpdateByQueryAsync<PersistentEvent>(request => request | ||
| .Indices($"{_config.Events.VersionedName}-*") | ||
| .Query(query => query.Bool(filter => filter.MustNot(mustNot => mustNot.Exists(exists => exists.Field($"idx.{indexKey}"))))) | ||
| .Script(value => value.Source(script).Lang(ScriptLanguage.Painless)) | ||
| .WaitForCompletion(false)); | ||
| _logger.LogRequest(response, LogLevel.Information); | ||
|
|
||
| if (!response.IsValidResponse || response.Task is null) | ||
| throw new ApplicationException($"Unable to start parent-reference backfill: {response.DebugInformation}"); | ||
|
|
||
| int attempts = 0; | ||
| while (!context.CancellationToken.IsCancellationRequested) | ||
| { | ||
| var taskStatus = await _client.Tasks.GetAsync(response.Task.FullyQualifiedId, context.CancellationToken); | ||
| if (!taskStatus.IsValidResponse) | ||
| throw new ApplicationException($"Unable to monitor parent-reference backfill: {taskStatus.DebugInformation}"); | ||
|
|
||
| if (taskStatus.Completed) | ||
| { | ||
| EnsureTaskSucceeded(taskStatus); | ||
| _logger.LogInformation("Finished parent-reference backfill: Duration={Duration}", stopwatch.Elapsed); | ||
| return; | ||
| } | ||
|
|
||
| attempts++; | ||
| await context.Lock.RenewAsync(); | ||
| await Task.Delay(TimeSpan.FromSeconds(attempts <= 5 ? 1 : 5), _timeProvider, context.CancellationToken); | ||
| } | ||
|
|
||
| context.CancellationToken.ThrowIfCancellationRequested(); | ||
| } | ||
|
|
||
| internal static void EnsureTaskSucceeded(GetTasksResponse taskStatus) | ||
| { | ||
| if (taskStatus.Error is not null) | ||
| throw new ApplicationException($"Parent-reference backfill failed: {JsonSerializer.Serialize(taskStatus.Error)}"); | ||
|
|
||
| if (taskStatus.Response is null) | ||
| return; | ||
|
|
||
| JsonElement response = JsonSerializer.SerializeToElement(taskStatus.Response); | ||
| if (response.ValueKind == JsonValueKind.Object | ||
| && response.TryGetProperty("version_conflicts", out var versionConflicts) | ||
| && versionConflicts.TryGetInt64(out long conflictCount) | ||
| && conflictCount > 0) | ||
| { | ||
| throw new ApplicationException($"Parent-reference backfill failed with {conflictCount} version conflicts."); | ||
| } | ||
|
|
||
| if (response.ValueKind == JsonValueKind.Object | ||
| && response.TryGetProperty("failures", out var failures) | ||
| && failures.ValueKind == JsonValueKind.Array | ||
| && failures.GetArrayLength() > 0) | ||
| { | ||
| throw new ApplicationException($"Parent-reference backfill failed: {failures}"); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,6 +52,10 @@ | |
| return refs; | ||
| }); | ||
|
|
||
| function isValidReferenceName(name: string): boolean { | ||
| return /^[\p{L}\p{Nd}-]{1,25}$/u.test(name); | ||
| } | ||
|
|
||
| let level = $derived(event.data?.['@level']?.toLowerCase()); | ||
| let location = $derived(getLocation(event)); | ||
|
|
||
|
|
@@ -95,9 +99,17 @@ | |
| {#if reference.name === 'session'} | ||
| <Table.Head class="w-40 font-semibold whitespace-nowrap">Session</Table.Head> | ||
| <Table.Cell class="w-4 pr-0"><EventsFacetedFilter.SessionTrigger changed={filterChanged} value={reference.id} /></Table.Cell> | ||
| {:else} | ||
| {:else if reference.name === 'parent'} | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a retained event uses AGENTS.md reference: AGENTS.md:L72-L75 Useful? React with 👍 / 👎. |
||
| <Table.Head class="w-40 font-semibold whitespace-nowrap">{reference.name}</Table.Head> | ||
| <Table.Cell class="w-4 pr-0"><EventsFacetedFilter.ReferenceTrigger changed={filterChanged} value={reference.id} /></Table.Cell> | ||
| {:else if isValidReferenceName(reference.name)} | ||
| <Table.Head class="w-40 font-semibold whitespace-nowrap">{reference.name}</Table.Head> | ||
| <Table.Cell class="w-4 pr-0" | ||
| ><EventsFacetedFilter.StringTrigger changed={filterChanged} term={`ref.${reference.name}`} value={reference.id} /></Table.Cell | ||
|
niemyjski marked this conversation as resolved.
|
||
| > | ||
| {:else} | ||
| <Table.Head class="w-40 font-semibold whitespace-nowrap">{reference.name}</Table.Head> | ||
| <Table.Cell class="w-4 pr-0"></Table.Cell> | ||
| {/if} | ||
| <Table.Cell><A href={resolve('/(app)/event/by-ref/[referenceId]', { referenceId: reference.id })}>{reference.id}</A></Table.Cell> | ||
| </Table.Row> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this ships to an environment that has already run the existing
003_RepairVerifiedUserEmailVerificationMigration, this backfill reusesVersion = 3, so the versioned migration stream now has two registered migrations with the same version and the existingMigrationRegistrationTests.RegisteredVersionedMigrations_AllVersions_AreUniquewill fail; more importantly, migration state for version 3 can prevent this legacy parent-reference backfill from running reliably. Give this migration the next unused version instead of reusing 3.AGENTS.md reference: AGENTS.md:L72-L74
Useful? React with 👍 / 👎.