Skip to content
Open
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
16 changes: 14 additions & 2 deletions v2/components/gossip/tests/TestNode.zig
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ const SnapshotSource = struct {
slot: Slot,
hash: Hash,
};
const TestLogStore = lib.telemetry.TestLogStore;
const TestMetricStore = lib.telemetry.TestMetricStore;

allocator: std.mem.Allocator,
/// Backing memory for the node's fixed-buffer allocations, reused by reset.
scratch: []u8,
log_store: TestLogStore,
metric_store: TestMetricStore,
effects: *Effects,
/// GossipNode under test, reinitialized by reset.
Expand Down Expand Up @@ -130,6 +132,9 @@ pub fn init(
errdefer allocator.free(scratch);
var fixed_buffer: std.heap.FixedBufferAllocator = .init(scratch);

var log_store = try TestLogStore.init(allocator, .{});
errdefer log_store.deinit();

var metric_store = try TestMetricStore.init(allocator, .{});
errdefer metric_store.deinit();
const metrics = appendGossipMetrics(&metric_store);
Expand All @@ -149,6 +154,7 @@ pub fn init(
return .{
.allocator = allocator,
.scratch = scratch,
.log_store = log_store,
.metric_store = metric_store,
.effects = effects,
.node = node,
Expand All @@ -157,6 +163,7 @@ pub fn init(
}

pub fn deinit(self: *TestNode) void {
self.log_store.deinit();
self.metric_store.deinit();
self.allocator.free(self.scratch);
self.allocator.destroy(self.effects);
Expand All @@ -173,6 +180,7 @@ pub fn reset(self: *TestNode, now_ms: u64) !void {
self.effects.packets_len = 0;
self.effects.snapshot_sources_len = 0;

self.log_store.reset();
self.metric_store.reset();
const metrics = appendGossipMetrics(&self.metric_store);

Expand All @@ -183,12 +191,16 @@ pub fn reset(self: *TestNode, now_ms: u64) !void {
self.node.assertInvariants();
}

pub fn logs(self: *TestNode) *TestLogStore {
return &self.log_store;
}

pub fn identity(self: *const TestNode) Pubkey {
return self.effects.keypair.pubkey;
}

pub fn poll(self: *TestNode) !void {
try self.node.poll(.noop, self.now_ms);
try self.node.poll(self.log_store.logger("poll"), self.now_ms);
self.node.assertInvariants();
}

Expand All @@ -197,7 +209,7 @@ pub fn advanceMs(self: *TestNode, duration_ms: u64) void {
}

pub fn receivePacket(self: *TestNode, packet: *const Packet) void {
self.node.processPacket(.noop, self.now_ms, packet);
self.node.processPacket(self.log_store.logger("processPacket"), self.now_ms, packet);
self.node.assertInvariants();
}

Expand Down
31 changes: 24 additions & 7 deletions v2/lib/telemetry.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const tracy = @import("tracy");
pub const metric = @import("telemetry/metric.zig");
pub const log = @import("telemetry/log.zig");
pub const prometheus = @import("telemetry/prometheus.zig");
pub const TestLogStore = @import("telemetry/tests/TestLogStore.zig");
pub const TestMetricStore = @import("telemetry/tests/TestMetricStore.zig");
comptime {
if (@import("builtin").is_test) {
Expand Down Expand Up @@ -316,6 +317,17 @@ pub fn Logger(comptime scope_str: []const u8) type {
};
}

/// Labels this message as an alert for the specified audience.
pub fn alert(
self: *const EntrySelf,
comptime audience: log_zig.Alert,
) Entry(entry_count + 1) {
const AlertValue = struct {
const value = @tagName(audience);
};
return self.field("alert", &AlertValue.value);
Comment on lines +325 to +328

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you try this already and it had a problem?

Suggested change
const AlertValue = struct {
const value = @tagName(audience);
};
return self.field("alert", &AlertValue.value);
return self.field("alert", @tagName(audience));

tagName already returns a pointer. You don't need a pointer to a pointer. I think you can eliminate this struct too since the lifetime of the pointer returned by tagName should be static.

}

/// Returns the field format string for common types: strings,
/// numbers, and types with `format` functions.
///
Expand Down Expand Up @@ -387,6 +399,8 @@ pub fn Logger(comptime scope_str: []const u8) type {
},
}

if (self.logger.sink == .noop) return;

const message: log_zig.Message = .{
.epoch_millis = clock.wallclock(.ms),
.scope = scope,
Expand All @@ -395,16 +409,20 @@ pub fn Logger(comptime scope_str: []const u8) type {
.level = self.level,
};

var field_value_plan_storage: [entry_count]log_zig.EntryValueFmt.EncodingPlan =
undefined;
const encoding = message.computeEncodingPlan(&field_value_plan_storage);

switch (self.logger.sink) {
.noop => return,
// already determined not a noop, this is just for exhaustive switch
.noop => unreachable,
.writer => |w| {
_ = message.write(w) catch |e| switch (e) {
message.write(w, encoding) catch |e| switch (e) {
error.WriteFailed => {},
};
},
.swap_buffer => |sb| {
const expected_header = message.computeHeader();
const encoded_len = expected_header.encodedLength();
const encoded_len = encoding.header.encodedLength();

// NOTE: although the retry path is highly unlikely
// assuming the swapbuffer is sufficiently large,
Expand All @@ -427,13 +445,12 @@ pub fn Logger(comptime scope_str: []const u8) type {
);

var fbw: std.Io.Writer = .fixed(writable.slice);
const message_header = message.write(&fbw) catch |e| switch (e) {
message.write(&fbw, encoding) catch |e| switch (e) {
// we already know there's enough space in the
// buffer for the message.
error.WriteFailed => unreachable,
};
std.debug.assert(message_header.encodedLength() == encoded_len);
std.debug.assert(std.meta.eql(message_header, expected_header));
std.debug.assert(fbw.buffered().len == encoded_len);
writable.commit(encoded_len);
},
}
Expand Down
Loading
Loading