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 = NewHistogram("ingest_pipeline_ns")
  16  
  17  	// SigVerifyNs measures BIP340 Schnorr verification time per event.
  18  	SigVerifyNs = NewHistogram("sig_verify_ns")
  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 = NewHistogram("acl_allowwrite_ns")
  23  
  24  	// WALAppendNs measures WAL.Append wall time, including the optional
  25  	// fsync at the configured cadence (default every 100 writes).
  26  	WALAppendNs = NewHistogram("wal_append_ns")
  27  
  28  	// WALFsyncNs measures the explicit fsync syscall when triggered.
  29  	WALFsyncNs = NewHistogram("wal_fsync_ns")
  30  
  31  	// AcceptLoopHandleNs measures wall time of one EpollWait → handler
  32  	// dispatch cycle (excluding the EpollWait blocking wait itself).
  33  	AcceptLoopHandleNs = NewHistogram("accept_handle_ns")
  34  
  35  	// AcceptLoopWaitNs measures EpollWait blocking wait.
  36  	AcceptLoopWaitNs = NewHistogram("accept_wait_ns")
  37  )
  38  
  39  var allHistograms = []*Histogram{
  40  	IngestPipelineNs,
  41  	SigVerifyNs,
  42  	ACLAllowWriteNs,
  43  	WALAppendNs,
  44  	WALFsyncNs,
  45  	AcceptLoopHandleNs,
  46  	AcceptLoopWaitNs,
  47  }
  48  
  49  // SnapshotAll emits a JSON object of all registered histograms.
  50  func SnapshotAll(buf []byte) []byte {
  51  	buf = append(buf, '{')
  52  	for i, h := range allHistograms {
  53  		if i > 0 {
  54  			buf = append(buf, ',')
  55  		}
  56  		buf = h.AppendJSON(buf)
  57  	}
  58  	buf = append(buf, '}')
  59  	return buf
  60  }
  61  
  62  // ResetAll clears all histograms. Used between benchmark phases.
  63  func ResetAll() {
  64  	for _, h := range allHistograms {
  65  		h.Reset()
  66  	}
  67  }
  68