-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
145 lines (122 loc) · 5.31 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package api
import (
"context"
"crypto/x509"
"time"
"github.com/absmach/certs"
"github.com/go-kit/kit/metrics"
)
var _ certs.Service = (*metricsMiddleware)(nil)
type metricsMiddleware struct {
counter metrics.Counter
latency metrics.Histogram
svc certs.Service
}
// MetricsMiddleware instruments core service by tracking request count and latency.
func MetricsMiddleware(svc certs.Service, counter metrics.Counter, latency metrics.Histogram) certs.Service {
return &metricsMiddleware{
counter: counter,
latency: latency,
svc: svc,
}
}
func (mm *metricsMiddleware) RenewCert(ctx context.Context, cmpId string) error {
defer func(begin time.Time) {
mm.counter.With("method", "renew_certificate").Add(1)
mm.latency.With("method", "renew_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RenewCert(ctx, cmpId)
}
func (mm *metricsMiddleware) RetrieveCert(ctx context.Context, token, serialNumber string) (certs.Certificate, []byte, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get_certificate").Add(1)
mm.latency.With("method", "get_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RetrieveCert(ctx, token, serialNumber)
}
func (mm *metricsMiddleware) RevokeCert(ctx context.Context, serialNumber string) error {
defer func(begin time.Time) {
mm.counter.With("method", "revoke_certificate").Add(1)
mm.latency.With("method", "revoke_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RevokeCert(ctx, serialNumber)
}
func (mm *metricsMiddleware) RetrieveCertDownloadToken(ctx context.Context, serialNumber string) (string, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get_certificate_download_token").Add(1)
mm.latency.With("method", "get_certificate_download_token").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RetrieveCertDownloadToken(ctx, serialNumber)
}
func (mm *metricsMiddleware) RetrieveCAToken(ctx context.Context) (string, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get_CA_token").Add(1)
mm.latency.With("method", "get_CA_token").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RetrieveCAToken(ctx)
}
func (mm *metricsMiddleware) IssueCert(ctx context.Context, entityID, ttl string, ipAddrs []string, options certs.SubjectOptions) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "issue_certificate").Add(1)
mm.latency.With("method", "issue_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.IssueCert(ctx, entityID, ttl, ipAddrs, options)
}
func (mm *metricsMiddleware) ListCerts(ctx context.Context, pm certs.PageMetadata) (certs.CertificatePage, error) {
defer func(begin time.Time) {
mm.counter.With("method", "list_certificates").Add(1)
mm.latency.With("method", "list_certificates").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.ListCerts(ctx, pm)
}
func (mm *metricsMiddleware) RemoveCert(ctx context.Context, entityId string) error {
defer func(begin time.Time) {
mm.counter.With("method", "remove_certificate").Add(1)
mm.latency.With("method", "remove_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.RemoveCert(ctx, entityId)
}
func (mm *metricsMiddleware) ViewCert(ctx context.Context, serialNumber string) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "view_certificate").Add(1)
mm.latency.With("method", "view_certificate").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.ViewCert(ctx, serialNumber)
}
func (mm *metricsMiddleware) OCSP(ctx context.Context, serialNumber string) (*certs.Certificate, int, *x509.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "ocsp").Add(1)
mm.latency.With("method", "ocsp").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.OCSP(ctx, serialNumber)
}
func (mm *metricsMiddleware) GetEntityID(ctx context.Context, serialNumber string) (string, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get_entity_id").Add(1)
mm.latency.With("method", "get_entity_id").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.GetEntityID(ctx, serialNumber)
}
func (mm *metricsMiddleware) GenerateCRL(ctx context.Context, caType certs.CertType) ([]byte, error) {
defer func(begin time.Time) {
mm.counter.With("method", "generate_crl").Add(1)
mm.latency.With("method", "generate_crl").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.GenerateCRL(ctx, caType)
}
func (mm *metricsMiddleware) GetChainCA(ctx context.Context, token string) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "get_chain_ca").Add(1)
mm.latency.With("method", "get_chain_ca").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.GetChainCA(ctx, token)
}
func (mm *metricsMiddleware) IssueFromCSR(ctx context.Context, entityID, ttl string, csr certs.CSR) (certs.Certificate, error) {
defer func(begin time.Time) {
mm.counter.With("method", "issue_from_csr").Add(1)
mm.latency.With("method", "issue_from_csr").Observe(time.Since(begin).Seconds())
}(time.Now())
return mm.svc.IssueFromCSR(ctx, entityID, ttl, csr)
}