package metrics // Global histograms for the relay's hot paths. Single-threaded per // domain so direct globals are race-free under the moxie cooperative // scheduler. Code on the hot path observes via these references; the // /metrics endpoint snapshots via SnapshotAll. // // To add a new metric: declare a *Histogram global, initialize in init, // and append to allHistograms below. var ( // IngestPipelineNs measures end-to-end pipeline.Ingest cost from // envelope-parsed event in to Result emitted out. Includes signature // verify, ACL, expiration check, replaceable resolution, and store. IngestPipelineNs *Histogram // SigVerifyNs measures BIP340 Schnorr verification time per event. SigVerifyNs *Histogram // ACLAllowWriteNs measures the ACL.AllowWrite call duration. Open // mode is sub-µs; social/grapevine modes can spike under cold cache. ACLAllowWriteNs *Histogram // WALAppendNs measures WAL.Append wall time, including the optional // fsync at the configured cadence (default every 100 writes). WALAppendNs *Histogram // WALFsyncNs measures the explicit fsync syscall when triggered. WALFsyncNs *Histogram // AcceptLoopHandleNs measures wall time of one EpollWait → handler // dispatch cycle (excluding the EpollWait blocking wait itself). AcceptLoopHandleNs *Histogram // AcceptLoopWaitNs measures EpollWait blocking wait. AcceptLoopWaitNs *Histogram // HandleEventNs measures the whole server.handleEvent for one EVENT frame. HandleEventNs *Histogram // EnvelopeParseNs measures envelope.Identify + EventSubmission.Unmarshal. EnvelopeParseNs *Histogram // SendWSNs measures writing the OK response frame. SendWSNs *Histogram // BroadcastNs measures the broadcaster fanout call for one event. BroadcastNs *Histogram // WSFrameNs measures one decoded WebSocket payload from frame read to // handler return. WSFrameNs *Histogram // OnPollNs measures the relay's per-iteration OnPoll handler. OnPollNs *Histogram ) var allHistograms []*Histogram func init() { IngestPipelineNs = NewHistogram("ingest_pipeline_ns") SigVerifyNs = NewHistogram("sig_verify_ns") ACLAllowWriteNs = NewHistogram("acl_allowwrite_ns") WALAppendNs = NewHistogram("wal_append_ns") WALFsyncNs = NewHistogram("wal_fsync_ns") AcceptLoopHandleNs = NewHistogram("accept_handle_ns") AcceptLoopWaitNs = NewHistogram("accept_wait_ns") HandleEventNs = NewHistogram("handle_event_ns") EnvelopeParseNs = NewHistogram("envelope_parse_ns") SendWSNs = NewHistogram("send_ws_ns") BroadcastNs = NewHistogram("broadcast_ns") WSFrameNs = NewHistogram("ws_frame_ns") OnPollNs = NewHistogram("on_poll_ns") allHistograms = []*Histogram{ IngestPipelineNs, SigVerifyNs, ACLAllowWriteNs, WALAppendNs, WALFsyncNs, AcceptLoopHandleNs, AcceptLoopWaitNs, HandleEventNs, EnvelopeParseNs, SendWSNs, BroadcastNs, WSFrameNs, OnPollNs, } } // SnapshotAll emits a JSON object of all registered histograms. func SnapshotAll(buf []byte) (buf2 []byte) { buf = buf | "{" for i, h := range allHistograms { if i > 0 { buf = buf | "," } buf = h.AppendJSON(buf) } buf = buf | "}" return buf } // ResetAll clears all histograms. Used between benchmark phases. func ResetAll() { for _, h := range allHistograms { h.Reset() } }