|
| 1 | +package metrics |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + |
| 6 | + "github.com/chia-network/go-chia-libs/pkg/types" |
| 7 | + "github.com/prometheus/client_golang/prometheus" |
| 8 | + log "github.com/sirupsen/logrus" |
| 9 | + |
| 10 | + wrappedPrometheus "github.com/chia-network/chia-exporter/internal/prometheus" |
| 11 | +) |
| 12 | + |
| 13 | +// Metrics that are based on Farmer RPC calls are in this file |
| 14 | + |
| 15 | +// FarmerServiceMetrics contains all metrics related to the harvester |
| 16 | +type FarmerServiceMetrics struct { |
| 17 | + // Holds a reference to the main metrics container this is a part of |
| 18 | + metrics *Metrics |
| 19 | + |
| 20 | + // Partial/Pooling Metrics |
| 21 | + submittedPartials *prometheus.CounterVec |
| 22 | + currentDifficulty *prometheus.GaugeVec |
| 23 | + pointsAckSinceStart *prometheus.GaugeVec |
| 24 | + |
| 25 | + // Proof Metrics |
| 26 | + proofsFound *wrappedPrometheus.LazyCounter |
| 27 | +} |
| 28 | + |
| 29 | +// InitMetrics sets all the metrics properties |
| 30 | +func (s *FarmerServiceMetrics) InitMetrics() { |
| 31 | + // Partial/Pooling Metrics, by launcher ID |
| 32 | + poolLabels := []string{"launcher_id"} |
| 33 | + s.submittedPartials = s.metrics.newCounterVec(chiaServiceFarmer, "submitted_partials", "Number of partials submitted since the exporter was started", poolLabels) |
| 34 | + s.currentDifficulty = s.metrics.newGaugeVec(chiaServiceFarmer, "current_difficulty", "Current difficulty for this launcher id", poolLabels) |
| 35 | + s.pointsAckSinceStart = s.metrics.newGaugeVec(chiaServiceFarmer, "points_acknowledged_since_start", "Points acknowledged since start. This is calculated by chia, NOT since start of the exporter.", poolLabels) |
| 36 | + |
| 37 | + // Proof Metrics |
| 38 | + s.proofsFound = s.metrics.newCounter(chiaServiceFarmer, "proofs_found", "Number of proofs found since the exporter has been running") |
| 39 | +} |
| 40 | + |
| 41 | +// InitialData is called on startup of the metrics server, to allow seeding metrics with current/initial data |
| 42 | +func (s *FarmerServiceMetrics) InitialData() {} |
| 43 | + |
| 44 | +// Disconnected clears/unregisters metrics when the connection drops |
| 45 | +func (s *FarmerServiceMetrics) Disconnected() {} |
| 46 | + |
| 47 | +// ReceiveResponse handles crawler responses that are returned over the websocket |
| 48 | +func (s *FarmerServiceMetrics) ReceiveResponse(resp *types.WebsocketResponse) { |
| 49 | + switch resp.Command { |
| 50 | + case "submitted_partial": |
| 51 | + s.SubmittedPartial(resp) |
| 52 | + case "proof": |
| 53 | + s.Proof(resp) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +// SubmittedPartial handles a received submitted_partial event |
| 58 | +func (s *FarmerServiceMetrics) SubmittedPartial(resp *types.WebsocketResponse) { |
| 59 | + partial := &types.EventFarmerSubmittedPartial{} |
| 60 | + err := json.Unmarshal(resp.Data, partial) |
| 61 | + if err != nil { |
| 62 | + log.Errorf("Error unmarshalling: %s\n", err.Error()) |
| 63 | + return |
| 64 | + } |
| 65 | + |
| 66 | + s.submittedPartials.WithLabelValues(partial.LauncherID).Inc() |
| 67 | + s.currentDifficulty.WithLabelValues(partial.LauncherID).Set(float64(partial.CurrentDifficulty)) |
| 68 | + s.pointsAckSinceStart.WithLabelValues(partial.LauncherID).Set(float64(partial.PointsAcknowledgedSinceStart)) |
| 69 | +} |
| 70 | + |
| 71 | +// Proof handles a received `proof` event from the farmer |
| 72 | +func (s *FarmerServiceMetrics) Proof(resp *types.WebsocketResponse) { |
| 73 | + proof := &types.EventFarmerProof{} |
| 74 | + err := json.Unmarshal(resp.Data, proof) |
| 75 | + if err != nil { |
| 76 | + log.Errorf("Error unmarshalling: %s\n", err.Error()) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + s.proofsFound.Inc() |
| 81 | +} |
0 commit comments