-
-
Notifications
You must be signed in to change notification settings - Fork 507
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
008495e
11f16c5
bf2ac52
90b02f9
2409d9c
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 = 6; | ||
| } | ||
|
|
||
| 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 |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| using Exceptionless.Core.Plugins.EventProcessor; | ||
| using Exceptionless.Core.Models; | ||
| using Exceptionless.Core.Plugins.EventProcessor; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Exceptionless.Core.Pipeline; | ||
|
|
@@ -11,7 +12,12 @@ public CopySimpleDataToIdxAction(AppOptions options, ILoggerFactory loggerFactor | |
| public override Task ProcessAsync(EventContext ctx) | ||
| { | ||
| if (!ctx.Organization.HasPremiumFeatures) | ||
| { | ||
| if (ctx.Event.GetEventReference(Event.KnownReferenceNames.Parent) is not null) | ||
| ctx.Event.CopyDataToIndex([$"@ref:{Event.KnownReferenceNames.Parent}"]); | ||
|
Comment on lines
+16
to
+17
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 raw event data contains a key with surrounding whitespace such as AGENTS.md reference: AGENTS.md:L72-L74 Useful? React with 👍 / 👎. |
||
|
|
||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| // TODO: Do we need a pipeline action to trim keys and remove null values that may be sent by other native clients. | ||
| ctx.Event.CopyDataToIndex([]); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| import { fireEvent, render, screen } from '@testing-library/svelte'; | ||
| import { describe, expect, it, vi } from 'vitest'; | ||
|
|
||
| vi.mock('$env/dynamic/public', () => ({ env: {} })); | ||
|
|
||
| import { ReferenceFilter, SessionFilter } from '$features/events/components/filters'; | ||
|
|
||
| import type { PersistentEvent } from '../../models'; | ||
|
|
||
| import Overview from './overview.svelte'; | ||
|
|
||
| describe('Overview', () => { | ||
| it.each([ | ||
| ['Parent', 'reference', ReferenceFilter], | ||
| ['SESSION', 'session', SessionFilter] | ||
| ])('uses the built-in %s filter case-insensitively', async (referenceName, filterName, FilterType) => { | ||
| const filterChanged = vi.fn(); | ||
| const event = { | ||
| data: { [`@ref:${referenceName}`]: 'reference-id' } | ||
| } as PersistentEvent; | ||
|
|
||
| render(Overview, { event, filterChanged }); | ||
| await fireEvent.click(screen.getByTitle(`Filter ${filterName}:reference-id`)); | ||
|
|
||
| expect(filterChanged).toHaveBeenCalledOnce(); | ||
| expect(filterChanged).toHaveBeenCalledWith(expect.any(FilterType)); | ||
| }); | ||
|
|
||
| it('does not offer a filter for a supplementary-plane reference name rejected by the backend', () => { | ||
| const event = { | ||
| data: { '@ref:𐐀': 'reference-id' } | ||
| } as unknown as PersistentEvent; | ||
|
|
||
| render(Overview, { event, filterChanged: vi.fn() }); | ||
|
|
||
| expect(screen.queryByTitle('Filter ref.𐐀:reference-id')).toBeNull(); | ||
| }); | ||
| }); |
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 an environment has already completed
MigrateLegacyStripeSuspensionUserIdversion 7, the migration manager treats all lower versioned migrations as complete—the setup inMigrateSavedViewColumnsIntegrationTests.cs:95-104explicitly records only version 7 to mark every current versioned migration complete. Introducing this backfill afterward as version 6 therefore skips it in those environments, leaving retained events withoutidx.parent-r; assign it a new version above the current maximum.AGENTS.md reference: AGENTS.md:L72-L74
Useful? React with 👍 / 👎.