Skip to content

Commit cd8e96b

Browse files
committed
feat: tx_failure_count metric for failed transactions
Signed-off-by: Chris Gianelloni <[email protected]>
1 parent ca6935e commit cd8e96b

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

internal/api/api.go

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ func Start(cfg *config.Config) error {
4343
// Use metrics middleware without exposing path in main app router
4444
metrics.UseWithoutExposingEndpoint(router)
4545

46-
// Custom metric
46+
// Custom metrics
47+
failureMetric := &ginmetrics.Metric{
48+
// This is a Gauge because input-output-hk's is a gauge
49+
Type: ginmetrics.Gauge,
50+
Name: "tx_failure_count",
51+
Description: "transactions failed",
52+
Labels: nil,
53+
}
4754
submittedMetric := &ginmetrics.Metric{
4855
// This is a Gauge because input-output-hk's is a gauge
4956
Type: ginmetrics.Gauge,
@@ -52,6 +59,7 @@ func Start(cfg *config.Config) error {
5259
Labels: nil,
5360
}
5461
// Add to global monitor object
62+
_ = ginmetrics.GetMonitor().AddMetric(failureMetric)
5563
_ = ginmetrics.GetMonitor().AddMetric(submittedMetric)
5664

5765
// Start metrics listener
@@ -85,6 +93,7 @@ func handleSubmitTx(c *gin.Context) {
8593
if err != nil {
8694
logger.Errorf("failed to read request body: %s", err)
8795
c.String(500, "failed to request body")
96+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
8897
return
8998
}
9099
if err := c.Request.Body.Close(); err != nil {
@@ -95,6 +104,7 @@ func handleSubmitTx(c *gin.Context) {
95104
if err := cbor.Unmarshal(rawTx, &txUnwrap); err != nil {
96105
logger.Errorf("failed to unwrap transaction CBOR: %s", err)
97106
c.String(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err))
107+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
98108
return
99109
}
100110
txId := blake2b.Sum256(txUnwrap[0])
@@ -118,6 +128,8 @@ func handleSubmitTx(c *gin.Context) {
118128
RejectTxFunc: func(reason interface{}) error {
119129
c.String(400, fmt.Sprintf("transaction rejected by node: %#v", reason))
120130
doneChan <- true
131+
// Increment custom metric
132+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
121133
return nil
122134
},
123135
},
@@ -132,18 +144,21 @@ func handleSubmitTx(c *gin.Context) {
132144
if err != nil {
133145
logger.Errorf("failure creating Ouroboros connection: %s", err)
134146
c.String(500, "failure communicating with node")
147+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
135148
return
136149
}
137150
if cfg.Node.Address != "" && cfg.Node.Port > 0 {
138151
if err := oConn.Dial("tcp", fmt.Sprintf("%s:%d", cfg.Node.Address, cfg.Node.Port)); err != nil {
139152
logger.Errorf("failure connecting to node via TCP: %s", err)
140153
c.String(500, "failure communicating with node")
154+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
141155
return
142156
}
143157
} else {
144158
if err := oConn.Dial("unix", cfg.Node.SocketPath); err != nil {
145159
logger.Errorf("failure connecting to node via UNIX socket: %s", err)
146160
c.String(500, "failure communicating with node")
161+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
147162
return
148163
}
149164
}
@@ -153,6 +168,7 @@ func handleSubmitTx(c *gin.Context) {
153168
if ok {
154169
logger.Errorf("failure communicating with node: %s", err)
155170
c.String(500, "failure communicating with node")
171+
_ = ginmetrics.GetMonitor().GetMetric("tx_failure_count").Inc(nil)
156172
doneChan <- true
157173
}
158174
}()

0 commit comments

Comments
 (0)