Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions src/infra/docs-lambda-changelog-scrubber/EmfMetricsEmitter.cs
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

Copy link
Copy Markdown
Member

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?

/// (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 src/infra/docs-lambda-changelog-scrubber/LambdaLoggerFactory.cs
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"
};
}
}
Loading
Loading