test(grpc-data): pinned pre-H2 SOURCE option (rig.py up --source-revision), wire tap, H05 v1-origin gate (#921); A09 driver-failure ack revert scenario (#927)

This commit is contained in:
Joseph Doherty
2026-11-28 09:28:59 -05:00
parent a2a1104f20
commit a0901ddf39
7 changed files with 737 additions and 21 deletions
@@ -0,0 +1,104 @@
using System.Text.Json;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Features;
using Microsoft.Extensions.DependencyInjection;
[assembly: HostingStartup(typeof(GrpcDataNodeProbe.WireTapStartup))]
namespace GrpcDataNodeProbe;
/// <summary>
/// Rig-only data-listener wire tap (lmxopcua#921). Off unless <c>GRPC_DATA_WIRE_TAP=1</c>; the rig sets it only on a
/// pinned-origin SOURCE (<c>rig.py up --source-revision</c>). It writes one JSON line when each request arrives on the
/// data listener (port 4057) and one when it completes: the UTC time, the gRPC path, the value of the
/// <c>x-otopcua-data-features</c> request header (null when absent), the remote address and, at completion, the
/// gRPC status. It records no credential, header other than the feature header, or message body.
/// </summary>
/// <remarks>
/// Self-contained on purpose: it uses ASP.NET Core only, so the rig copies this one file into a pinned older tree's
/// node probe and the same tap runs on a Host built from that revision. The arrival line is written before the gRPC
/// pipeline runs, so a call the older Host refuses (an unimplemented method, an unauthenticated caller) is recorded
/// too. The file is bounded (<see cref="MaximumLines"/>); past it, a single truncation line is written.
/// </remarks>
public sealed class WireTapStartup : IHostingStartup
{
/// <summary>The tap's output file name inside the node's probe directory.</summary>
public const string FileName = "wire-tap.jsonl";
/// <inheritdoc />
public void Configure(IWebHostBuilder builder)
{
if (Environment.GetEnvironmentVariable("GRPC_DATA_WIRE_TAP") != "1") return;
var directory = Environment.GetEnvironmentVariable("GRPC_DATA_NODE_PROBE_DIR");
if (string.IsNullOrWhiteSpace(directory) || !Directory.Exists(directory)) return;
var tap = new WireTapLog(Path.Combine(directory, FileName));
builder.ConfigureServices(services => services.AddSingleton<IStartupFilter>(new WireTapStartupFilter(tap)));
}
}
/// <summary>The bounded append-only line log behind the tap.</summary>
public sealed class WireTapLog(string path)
{
/// <summary>Lines written before the log stops (a soak would otherwise fill the probe directory).</summary>
public const int MaximumLines = 50_000;
private readonly object _gate = new();
private int _lines;
/// <summary>Appends one record as a JSON line; never throws into the request pipeline.</summary>
public void Write(object record)
{
try
{
lock (_gate)
{
if (_lines > MaximumLines) return;
var line = _lines++ == MaximumLines ? JsonSerializer.Serialize(new { truncated = true }) : JsonSerializer.Serialize(record);
File.AppendAllText(path, line + "\n");
}
}
catch (IOException) { }
catch (UnauthorizedAccessException) { }
}
}
/// <summary>Records each data-listener request at arrival (before authentication) and at completion.</summary>
public sealed class WireTapStartupFilter(WireTapLog log) : IStartupFilter
{
private const int DataListenerPort = 4057;
private const string FeaturesHeader = "x-otopcua-data-features";
private static long _sequence;
/// <inheritdoc />
public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next) => app =>
{
app.Use(nextMiddleware => async context =>
{
if (context.Connection.LocalPort != DataListenerPort)
{
await nextMiddleware(context);
return;
}
var sequence = Interlocked.Increment(ref _sequence);
var path = context.Request.Path.Value;
string? features = context.Request.Headers.TryGetValue(FeaturesHeader, out var value) ? value.ToString() : null;
var remote = context.Connection.RemoteIpAddress?.ToString();
log.Write(new { phase = "arrival", sequence, utc = DateTime.UtcNow.ToString("O"), path, features, remote, protocol = context.Request.Protocol });
try
{
await nextMiddleware(context);
}
finally
{
// A trailers-only answer (e.g. Unimplemented) carries grpc-status in the headers; a normal one in the trailers.
var trailers = context.Features.Get<IHttpResponseTrailersFeature>()?.Trailers;
string? status = trailers is not null && trailers.TryGetValue("grpc-status", out var t) ? t.ToString()
: context.Response.Headers.TryGetValue("grpc-status", out var h) ? h.ToString() : null;
log.Write(new { phase = "completion", sequence, utc = DateTime.UtcNow.ToString("O"), path, grpcStatus = status, httpStatus = context.Response.StatusCode });
}
});
next(app);
};
}