From 14aac25f21c7e3d7efadb4887ed0f300352c7a66 Mon Sep 17 00:00:00 2001
From: KonradStaniec <konrad.staniec@gmail.com>
Date: Mon, 27 Jan 2025 15:21:22 +0100
Subject: [PATCH] additional covenant signer metrics (#99)

* additional covenant signer metrics
---
 CHANGELOG.md                                  |  2 ++
 .../observability/metrics/signer.go           | 30 ++++++++++++++++---
 .../handlers/signtransactions.go              |  5 ++++
 3 files changed, 33 insertions(+), 4 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index f9587de..606ce5c 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -45,6 +45,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 * [#95](https://github.com/babylonlabs-io/covenant-emulator/pull/95) removed local signer option
 as the covenant emulator should only connect to a remote signer
 * [#96](https://github.com/babylonlabs-io/covenant-emulator/pull/96) add pagination to `queryDelegationsWithStatus`
+* [#99](https://github.com/babylonlabs-io/covenant-emulator/pull/99) add more metrics
+covenant-signer
 
 ## v0.11.3
 
diff --git a/covenant-signer/observability/metrics/signer.go b/covenant-signer/observability/metrics/signer.go
index ba3dde5..bbd688d 100644
--- a/covenant-signer/observability/metrics/signer.go
+++ b/covenant-signer/observability/metrics/signer.go
@@ -6,10 +6,12 @@ import (
 )
 
 type CovenantSignerMetrics struct {
-	Registry                  *prometheus.Registry
-	ReceivedSigningRequests   prometheus.Counter
-	SuccessfulSigningRequests prometheus.Counter
-	FailedSigningRequests     prometheus.Counter
+	Registry                   *prometheus.Registry
+	ReceivedSigningRequests    prometheus.Counter
+	SuccessfulSigningRequests  prometheus.Counter
+	FailedSigningRequests      prometheus.Counter
+	SignerUnlockStatus         prometheus.Gauge
+	SignerFailedUnlockRequests prometheus.Counter
 }
 
 func NewCovenantSignerMetrics() *CovenantSignerMetrics {
@@ -30,6 +32,14 @@ func NewCovenantSignerMetrics() *CovenantSignerMetrics {
 			Name: "signer_failed_signing_requests",
 			Help: "The total number of times signer responded with an internal error",
 		}),
+		SignerUnlockStatus: registerer.NewGauge(prometheus.GaugeOpts{
+			Name: "signer_unlock_status",
+			Help: "The status indicating if the signer is unlocked or locked. 1 for unlocked, 0 for locked",
+		}),
+		SignerFailedUnlockRequests: registerer.NewCounter(prometheus.CounterOpts{
+			Name: "signer_failed_unlock_requests",
+			Help: "The total number of times the signer failed to unlock",
+		}),
 	}
 
 	return uwMetrics
@@ -46,3 +56,15 @@ func (m *CovenantSignerMetrics) IncSuccessfulSigningRequests() {
 func (m *CovenantSignerMetrics) IncFailedSigningRequests() {
 	m.FailedSigningRequests.Inc()
 }
+
+func (m *CovenantSignerMetrics) SetSignerUnlocked() {
+	m.SignerUnlockStatus.Set(1)
+}
+
+func (m *CovenantSignerMetrics) SetSignerLocked() {
+	m.SignerUnlockStatus.Set(0)
+}
+
+func (m *CovenantSignerMetrics) IncFailedUnlockRequests() {
+	m.SignerFailedUnlockRequests.Inc()
+}
diff --git a/covenant-signer/signerservice/handlers/signtransactions.go b/covenant-signer/signerservice/handlers/signtransactions.go
index f0109b7..8d47036 100644
--- a/covenant-signer/signerservice/handlers/signtransactions.go
+++ b/covenant-signer/signerservice/handlers/signtransactions.go
@@ -64,9 +64,12 @@ func (h *Handler) Unlock(request *http.Request) (*Result, *types.Error) {
 
 	err = h.s.Unlock(request.Context(), payload.Passphrase)
 	if err != nil {
+		h.m.IncFailedUnlockRequests()
 		return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, err.Error())
 	}
 
+	h.m.SetSignerUnlocked()
+
 	return NewResult(&types.UnlockResponse{}), nil
 }
 
@@ -76,5 +79,7 @@ func (h *Handler) Lock(request *http.Request) (*Result, *types.Error) {
 		return nil, types.NewErrorWithMsg(http.StatusBadRequest, types.BadRequest, err.Error())
 	}
 
+	h.m.SetSignerLocked()
+
 	return NewResult(&types.LockResponse{}), nil
 }