Skip to content

Commit

Permalink
Add etcd-manager metrics, add openstack API metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab authored and justinsb committed Mar 1, 2021
1 parent b45c6df commit 2897439
Show file tree
Hide file tree
Showing 171 changed files with 31,841 additions and 32 deletions.
1 change: 1 addition & 0 deletions cmd/etcd-manager/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ go_library(
"//pkg/hosts",
"//pkg/legacy",
"//pkg/locking",
"//pkg/metrics",
"//pkg/pki",
"//pkg/privateapi",
"//pkg/privateapi/discovery",
Expand Down
11 changes: 11 additions & 0 deletions cmd/etcd-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"sigs.k8s.io/etcdadm/etcd-manager/pkg/hosts"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/legacy"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/locking"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/metrics"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/pki"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/privateapi"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/privateapi/discovery"
Expand Down Expand Up @@ -75,6 +76,7 @@ func main() {
flag.StringVar(&o.Address, "address", o.Address, "local address to use")
flag.StringVar(&o.PeerUrls, "peer-urls", o.PeerUrls, "peer-urls to use")
flag.IntVar(&o.GrpcPort, "grpc-port", o.GrpcPort, "grpc-port to use")
flag.IntVar(&o.EtcdManagerMetricsPort, "etcd-manager-metrics-port", o.EtcdManagerMetricsPort, "etcd-manager prometheus metrics port")
flag.StringVar(&o.ListenMetricsURLs, "listen-metrics-urls", o.ListenMetricsURLs, "listen-metrics-urls configure etcd dedicated metrics URL endpoints")
flag.StringVar(&o.ClientUrls, "client-urls", o.ClientUrls, "client-urls to use for normal operation")
flag.StringVar(&o.QuarantineClientUrls, "quarantine-client-urls", o.QuarantineClientUrls, "client-urls to use when etcd should be quarantined e.g. when offline")
Expand Down Expand Up @@ -153,6 +155,9 @@ type EtcdManagerOptions struct {

// ListenMetricsURLs allows configuration of the special etcd metrics urls
ListenMetricsURLs string

// EtcdManagerMetricsPort allows exposing statistics from etcd-manager
EtcdManagerMetricsPort int
}

// InitDefaults populates the default flag values
Expand All @@ -179,6 +184,7 @@ func (o *EtcdManagerOptions) InitDefaults() {

o.Insecure = false
o.EtcdInsecure = false
o.EtcdManagerMetricsPort = 0
}

// RunEtcdManager runs the etcd-manager, returning only we should exit.
Expand All @@ -203,6 +209,11 @@ func RunEtcdManager(o *EtcdManagerOptions) error {
var discoveryProvider discovery.Interface
var myPeerId privateapi.PeerId

// start etcd-manager metrics if the etcd manager metrics port is defined
if o.EtcdManagerMetricsPort != 0 {
go metrics.RegisterMetrics(o.EtcdManagerMetricsPort, o.VolumeProviderID)
}

if o.VolumeProviderID != "" {
var volumeProvider volumes.Volumes

Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,16 @@ require (
github.com/digitalocean/godo v1.54.0
github.com/go-logr/logr v0.3.0 // indirect
github.com/golang/protobuf v1.4.2
github.com/google/go-cmp v0.5.4 // indirect
github.com/gophercloud/gophercloud v0.11.1-0.20200518183226-7aec46f32c19
github.com/pkg/sftp v0.0.0-20180127012644-738e088bbd93 // indirect
github.com/prometheus/client_golang v1.7.1
go.etcd.io/etcd v0.5.0-alpha.5.0.20200819165624-17cef6e3e9d5
golang.org/x/mod v0.4.0 // indirect
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d
golang.org/x/text v0.3.4 // indirect
golang.org/x/tools v0.1.0 // indirect
golang.org/x/tools/gopls v0.6.3 // indirect
google.golang.org/api v0.22.0
google.golang.org/grpc v1.27.1
gopkg.in/gcfg.v1 v1.2.3
Expand Down
26 changes: 0 additions & 26 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY=
github.com/cyphar/filepath-securejoin v0.2.2/go.mod h1:FpkQEhXnPnOthhzymB7CGsFk2G9VLXONKD9G7QGMM+4=
github.com/davecgh/go-spew v0.0.0-20161028175848-04cdfd42973b/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -396,7 +395,6 @@ github.com/google/pprof v0.0.0-20191218002539-d4f498aebedc/go.mod h1:ZgVRPoUq/hf
github.com/google/pprof v0.0.0-20200212024743-f11f1df84d12/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/pprof v0.0.0-20200229191704-1ebb73c60ed3/go.mod h1:ZgVRPoUq/hfqzAqh7sHMqb3I9Rq5C59dIz2SbBwJ4eM=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/safehtml v0.0.2/go.mod h1:L4KWwDsUJdECRAEpZoBn3O64bQaywRscowZjJAzjHnU=
github.com/google/uuid v1.0.0 h1:b4Gk+7WdP/d3HZH8EJsZpvV7EtDOgaZLtnaNGIu1adA=
github.com/google/uuid v1.0.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
Expand Down Expand Up @@ -492,7 +490,6 @@ github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NH
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/ishidawataru/sctp v0.0.0-20190723014705-7c296d48a2b5/go.mod h1:DM4VvS+hD/kDi1U1QsX2fnZowwBhqD0Dk3bRPKF/Oc8=
github.com/jacksontj/memberlistmesh v0.0.0-20190905163944-93462b9d2bb7/go.mod h1:fFX3XoduobgoJsVtpzIFRTgKZAbNhsSJIDNOgeUU5g4=
github.com/jba/templatecheck v0.5.0/go.mod h1:/1k7EajoSErFI9GLHAsiIJEaNLt3ALKNw2TV7z2SYv4=
github.com/jimstudt/http-authentication v0.0.0-20140401203705-3eca13d6893a/go.mod h1:wK6yTYYcgjHE1Z1QtXACPDjcFJyBskHEdagmnq3vsP8=
github.com/jmespath/go-jmespath v0.0.0-20160202185014-0b12d6b521d8/go.mod h1:Nht3zPeWKUH0NzdCt2Blrr5ys8VGpn0CEB0cQHVjt7k=
github.com/jmespath/go-jmespath v0.0.0-20180206201540-c2b33e8439af h1:pmfjZENx5imkbgOkpRUYLnmbU7UEFbjtDA2hxJ1ichM=
Expand Down Expand Up @@ -660,7 +657,6 @@ github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/sftp v0.0.0-20160930220758-4d0e916071f6/go.mod h1:NxmoDg/QLVWluQDUYG7XBZTLUpKeFa8e3aMf1BfjyHk=
github.com/pkg/sftp v0.0.0-20180127012644-738e088bbd93 h1:rhjWIsqwNQMc2apyNCEO01l3ZABdEhJKCW1cD+pDX3s=
github.com/pkg/sftp v0.0.0-20180127012644-738e088bbd93/go.mod h1:NxmoDg/QLVWluQDUYG7XBZTLUpKeFa8e3aMf1BfjyHk=
github.com/pmezard/go-difflib v0.0.0-20151028094244-d8ed2627bdf0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/posener/complete v1.1.1/go.mod h1:em0nMJCgc9GFtwrmVmEMR/ZL6WyhyjMBndrE9hABlRI=
Expand Down Expand Up @@ -701,8 +697,6 @@ github.com/robertkrimen/otto v0.0.0-20191219234010-c382bd3c16ff/go.mod h1:xvqspo
github.com/robfig/cron v1.1.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.5.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.6.2/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rubiojr/go-vhd v0.0.0-20200706105327-02e210299021/go.mod h1:DM5xW0nvfNNm2uytzsvhI3OnX8uzaRAg8UX/CnDqbto=
github.com/russross/blackfriday v0.0.0-20170610170232-067529f716f4/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
github.com/russross/blackfriday v1.5.2/go.mod h1:JO/DiYxRf+HjHt06OyowR9PTA263kcR/rfWxYHBV53g=
Expand All @@ -711,13 +705,10 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
github.com/ryanuber/columnize v2.1.0+incompatible/go.mod h1:sm1tb6uqfes/u+d4ooFouqFdy9/2g9QGwK3SQygK0Ts=
github.com/ryanuber/go-glob v1.0.0 h1:iQh3xXAumdQ+4Ufa5b25cRpC5TYKlno6hsv6Cb3pkBk=
github.com/ryanuber/go-glob v1.0.0/go.mod h1:807d1WSdnB0XRJzKNil9Om6lcp/3a0v4qIHxIXzX/Yc=
github.com/sanity-io/litter v1.3.0/go.mod h1:5Z71SvaYy5kcGtyglXOC9rrUi3c1E8CamFWjQsazTh0=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
github.com/seccomp/libseccomp-golang v0.9.1/go.mod h1:GbW5+tmTXfcxTToHLXlScSlAvWlF4P2Ca7zGrPiEpWo=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.2.0 h1:juTguoYk5qI21pwyTXY3B3Y5cOTH3ZUyZCg1v/mihuo=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
Expand Down Expand Up @@ -760,7 +751,6 @@ github.com/storageos/go-api v0.0.0-20180912212459-343b3eff91fc/go.mod h1:ZrLn+e0
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v0.0.0-20161117074351-18a02ba4a312/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand Down Expand Up @@ -999,8 +989,6 @@ golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4 h1:5/PjkGUjvEU5Gl6BxmvKRPpqo
golang.org/x/sys v0.0.0-20200622214017-ed371f2e16b4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201112073958-5cba982894dd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201126233918-771906719818 h1:f1CIuDlJhwANEC2MM87MBEVMr3jl5bifgsfj90XAF9c=
golang.org/x/sys v0.0.0-20201126233918-771906719818/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4 h1:myAQVi0cGEoqQVR5POX+8RR2mrocKqNN1hmeMqhX27k=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20170915032832-14c0d48ead0c/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down Expand Up @@ -1072,18 +1060,8 @@ golang.org/x/tools v0.0.0-20200616133436-c1934b75d054 h1:HHeAlu5H9b71C+Fx0K+1dGg
golang.org/x/tools v0.0.0-20200616133436-c1934b75d054/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f h1:JcoF/bowzCDI+MXu1yLqQGNO3ibqWsWq+Sk7pOT218w=
golang.org/x/tools v0.0.0-20200626171337-aa94e735be7f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20201125231158-b5590deeca9b h1:Lq5JUTFhiybGVf28jB6QRpqd13/JPOaCnET17PVzYJE=
golang.org/x/tools v0.0.0-20201125231158-b5590deeca9b/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210101214203-2dba1e4ea05c/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.0.0-20210112235408-75fd75db8797/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.1.0 h1:po9/4sTYwZU9lPhi1tOrb4hCv3qrhiQ77LZfGa2OjwY=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools v0.1.1-0.20210119222907-0a1a9685734a h1:ttr/sghFptib3jlHwNS65KwGoqjr03TFzso3kpHyia8=
golang.org/x/tools v0.1.1-0.20210119222907-0a1a9685734a/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/tools/gopls v0.6.3/go.mod h1:fX89P36HonR7+eFbWNbyTOQ1ozJxuttb6hFChipxj6U=
golang.org/x/tools/gopls v0.6.4-pre.1/go.mod h1:7KC7VSlf+Sc1rkpgvIOHNGDfJzD9kzKcxpgBXDfwJ3I=
golang.org/x/tools/gopls v0.6.4 h1:PO8rURGmi+QEKZ3pk+ZXJh51EFTVTSXqxzoRzrGSTJM=
golang.org/x/tools/gopls v0.6.4/go.mod h1:7KC7VSlf+Sc1rkpgvIOHNGDfJzD9kzKcxpgBXDfwJ3I=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
Expand Down Expand Up @@ -1280,10 +1258,6 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
mvdan.cc/gofumpt v0.1.0 h1:hsVv+Y9UsZ/mFZTxJZuHVI6shSQCtzZ11h1JEFPAZLw=
mvdan.cc/gofumpt v0.1.0/go.mod h1:yXG1r1WqZVKWbVRtBWKWX9+CxGYfA51nSomhM0woR48=
mvdan.cc/xurls/v2 v2.2.0 h1:NSZPykBXJFCetGZykLAxaL6SIpvbVy/UFEniIfHAa8A=
mvdan.cc/xurls/v2 v2.2.0/go.mod h1:EV1RMtya9D6G5DMYPGD8zTQzaHet6Jh8gFlRgGRJeO8=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
Expand Down
14 changes: 14 additions & 0 deletions pkg/metrics/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
load("@io_bazel_rules_go//go:def.bzl", "go_library")

go_library(
name = "metrics",
srcs = ["metrics.go"],
importpath = "sigs.k8s.io/etcdadm/etcd-manager/pkg/metrics",
visibility = ["//visibility:public"],
deps = [
"//pkg/volumes/openstack",
"//vendor/github.com/prometheus/client_golang/prometheus",
"//vendor/github.com/prometheus/client_golang/prometheus/promhttp",
"//vendor/k8s.io/klog/v2:klog",
],
)
44 changes: 44 additions & 0 deletions pkg/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package metrics

import (
"fmt"
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"k8s.io/klog/v2"
"sigs.k8s.io/etcdadm/etcd-manager/pkg/volumes/openstack"
)

func RegisterMetrics(port int, provider string) {
if provider == "openstack" {
openstack.RegisterMetrics()
}
http.Handle("/metrics", promhttp.HandlerFor(
prometheus.DefaultGatherer,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
},
))
klog.Infof("Listening etcd-manager metrics in port %d", port)
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
klog.Fatalf("Unable to start etcd-manager metrics: %v", err)
}
}
3 changes: 3 additions & 0 deletions pkg/volumes/openstack/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ go_library(
srcs = [
"config.go",
"discovery.go",
"metrics.go",
"metrics_api.go",
"util.go",
"volumes.go",
],
Expand All @@ -19,6 +21,7 @@ go_library(
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/availabilityzones",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/extensions/volumeattach",
"//vendor/github.com/gophercloud/gophercloud/openstack/compute/v2/servers",
"//vendor/github.com/prometheus/client_golang/prometheus",
"//vendor/gopkg.in/gcfg.v1:gcfg_v1",
"//vendor/k8s.io/klog/v2:klog",
"//vendor/k8s.io/utils/exec",
Expand Down
3 changes: 2 additions & 1 deletion pkg/volumes/openstack/discovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ func (os *OpenstackVolumes) Poll() (map[string]discovery.Node, error) {
}
}
for i, volume := range instanceToVolumeMap {
mc := NewMetricContext("server", "get")
server, err := servers.Get(os.computeClient, i).Extract()
if err != nil {
if mc.ObserveRequest(err) != nil {
klog.Warningf("Could not find server with id '%s': %v", i, err)
continue
}
Expand Down
64 changes: 64 additions & 0 deletions pkg/volumes/openstack/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openstack

import (
"time"

"github.com/prometheus/client_golang/prometheus"
)

type OpenstackMetrics struct {
Duration *prometheus.HistogramVec
Total *prometheus.CounterVec
Errors *prometheus.CounterVec
}

// MetricContext indicates the context for OpenStack metrics.
type MetricContext struct {
Start time.Time
Attributes []string
Metrics *OpenstackMetrics
}

// NewMetricContext creates a new MetricContext.
func NewMetricContext(resource string, request string) *MetricContext {
return &MetricContext{
Start: time.Now(),
Attributes: []string{resource + "_" + request},
}
}

// ObserveRequest records the request latency and counts the errors.
func (mc *MetricContext) Observe(om *OpenstackMetrics, err error) error {
if om == nil {
// mc.RequestMetrics not set, ignore this request
return nil
}

om.Duration.WithLabelValues(mc.Attributes...).Observe(
time.Since(mc.Start).Seconds())
om.Total.WithLabelValues(mc.Attributes...).Inc()
if err != nil {
om.Errors.WithLabelValues(mc.Attributes...).Inc()
}
return err
}

func RegisterMetrics() {
doRegisterAPIMetrics()
}
61 changes: 61 additions & 0 deletions pkg/volumes/openstack/metrics_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package openstack

import (
"sync"

"github.com/prometheus/client_golang/prometheus"
)

var (
APIRequestMetrics = &OpenstackMetrics{
Duration: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "openstack_api_request_duration_seconds",
Help: "Latency of an OpenStack API call",
}, []string{"request"}),
Total: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "openstack_api_requests_total",
Help: "Total number of OpenStack API calls",
}, []string{"request"}),
Errors: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "openstack_api_request_errors_total",
Help: "Total number of errors for an OpenStack API call",
}, []string{"request"}),
}
)

// ObserveRequest records the request latency and counts the errors.
func (mc *MetricContext) ObserveRequest(err error) error {
return mc.Observe(APIRequestMetrics, err)
}

var registerAPIMetrics sync.Once

// RegisterMetrics registers OpenStack metrics.
func doRegisterAPIMetrics() {
registerAPIMetrics.Do(func() {
prometheus.MustRegister(
APIRequestMetrics.Duration,
APIRequestMetrics.Total,
APIRequestMetrics.Errors,
)
})
}
Loading

0 comments on commit 2897439

Please sign in to comment.