Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/components/usage/pkg/stripe/reporter.go
2499 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package stripe
6
7
import (
8
"fmt"
9
"time"
10
11
"github.com/prometheus/client_golang/prometheus"
12
"github.com/stripe/stripe-go/v72"
13
)
14
15
var (
16
stripeUsageUpdateTotal = prometheus.NewCounterVec(prometheus.CounterOpts{
17
Namespace: "gitpod",
18
Subsystem: "stripe",
19
Name: "usage_records_updated_total",
20
Help: "Counter of usage records updated",
21
}, []string{"outcome"})
22
23
stripeClientRequestsStarted = prometheus.NewCounterVec(prometheus.CounterOpts{
24
Namespace: "gitpod",
25
Subsystem: "stripe",
26
Name: "requests_started_total",
27
Help: "Counter of requests started by stripe clients",
28
}, []string{"resource"})
29
30
stripeClientRequestsCompletedSeconds = prometheus.NewHistogramVec(prometheus.HistogramOpts{
31
Namespace: "gitpod",
32
Subsystem: "stripe",
33
Name: "requests_completed_seconds",
34
Help: "Histogram of requests completed by stripe clients",
35
}, []string{"resource", "code"})
36
)
37
38
func RegisterMetrics(reg *prometheus.Registry) error {
39
metrics := []prometheus.Collector{
40
stripeUsageUpdateTotal,
41
stripeClientRequestsStarted,
42
stripeClientRequestsCompletedSeconds,
43
}
44
for _, metric := range metrics {
45
err := reg.Register(metric)
46
if err != nil {
47
return fmt.Errorf("failed to register metric: %w", err)
48
}
49
}
50
51
return nil
52
}
53
54
func reportStripeUsageUpdate(err error) {
55
outcome := "success"
56
if err != nil {
57
outcome = "fail"
58
}
59
stripeUsageUpdateTotal.WithLabelValues(outcome).Inc()
60
}
61
62
func reportStripeRequestStarted(resource string) {
63
stripeClientRequestsStarted.WithLabelValues(resource).Inc()
64
}
65
66
func reportStripeRequestCompleted(resource string, err error, took time.Duration) {
67
code := "ok"
68
if err != nil {
69
code = "unknown"
70
if stripeErr, ok := err.(*stripe.Error); ok {
71
code = string(stripeErr.Code)
72
}
73
}
74
75
stripeClientRequestsCompletedSeconds.WithLabelValues(resource, code).Observe(took.Seconds())
76
}
77
78