Collect implements, along with Describe, the prometheus.Collector interface for metrics
(metrics chan<- prometheus.Metric)
| 646 | // Collect implements, along with Describe, the prometheus.Collector interface |
| 647 | // for metrics |
| 648 | func (p *PGPubsub) Collect(metrics chan<- prometheus.Metric) { |
| 649 | // explicit metrics |
| 650 | p.publishesTotal.Collect(metrics) |
| 651 | p.subscribesTotal.Collect(metrics) |
| 652 | p.messagesTotal.Collect(metrics) |
| 653 | p.publishedBytesTotal.Collect(metrics) |
| 654 | p.receivedBytesTotal.Collect(metrics) |
| 655 | p.disconnectionsTotal.Collect(metrics) |
| 656 | p.connected.Collect(metrics) |
| 657 | |
| 658 | // implicit metrics |
| 659 | p.qMu.Lock() |
| 660 | events := len(p.queues) |
| 661 | subs := 0 |
| 662 | for _, qSet := range p.queues { |
| 663 | subs += len(qSet.m) |
| 664 | } |
| 665 | p.qMu.Unlock() |
| 666 | metrics <- prometheus.MustNewConstMetric(currentSubscribersDesc, prometheus.GaugeValue, float64(subs)) |
| 667 | metrics <- prometheus.MustNewConstMetric(currentEventsDesc, prometheus.GaugeValue, float64(events)) |
| 668 | |
| 669 | // additional metrics |
| 670 | ctx, cancel := context.WithTimeout(context.Background(), LatencyMeasureTimeout) |
| 671 | defer cancel() |
| 672 | send, recv, err := p.latencyMeasurer.Measure(ctx, p) |
| 673 | |
| 674 | metrics <- prometheus.MustNewConstMetric(pubsubLatencyMeasureCountDesc, prometheus.CounterValue, float64(p.latencyMeasureCounter.Add(1))) |
| 675 | if err != nil { |
| 676 | p.logger.Warn(context.Background(), "failed to measure latency", slog.Error(err)) |
| 677 | metrics <- prometheus.MustNewConstMetric(pubsubLatencyMeasureErrDesc, prometheus.CounterValue, float64(p.latencyErrCounter.Add(1))) |
| 678 | return |
| 679 | } |
| 680 | metrics <- prometheus.MustNewConstMetric(pubsubSendLatencyDesc, prometheus.GaugeValue, send.Seconds()) |
| 681 | metrics <- prometheus.MustNewConstMetric(pubsubRecvLatencyDesc, prometheus.GaugeValue, recv.Seconds()) |
| 682 | } |
| 683 | |
| 684 | // New creates a new Pubsub implementation using a PostgreSQL connection. |
| 685 | func New(startCtx context.Context, logger slog.Logger, db *sql.DB, connectURL string) (*PGPubsub, error) { |