-
Notifications
You must be signed in to change notification settings - Fork 43
Move public changelog registry ownership to the scrubber Lambda #3738
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
112 changes: 112 additions & 0 deletions
112
src/infra/docs-lambda-changelog-scrubber/EmfMetricsEmitter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using System.Text.Json; | ||
| using System.Text.Json.Serialization; | ||
| using Elastic.Changelog.Reconciliation; | ||
|
|
||
| namespace Elastic.Documentation.Lambda.ChangelogScrubber; | ||
|
|
||
| /// <summary> | ||
| /// Emits the per-invocation reconcile counters as a CloudWatch Embedded Metric Format line | ||
| /// (elastic/docs-eng-team#688 Phase 0 observability: these numbers gate any later SQS/Lambda | ||
| /// tuning). The counter names are static, so the payload is a fixed source-generated contract; | ||
| /// it is written to stdout unwrapped, which is what the EMF parser requires. | ||
| /// </summary> | ||
| internal static class EmfMetricsEmitter | ||
| { | ||
| private const string Namespace = "docs-changelog-scrubber"; | ||
|
|
||
| private static readonly IReadOnlyList<EmfMetricDefinition> MetricDefinitions = | ||
| [ | ||
| new() { Name = "ObjectReconciles" }, | ||
| new() { Name = "ObjectReconcileRetries" }, | ||
| new() { Name = "GroupReconciles" }, | ||
| new() { Name = "RegistryWrites" }, | ||
| new() { Name = "RegistryDeletes" }, | ||
| new() { Name = "RegistryUnchanged" }, | ||
| new() { Name = "WriteConflicts" }, | ||
| new() { Name = "ObjectsListed" }, | ||
| new() { Name = "EntriesRecomputed" }, | ||
| new() { Name = "ShallowRegistryWrites" }, | ||
| new() { Name = "ShallowRegistryUnchanged" }, | ||
| new() { Name = "FailedMessages" } | ||
| ]; | ||
|
|
||
| public static void Emit(ReconcileMetrics metrics) | ||
| { | ||
| var payload = new EmfPayload | ||
| { | ||
| Aws = new EmfEnvelope | ||
| { | ||
| Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), | ||
| CloudWatchMetrics = | ||
| [ | ||
| new EmfMetricDirective | ||
| { | ||
| Namespace = Namespace, | ||
| Dimensions = [[]], | ||
| Metrics = MetricDefinitions | ||
| } | ||
| ] | ||
| }, | ||
| ObjectReconciles = metrics.ObjectReconciles, | ||
| ObjectReconcileRetries = metrics.ObjectReconcileRetries, | ||
| GroupReconciles = metrics.GroupReconciles, | ||
| RegistryWrites = metrics.RegistryWrites, | ||
| RegistryDeletes = metrics.RegistryDeletes, | ||
| RegistryUnchanged = metrics.RegistryUnchanged, | ||
| WriteConflicts = metrics.WriteConflicts, | ||
| ObjectsListed = metrics.ObjectsListed, | ||
| EntriesRecomputed = metrics.EntriesRecomputed, | ||
| ShallowRegistryWrites = metrics.ShallowRegistryWrites, | ||
| ShallowRegistryUnchanged = metrics.ShallowRegistryUnchanged, | ||
| FailedMessages = metrics.FailedMessages | ||
| }; | ||
|
|
||
| Console.WriteLine(JsonSerializer.Serialize(payload, EmfJsonContext.Default.EmfPayload)); | ||
| } | ||
| } | ||
|
|
||
| /// <summary>One EMF log line: the <c>_aws</c> envelope plus the metric values as top-level members.</summary> | ||
| internal sealed record EmfPayload | ||
| { | ||
| [JsonPropertyName("_aws")] | ||
| public required EmfEnvelope Aws { get; init; } | ||
|
|
||
| public required int ObjectReconciles { get; init; } | ||
| public required int ObjectReconcileRetries { get; init; } | ||
| public required int GroupReconciles { get; init; } | ||
| public required int RegistryWrites { get; init; } | ||
| public required int RegistryDeletes { get; init; } | ||
| public required int RegistryUnchanged { get; init; } | ||
| public required int WriteConflicts { get; init; } | ||
| public required int ObjectsListed { get; init; } | ||
| public required int EntriesRecomputed { get; init; } | ||
| public required int ShallowRegistryWrites { get; init; } | ||
| public required int ShallowRegistryUnchanged { get; init; } | ||
| public required int FailedMessages { get; init; } | ||
| } | ||
|
|
||
| internal sealed record EmfEnvelope | ||
| { | ||
| public required long Timestamp { get; init; } | ||
| public required IReadOnlyList<EmfMetricDirective> CloudWatchMetrics { get; init; } | ||
| } | ||
|
|
||
| internal sealed record EmfMetricDirective | ||
| { | ||
| public required string Namespace { get; init; } | ||
| public required IReadOnlyList<IReadOnlyList<string>> Dimensions { get; init; } | ||
| public required IReadOnlyList<EmfMetricDefinition> Metrics { get; init; } | ||
| } | ||
|
|
||
| internal sealed record EmfMetricDefinition | ||
| { | ||
| public required string Name { get; init; } | ||
| public string Unit { get; init; } = "Count"; | ||
| } | ||
|
|
||
| [JsonSerializable(typeof(EmfPayload))] | ||
| internal sealed partial class EmfJsonContext : JsonSerializerContext; | ||
57 changes: 57 additions & 0 deletions
57
src/infra/docs-lambda-changelog-scrubber/LambdaLoggerFactory.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| // Licensed to Elasticsearch B.V under one or more agreements. | ||
| // Elasticsearch B.V licenses this file to you under the Apache 2.0 License. | ||
| // See the LICENSE file in the project root for more information | ||
|
|
||
| using Amazon.Lambda.Core; | ||
| using Microsoft.Extensions.Logging; | ||
| using LogLevel = Microsoft.Extensions.Logging.LogLevel; | ||
|
|
||
| namespace Elastic.Documentation.Lambda.ChangelogScrubber; | ||
|
|
||
| /// <summary> | ||
| /// Routes <see cref="ILogger"/> calls from the shared Elastic.Changelog processing code to the | ||
| /// Lambda's own logger, so the extracted processor stays free of Lambda dependencies. | ||
| /// </summary> | ||
| internal sealed class LambdaLoggerFactory(ILambdaLogger lambdaLogger) : ILoggerFactory | ||
| { | ||
| public ILogger CreateLogger(string categoryName) => new LambdaLoggerAdapter(categoryName, lambdaLogger); | ||
|
|
||
| public void AddProvider(ILoggerProvider provider) | ||
| { | ||
| // Providers are meaningless here; everything goes to the Lambda logger. | ||
| } | ||
|
|
||
| public void Dispose() | ||
| { | ||
| } | ||
|
|
||
| private sealed class LambdaLoggerAdapter(string categoryName, ILambdaLogger lambdaLogger) : ILogger | ||
| { | ||
| public IDisposable? BeginScope<TState>(TState state) where TState : notnull => null; | ||
|
|
||
| public bool IsEnabled(LogLevel logLevel) => logLevel >= LogLevel.Information; | ||
|
|
||
| public void Log<TState>(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func<TState, Exception?, string> formatter) | ||
| { | ||
| if (!IsEnabled(logLevel)) | ||
| return; | ||
|
|
||
| var message = $"{categoryName}: {formatter(state, exception)}"; | ||
| if (exception is not null) | ||
| message = $"{message} | {exception}"; | ||
|
|
||
| lambdaLogger.Log(MapLevel(logLevel), message); | ||
| } | ||
|
|
||
| private static string MapLevel(LogLevel level) => level switch | ||
| { | ||
| LogLevel.Trace => "TRACE", | ||
| LogLevel.Debug => "DEBUG", | ||
| LogLevel.Information => "INFO", | ||
| LogLevel.Warning => "WARN", | ||
| LogLevel.Error => "ERROR", | ||
| LogLevel.Critical => "CRITICAL", | ||
| _ => "INFO" | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is overly defensive IMO, we use AOT trimming and source generated json contracts heavily. We should not fallback to handcrafted json emission.
These are emitted as cloudwatch metrics, should we push (some of) them as OTEL metrics too?