Repository navigation
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
46c440c
cdf1f6d
22d506f
18b9d65
ded4306
b6e9cec
ba02eb1
00db448
da31aeb
900e9a0
3c3ad74
f217dc0
ad2c754
a2da68b
a91445f
f813f07
709d378
102a487
d75bddc
0ee733f
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,118 @@ | ||
| 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 = 11; | ||
| } | ||
|
|
||
| public override async Task RunAsync(MigrationContext context) | ||
| { | ||
| string referenceKey = $"@ref:{Event.KnownReferenceNames.Parent}"; | ||
| string indexKey = $"{Event.KnownReferenceNames.Parent}-r"; | ||
| // Painless trim() only handles ASCII whitespace; match CopyDataToIndex's String.Trim(). | ||
| string whitespace = String.Concat(Enumerable.Range(Char.MinValue, Char.MaxValue + 1) | ||
| .Select(value => (char)value).Where(Char.IsWhiteSpace)); | ||
| string script = $$""" | ||
| def parentReference = null; | ||
| if (ctx._source.data != null) { | ||
| for (def entry : ctx._source.data.entrySet()) { | ||
| String key = entry.getKey(); | ||
| int start = 0; | ||
| int end = key.length(); | ||
| while (start < end && params.whitespace.indexOf(key.substring(start, start + 1)) >= 0) start++; | ||
| while (end > start && params.whitespace.indexOf(key.substring(end - 1, end)) >= 0) end--; | ||
| if (key.substring(start, end).equalsIgnoreCase('{{referenceKey}}')) { | ||
| parentReference = entry.getValue(); | ||
| } | ||
| } | ||
| } | ||
| 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) | ||
| .Params(new Dictionary<string, object> { ["whitespace"] = whitespace })) | ||
| .WaitForCompletion(false), context.CancellationToken); | ||
| _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 |
|---|---|---|
|
|
@@ -47,16 +47,20 @@ | |
| let references = $derived.by(() => { | ||
| let refs: { id: string; name: string }[] = []; | ||
| Object.entries(event.data || {}).forEach(([key, value]) => { | ||
| if (key.startsWith(referencePrefix)) { | ||
| if (key.startsWith(referencePrefix) && value != null) { | ||
| refs.push({ | ||
| id: value as string, | ||
| id: String(value), | ||
|
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 an event payload supplies AGENTS.md reference: AGENTS.md:L83-L83 Useful? React with 👍 / 👎. |
||
| name: key.slice(5) | ||
| }); | ||
| } | ||
| }); | ||
| return refs; | ||
| }); | ||
|
|
||
| function isValidReferenceName(name: string): boolean { | ||
| return !/[\uD800-\uDFFF]/.test(name) && /^[\p{L}\p{Nd}-]{1,25}$/u.test(name); | ||
| } | ||
|
|
||
| let level = $derived(event.data?.['@level']?.toLowerCase()); | ||
| let location = $derived(getLocation(event)); | ||
|
|
||
|
|
@@ -110,12 +114,20 @@ | |
| {/if} | ||
| {#each references as reference (reference.id)} | ||
| <Table.Row class="group"> | ||
| {#if reference.name === 'session'} | ||
| {#if reference.name.toLowerCase() === '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.toLowerCase() === 'parent'} | ||
| <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> | ||
|
niemyjski marked this conversation as resolved.
|
||
| {: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 | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.