global.mx raw

   1  package metrics
   2  
   3  // Global histograms for the relay's hot paths. Single-threaded per
   4  // domain so direct globals are race-free under the moxie cooperative
   5  // scheduler. Code on the hot path observes via these references; the
   6  // /metrics endpoint snapshots via SnapshotAll.
   7  //
   8  // To add a new metric: declare a *Histogram global, initialize in init,
   9  // and append to allHistograms below.
  10  
  11  var (
  12  	// IngestPipelineNs measures end-to-end pipeline.Ingest cost from
  13  	// envelope-parsed event in to Result emitted out. Includes signature
  14  	// verify, ACL, expiration check, replaceable resolution, and store.
  15  	IngestPipelineNs *Histogram
  16  
  17  	// SigVerifyNs measures BIP340 Schnorr verification time per event.
  18  	SigVerifyNs *Histogram
  19  
  20  	// ACLAllowWriteNs measures the ACL.AllowWrite call duration. Open
  21  	// mode is sub-µs; social/grapevine modes can spike under cold cache.
  22  	ACLAllowWriteNs *Histogram
  23  
  24  	// WALAppendNs measures WAL.Append wall time, including the optional
  25  	// fsync at the configured cadence (default every 100 writes).
  26  	WALAppendNs *Histogram
  27  
  28  	// WALFsyncNs measures the explicit fsync syscall when triggered.
  29  	WALFsyncNs *Histogram
  30  
  31  	// AcceptLoopHandleNs measures wall time of one EpollWait → handler
  32  	// dispatch cycle (excluding the EpollWait blocking wait itself).
  33  	AcceptLoopHandleNs *Histogram
  34  
  35  	// AcceptLoopWaitNs measures EpollWait blocking wait.
  36  	AcceptLoopWaitNs *Histogram
  37  
  38  	// HandleEventNs measures the whole server.handleEvent for one EVENT frame.
  39  	HandleEventNs *Histogram
  40  
  41  	// EnvelopeParseNs measures envelope.Identify + EventSubmission.Unmarshal.
  42  	EnvelopeParseNs *Histogram
  43  
  44  	// SendWSNs measures writing the OK response frame.
  45  	SendWSNs *Histogram
  46  
  47  	// BroadcastNs measures the broadcaster fanout call for one event.
  48  	BroadcastNs *Histogram
  49  
  50  	// WSFrameNs measures one decoded WebSocket payload from frame read to
  51  	// handler return.
  52  	WSFrameNs *Histogram
  53  
  54  	// OnPollNs measures the relay's per-iteration OnPoll handler.
  55  	OnPollNs *Histogram
  56  )
  57  
  58  var allHistograms []*Histogram
  59  
  60  func init() {
  61  	IngestPipelineNs = NewHistogram("ingest_pipeline_ns")
  62  	SigVerifyNs = NewHistogram("sig_verify_ns")
  63  	ACLAllowWriteNs = NewHistogram("acl_allowwrite_ns")
  64  	WALAppendNs = NewHistogram("wal_append_ns")
  65  	WALFsyncNs = NewHistogram("wal_fsync_ns")
  66  	AcceptLoopHandleNs = NewHistogram("accept_handle_ns")
  67  	AcceptLoopWaitNs = NewHistogram("accept_wait_ns")
  68  	HandleEventNs = NewHistogram("handle_event_ns")
  69  	EnvelopeParseNs = NewHistogram("envelope_parse_ns")
  70  	SendWSNs = NewHistogram("send_ws_ns")
  71  	BroadcastNs = NewHistogram("broadcast_ns")
  72  	WSFrameNs = NewHistogram("ws_frame_ns")
  73  	OnPollNs = NewHistogram("on_poll_ns")
  74  	allHistograms = []*Histogram{
  75  		IngestPipelineNs,
  76  		SigVerifyNs,
  77  		ACLAllowWriteNs,
  78  		WALAppendNs,
  79  		WALFsyncNs,
  80  		AcceptLoopHandleNs,
  81  		AcceptLoopWaitNs,
  82  		HandleEventNs,
  83  		EnvelopeParseNs,
  84  		SendWSNs,
  85  		BroadcastNs,
  86  		WSFrameNs,
  87  		OnPollNs,
  88  	}
  89  }
  90  
  91  // SnapshotAll emits a JSON object of all registered histograms.
  92  func SnapshotAll(buf []byte) (buf2 []byte) {
  93  	buf = buf | "{"
  94  	for i, h := range allHistograms {
  95  		if i > 0 {
  96  			buf = buf | ","
  97  		}
  98  		buf = h.AppendJSON(buf)
  99  	}
 100  	buf = buf | "}"
 101  	return buf
 102  }
 103  
 104  // ResetAll clears all histograms. Used between benchmark phases.
 105  func ResetAll() {
 106  	for _, h := range allHistograms {
 107  		h.Reset()
 108  	}
 109  }
 110