const std = @import("std"); const config = @import("../config.zig"); const runner_log = @import("log.zig"); const runner_metrics = @import("metrics.zig"); const retry_initial_backoff_nanos: u64 = 250 * std.time.ns_per_ms; const retry_max_backoff_nanos: u64 = 5 * std.time.ns_per_s; pub const Labels = struct { scenario_name: []const u8, scenario_mode: []const u8, protocol: config.Protocol, }; pub const QueuedExport = struct { allocator: std.mem.Allocator, otlp: config.OtlpExporter, labels: Labels, measured: runner_metrics.WindowSummary, warmup: runner_metrics.WindowSummary, pub fn clone( allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, summary: *const runner_metrics.FinalSummary, otlp_exporter: config.OtlpExporter, ) !QueuedExport { return .{ .allocator = allocator, .otlp = .{ .url = try allocator.dupe(u8, otlp_exporter.url), .retry_max_attempts = otlp_exporter.retry_max_attempts, .retry_max_elapsed = otlp_exporter.retry_max_elapsed, .buffer_max_exports = otlp_exporter.buffer_max_exports, }, .labels = .{ .scenario_name = try allocator.dupe(u8, scenario.scenario.name), .scenario_mode = scenario.scenario.mode().label(), .protocol = scenario.scenario.protocol, }, .measured = try cloneWindowSummary(allocator, summary.output.measured), .warmup = try cloneWindowSummary(allocator, summary.output.warmup), }; } pub fn deinit(self: *QueuedExport) void { self.allocator.free(self.otlp.url); self.allocator.free(self.labels.scenario_name); deinitClonedWindowSummary(self.allocator, &self.measured); deinitClonedWindowSummary(self.allocator, &self.warmup); self.* = undefined; } }; var pending_otlp_exports: std.ArrayListUnmanaged(QueuedExport) = .empty; var pending_otlp_mutex: std.Thread.Mutex = .{}; var queued_export_send_override: ?*const fn (std.mem.Allocator, *const QueuedExport) anyerror!void = null; pub fn enqueueOtlpExport( allocator: std.mem.Allocator, scenario: *const config.ScenarioConfig, summary: *const runner_metrics.FinalSummary, otlp_exporter: config.OtlpExporter, logger: *runner_log.Logger, ) !void { var queued = try QueuedExport.clone(allocator, scenario, summary, otlp_exporter); errdefer queued.deinit(); var dropped: ?QueuedExport = null; pending_otlp_mutex.lock(); errdefer pending_otlp_mutex.unlock(); if (pending_otlp_exports.items.len >= otlp_exporter.buffer_max_exports) { dropped = pending_otlp_exports.orderedRemove(0); } errdefer if (dropped) |*value| value.deinit(); try pending_otlp_exports.append(allocator, queued); pending_otlp_mutex.unlock(); if (dropped) |*value| { value.deinit(); try logger.log(.warn, "otlp export buffer full; dropped oldest pending batch", .{ .component = "exporter", .source = .host, .event = "otlp_buffer_drop", }); } } pub fn flushQueuedOtlpExports( allocator: std.mem.Allocator, logger: *runner_log.Logger, send_fn: *const fn (std.mem.Allocator, *const QueuedExport) anyerror!void, ) !void { while (true) { pending_otlp_mutex.lock(); if (pending_otlp_exports.items.len == 0) { pending_otlp_mutex.unlock(); return; } const succeeded = try exportQueuedWithRetry(allocator, &pending_otlp_exports.items[0], logger, send_fn); if (!succeeded) { pending_otlp_mutex.unlock(); return; } var exported = pending_otlp_exports.orderedRemove(0); pending_otlp_mutex.unlock(); exported.deinit(); } } pub fn resetQueuedOtlpExportsForTests() void { pending_otlp_mutex.lock(); defer pending_otlp_mutex.unlock(); for (pending_otlp_exports.items) |*queued| queued.deinit(); pending_otlp_exports.clearAndFree(std.testing.allocator); } pub fn bufferedExportCount() usize { pending_otlp_mutex.lock(); defer pending_otlp_mutex.unlock(); return pending_otlp_exports.items.len; } pub fn setSendOverride(send_override: ?*const fn (std.mem.Allocator, *const QueuedExport) anyerror!void) void { queued_export_send_override = send_override; } fn exportQueuedWithRetry( allocator: std.mem.Allocator, queued: *const QueuedExport, logger: *runner_log.Logger, send_fn: *const fn (std.mem.Allocator, *const QueuedExport) anyerror!void, ) !bool { const started_nanos = std.time.nanoTimestamp(); var attempt: u32 = 0; var backoff_nanos: u64 = retry_initial_backoff_nanos; while (attempt < queued.otlp.retry_max_attempts) : (attempt += 1) { sendQueuedExport(allocator, queued, send_fn) catch |err| { const elapsed_nanos: u64 = @intCast(@max(std.time.nanoTimestamp() - started_nanos, 0)); const next_attempt = attempt + 1; if (next_attempt >= queued.otlp.retry_max_attempts or elapsed_nanos >= queued.otlp.retry_max_elapsed.nanos) { try logger.logf(.warn, .{ .component = "exporter", .source = .host, .event = "otlp_export_buffered", }, "otlp export failed after {d} attempts: {s}; keeping batch buffered for the next export", .{ next_attempt, @errorName(err) }); return false; } try logger.logf(.warn, .{ .component = "exporter", .source = .host, .event = "otlp_retry_scheduled", }, "otlp export attempt {d} failed: {s}; retrying", .{ next_attempt, @errorName(err) }); std.Thread.sleep(backoff_nanos); backoff_nanos = @min(backoff_nanos * 2, retry_max_backoff_nanos); continue; }; return true; } return false; } fn sendQueuedExport( allocator: std.mem.Allocator, queued: *const QueuedExport, send_fn: *const fn (std.mem.Allocator, *const QueuedExport) anyerror!void, ) !void { if (queued_export_send_override) |send_override| { return try send_override(allocator, queued); } return try send_fn(allocator, queued); } fn cloneWindowSummary(allocator: std.mem.Allocator, summary: runner_metrics.WindowSummary) !runner_metrics.WindowSummary { var cloned = summary; cloned.grpc_status_counts = if (summary.grpc_status_counts.len != 0) try allocator.dupe(runner_metrics.StatusCount, summary.grpc_status_counts) else &.{}; if (summary.request_summaries.len == 0) { cloned.request_summaries = &.{}; return cloned; } var request_summaries = try allocator.alloc(runner_metrics.NamedRequestSummary, summary.request_summaries.len); errdefer { for (request_summaries[0..summary.request_summaries.len]) |item| { if (item.name.len != 0) allocator.free(item.name); if (item.summary.grpc_status_counts.len != 0) allocator.free(item.summary.grpc_status_counts); } allocator.free(request_summaries); } for (summary.request_summaries, 0..) |request_summary, index| { request_summaries[index] = .{ .name = try allocator.dupe(u8, request_summary.name), .summary = request_summary.summary, }; request_summaries[index].summary.grpc_status_counts = if (request_summary.summary.grpc_status_counts.len != 0) try allocator.dupe(runner_metrics.StatusCount, request_summary.summary.grpc_status_counts) else &.{}; } cloned.request_summaries = request_summaries; return cloned; } fn deinitClonedWindowSummary(allocator: std.mem.Allocator, summary: *runner_metrics.WindowSummary) void { if (summary.grpc_status_counts.len != 0) allocator.free(summary.grpc_status_counts); for (summary.request_summaries) |request_summary| { allocator.free(request_summary.name); if (request_summary.summary.grpc_status_counts.len != 0) { allocator.free(request_summary.summary.grpc_status_counts); } } if (summary.request_summaries.len != 0) allocator.free(summary.request_summaries); summary.* = undefined; }