Skip to content

Commit ea12753

Browse files
y-myajimat4niwa
andauthored
Add metrics for http origin latency (#30)
* add metrics Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * add metrics config Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * fix metrics config Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * Summary to Histogram Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * fix handler test Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * add metrics test Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * add metrics test Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * add metrics ListenAndServe test Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * update .licenserc.yaml Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * fix authoz_proxyd_test Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * change metrics name Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> * update Signed-off-by: taniwa <[email protected]> * fix metrics_test Signed-off-by: taniwa <[email protected]> * fix metrics.go Signed-off-by: taniwa <[email protected]> * fix metrics interface, unit test Signed-off-by: taniwa <[email protected]> * Update transport_test Signed-off-by: taniwa <[email protected]> * empty Signed-off-by: myajima <[email protected]> * update prometheus Signed-off-by: myajima <[email protected]> * prometheus version Signed-off-by: myajima <[email protected]> --------- Signed-off-by: myajima <[email protected]> Signed-off-by: taniwa <[email protected]> Signed-off-by: myajima <[email protected]> Co-authored-by: taniwa <[email protected]> Co-authored-by: t4niwa <[email protected]>
1 parent 750505e commit ea12753

File tree

16 files changed

+803
-9
lines changed

16 files changed

+803
-9
lines changed

config/config.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ type Server struct {
7272

7373
// Debug represents the debug server configuration.
7474
Debug Debug `yaml:"debug"`
75+
76+
// Metrics represents the metrics server configuration.
77+
Metrics `yaml:"metrics"`
7578
}
7679

7780
// TLS represents the TLS configuration of the authorization proxy.
@@ -98,6 +101,12 @@ type TLS struct {
98101
EnableInsecureCipherSuites []string `yaml:"enableInsecureCipherSuites"`
99102
}
100103

104+
// Metrics represents the metrics server configuration.
105+
type Metrics struct {
106+
// MetricsServerAddr represents the metrics server listening port.
107+
Port int `yaml:"port"`
108+
}
109+
101110
// HealthCheck represents the health check server configuration.
102111
type HealthCheck struct {
103112
// Port represents the server listening port.

config/config_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ func TestNew(t *testing.T) {
110110
Dump: true,
111111
Profiling: true,
112112
},
113+
Metrics: Metrics{
114+
Port: 6084,
115+
},
113116
},
114117
Athenz: Athenz{
115118
URL: "https://athenz.io:4443/zts/v1",

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ replace (
99
github.com/google/go-cmp => github.com/google/go-cmp v0.6.0
1010
github.com/google/pprof => github.com/google/pprof v0.0.0-20240409012703-83162a5b38cd
1111
github.com/mwitkow/grpc-proxy => github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76
12+
github.com/prometheus/client_golang => github.com/prometheus/client_golang v1.19.1
13+
github.com/prometheus/common => github.com/prometheus/common v0.48.0
1214
golang.org/x/crypto => golang.org/x/crypto v0.22.0
1315
golang.org/x/exp => golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
1416
golang.org/x/image => golang.org/x/image v0.15.0
@@ -36,6 +38,7 @@ require (
3638
github.com/kpango/glg v1.6.15
3739
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76
3840
github.com/pkg/errors v0.9.1
41+
github.com/prometheus/client_golang v1.18.0
3942
golang.org/x/sync v0.7.0
4043
google.golang.org/grpc v1.58.2
4144
google.golang.org/protobuf v1.33.0
@@ -45,19 +48,25 @@ require (
4548
require (
4649
github.com/AthenZ/athenz v1.11.43 // indirect
4750
github.com/ardielle/ardielle-go v1.5.2 // indirect
51+
github.com/beorn7/perks v1.0.1 // indirect
52+
github.com/cespare/xxhash/v2 v2.2.0 // indirect
4853
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
4954
github.com/goccy/go-json v0.10.2 // indirect
5055
github.com/golang-jwt/jwt/v4 v4.5.0 // indirect
5156
github.com/golang/protobuf v1.5.4 // indirect
5257
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
5358
github.com/kpango/fastime v1.1.9 // indirect
5459
github.com/kpango/gache v1.2.8 // indirect
60+
github.com/kr/text v0.2.0 // indirect
5561
github.com/lestrrat-go/backoff/v2 v2.0.8 // indirect
5662
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
5763
github.com/lestrrat-go/httpcc v1.0.1 // indirect
5864
github.com/lestrrat-go/iter v1.0.2 // indirect
5965
github.com/lestrrat-go/jwx v1.2.26 // indirect
6066
github.com/lestrrat-go/option v1.0.1 // indirect
67+
github.com/prometheus/client_model v0.5.0 // indirect
68+
github.com/prometheus/common v0.48.0 // indirect
69+
github.com/prometheus/procfs v0.12.0 // indirect
6170
github.com/zeebo/xxh3 v1.0.2 // indirect
6271
golang.org/x/crypto v0.22.0 // indirect
6372
golang.org/x/net v0.24.0 // indirect

go.sum

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ github.com/AthenZ/athenz-authorizer/v5 v5.5.2 h1:Wbmv1AKk+9Z1UejEPDCxm4Vxy1a9H3Q
44
github.com/AthenZ/athenz-authorizer/v5 v5.5.2/go.mod h1:WXL+x349690VcELKsQh0srR30OoV68RYLRrVZ/T5qeU=
55
github.com/ardielle/ardielle-go v1.5.2 h1:TilHTpHIQJ27R1Tl/iITBzMwiUGSlVfiVhwDNGM3Zj4=
66
github.com/ardielle/ardielle-go v1.5.2/go.mod h1:I4hy1n795cUhaVt/ojz83SNVCYIGsAFAONtv2Dr7HUI=
7+
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
8+
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
9+
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
10+
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
11+
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
712
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
813
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
914
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -26,6 +31,9 @@ github.com/kpango/gache v1.2.8 h1:+OjREOmuWO4qrJksDhzWJq80o9iwHiezdVmMR1jtCG0=
2631
github.com/kpango/gache v1.2.8/go.mod h1:UyBo0IoPFDSJypK2haDXeV6PwHEmBcXQA0BLuOYEvWg=
2732
github.com/kpango/glg v1.6.15 h1:nw0xSxpSyrDIWHeb3dvnE08PW+SCbK+aYFETT75IeLA=
2833
github.com/kpango/glg v1.6.15/go.mod h1:cmsc7Yeu8AS3wHLmN7bhwENXOpxfq+QoqxCIk2FneRk=
34+
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
35+
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
36+
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
2937
github.com/lestrrat-go/backoff/v2 v2.0.8 h1:oNb5E5isby2kiro9AgdHLv5N5tint1AnDVVf2E2un5A=
3038
github.com/lestrrat-go/backoff/v2 v2.0.8/go.mod h1:rHP/q/r9aT27n24JQLa7JhSQZCKBBOiM/uP402WwN8Y=
3139
github.com/lestrrat-go/blackmagic v1.0.1/go.mod h1:UrEqBzIR2U6CnzVyUtfM6oZNMt/7O7Vohk2J0OGSAtU=
@@ -46,6 +54,15 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
4654
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
4755
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4856
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
57+
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
58+
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
59+
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
60+
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
61+
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
62+
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
63+
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
64+
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
65+
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
4966
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
5067
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5168
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
@@ -82,8 +99,8 @@ google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
8299
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
83100
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
84101
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
85-
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
86102
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
103+
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
87104
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
88105
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
89106
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

handler/handler.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import (
3838
type Func func(http.ResponseWriter, *http.Request) error
3939

4040
// New creates a handler for handling different HTTP requests based on the given services. It also contains a reverse proxy for handling proxy request.
41-
func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd) http.Handler {
41+
func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd, metrics service.Metrics) http.Handler {
4242
scheme := "http"
4343
if cfg.Scheme != "" {
4444
scheme = cfg.Scheme
@@ -93,6 +93,7 @@ func New(cfg config.Proxy, bp httputil.BufferPool, prov service.Authorizationd)
9393
cfg: cfg,
9494
noAuthPaths: mapPathToAssertion(cfg.NoAuthPaths),
9595
insecureCipherSuites: tls.InsecureCipherSuites(),
96+
metrics: metrics,
9697
},
9798
ErrorHandler: handleError,
9899
}

handler/handler_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ import (
4040

4141
func TestNew(t *testing.T) {
4242
type args struct {
43-
cfg config.Proxy
44-
bp httputil.BufferPool
45-
prov service.Authorizationd
43+
cfg config.Proxy
44+
bp httputil.BufferPool
45+
prov service.Authorizationd
46+
metrics service.Metrics
4647
}
4748
type test struct {
4849
name string
@@ -573,7 +574,7 @@ func TestNew(t *testing.T) {
573574
}
574575
for _, tt := range tests {
575576
t.Run(tt.name, func(t *testing.T) {
576-
got := New(tt.args.cfg, tt.args.bp, tt.args.prov)
577+
got := New(tt.args.cfg, tt.args.bp, tt.args.prov, tt.args.metrics)
577578
if err := tt.checkFunc(got); err != nil {
578579
t.Errorf("New() error: %v", err)
579580
}

handler/transport.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"net/http"
2020
"strconv"
2121
"strings"
22+
"time"
2223

2324
authorizerd "github.com/AthenZ/athenz-authorizer/v5"
2425
"github.com/AthenZ/athenz-authorizer/v5/policy"
@@ -37,24 +38,40 @@ type transport struct {
3738
noAuthPaths []*policy.Assertion
3839
// List to check for deprecated cipher suites
3940
insecureCipherSuites []*tls.CipherSuite
41+
metrics service.Metrics
4042
}
4143

4244
// Based on the following.
4345
// https://github.com/golang/oauth2/blob/bf48bf16ab8d622ce64ec6ce98d2c98f916b6303/transport.go
4446
func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
47+
var startTime time.Time
48+
49+
if t.metrics != nil {
50+
defer func() {
51+
// skip metrics if request is not forwarded
52+
if !startTime.IsZero() {
53+
err := t.metrics.Observe(service.HTTP_ORIGIN_LATENCY, float64(time.Since(startTime).Seconds()))
54+
if err != nil {
55+
glg.Errorf("cannot observe origin latency on: %s, err: %v", r.URL.Path, err)
56+
}
57+
}
58+
}()
59+
}
4560
// bypass authoriztion
4661
if len(r.URL.Path) != 0 { // prevent bypassing empty path on default config
4762
for _, urlPath := range t.cfg.OriginHealthCheckPaths {
4863
if urlPath == r.URL.Path {
4964
glg.Info("Authorization checking skipped on: " + r.URL.Path)
5065
r.TLS = nil
66+
startTime = time.Now()
5167
return t.RoundTripper.RoundTrip(r)
5268
}
5369
}
5470
for _, ass := range t.noAuthPaths {
5571
if ass.ResourceRegexp.MatchString(strings.ToLower(r.URL.Path)) {
5672
glg.Infof("Authorization checking skipped by %s on: %s", ass.ResourceRegexpString, r.URL.Path)
5773
r.TLS = nil
74+
startTime = time.Now()
5875
return t.RoundTripper.RoundTrip(r)
5976
}
6077
}
@@ -98,6 +115,7 @@ func (t *transport) RoundTrip(r *http.Request) (*http.Response, error) {
98115
req2.TLS = nil
99116
// req.Body is assumed to be closed by the base RoundTripper.
100117
reqBodyClosed = true
118+
startTime = time.Now()
101119
return t.RoundTripper.RoundTrip(req2)
102120
}
103121

handler/transport_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func Test_transport_RoundTrip(t *testing.T) {
5656
cfg config.Proxy
5757
noAuthPaths []*policy.Assertion
5858
insecureCipherSuites []*tls.CipherSuite
59+
metrics service.Metrics
5960
}
6061
type args struct {
6162
r *http.Request
@@ -449,6 +450,88 @@ func Test_transport_RoundTrip(t *testing.T) {
449450
wantErr: false,
450451
wantCloseCount: 0,
451452
},
453+
{
454+
name: "skipped t.metrics.Observe(), if request is not forwarded",
455+
fields: fields{
456+
RoundTripper: &RoundTripperMock{
457+
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
458+
return &http.Response{
459+
StatusCode: 200,
460+
}, nil
461+
},
462+
},
463+
prov: &service.AuthorizerdMock{
464+
VerifyFunc: func(r *http.Request, act, res string) (authorizerd.Principal, error) {
465+
return nil, errors.New("dummy error")
466+
},
467+
},
468+
cfg: config.Proxy{},
469+
metrics: &service.MetricsMock{
470+
ObserveFunc: func(name string, value float64) error {
471+
panic("t.metrics.Observe() should not be called")
472+
},
473+
},
474+
},
475+
args: args{
476+
r: func() *http.Request {
477+
r, _ := http.NewRequest("GET", "http://athenz.io", nil)
478+
return r
479+
}(),
480+
},
481+
want: nil,
482+
wantErr: true,
483+
wantCloseCount: 0,
484+
},
485+
{
486+
name: "called t.metrics.Observe(), if request is forwarded",
487+
fields: fields{
488+
RoundTripper: &RoundTripperMock{
489+
RoundTripFunc: func(req *http.Request) (*http.Response, error) {
490+
return &http.Response{
491+
StatusCode: 200,
492+
}, nil
493+
},
494+
},
495+
prov: &service.AuthorizerdMock{
496+
VerifyFunc: func(r *http.Request, act, res string) (authorizerd.Principal, error) {
497+
return &PrincipalMock{
498+
NameFunc: func() string {
499+
return "testPrincipal"
500+
},
501+
RolesFunc: func() []string {
502+
return []string{"testRole1", "testRole2"}
503+
},
504+
DomainFunc: func() string {
505+
return "testDomain"
506+
},
507+
IssueTimeFunc: func() int64 {
508+
return 0
509+
},
510+
ExpiryTimeFunc: func() int64 {
511+
return 0
512+
},
513+
}, nil
514+
},
515+
},
516+
cfg: config.Proxy{},
517+
metrics: &service.MetricsMock{
518+
ObserveFunc: func(name string, value float64) error {
519+
return nil
520+
},
521+
},
522+
},
523+
args: args{
524+
r: func() *http.Request {
525+
r, _ := http.NewRequest("GET", "http://athenz.io", nil)
526+
return r
527+
}(),
528+
},
529+
want: &http.Response{
530+
StatusCode: 200,
531+
},
532+
wantErr: false,
533+
wantCloseCount: 0,
534+
},
452535
}
453536
for _, tt := range tests {
454537
t.Run(tt.name, func(t *testing.T) {
@@ -458,6 +541,7 @@ func Test_transport_RoundTrip(t *testing.T) {
458541
cfg: tt.fields.cfg,
459542
noAuthPaths: tt.fields.noAuthPaths,
460543
insecureCipherSuites: tt.fields.insecureCipherSuites,
544+
metrics: tt.fields.metrics,
461545
}
462546
if tt.args.body != nil {
463547
tt.args.r.Body = tt.args.body

service/metrics.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright 2023 LY Corporation
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package service
16+
17+
import (
18+
"github.com/kpango/glg"
19+
"github.com/pkg/errors"
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
const (
24+
HTTP_ORIGIN_LATENCY = "http_origin_latency_in_seconds"
25+
)
26+
27+
type Metrics interface {
28+
Observe(string, float64) error
29+
}
30+
31+
type metrics struct {
32+
httpOriginLatency prometheus.Histogram
33+
}
34+
35+
func NewMetrics() (Metrics, error) {
36+
m := &metrics{
37+
httpOriginLatency: prometheus.NewHistogram(prometheus.HistogramOpts{
38+
Name: HTTP_ORIGIN_LATENCY,
39+
Help: "Origin latency in seconds",
40+
Buckets: prometheus.DefBuckets,
41+
}),
42+
}
43+
44+
err := prometheus.Register(m.httpOriginLatency)
45+
if err != nil {
46+
return nil, errors.Wrap(err, "cannot register metrics")
47+
}
48+
49+
return m, nil
50+
}
51+
52+
func (m *metrics) Observe(name string, value float64) error {
53+
switch name {
54+
case HTTP_ORIGIN_LATENCY:
55+
m.httpOriginLatency.Observe(value)
56+
default:
57+
return glg.Errorf("unknown metric name: %s", name)
58+
}
59+
return nil
60+
}

0 commit comments

Comments
 (0)