Skip to content

Commit 8a5f9d8

Browse files
committed
add bus routes
1 parent 17d92aa commit 8a5f9d8

File tree

3 files changed

+131
-28
lines changed

3 files changed

+131
-28
lines changed

api/prometheus.go

Lines changed: 100 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ import (
99
"go.sia.tech/renterd/internal/prometheus"
1010
)
1111

12+
func (c ConsensusState) PrometheusMetric() (metrics []prometheus.Metric) {
13+
metrics = append(metrics, prometheus.Metric{
14+
Name: "renterd_consensus_state_synced",
15+
Value: func() float64 {
16+
if c.Synced {
17+
return 1
18+
}
19+
return 0
20+
}(),
21+
})
22+
metrics = append(metrics, prometheus.Metric{
23+
Name: "renterd_consensus_state_chain_index_height",
24+
Value: float64(c.BlockHeight),
25+
})
26+
return
27+
}
1228
func (asr AutopilotStateResponse) PrometheusMetric() (metrics []prometheus.Metric) {
1329
labels := map[string]any{
1430
"version": asr.Version,
@@ -441,6 +457,33 @@ func (host HostAddress) PrometheusMetric() (metrics []prometheus.Metric) {
441457
return
442458
}
443459

460+
func (c ContractMetadata) PrometheusMetric() (metrics []prometheus.Metric) {
461+
metrics = append(metrics, prometheus.Metric{
462+
Name: "renterd_contract",
463+
Labels: map[string]any{
464+
"hostIP": c.HostIP,
465+
"state": c.State,
466+
"hostKey": c.HostKey.String(),
467+
"siamuxAddr": c.SiamuxAddr,
468+
"contractPrice": c.ContractPrice.Siacoins(),
469+
},
470+
Value: c.TotalCost.Siacoins(),
471+
})
472+
return
473+
}
474+
475+
func (c ContractsPrunableDataResponse) PrometheusMetric() (metrics []prometheus.Metric) {
476+
metrics = append(metrics, prometheus.Metric{
477+
Name: "renterd_prunable_contracts_total",
478+
Value: float64(c.TotalPrunable),
479+
})
480+
metrics = append(metrics, prometheus.Metric{
481+
Name: "renterd_prunable_contracts_size",
482+
Value: float64(c.TotalSize),
483+
})
484+
return
485+
}
486+
444487
func formatSettingsMetricName(gp GougingParams, name string) (metrics []prometheus.Metric) {
445488
metrics = append(metrics, prometheus.Metric{
446489
Name: fmt.Sprintf("renterd_%s_consensusstate_synced", name),
@@ -538,6 +581,46 @@ func (up UploadParams) PrometheusMetric() (metrics []prometheus.Metric) {
538581
return
539582
}
540583

584+
type SlabBuffersResp []SlabBuffer
585+
586+
func (sb SlabBuffersResp) PrometheusMetric() (metrics []prometheus.Metric) {
587+
totalSize := int64(0)
588+
for _, buffer := range sb {
589+
totalSize += buffer.Size
590+
}
591+
metrics = append(metrics, prometheus.Metric{
592+
Name: "renterd_slabbuffers_totalsize",
593+
Value: float64(totalSize),
594+
})
595+
metrics = append(metrics, prometheus.Metric{
596+
Name: "renterd_slabbuffers_totalslabs",
597+
Value: float64(len(sb)),
598+
})
599+
return
600+
}
601+
602+
type SearchObjectsResp []ObjectMetadata
603+
604+
func (so SearchObjectsResp) PrometheusMetric() (metrics []prometheus.Metric) {
605+
unavailableObjs := 0
606+
avgHealth := float64(0.0)
607+
for _, obj := range so {
608+
if obj.Health == 0 {
609+
unavailableObjs += 1
610+
}
611+
avgHealth += obj.Health
612+
}
613+
metrics = append(metrics, prometheus.Metric{
614+
Name: "renterd_objects_avghealth",
615+
Value: float64(avgHealth),
616+
})
617+
metrics = append(metrics, prometheus.Metric{
618+
Name: "renterd_objects_unavailable",
619+
Value: float64(unavailableObjs),
620+
})
621+
return
622+
}
623+
541624
// No specific prometheus response from:
542625
// /setting/contractset - already available in /params/upload
543626
// /setting/gouging - already available in /params/gouging
@@ -689,6 +772,21 @@ func (a AllowListResp) PrometheusMetric() (metrics []prometheus.Metric) {
689772
return
690773
}
691774

775+
type BlockListResp []string
776+
777+
func (b BlockListResp) PrometheusMetric() (metrics []prometheus.Metric) {
778+
for _, host := range b {
779+
metrics = append(metrics, prometheus.Metric{
780+
Name: "renterd_blocked_host",
781+
Labels: map[string]any{
782+
"publicKey": host,
783+
},
784+
Value: 1,
785+
})
786+
}
787+
return
788+
}
789+
692790
type SyncerAddrResp string
693791

694792
func (sar SyncerAddrResp) PrometheusMetric() (metrics []prometheus.Metric) {
@@ -717,11 +815,11 @@ func (srp SyncerPeersResp) PrometheusMetric() (metrics []prometheus.Metric) {
717815
return
718816
}
719817

720-
type TxPoolResp string
818+
type TxPoolResp types.Currency
721819

722820
func (t TxPoolResp) ToFloat64() (float64, error) {
723821
// Convert string to float64
724-
return strconv.ParseFloat(string(t), 64)
822+
return strconv.ParseFloat(types.Currency(t).String(), 64)
725823
}
726824

727825
func (tpr TxPoolResp) PrometheusMetric() (metrics []prometheus.Metric) {

bus/routes.go

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
rhpv2 "go.sia.tech/core/rhp/v2"
1717

18+
"go.sia.tech/renterd/internal/prometheus"
1819
rhp3 "go.sia.tech/renterd/internal/rhp/v3"
1920

2021
ibus "go.sia.tech/renterd/internal/bus"
@@ -148,15 +149,15 @@ func (b *Bus) consensusAcceptBlock(jc jape.Context) {
148149
}
149150

150151
func (b *Bus) syncerAddrHandler(jc jape.Context) {
151-
jc.Encode(b.s.Addr())
152+
api.WriteResponse(jc, http.StatusOK, api.SyncerAddrResp(b.s.Addr()))
152153
}
153154

154155
func (b *Bus) syncerPeersHandler(jc jape.Context) {
155156
var peers []string
156157
for _, p := range b.s.Peers() {
157158
peers = append(peers, p.String())
158159
}
159-
jc.Encode(peers)
160+
api.WriteResponse(jc, http.StatusOK, api.SyncerPeersResp(peers))
160161
}
161162

162163
func (b *Bus) syncerConnectHandler(jc jape.Context) {
@@ -172,7 +173,7 @@ func (b *Bus) consensusStateHandler(jc jape.Context) {
172173
if jc.Check("couldn't fetch consensus state", err) != nil {
173174
return
174175
}
175-
jc.Encode(cs)
176+
api.WriteResponse(jc, http.StatusOK, cs)
176177
}
177178

178179
func (b *Bus) consensusNetworkHandler(jc jape.Context) {
@@ -182,11 +183,11 @@ func (b *Bus) consensusNetworkHandler(jc jape.Context) {
182183
}
183184

184185
func (b *Bus) txpoolFeeHandler(jc jape.Context) {
185-
jc.Encode(b.cm.RecommendedFee())
186+
api.WriteResponse(jc, http.StatusOK, api.TxPoolResp(b.cm.RecommendedFee()))
186187
}
187188

188189
func (b *Bus) txpoolTransactionsHandler(jc jape.Context) {
189-
jc.Encode(b.cm.PoolTransactions())
190+
api.WriteResponse(jc, http.StatusOK, api.TxPoolTxResp(b.cm.PoolTransactions()))
190191
}
191192

192193
func (b *Bus) txpoolBroadcastHandler(jc jape.Context) {
@@ -208,7 +209,7 @@ func (b *Bus) bucketsHandlerGET(jc jape.Context) {
208209
if jc.Check("couldn't list buckets", err) != nil {
209210
return
210211
}
211-
jc.Encode(resp)
212+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(resp))
212213
}
213214

214215
func (b *Bus) bucketsHandlerPOST(jc jape.Context) {
@@ -273,7 +274,7 @@ func (b *Bus) walletHandler(jc jape.Context) {
273274
}
274275

275276
tip := b.w.Tip()
276-
jc.Encode(api.WalletResponse{
277+
api.WriteResponse(jc, http.StatusOK, api.WalletResponse{
277278
ScanHeight: tip.Height,
278279
Address: address,
279280
Confirmed: balance.Confirmed,
@@ -362,11 +363,15 @@ func (b *Bus) walletTransactionsHandler(jc jape.Context) {
362363
}
363364
}
364365
events = filtered
366+
367+
var txns []api.Transaction
365368
if limit == 0 || limit == -1 {
366-
jc.Encode(convertToTransactions(events[offset:]))
369+
txns = convertToTransactions(events[offset:])
367370
} else {
368-
jc.Encode(convertToTransactions(events[offset : offset+limit]))
371+
txns = convertToTransactions(events[offset : offset+limit])
369372
}
373+
374+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(txns))
370375
}
371376

372377
func (b *Bus) walletOutputsHandler(jc jape.Context) {
@@ -384,7 +389,7 @@ func (b *Bus) walletOutputsHandler(jc jape.Context) {
384389
MaturityHeight: sce.MaturityHeight,
385390
}
386391
}
387-
jc.Encode(elements)
392+
api.WriteResponse(jc, http.StatusOK, api.WalletOutputsResp(elements))
388393
}
389394
}
390395

@@ -602,7 +607,7 @@ func (b *Bus) hostsHandlerGETDeprecated(jc jape.Context) {
602607
if jc.Check(fmt.Sprintf("couldn't fetch hosts %d-%d", offset, offset+limit), err) != nil {
603608
return
604609
}
605-
jc.Encode(hosts)
610+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(hosts))
606611
}
607612

608613
func (b *Bus) searchHostsHandlerPOST(jc jape.Context) {
@@ -619,7 +624,7 @@ func (b *Bus) searchHostsHandlerPOST(jc jape.Context) {
619624
if jc.Check(fmt.Sprintf("couldn't fetch hosts %d-%d", req.Offset, req.Offset+req.Limit), err) != nil {
620625
return
621626
}
622-
jc.Encode(hosts)
627+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(hosts))
623628
}
624629

625630
func (b *Bus) hostsRemoveHandlerPOST(jc jape.Context) {
@@ -711,7 +716,7 @@ func (b *Bus) contractsSpendingHandlerPOST(jc jape.Context) {
711716
func (b *Bus) hostsAllowlistHandlerGET(jc jape.Context) {
712717
allowlist, err := b.hs.HostAllowlist(jc.Request.Context())
713718
if jc.Check("couldn't load allowlist", err) == nil {
714-
jc.Encode(allowlist)
719+
api.WriteResponse(jc, http.StatusOK, api.AllowListResp(allowlist))
715720
}
716721
}
717722

@@ -731,7 +736,7 @@ func (b *Bus) hostsAllowlistHandlerPUT(jc jape.Context) {
731736
func (b *Bus) hostsBlocklistHandlerGET(jc jape.Context) {
732737
blocklist, err := b.hs.HostBlocklist(jc.Request.Context())
733738
if jc.Check("couldn't load blocklist", err) == nil {
734-
jc.Encode(blocklist)
739+
api.WriteResponse(jc, http.StatusOK, api.BlockListResp(blocklist))
735740
}
736741
}
737742

@@ -757,7 +762,7 @@ func (b *Bus) contractsHandlerGET(jc jape.Context) {
757762
ContractSet: cs,
758763
})
759764
if jc.Check("couldn't load contracts", err) == nil {
760-
jc.Encode(contracts)
765+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(contracts))
761766
}
762767
}
763768

@@ -1014,7 +1019,7 @@ func (b *Bus) contractsPrunableDataHandlerGET(jc jape.Context) {
10141019
return contracts[i].Prunable > contracts[j].Prunable
10151020
})
10161021

1017-
jc.Encode(api.ContractsPrunableDataResponse{
1022+
api.WriteResponse(jc, http.StatusOK, api.ContractsPrunableDataResponse{
10181023
Contracts: contracts,
10191024
TotalPrunable: totalPrunable,
10201025
TotalSize: totalSize,
@@ -1240,7 +1245,7 @@ func (b *Bus) searchObjectsHandlerGET(jc jape.Context) {
12401245
if jc.Check("couldn't list objects", err) != nil {
12411246
return
12421247
}
1243-
jc.Encode(keys)
1248+
api.WriteResponse(jc, http.StatusOK, api.SearchObjectsResp(keys))
12441249
}
12451250

12461251
func (b *Bus) objectsHandlerGET(jc jape.Context) {
@@ -1422,7 +1427,7 @@ func (b *Bus) slabbuffersHandlerGET(jc jape.Context) {
14221427
if jc.Check("couldn't get slab buffers info", err) != nil {
14231428
return
14241429
}
1425-
jc.Encode(buffers)
1430+
api.WriteResponse(jc, http.StatusOK, api.SlabBuffersResp(buffers))
14261431
}
14271432

14281433
func (b *Bus) objectsStatshandlerGET(jc jape.Context) {
@@ -1434,7 +1439,7 @@ func (b *Bus) objectsStatshandlerGET(jc jape.Context) {
14341439
if jc.Check("couldn't get objects stats", err) != nil {
14351440
return
14361441
}
1437-
jc.Encode(info)
1442+
api.WriteResponse(jc, http.StatusOK, info)
14381443
}
14391444

14401445
func (b *Bus) packedSlabsHandlerFetchPOST(jc jape.Context) {
@@ -1815,7 +1820,7 @@ func (b *Bus) paramsHandlerUploadGET(jc jape.Context) {
18151820
uploadPacking = pus.Enabled
18161821
}
18171822

1818-
jc.Encode(api.UploadParams{
1823+
api.WriteResponse(jc, http.StatusOK, api.UploadParams{
18191824
ContractSet: contractSet,
18201825
CurrentHeight: b.cm.TipState().Index.Height,
18211826
GougingParams: gp,
@@ -1847,7 +1852,7 @@ func (b *Bus) paramsHandlerGougingGET(jc jape.Context) {
18471852
if jc.Check("could not get gouging parameters", err) != nil {
18481853
return
18491854
}
1850-
jc.Encode(gp)
1855+
api.WriteResponse(jc, http.StatusOK, gp)
18511856
}
18521857

18531858
func (b *Bus) gougingParams(ctx context.Context) (api.GougingParams, error) {
@@ -1883,7 +1888,7 @@ func (b *Bus) handleGETAlertsDeprecated(jc jape.Context) {
18831888
if jc.Check("failed to fetch alerts", err) != nil {
18841889
return
18851890
}
1886-
jc.Encode(ar.Alerts)
1891+
api.WriteResponse(jc, http.StatusOK, prometheus.PrometheusSlice(ar.Alerts))
18871892
}
18881893

18891894
func (b *Bus) handleGETAlerts(jc jape.Context) {
@@ -2046,7 +2051,7 @@ func (b *Bus) contractTaxHandlerGET(jc jape.Context) {
20462051
}
20472052

20482053
func (b *Bus) stateHandlerGET(jc jape.Context) {
2049-
jc.Encode(api.BusStateResponse{
2054+
api.WriteResponse(jc, http.StatusOK, api.BusStateResponse{
20502055
StartTime: api.TimeRFC3339(b.startTime),
20512056
BuildState: api.BuildState{
20522057
Version: build.Version(),

internal/prometheus/encoder.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@ func (e *Encoder) AppendSlice(marshallers []Marshaller) error {
127127
}
128128
e.used = true
129129

130-
for _, marshaller := range marshallers {
131-
for i, m := range marshaller.PrometheusMetric() {
132-
if i > 0 {
130+
for i, marshaller := range marshallers {
131+
for j, m := range marshaller.PrometheusMetric() {
132+
if i > 0 || j > 0 {
133133
// each sample must be separated by a newline
134134
e.sb.Write([]byte("\n"))
135135
}

0 commit comments

Comments
 (0)