Path: blob/main/components/usage/pkg/stripe/reporter.go
2499 views
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package stripe56import (7"fmt"8"time"910"github.com/prometheus/client_golang/prometheus"11"github.com/stripe/stripe-go/v72"12)1314var (15stripeUsageUpdateTotal = prometheus.NewCounterVec(prometheus.CounterOpts{16Namespace: "gitpod",17Subsystem: "stripe",18Name: "usage_records_updated_total",19Help: "Counter of usage records updated",20}, []string{"outcome"})2122stripeClientRequestsStarted = prometheus.NewCounterVec(prometheus.CounterOpts{23Namespace: "gitpod",24Subsystem: "stripe",25Name: "requests_started_total",26Help: "Counter of requests started by stripe clients",27}, []string{"resource"})2829stripeClientRequestsCompletedSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{30Namespace: "gitpod",31Subsystem: "stripe",32Name: "requests_completed_seconds",33Help: "Histogram of requests completed by stripe clients",34}, []string{"resource", "code"})35)3637func RegisterMetrics(reg *prometheus.Registry) error {38metrics := []prometheus.Collector{39stripeUsageUpdateTotal,40stripeClientRequestsStarted,41stripeClientRequestsCompletedSeconds,42}43for _, metric := range metrics {44err := reg.Register(metric)45if err != nil {46return fmt.Errorf("failed to register metric: %w", err)47}48}4950return nil51}5253func reportStripeUsageUpdate(err error) {54outcome := "success"55if err != nil {56outcome = "fail"57}58stripeUsageUpdateTotal.WithLabelValues(outcome).Inc()59}6061func reportStripeRequestStarted(resource string) {62stripeClientRequestsStarted.WithLabelValues(resource).Inc()63}6465func reportStripeRequestCompleted(resource string, err error, took time.Duration) {66code := "ok"67if err != nil {68code = "unknown"69if stripeErr, ok := err.(*stripe.Error); ok {70code = string(stripeErr.Code)71}72}7374stripeClientRequestsCompletedSeconds.WithLabelValues(resource, code).Observe(took.Seconds())75}767778