From 3e7a8a69c0cc9cfe2b1c5537d379d5d0133e38e3 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 1 Jul 2022 23:15:37 +0200 Subject: [PATCH 01/29] Add apm and prometheus packages --- go.mod | 11 +++++-- go.sum | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 5d35552b8..d09978a39 100644 --- a/go.mod +++ b/go.mod @@ -13,13 +13,15 @@ require ( github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 github.com/magefile/mage v1.9.0 github.com/pkg/errors v0.9.1 + github.com/prometheus/client_golang v1.12.2 github.com/stretchr/testify v1.7.1 - go.elastic.co/apm v1.14.0 + go.elastic.co/apm v1.15.0 go.elastic.co/apm/module/apmgorilla v1.14.0 + go.elastic.co/apm/module/apmprometheus v1.15.0 go.elastic.co/ecszap v1.0.1 go.uber.org/zap v1.21.0 golang.org/x/tools v0.1.7 - gopkg.in/yaml.v2 v2.2.8 + gopkg.in/yaml.v2 v2.4.0 ) require ( @@ -28,6 +30,8 @@ require ( cloud.google.com/go/iam v0.3.0 // indirect cloud.google.com/go/pubsub v1.21.1 // indirect github.com/armon/go-radix v1.0.0 // indirect + github.com/beorn7/perks v1.0.1 // indirect + github.com/cespare/xxhash/v2 v2.1.2 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/elastic/go-sysinfo v1.7.1 // indirect github.com/elastic/go-windows v1.0.1 // indirect @@ -39,8 +43,11 @@ require ( github.com/gorilla/handlers v1.5.1 // indirect github.com/jcchavezs/porto v0.3.0 // indirect github.com/kr/pretty v0.3.0 // indirect + github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect github.com/pkg/xattr v0.4.7 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/prometheus/client_model v0.2.0 // indirect + github.com/prometheus/common v0.32.1 // indirect github.com/prometheus/procfs v0.7.3 // indirect github.com/santhosh-tekuri/jsonschema v1.2.4 // indirect github.com/sirupsen/logrus v1.8.1 // indirect diff --git a/go.sum b/go.sum index 102c2a6b3..26fa44253 100644 --- a/go.sum +++ b/go.sum @@ -67,14 +67,25 @@ github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym github.com/Masterminds/semver/v3 v3.1.0 h1:Y2lUDsFKVRSYGojLJ1yLxSXdMmMYTYls0rCvoqmMUQk= github.com/Masterminds/semver/v3 v3.1.0/go.mod h1:VPu/7SZ7ePZ3QOrcuXROw5FAcLl4a0cBrbBpGY/8hQs= github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU= +github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc= +github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0= +github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI= github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8= github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8= github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= +github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8= +github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= +github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc= github.com/cespare/xxhash/v2 v2.1.1/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU= @@ -121,6 +132,14 @@ github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeME github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8= +github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as= +github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY= +github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE= +github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk= +github.com/go-logfmt/logfmt v0.5.0/go.mod h1:wCYkCAKZfumFQihp8CzCvQ3paCTfi41vtzG1KdI/P7A= +github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY= +github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ= github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= github.com/golang/groupcache v0.0.0-20191227052852-215e87163ea7/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc= @@ -172,6 +191,7 @@ github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/martian v2.1.0+incompatible h1:/CP5g8u/VJHijgedC/Legn3BAbAaWPgecwXBIDzw5no= github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs= github.com/google/martian/v3 v3.0.0/go.mod h1:y5Zk1BBys9G+gd6Jrk0W3cC1+ELVxBWuIGO+w/tUAp0= @@ -220,9 +240,19 @@ github.com/jcchavezs/porto v0.3.0/go.mod h1:fESH0gzDHiutHRdX2hv27ojnOVFco37hg1W6 github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901 h1:rp+c0RAYOWj8l6qbCUTSiRLG/iKnW3K3/QfPPuSsBt4= github.com/joeshaw/multierror v0.0.0-20140124173710-69b34d4ec901/go.mod h1:Z86h9688Y0wesXCyonoVr47MasHilkuLMqGhRZ4Hpak= +github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= +github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU= +github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= +github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w= +github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= @@ -232,6 +262,15 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/magefile/mage v1.9.0 h1:t3AU2wNwehMCW97vuqQLtw6puppWXHO+O2MHo5a50XE= github.com/magefile/mage v1.9.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A= +github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU= +github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= +github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= @@ -240,8 +279,30 @@ github.com/pkg/xattr v0.4.7 h1:XoA3KzmFvyPlH4RwX5eMcgtzcaGBaSvgt3IoFQfbrmQ= github.com/pkg/xattr v0.4.7/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6ktU= 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/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= +github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= +github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= +github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= +github.com/prometheus/client_golang v1.12.2 h1:51L9cDoUHVrXx4zWYlcLQIZ+d+VXHgqnYKkIuq4g/34= +github.com/prometheus/client_golang v1.12.2/go.mod h1:3Z9XVyYiZYEO+YQWt3RD2R3jrbd179Rt297l4aS6nDY= +github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= +github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= +github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= +github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= +github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= +github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= +github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= +github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= +github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/prometheus/procfs v0.7.3 h1:4jVXhlkAyzOScmCkXBTOLRLTz8EeU+eyjrwB/EPq0VU= github.com/prometheus/procfs v0.7.3/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= @@ -250,10 +311,14 @@ github.com/rogpeppe/go-internal v1.6.1 h1:/FiVV8dS/e+YqF2JvO3yXRFbBLTIuSDkuC7aBO github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/santhosh-tekuri/jsonschema v1.2.4 h1:hNhW8e7t+H1vgY+1QeEQpveR6D4+OwKPXCfD2aieJis= github.com/santhosh-tekuri/jsonschema v1.2.4/go.mod h1:TEAUOeZSmIxTTuHatJzrvARHiuO9LYd+cIxzgEHCQI4= +github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo= +github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE= github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA= 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/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= @@ -267,12 +332,15 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= -go.elastic.co/apm v1.14.0 h1:9yilcTbWpqhfyunUj6/SDpZbR4FOVB50xQgODe0TW/0= go.elastic.co/apm v1.14.0/go.mod h1:dylGv2HKR0tiCV+wliJz1KHtDyuD8SPe69oV7VyK6WY= +go.elastic.co/apm v1.15.0 h1:uPk2g/whK7c7XiZyz/YCUnAUBNPiyNeE3ARX3G6Gx7Q= +go.elastic.co/apm v1.15.0/go.mod h1:dylGv2HKR0tiCV+wliJz1KHtDyuD8SPe69oV7VyK6WY= go.elastic.co/apm/module/apmgorilla v1.14.0 h1:espCHSZ3ibkrffR6KLua+0jMeBSgO/087U9BZ46Cyv8= go.elastic.co/apm/module/apmgorilla v1.14.0/go.mod h1:+cDGiyPXN3EvTxoh7zcWOL1/Yw6zKhNkUU0a0OGyXsg= go.elastic.co/apm/module/apmhttp v1.14.0 h1:uDSIPr1BJOt1A/T5J9Beq9VtMtQHqOdqQUXCPRQF4C4= go.elastic.co/apm/module/apmhttp v1.14.0/go.mod h1:PY8hyV0X3eKqXYYoN0pyu1pWcvFCwGmh5eUFuS39Zmo= +go.elastic.co/apm/module/apmprometheus v1.15.0 h1:TgErQRlmy0Uc3zolIZ2UuompZ5++Ga3Zmx+lEkqQjcU= +go.elastic.co/apm/module/apmprometheus v1.15.0/go.mod h1:BaqqtNnlq1lwyMxVvDZ2lpDKUGEk4IqaPf+c3xMn8ZY= go.elastic.co/ecszap v1.0.1 h1:mBxqEJAEXBlpi5+scXdzL7LTFGogbuxipJC0KTZicyA= go.elastic.co/ecszap v1.0.1/go.mod h1:SVjazT+QgNeHSGOCUHvRgN+ZRj5FkB7IXQQsncdF57A= go.elastic.co/fastjson v1.1.0 h1:3MrGBWWVIxe/xvsbpghtkFoPciPhOCmjsR/HfwEeQR4= @@ -294,6 +362,7 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4= go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= +golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= @@ -339,6 +408,8 @@ golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= @@ -346,6 +417,7 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn golang.org/x/net v0.0.0-20190501004415-9ce7a6920f09/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks= +golang.org/x/net v0.0.0-20190613194153-d28f0bde5980/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190628185345-da137c7871d7/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -372,6 +444,7 @@ golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4/go.mod h1:RBQZq4jEuRlivfhVLdyRGr576XBO4/greRjx4P4O3yc= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/net v0.0.0-20210503060351-7fd8e65b6420/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= +golang.org/x/net v0.0.0-20210525063256-abc453219eb5/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220127200216-cd36cc0744dd/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220225172249-27dd8689420f/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= golang.org/x/net v0.0.0-20220325170049-de3da57026de/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk= @@ -411,9 +484,12 @@ golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190502145724-3ef323f4f1fd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190507160741-ecd444e8653b/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190606165138-5da285871e9c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -425,6 +501,7 @@ golang.org/x/sys v0.0.0-20191025021431-6c3a3bfe00ae/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191204072324-ce4227a45e2e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20191228213918-04cbcbbfeed8/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200106162015-b016eb3dc98e/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200113162924-86b910548bc1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200122134326-e047566fdf82/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200202164722-d101bd2416d5/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -437,6 +514,8 @@ golang.org/x/sys v0.0.0-20200501052902-10377860bb8e/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20200511232937-7e40ca221e25/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200515095857-1151b9dac4a9/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200523222454-059865788121/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200803210538-64077c9b5642/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200905004654-be1d3432aa8f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -453,6 +532,7 @@ golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7w golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210514084401-e8d321eab015/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20210603081109-ebe580a85c40/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210603125802-9665404d3644/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210616094352-59db8d763f22/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -463,6 +543,7 @@ golang.org/x/sys v0.0.0-20210908233432-aa78b53d3365/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20211124211545-fe61309f8881/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211210111614-af8b64212486/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.0.0-20220114195835-da31bd327af9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220209214540-3681064d5158/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -716,15 +797,21 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw= google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY= gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= +gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10= +gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.2.5/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= +gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= From 352170c1ebf5b6b8300b82bc705313a20d495dcc Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Fri, 1 Jul 2022 23:26:20 +0200 Subject: [PATCH 02/29] Add basic metrics for every request Add metrics for every request in flight requests, response size, request size and duration for every request including as label code, method and path. Uptime of the application could be obtanied using metric "process_start_time_seconds" --- main.go | 6 +++ util/logging.go | 10 ++--- util/metrics.go | 115 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 5 deletions(-) create mode 100644 util/metrics.go diff --git a/main.go b/main.go index 191045ec1..807867d37 100644 --- a/main.go +++ b/main.go @@ -19,8 +19,11 @@ import ( gstorage "cloud.google.com/go/storage" "github.com/gorilla/mux" "github.com/pkg/errors" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" "go.elastic.co/apm" "go.elastic.co/apm/module/apmgorilla" + "go.elastic.co/apm/module/apmprometheus" "go.uber.org/zap" ucfgYAML "github.com/elastic/go-ucfg/yaml" @@ -186,6 +189,7 @@ func initAPMTracer(logger *zap.Logger) *apm.Tracer { if err != nil { logger.Fatal("Failed to initialize APM agent", zap.Error(err)) } + tracer.RegisterMetricsGatherer(apmprometheus.Wrap(prometheus.DefaultGatherer)) return tracer } @@ -281,7 +285,9 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router router.HandleFunc(signaturesRouterPath, signaturesHandler) router.HandleFunc(packageIndexRouterPath, packageIndexHandler) router.HandleFunc(staticRouterPath, staticHandler) + router.Handle("/metrics", promhttp.Handler()) router.Use(util.LoggingMiddleware(logger)) + router.Use(util.MeticsMiddleware()) router.NotFoundHandler = http.Handler(notFoundHandler(fmt.Errorf("404 page not found"))) return router, nil } diff --git a/util/logging.go b/util/logging.go index 0995b2e51..d0b1bf9ef 100644 --- a/util/logging.go +++ b/util/logging.go @@ -97,13 +97,13 @@ func LoggingMiddleware(logger *zap.Logger) mux.MiddlewareFunc { logger = logger.Named("http").WithOptions(zap.WithCaller(false)) return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // Do not log requests to the health endpoint - if r.RequestURI == "/health" { + switch r.RequestURI { + case "/health", "/metrics": + // Do not log requests to these endpoints next.ServeHTTP(w, r) - return + default: + logRequest(logger, next, w, r) } - - logRequest(logger, next, w, r) }) } } diff --git a/util/metrics.go b/util/metrics.go new file mode 100644 index 000000000..e429bbb72 --- /dev/null +++ b/util/metrics.go @@ -0,0 +1,115 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package util + +import ( + "net/http" + + "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +const metricsNamespace = "epr" + +var ( + httpInFlightRequests = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: metricsNamespace, + Name: "in_flight_requests", + Help: "A gauge of requests currently being served by the http server.", + }) + + httpRequestsTotal = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Name: "http_requests_total", + Help: "A counter for requests to the http server.", + }, + []string{"code", "method", "path"}, + ) + + httpRequestDurationSeconds = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "http_request_duration_seconds", + Help: "A histogram of latencies for requests to the http server.", + }, + []string{"code", "method", "path"}, + ) + + httpRequestSizeBytes = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "http_request_size_bytes", + Help: "A histogram of sizes of requests to the http server.", + Buckets: []float64{ + 16, + 32, + 64, + 128, + 256, + 512, + 1024, /* 1KiB */ + 64 * 1024, /* 64KiB */ + 256 * 1024, /* 256KiB */ + 1024 * 1024, /* 1MiB */ + 64 * 1024 * 1024, /* 64MiB */ + }, + }, + []string{"code", "method", "path"}, + ) + + httpResponseSizeBytes = prometheus.NewHistogramVec( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "http_response_size_bytes", + Help: "A histogram of response sizes for requests to the http server.", + Buckets: []float64{ + 10, + 64, + 256, + 512, + 1024, /* 1KiB */ + 64 * 1024, /* 64KiB */ + 256 * 1024, /* 256KiB */ + 512 * 1024, /* 512KiB */ + 1024 * 1024, /* 1MiB */ + 64 * 1024 * 1024, /* 64MiB */ + 512 * 1024 * 1024, /* 512MiB */ + }, + }, + []string{"code", "method", "path"}, + ) +) + +// MetricsMiddleware is a middleware used to metric every request received +func MeticsMiddleware() mux.MiddlewareFunc { + // Rergister all metrics + prometheus.MustRegister(httpInFlightRequests) + prometheus.MustRegister(httpRequestsTotal) + prometheus.MustRegister(httpRequestDurationSeconds) + prometheus.MustRegister(httpRequestSizeBytes) + prometheus.MustRegister(httpResponseSizeBytes) + + return func(next http.Handler) http.Handler { + handler := next + + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + route := mux.CurrentRoute(req) + path, _ := route.GetPathTemplate() + if req.RequestURI == "/metrics" { + next.ServeHTTP(w, req) + return + } + labels := prometheus.Labels{"path": path} + handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerInFlight(httpInFlightRequests, handler) + handler = promhttp.InstrumentHandlerRequestSize(httpRequestSizeBytes.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerResponseSize(httpResponseSizeBytes.MustCurryWith(labels), handler) + handler.ServeHTTP(w, req) + }) + } +} From f5132cd67b1c618168ca4a499a0e803bd077ec13 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Mon, 4 Jul 2022 17:09:22 +0200 Subject: [PATCH 03/29] Fix typo metrics middleware Co-authored-by: Jaime Soriano Pastor --- util/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/metrics.go b/util/metrics.go index e429bbb72..ed92c3da6 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -85,7 +85,7 @@ var ( ) // MetricsMiddleware is a middleware used to metric every request received -func MeticsMiddleware() mux.MiddlewareFunc { +func MetricsMiddleware() mux.MiddlewareFunc { // Rergister all metrics prometheus.MustRegister(httpInFlightRequests) prometheus.MustRegister(httpRequestsTotal) From 4e4cea2afa3bdb4875ff4d4c0ed82f03ff7e0741 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Mon, 4 Jul 2022 17:58:49 +0200 Subject: [PATCH 04/29] Fix typo in usage --- main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/main.go b/main.go index 807867d37..51ad9d293 100644 --- a/main.go +++ b/main.go @@ -287,7 +287,7 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router router.HandleFunc(staticRouterPath, staticHandler) router.Handle("/metrics", promhttp.Handler()) router.Use(util.LoggingMiddleware(logger)) - router.Use(util.MeticsMiddleware()) + router.Use(util.MetricsMiddleware()) router.NotFoundHandler = http.Handler(notFoundHandler(fmt.Errorf("404 page not found"))) return router, nil } From 5e02260accef2e547c7f72a1b42119a92c9bfb2f Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Mon, 4 Jul 2022 18:36:10 +0200 Subject: [PATCH 05/29] Allow metrics endpoint to be served in a different address --- main.go | 23 ++++++++++++++++++++++- util/logging.go | 2 +- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 51ad9d293..b21bb45d4 100644 --- a/main.go +++ b/main.go @@ -41,6 +41,7 @@ const ( var ( address string httpProfAddress string + metricsAddress string tlsCertFile string tlsKeyFile string @@ -63,6 +64,7 @@ var ( func init() { flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.") + flag.StringVar(&metricsAddress, "metrics-address", "localhost:9000", "Address to expose the Prometheus metrics.") flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.") flag.StringVar(&tlsKeyFile, "tls-key", "", "Path of the TLS key.") flag.StringVar(&configPath, "config", "config.yml", "Path to the configuration file.") @@ -105,6 +107,8 @@ func main() { } }() + initMetricsServer(logger) + stop := make(chan os.Signal, 1) signal.Notify(stop, os.Interrupt, syscall.SIGTERM) <-stop @@ -129,6 +133,24 @@ func initHttpProf(logger *zap.Logger) { }() } +func initMetricsServer(logger *zap.Logger) { + // If -dry-run=true is set, service stops here after validation + if dryRun { + os.Exit(0) + } + + logger.Info("Starting http metrics in " + metricsAddress) + go func() { + router := http.NewServeMux() + router.Handle("/metrics", promhttp.Handler()) + err := http.ListenAndServe(metricsAddress, router) + if err != nil { + logger.Fatal("failed to start Prometheus metrics endpoint", zap.Error(err)) + } + }() + +} + func initServer(logger *zap.Logger) *http.Server { apmTracer := initAPMTracer(logger) tx := apmTracer.StartTransaction("initServer", "backend.init") @@ -285,7 +307,6 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router router.HandleFunc(signaturesRouterPath, signaturesHandler) router.HandleFunc(packageIndexRouterPath, packageIndexHandler) router.HandleFunc(staticRouterPath, staticHandler) - router.Handle("/metrics", promhttp.Handler()) router.Use(util.LoggingMiddleware(logger)) router.Use(util.MetricsMiddleware()) router.NotFoundHandler = http.Handler(notFoundHandler(fmt.Errorf("404 page not found"))) diff --git a/util/logging.go b/util/logging.go index d0b1bf9ef..4b74edc77 100644 --- a/util/logging.go +++ b/util/logging.go @@ -98,7 +98,7 @@ func LoggingMiddleware(logger *zap.Logger) mux.MiddlewareFunc { return func(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { switch r.RequestURI { - case "/health", "/metrics": + case "/health": // Do not log requests to these endpoints next.ServeHTTP(w, r) default: From 0fb89c3937d8c32c6cdb75ef2a397e4bf50e4567 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 10:22:57 +0200 Subject: [PATCH 06/29] Rephrase comment --- util/metrics.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/metrics.go b/util/metrics.go index ed92c3da6..ca9064b4f 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -84,7 +84,7 @@ var ( ) ) -// MetricsMiddleware is a middleware used to metric every request received +// MetricsMiddleware is a middleware used to measure every request received func MetricsMiddleware() mux.MiddlewareFunc { // Rergister all metrics prometheus.MustRegister(httpInFlightRequests) From 54f8d761b155aedd71ecc1494235e90e1fc39cd1 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 11:29:48 +0200 Subject: [PATCH 07/29] Move os.Exit calls to main --- main.go | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index b21bb45d4..db6cc53b7 100644 --- a/main.go +++ b/main.go @@ -100,6 +100,9 @@ func main() { initHttpProf(logger) server := initServer(logger) + if dryRun { + os.Exit(0) + } go func() { err := runServer(server) if err != nil && err != http.ErrServerClosed { @@ -124,6 +127,10 @@ func initHttpProf(logger *zap.Logger) { return } + if dryRun { + return + } + logger.Info("Starting http pprof in " + httpProfAddress) go func() { err := http.ListenAndServe(httpProfAddress, nil) @@ -134,11 +141,9 @@ func initHttpProf(logger *zap.Logger) { } func initMetricsServer(logger *zap.Logger) { - // If -dry-run=true is set, service stops here after validation if dryRun { - os.Exit(0) + return } - logger.Info("Starting http metrics in " + metricsAddress) go func() { router := http.NewServeMux() @@ -148,7 +153,6 @@ func initMetricsServer(logger *zap.Logger) { logger.Fatal("failed to start Prometheus metrics endpoint", zap.Error(err)) } }() - } func initServer(logger *zap.Logger) *http.Server { @@ -179,11 +183,6 @@ func initServer(logger *zap.Logger) *http.Server { combinedIndexer := NewCombinedIndexer(indexers...) ensurePackagesAvailable(ctx, logger, combinedIndexer) - // If -dry-run=true is set, service stops here after validation - if dryRun { - os.Exit(0) - } - router := mustLoadRouter(logger, config, combinedIndexer) apmgorilla.Instrument(router, apmgorilla.WithTracer(apmTracer)) From 73cca37cc08ef915f659e36b750604756d9ea6be Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 11:42:28 +0200 Subject: [PATCH 08/29] Downgrade to apm 1.14.0 and remove apmprometheus --- go.mod | 3 +-- go.sum | 9 +-------- main.go | 3 --- 3 files changed, 2 insertions(+), 13 deletions(-) diff --git a/go.mod b/go.mod index d09978a39..5da2db2a7 100644 --- a/go.mod +++ b/go.mod @@ -15,9 +15,8 @@ require ( github.com/pkg/errors v0.9.1 github.com/prometheus/client_golang v1.12.2 github.com/stretchr/testify v1.7.1 - go.elastic.co/apm v1.15.0 + go.elastic.co/apm v1.14.0 go.elastic.co/apm/module/apmgorilla v1.14.0 - go.elastic.co/apm/module/apmprometheus v1.15.0 go.elastic.co/ecszap v1.0.1 go.uber.org/zap v1.21.0 golang.org/x/tools v0.1.7 diff --git a/go.sum b/go.sum index 26fa44253..92edd76b5 100644 --- a/go.sum +++ b/go.sum @@ -280,7 +280,6 @@ github.com/pkg/xattr v0.4.7/go.mod h1:di8WF84zAKk8jzR1UBTEWh9AUlIZZ7M/JNt8e9B6kt 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/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= -github.com/prometheus/client_golang v0.9.2/go.mod h1:OsXs2jCmiKlQ1lTBmv21f2mNfw4xf/QclQDMrYNZzcM= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= github.com/prometheus/client_golang v1.7.1/go.mod h1:PY5Wy2awLA44sXw4AOSfFBetzPP4j5+D6mVACh+pe2M= github.com/prometheus/client_golang v1.11.0/go.mod h1:Z6t4BnS23TR94PD6BsDNk8yVqroYurpAkEiz0P2BEV0= @@ -291,14 +290,12 @@ github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1: github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/prometheus/client_model v0.2.0 h1:uq5h0d+GuxiXLJLNABMgp2qUWDPiLvgCzz2dUR+/W/M= github.com/prometheus/client_model v0.2.0/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= -github.com/prometheus/common v0.0.0-20181126121408-4724e9255275/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= github.com/prometheus/common v0.4.1/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4= github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB80sz/V91rCo= github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc= github.com/prometheus/common v0.32.1 h1:hWIdL3N2HoUx3B8j3YN9mWor0qhY/NlEKZEaXxuIRh4= github.com/prometheus/common v0.32.1/go.mod h1:vu+V0TpY+O6vW9J44gczi3Ap/oXXR10b+M/gUGO4Hls= github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= -github.com/prometheus/procfs v0.0.0-20181204211112-1dc9a6cbc91a/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= github.com/prometheus/procfs v0.0.0-20190425082905-87a4384529e0/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA= github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU= @@ -332,15 +329,12 @@ github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9de github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= +go.elastic.co/apm v1.14.0 h1:9yilcTbWpqhfyunUj6/SDpZbR4FOVB50xQgODe0TW/0= go.elastic.co/apm v1.14.0/go.mod h1:dylGv2HKR0tiCV+wliJz1KHtDyuD8SPe69oV7VyK6WY= -go.elastic.co/apm v1.15.0 h1:uPk2g/whK7c7XiZyz/YCUnAUBNPiyNeE3ARX3G6Gx7Q= -go.elastic.co/apm v1.15.0/go.mod h1:dylGv2HKR0tiCV+wliJz1KHtDyuD8SPe69oV7VyK6WY= go.elastic.co/apm/module/apmgorilla v1.14.0 h1:espCHSZ3ibkrffR6KLua+0jMeBSgO/087U9BZ46Cyv8= go.elastic.co/apm/module/apmgorilla v1.14.0/go.mod h1:+cDGiyPXN3EvTxoh7zcWOL1/Yw6zKhNkUU0a0OGyXsg= go.elastic.co/apm/module/apmhttp v1.14.0 h1:uDSIPr1BJOt1A/T5J9Beq9VtMtQHqOdqQUXCPRQF4C4= go.elastic.co/apm/module/apmhttp v1.14.0/go.mod h1:PY8hyV0X3eKqXYYoN0pyu1pWcvFCwGmh5eUFuS39Zmo= -go.elastic.co/apm/module/apmprometheus v1.15.0 h1:TgErQRlmy0Uc3zolIZ2UuompZ5++Ga3Zmx+lEkqQjcU= -go.elastic.co/apm/module/apmprometheus v1.15.0/go.mod h1:BaqqtNnlq1lwyMxVvDZ2lpDKUGEk4IqaPf+c3xMn8ZY= go.elastic.co/ecszap v1.0.1 h1:mBxqEJAEXBlpi5+scXdzL7LTFGogbuxipJC0KTZicyA= go.elastic.co/ecszap v1.0.1/go.mod h1:SVjazT+QgNeHSGOCUHvRgN+ZRj5FkB7IXQQsncdF57A= go.elastic.co/fastjson v1.1.0 h1:3MrGBWWVIxe/xvsbpghtkFoPciPhOCmjsR/HfwEeQR4= @@ -409,7 +403,6 @@ golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= -golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190108225652-1e06a53dbb7e/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= diff --git a/main.go b/main.go index db6cc53b7..06caf93cc 100644 --- a/main.go +++ b/main.go @@ -19,11 +19,9 @@ import ( gstorage "cloud.google.com/go/storage" "github.com/gorilla/mux" "github.com/pkg/errors" - "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "go.elastic.co/apm" "go.elastic.co/apm/module/apmgorilla" - "go.elastic.co/apm/module/apmprometheus" "go.uber.org/zap" ucfgYAML "github.com/elastic/go-ucfg/yaml" @@ -210,7 +208,6 @@ func initAPMTracer(logger *zap.Logger) *apm.Tracer { if err != nil { logger.Fatal("Failed to initialize APM agent", zap.Error(err)) } - tracer.RegisterMetricsGatherer(apmprometheus.Wrap(prometheus.DefaultGatherer)) return tracer } From a1853ee562d9d5fba2850b68cd93b6188705de0b Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 12:53:38 +0200 Subject: [PATCH 09/29] Add indexed packages metric --- main.go | 1 + storage/indexer.go | 1 + util/metrics.go | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/main.go b/main.go index 06caf93cc..77b3921a5 100644 --- a/main.go +++ b/main.go @@ -266,6 +266,7 @@ func ensurePackagesAvailable(ctx context.Context, logger *zap.Logger, indexer In } logger.Info(fmt.Sprintf("%v package manifests loaded", len(packages))) + util.NumberIndexedPackages.Set(float64(len(packages))) } func mustLoadRouter(logger *zap.Logger, config *Config, indexer Indexer) *mux.Router { diff --git a/storage/indexer.go b/storage/indexer.go index 3f968869e..10ab0b3a6 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -160,6 +160,7 @@ func (i *Indexer) updateIndex(ctx context.Context) error { defer i.m.Unlock() i.cursor = storageCursor.Current i.packageList = refreshedList + util.NumberIndexedPackages.Set(float64(len(i.packageList))) return nil } diff --git a/util/metrics.go b/util/metrics.go index ca9064b4f..434503867 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -14,6 +14,16 @@ import ( const metricsNamespace = "epr" +// storage metrics +var ( + NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: metricsNamespace, + Name: "number_indexed_packages", + Help: "A gauge for number of indexed packages", + }) +) + +// common metrics for http requests var ( httpInFlightRequests = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: metricsNamespace, @@ -93,6 +103,8 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(httpRequestSizeBytes) prometheus.MustRegister(httpResponseSizeBytes) + prometheus.MustRegister(NumberIndexedPackages) + return func(next http.Handler) http.Handler { handler := next From 7d41766098cfc31bd5a1df91b23d957c528a589c Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 13:48:23 +0200 Subject: [PATCH 10/29] Add search duration histogram metric with default buckets --- search.go | 2 ++ util/metrics.go | 12 ++++++++++++ 2 files changed, 14 insertions(+) diff --git a/search.go b/search.go index 2095e3ac0..8a9e6828f 100644 --- a/search.go +++ b/search.go @@ -32,11 +32,13 @@ func searchHandler(indexer Indexer, cacheTime time.Duration) func(w http.Respons Filter: filter, } + start := time.Now() packages, err := indexer.Get(r.Context(), &opts) if err != nil { notFoundError(w, errors.Wrapf(err, "fetching package failed")) return } + util.SearchProcessDurationSeconds.Observe(time.Since(start).Seconds()) data, err := getPackageOutput(r.Context(), packages) if err != nil { diff --git a/util/metrics.go b/util/metrics.go index 434503867..d489388b7 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -14,6 +14,17 @@ import ( const metricsNamespace = "epr" +// search metrics +var ( + SearchProcessDurationSeconds = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "search_process_duration_seconds", + Help: "A histogram of package search processes.", + }, + ) +) + // storage metrics var ( NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ @@ -104,6 +115,7 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(httpResponseSizeBytes) prometheus.MustRegister(NumberIndexedPackages) + prometheus.MustRegister(SearchProcessDurationSeconds) return func(next http.Handler) http.Handler { handler := next From 105e637b770750def0a361b2ce6c1515b08efd73 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 13:49:11 +0200 Subject: [PATCH 11/29] Update buckets for byte related metrics to fit storage v2 --- util/metrics.go | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/util/metrics.go b/util/metrics.go index d489388b7..902388d89 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -72,11 +72,9 @@ var ( 128, 256, 512, - 1024, /* 1KiB */ - 64 * 1024, /* 64KiB */ - 256 * 1024, /* 256KiB */ - 1024 * 1024, /* 1MiB */ - 64 * 1024 * 1024, /* 64MiB */ + 1024, /* 1KiB */ + 64 * 1024, /* 64KiB */ + 256 * 1024, /* 256KiB */ }, }, []string{"code", "method", "path"}, @@ -88,17 +86,15 @@ var ( Name: "http_response_size_bytes", Help: "A histogram of response sizes for requests to the http server.", Buckets: []float64{ - 10, + 16, + 32, 64, + 128, 256, 512, - 1024, /* 1KiB */ - 64 * 1024, /* 64KiB */ - 256 * 1024, /* 256KiB */ - 512 * 1024, /* 512KiB */ - 1024 * 1024, /* 1MiB */ - 64 * 1024 * 1024, /* 64MiB */ - 512 * 1024 * 1024, /* 512MiB */ + 1024, /* 1KiB */ + 64 * 1024, /* 64KiB */ + 256 * 1024, /* 256KiB */ }, }, []string{"code", "method", "path"}, From 9aa7948b3e73ccfb193394d816752988b8b5e83f Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 14:28:49 +0200 Subject: [PATCH 12/29] Add cache time output for index handler --- main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/main.go b/main.go index 77b3921a5..3179a3d75 100644 --- a/main.go +++ b/main.go @@ -245,6 +245,8 @@ func getPackagesBasePaths(config *Config) []string { func printConfig(logger *zap.Logger, config *Config) { logger.Info("Packages paths: " + strings.Join(config.PackagePaths, ", ")) + logger.Info("Cache time for /: " + config.CacheTimeIndex.String()) + logger.Info("Cache time for /index.json: " + config.CacheTimeIndex.String()) logger.Info("Cache time for /search: " + config.CacheTimeSearch.String()) logger.Info("Cache time for /categories: " + config.CacheTimeCategories.String()) logger.Info("Cache time for all others: " + config.CacheTimeCatchAll.String()) From e83079aac12ad627e6ef8020a4fb7e8cedb91583 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 18:25:34 +0200 Subject: [PATCH 13/29] Add metric about service info With this information added a parameter (-version) to show the version of the elastic package registry binary. --- main.go | 18 ++++++++++++++++-- util/metrics.go | 38 +++++++++++++++++++++++--------------- 2 files changed, 39 insertions(+), 17 deletions(-) diff --git a/main.go b/main.go index 3179a3d75..8a745a9dc 100644 --- a/main.go +++ b/main.go @@ -32,8 +32,9 @@ import ( ) const ( - serviceName = "package-registry" - version = "1.9.1" + serviceName = "package-registry" + version = "1.9.1" + defaultInstance = "localhost" ) var ( @@ -47,6 +48,8 @@ var ( dryRun bool configPath string + printVersionInfo bool + featureStorageIndexer bool storageIndexerBucketInternal string storageEndpoint string @@ -61,6 +64,7 @@ var ( ) func init() { + flag.BoolVar(&printVersionInfo, "version", false, "Print Elastic Package Registry version") flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.") flag.StringVar(&metricsAddress, "metrics-address", "localhost:9000", "Address to expose the Prometheus metrics.") flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.") @@ -89,6 +93,11 @@ type Config struct { func main() { parseFlags() + if printVersionInfo { + fmt.Printf("Elastic Package Registry version %v\n", version) + os.Exit(0) + } + logger := util.Logger() defer logger.Sync() @@ -143,6 +152,11 @@ func initMetricsServer(logger *zap.Logger) { return } logger.Info("Starting http metrics in " + metricsAddress) + hostname, found := os.LookupEnv("HOSTNAME") + if !found { + hostname = defaultInstance + } + util.ServiceInfo.WithLabelValues(version, hostname).Set(1) go func() { router := http.NewServeMux() router.Handle("/metrics", promhttp.Handler()) diff --git a/util/metrics.go b/util/metrics.go index 902388d89..d5cc47634 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -14,26 +14,32 @@ import ( const metricsNamespace = "epr" -// search metrics -var ( - SearchProcessDurationSeconds = prometheus.NewHistogram( - prometheus.HistogramOpts{ - Namespace: metricsNamespace, - Name: "search_process_duration_seconds", - Help: "A histogram of package search processes.", - }, - ) +// info metric +var ServiceInfo = prometheus.NewGaugeVec( + prometheus.GaugeOpts{ + Namespace: metricsNamespace, + Name: "service_info", + Help: "Version information about this binary", + }, + []string{"version", "instance"}, ) -// storage metrics -var ( - NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ +// search metrics +var SearchProcessDurationSeconds = prometheus.NewHistogram( + prometheus.HistogramOpts{ Namespace: metricsNamespace, - Name: "number_indexed_packages", - Help: "A gauge for number of indexed packages", - }) + Name: "search_process_duration_seconds", + Help: "A histogram of package search processes.", + }, ) +// storage metrics +var NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: metricsNamespace, + Name: "number_indexed_packages", + Help: "A gauge for number of indexed packages", +}) + // common metrics for http requests var ( httpInFlightRequests = prometheus.NewGauge(prometheus.GaugeOpts{ @@ -104,6 +110,8 @@ var ( // MetricsMiddleware is a middleware used to measure every request received func MetricsMiddleware() mux.MiddlewareFunc { // Rergister all metrics + prometheus.MustRegister(ServiceInfo) + prometheus.MustRegister(httpInFlightRequests) prometheus.MustRegister(httpRequestsTotal) prometheus.MustRegister(httpRequestDurationSeconds) From 3fbc7360de287decd549dc6c36b0ad4c8c79cac7 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Tue, 5 Jul 2022 18:32:08 +0200 Subject: [PATCH 14/29] Remove filter /metrics as it is not in the same router --- main.go | 12 +++++++----- util/metrics.go | 5 +---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 8a745a9dc..cf1c7019c 100644 --- a/main.go +++ b/main.go @@ -32,9 +32,9 @@ import ( ) const ( - serviceName = "package-registry" - version = "1.9.1" - defaultInstance = "localhost" + serviceName = "package-registry" + version = "1.9.1" + defaultInstanceName = "localhost" ) var ( @@ -151,12 +151,14 @@ func initMetricsServer(logger *zap.Logger) { if dryRun { return } - logger.Info("Starting http metrics in " + metricsAddress) + hostname, found := os.LookupEnv("HOSTNAME") if !found { - hostname = defaultInstance + hostname = defaultInstanceName } util.ServiceInfo.WithLabelValues(version, hostname).Set(1) + + logger.Info("Starting http metrics in " + metricsAddress) go func() { router := http.NewServeMux() router.Handle("/metrics", promhttp.Handler()) diff --git a/util/metrics.go b/util/metrics.go index d5cc47634..ad928f569 100644 --- a/util/metrics.go +++ b/util/metrics.go @@ -127,11 +127,8 @@ func MetricsMiddleware() mux.MiddlewareFunc { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { route := mux.CurrentRoute(req) path, _ := route.GetPathTemplate() - if req.RequestURI == "/metrics" { - next.ServeHTTP(w, req) - return - } labels := prometheus.Labels{"path": path} + handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(labels), handler) handler = promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(labels), handler) handler = promhttp.InstrumentHandlerInFlight(httpInFlightRequests, handler) From b510843aaa5c10ce63e16c8f8f6797ac24d0ef65 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 09:56:07 +0200 Subject: [PATCH 15/29] Use specific os function to get hostname --- main.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index cf1c7019c..fed10a995 100644 --- a/main.go +++ b/main.go @@ -152,8 +152,8 @@ func initMetricsServer(logger *zap.Logger) { return } - hostname, found := os.LookupEnv("HOSTNAME") - if !found { + hostname, err := os.Hostname() + if err != nil { hostname = defaultInstanceName } util.ServiceInfo.WithLabelValues(version, hostname).Set(1) From 6d2999061ae495eb7c4727a119401e60ab877375 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 10:11:52 +0200 Subject: [PATCH 16/29] Moved metrics out from util package --- main.go | 7 +++--- {util => metrics}/metrics.go | 38 +----------------------------- metrics/middleware.go | 45 ++++++++++++++++++++++++++++++++++++ search.go | 3 ++- storage/indexer.go | 3 ++- 5 files changed, 54 insertions(+), 42 deletions(-) rename {util => metrics}/metrics.go (65%) create mode 100644 metrics/middleware.go diff --git a/main.go b/main.go index fed10a995..8d9fa9e81 100644 --- a/main.go +++ b/main.go @@ -26,6 +26,7 @@ import ( ucfgYAML "github.com/elastic/go-ucfg/yaml" + "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/packages" "github.com/elastic/package-registry/storage" "github.com/elastic/package-registry/util" @@ -156,7 +157,7 @@ func initMetricsServer(logger *zap.Logger) { if err != nil { hostname = defaultInstanceName } - util.ServiceInfo.WithLabelValues(version, hostname).Set(1) + metrics.ServiceInfo.WithLabelValues(version, hostname).Set(1) logger.Info("Starting http metrics in " + metricsAddress) go func() { @@ -284,7 +285,7 @@ func ensurePackagesAvailable(ctx context.Context, logger *zap.Logger, indexer In } logger.Info(fmt.Sprintf("%v package manifests loaded", len(packages))) - util.NumberIndexedPackages.Set(float64(len(packages))) + metrics.NumberIndexedPackages.Set(float64(len(packages))) } func mustLoadRouter(logger *zap.Logger, config *Config, indexer Indexer) *mux.Router { @@ -323,7 +324,7 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router router.HandleFunc(packageIndexRouterPath, packageIndexHandler) router.HandleFunc(staticRouterPath, staticHandler) router.Use(util.LoggingMiddleware(logger)) - router.Use(util.MetricsMiddleware()) + router.Use(metrics.MetricsMiddleware()) router.NotFoundHandler = http.Handler(notFoundHandler(fmt.Errorf("404 page not found"))) return router, nil } diff --git a/util/metrics.go b/metrics/metrics.go similarity index 65% rename from util/metrics.go rename to metrics/metrics.go index ad928f569..e3010ffe4 100644 --- a/util/metrics.go +++ b/metrics/metrics.go @@ -2,14 +2,10 @@ // or more contributor license agreements. Licensed under the Elastic License; // you may not use this file except in compliance with the Elastic License. -package util +package metrics import ( - "net/http" - - "github.com/gorilla/mux" "github.com/prometheus/client_golang/prometheus" - "github.com/prometheus/client_golang/prometheus/promhttp" ) const metricsNamespace = "epr" @@ -106,35 +102,3 @@ var ( []string{"code", "method", "path"}, ) ) - -// MetricsMiddleware is a middleware used to measure every request received -func MetricsMiddleware() mux.MiddlewareFunc { - // Rergister all metrics - prometheus.MustRegister(ServiceInfo) - - prometheus.MustRegister(httpInFlightRequests) - prometheus.MustRegister(httpRequestsTotal) - prometheus.MustRegister(httpRequestDurationSeconds) - prometheus.MustRegister(httpRequestSizeBytes) - prometheus.MustRegister(httpResponseSizeBytes) - - prometheus.MustRegister(NumberIndexedPackages) - prometheus.MustRegister(SearchProcessDurationSeconds) - - return func(next http.Handler) http.Handler { - handler := next - - return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { - route := mux.CurrentRoute(req) - path, _ := route.GetPathTemplate() - labels := prometheus.Labels{"path": path} - - handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(labels), handler) - handler = promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(labels), handler) - handler = promhttp.InstrumentHandlerInFlight(httpInFlightRequests, handler) - handler = promhttp.InstrumentHandlerRequestSize(httpRequestSizeBytes.MustCurryWith(labels), handler) - handler = promhttp.InstrumentHandlerResponseSize(httpResponseSizeBytes.MustCurryWith(labels), handler) - handler.ServeHTTP(w, req) - }) - } -} diff --git a/metrics/middleware.go b/metrics/middleware.go new file mode 100644 index 000000000..89e908e60 --- /dev/null +++ b/metrics/middleware.go @@ -0,0 +1,45 @@ +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one +// or more contributor license agreements. Licensed under the Elastic License; +// you may not use this file except in compliance with the Elastic License. + +package metrics + +import ( + "net/http" + + "github.com/gorilla/mux" + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" +) + +// MetricsMiddleware is a middleware used to measure every request received +func MetricsMiddleware() mux.MiddlewareFunc { + // Rergister all metrics + prometheus.MustRegister(ServiceInfo) + + prometheus.MustRegister(httpInFlightRequests) + prometheus.MustRegister(httpRequestsTotal) + prometheus.MustRegister(httpRequestDurationSeconds) + prometheus.MustRegister(httpRequestSizeBytes) + prometheus.MustRegister(httpResponseSizeBytes) + + prometheus.MustRegister(NumberIndexedPackages) + prometheus.MustRegister(SearchProcessDurationSeconds) + + return func(next http.Handler) http.Handler { + handler := next + + return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { + route := mux.CurrentRoute(req) + path, _ := route.GetPathTemplate() + labels := prometheus.Labels{"path": path} + + handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerDuration(httpRequestDurationSeconds.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerInFlight(httpInFlightRequests, handler) + handler = promhttp.InstrumentHandlerRequestSize(httpRequestSizeBytes.MustCurryWith(labels), handler) + handler = promhttp.InstrumentHandlerResponseSize(httpResponseSizeBytes.MustCurryWith(labels), handler) + handler.ServeHTTP(w, req) + }) + } +} diff --git a/search.go b/search.go index 8a9e6828f..63a8c14d5 100644 --- a/search.go +++ b/search.go @@ -17,6 +17,7 @@ import ( "github.com/pkg/errors" "go.elastic.co/apm" + "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/packages" "github.com/elastic/package-registry/util" ) @@ -38,7 +39,7 @@ func searchHandler(indexer Indexer, cacheTime time.Duration) func(w http.Respons notFoundError(w, errors.Wrapf(err, "fetching package failed")) return } - util.SearchProcessDurationSeconds.Observe(time.Since(start).Seconds()) + metrics.SearchProcessDurationSeconds.Observe(time.Since(start).Seconds()) data, err := getPackageOutput(r.Context(), packages) if err != nil { diff --git a/storage/indexer.go b/storage/indexer.go index 10ab0b3a6..4486c1e7f 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -16,6 +16,7 @@ import ( "github.com/pkg/errors" "go.uber.org/zap" + "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/packages" "github.com/elastic/package-registry/util" ) @@ -160,7 +161,7 @@ func (i *Indexer) updateIndex(ctx context.Context) error { defer i.m.Unlock() i.cursor = storageCursor.Current i.packageList = refreshedList - util.NumberIndexedPackages.Set(float64(len(i.packageList))) + metrics.NumberIndexedPackages.Set(float64(len(i.packageList))) return nil } From 871a8630e255221a641ada73b2492c5fa06fbd87 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 10:54:48 +0200 Subject: [PATCH 17/29] Refactor init indexers in its own function --- main.go | 43 ++++++++++++++++++++++--------------------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/main.go b/main.go index 8d9fa9e81..2deddc1e8 100644 --- a/main.go +++ b/main.go @@ -102,15 +102,19 @@ func main() { logger := util.Logger() defer logger.Sync() + config := mustLoadConfig(logger) + if dryRun { + logger.Info("Running dry-run mode") + _ = initIndexers(context.Background(), logger, config) + os.Exit(0) + } + logger.Info("Package registry started") defer logger.Info("Package registry stopped") initHttpProf(logger) - server := initServer(logger) - if dryRun { - os.Exit(0) - } + server := initServer(logger, config) go func() { err := runServer(server) if err != nil && err != http.ErrServerClosed { @@ -135,10 +139,6 @@ func initHttpProf(logger *zap.Logger) { return } - if dryRun { - return - } - logger.Info("Starting http pprof in " + httpProfAddress) go func() { err := http.ListenAndServe(httpProfAddress, nil) @@ -149,10 +149,6 @@ func initHttpProf(logger *zap.Logger) { } func initMetricsServer(logger *zap.Logger) { - if dryRun { - return - } - hostname, err := os.Hostname() if err != nil { hostname = defaultInstanceName @@ -170,17 +166,10 @@ func initMetricsServer(logger *zap.Logger) { }() } -func initServer(logger *zap.Logger) *http.Server { - apmTracer := initAPMTracer(logger) - tx := apmTracer.StartTransaction("initServer", "backend.init") - defer tx.End() - - ctx := apm.ContextWithTransaction(context.TODO(), tx) - - config := mustLoadConfig(logger) +func initIndexers(ctx context.Context, logger *zap.Logger, config *Config) CombinedIndexer { packagesBasePaths := getPackagesBasePaths(config) - var indexers []Indexer + var indexers CombinedIndexer if featureStorageIndexer { storageClient, err := gstorage.NewClient(ctx) if err != nil { @@ -198,6 +187,18 @@ func initServer(logger *zap.Logger) *http.Server { combinedIndexer := NewCombinedIndexer(indexers...) ensurePackagesAvailable(ctx, logger, combinedIndexer) + return combinedIndexer +} + +func initServer(logger *zap.Logger, config *Config) *http.Server { + apmTracer := initAPMTracer(logger) + tx := apmTracer.StartTransaction("initServer", "backend.init") + defer tx.End() + + ctx := apm.ContextWithTransaction(context.TODO(), tx) + + combinedIndexer := initIndexers(ctx, logger, config) + router := mustLoadRouter(logger, config, combinedIndexer) apmgorilla.Instrument(router, apmgorilla.WithTracer(apmTracer)) From 8906a033ccecaeac7f0a53e71c775b14ebe8e305 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 11:14:56 +0200 Subject: [PATCH 18/29] Update Readme and Changelog --- CHANGELOG.md | 1 + README.md | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 328113a24..7a00ed0cf 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Added * Update Go version and base Ubuntu image. [#821](https://github.com/elastic/package-registry/pull/821) +* Instrument package registry with Prometheus metrics. [#827](https://github.com/elastic/package-registry/pull/827) ### Deprecated diff --git a/README.md b/README.md index c859ab92d..0bc2b6ebe 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Endpoints: * `/search`: Search for packages. By default returns all the most recent packages available. * `/categories`: List of the existing package categories and how many packages are in each category. * `/package/{name}/{version}`: Info about a package -* `/epr/{name}/{name}-{version}.tar.gz`: Download a package +* `/epr/{name}/{name}-{version}.zip`: Download a package Examples for each API endpoint can be found here: https://github.com/elastic/package-registry/tree/main/docs/api @@ -236,6 +236,19 @@ It will be listening in the given address. You can read more about this profiler and the available endpoints in the [pprof documentation](https://pkg.go.dev/net/http/pprof). +## Metrics +Package registry is instrumented to expose Prometheus metrics. These metrics are exposed under the `/metrics` endpoint. +By default this endpoint runs `localhost:9000`. These metrics can be scraped like: +``` +curl http://localhost:9000 +``` + +The host and port where this endpoint runs can be configured by means of the parameter `-metrics-address` +(or `EPR_METRICS_ADDRESS` environment variable): +``` +package-registry --metrics-address 0.0.0.0:9000 +``` + ## Release New versions of the package registry need to be released from time to time. The following steps should be followed to create a new release: From 4d8226c3fa424739ae3e80d76155d6d047ee6c7c Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 12:43:08 +0200 Subject: [PATCH 19/29] Add two more metrics --- main.go | 2 +- metrics/metrics.go | 40 ++++++++++++++++++++++++++++++---------- metrics/middleware.go | 6 ++++-- packages/http.go | 9 +++++++++ storage/indexer.go | 1 + 5 files changed, 45 insertions(+), 13 deletions(-) diff --git a/main.go b/main.go index 2deddc1e8..862a1df14 100644 --- a/main.go +++ b/main.go @@ -153,7 +153,7 @@ func initMetricsServer(logger *zap.Logger) { if err != nil { hostname = defaultInstanceName } - metrics.ServiceInfo.WithLabelValues(version, hostname).Set(1) + metrics.SetServiceInfo(version, hostname) logger.Info("Starting http metrics in " + metricsAddress) go func() { diff --git a/metrics/metrics.go b/metrics/metrics.go index e3010ffe4..211e7dd69 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -10,8 +10,7 @@ import ( const metricsNamespace = "epr" -// info metric -var ServiceInfo = prometheus.NewGaugeVec( +var serviceInfo = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: metricsNamespace, Name: "service_info", @@ -20,7 +19,6 @@ var ServiceInfo = prometheus.NewGaugeVec( []string{"version", "instance"}, ) -// search metrics var SearchProcessDurationSeconds = prometheus.NewHistogram( prometheus.HistogramOpts{ Namespace: metricsNamespace, @@ -29,14 +27,31 @@ var SearchProcessDurationSeconds = prometheus.NewHistogram( }, ) -// storage metrics -var NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ - Namespace: metricsNamespace, - Name: "number_indexed_packages", - Help: "A gauge for number of indexed packages", -}) +var ( + NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ + Namespace: metricsNamespace, + Name: "number_indexed_packages", + Help: "A gauge for number of indexed packages", + }) + + CursorUpdatesTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Name: "cursor_updates_total", + Help: "A counter for updates of the cursor", + }, + ) + + StorageRequestsTotal = prometheus.NewCounterVec( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Name: "storage_requests_total", + Help: "A counter for requests performed to the storage", + }, + []string{"location", "component"}, + ) +) -// common metrics for http requests var ( httpInFlightRequests = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: metricsNamespace, @@ -102,3 +117,8 @@ var ( []string{"code", "method", "path"}, ) ) + +// SetServiceInfo is used to set the main information of the service +func SetServiceInfo(version, hostname string) { + serviceInfo.With(prometheus.Labels{"version": version, "instance": hostname}).Set(1) +} diff --git a/metrics/middleware.go b/metrics/middleware.go index 89e908e60..315122e6e 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -15,7 +15,7 @@ import ( // MetricsMiddleware is a middleware used to measure every request received func MetricsMiddleware() mux.MiddlewareFunc { // Rergister all metrics - prometheus.MustRegister(ServiceInfo) + prometheus.MustRegister(serviceInfo) prometheus.MustRegister(httpInFlightRequests) prometheus.MustRegister(httpRequestsTotal) @@ -23,8 +23,10 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(httpRequestSizeBytes) prometheus.MustRegister(httpResponseSizeBytes) - prometheus.MustRegister(NumberIndexedPackages) prometheus.MustRegister(SearchProcessDurationSeconds) + prometheus.MustRegister(NumberIndexedPackages) + prometheus.MustRegister(CursorUpdatesTotal) + prometheus.MustRegister(StorageRequestsTotal) return func(next http.Handler) http.Handler { handler := next diff --git a/packages/http.go b/packages/http.go index 71c5cff7c..a2583cad8 100644 --- a/packages/http.go +++ b/packages/http.go @@ -11,7 +11,10 @@ import ( "go.elastic.co/apm" "go.uber.org/zap" + "github.com/prometheus/client_golang/prometheus" + "github.com/elastic/package-registry/archiver" + "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/util" ) @@ -22,9 +25,11 @@ func ServePackage(w http.ResponseWriter, r *http.Request, p *Package) { if p.RemoteResolver() != nil { p.RemoteResolver().RedirectArtifactsHandler(w, r, p) + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "artifacts"}).Inc() return } serveLocalPackage(w, r, p, p.BasePath) + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "artifacts"}).Inc() } // ServePackageSignature is used by signaturesHandler to serve signatures. @@ -34,9 +39,11 @@ func ServePackageSignature(w http.ResponseWriter, r *http.Request, p *Package) { if p.RemoteResolver() != nil { p.RemoteResolver().RedirectSignaturesHandler(w, r, p) + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "signatures"}).Inc() return } serveLocalPackage(w, r, p, p.BasePath+".sig") + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "signatures"}).Inc() } func serveLocalPackage(w http.ResponseWriter, r *http.Request, p *Package, packagePath string) { @@ -75,6 +82,7 @@ func ServePackageResource(w http.ResponseWriter, r *http.Request, p *Package, pa if p.RemoteResolver() != nil { p.RemoteResolver().RedirectStaticHandler(w, r, p, packageFilePath) + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "remote", "component": "static"}).Inc() return } @@ -111,4 +119,5 @@ func ServePackageResource(w http.ResponseWriter, r *http.Request, p *Package, pa defer f.Close() http.ServeContent(w, r, packageFilePath, stat.ModTime(), f) + metrics.StorageRequestsTotal.With(prometheus.Labels{"location": "local", "component": "static"}).Inc() } diff --git a/storage/indexer.go b/storage/indexer.go index 4486c1e7f..97fb4ac72 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -161,6 +161,7 @@ func (i *Indexer) updateIndex(ctx context.Context) error { defer i.m.Unlock() i.cursor = storageCursor.Current i.packageList = refreshedList + metrics.CursorUpdatesTotal.Inc() metrics.NumberIndexedPackages.Set(float64(len(i.packageList))) return nil } From 42ecdd7568a915981819ebaf07874bc989ec0da9 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 12:58:32 +0200 Subject: [PATCH 20/29] Set service_info metrics as the other metrics --- main.go | 3 ++- metrics/metrics.go | 7 +------ metrics/middleware.go | 2 +- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/main.go b/main.go index 862a1df14..8815e71e0 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( gstorage "cloud.google.com/go/storage" "github.com/gorilla/mux" "github.com/pkg/errors" + "github.com/prometheus/client_golang/prometheus" "github.com/prometheus/client_golang/prometheus/promhttp" "go.elastic.co/apm" "go.elastic.co/apm/module/apmgorilla" @@ -153,7 +154,7 @@ func initMetricsServer(logger *zap.Logger) { if err != nil { hostname = defaultInstanceName } - metrics.SetServiceInfo(version, hostname) + metrics.ServiceInfo.With(prometheus.Labels{"version": version, "instance": hostname}).Set(1) logger.Info("Starting http metrics in " + metricsAddress) go func() { diff --git a/metrics/metrics.go b/metrics/metrics.go index 211e7dd69..f611a1448 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -10,7 +10,7 @@ import ( const metricsNamespace = "epr" -var serviceInfo = prometheus.NewGaugeVec( +var ServiceInfo = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: metricsNamespace, Name: "service_info", @@ -117,8 +117,3 @@ var ( []string{"code", "method", "path"}, ) ) - -// SetServiceInfo is used to set the main information of the service -func SetServiceInfo(version, hostname string) { - serviceInfo.With(prometheus.Labels{"version": version, "instance": hostname}).Set(1) -} diff --git a/metrics/middleware.go b/metrics/middleware.go index 315122e6e..68c32eb14 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -15,7 +15,7 @@ import ( // MetricsMiddleware is a middleware used to measure every request received func MetricsMiddleware() mux.MiddlewareFunc { // Rergister all metrics - prometheus.MustRegister(serviceInfo) + prometheus.MustRegister(ServiceInfo) prometheus.MustRegister(httpInFlightRequests) prometheus.MustRegister(httpRequestsTotal) From 00315215f6f90bcd159d82fcd22baa53f0b6f7f5 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 14:04:16 +0200 Subject: [PATCH 21/29] Set storage indexer metrics Removed epr_search_process_duration_seconds metric in favor of creating specific metrics for storage indexer get and update functions. --- indexer.go | 4 ++++ metrics/metrics.go | 24 ++++++++++++++++-------- metrics/middleware.go | 3 ++- search.go | 3 --- storage/indexer.go | 2 ++ 5 files changed, 24 insertions(+), 12 deletions(-) diff --git a/indexer.go b/indexer.go index 2568b3acb..8a91d547d 100644 --- a/indexer.go +++ b/indexer.go @@ -6,7 +6,9 @@ package main import ( "context" + "time" + "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/packages" ) @@ -32,6 +34,8 @@ func (c CombinedIndexer) Init(ctx context.Context) error { } func (c CombinedIndexer) Get(ctx context.Context, opts *packages.GetOptions) (packages.Packages, error) { + start := time.Now() + defer metrics.StorageIndexerGetDurationSeconds.Observe(time.Since(start).Seconds()) var packages packages.Packages for _, indexer := range c { p, err := indexer.Get(ctx, opts) diff --git a/metrics/metrics.go b/metrics/metrics.go index f611a1448..40ae55dca 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -19,14 +19,6 @@ var ServiceInfo = prometheus.NewGaugeVec( []string{"version", "instance"}, ) -var SearchProcessDurationSeconds = prometheus.NewHistogram( - prometheus.HistogramOpts{ - Namespace: metricsNamespace, - Name: "search_process_duration_seconds", - Help: "A histogram of package search processes.", - }, -) - var ( NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: metricsNamespace, @@ -50,6 +42,22 @@ var ( }, []string{"location", "component"}, ) + + StorageIndexerUpdateIndexDurationSeconds = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "storage_indexer_update_index_duration_seconds", + Help: "A histogram of update index processes run by the indexer", + }, + ) + + StorageIndexerGetDurationSeconds = prometheus.NewHistogram( + prometheus.HistogramOpts{ + Namespace: metricsNamespace, + Name: "storage_indexer_get_duration_seconds", + Help: "A histogram of package search processes.", + }, + ) ) var ( diff --git a/metrics/middleware.go b/metrics/middleware.go index 68c32eb14..c4098d201 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -23,9 +23,10 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(httpRequestSizeBytes) prometheus.MustRegister(httpResponseSizeBytes) - prometheus.MustRegister(SearchProcessDurationSeconds) prometheus.MustRegister(NumberIndexedPackages) prometheus.MustRegister(CursorUpdatesTotal) + prometheus.MustRegister(StorageIndexerGetDurationSeconds) + prometheus.MustRegister(StorageIndexerUpdateIndexDurationSeconds) prometheus.MustRegister(StorageRequestsTotal) return func(next http.Handler) http.Handler { diff --git a/search.go b/search.go index 63a8c14d5..2095e3ac0 100644 --- a/search.go +++ b/search.go @@ -17,7 +17,6 @@ import ( "github.com/pkg/errors" "go.elastic.co/apm" - "github.com/elastic/package-registry/metrics" "github.com/elastic/package-registry/packages" "github.com/elastic/package-registry/util" ) @@ -33,13 +32,11 @@ func searchHandler(indexer Indexer, cacheTime time.Duration) func(w http.Respons Filter: filter, } - start := time.Now() packages, err := indexer.Get(r.Context(), &opts) if err != nil { notFoundError(w, errors.Wrapf(err, "fetching package failed")) return } - metrics.SearchProcessDurationSeconds.Observe(time.Since(start).Seconds()) data, err := getPackageOutput(r.Context(), packages) if err != nil { diff --git a/storage/indexer.go b/storage/indexer.go index 97fb4ac72..353ff79cb 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -129,6 +129,8 @@ func (i *Indexer) watchIndices(ctx context.Context) { func (i *Indexer) updateIndex(ctx context.Context) error { logger := util.Logger() logger.Debug("Update indices") + start := time.Now() + defer metrics.StorageIndexerUpdateIndexDurationSeconds.Observe(time.Since(start).Seconds()) bucketName, rootStoragePath, err := extractBucketNameFromURL(i.options.PackageStorageBucketInternal) if err != nil { From 679bb0787936bbdfe09b71fcd1f4972f3b980b71 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 14:47:31 +0200 Subject: [PATCH 22/29] Add metric to count the erros while updating the index/cursor --- metrics/metrics.go | 8 ++++++++ metrics/middleware.go | 1 + storage/indexer.go | 4 ++++ 3 files changed, 13 insertions(+) diff --git a/metrics/metrics.go b/metrics/metrics.go index 40ae55dca..b8c67e0a2 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -43,6 +43,14 @@ var ( []string{"location", "component"}, ) + StorageIndexerUpdateIndexErrorsTotal = prometheus.NewCounter( + prometheus.CounterOpts{ + Namespace: metricsNamespace, + Name: "storage_indexer_update_index_errors_total", + Help: "A counter for all the update index processes that finished with error", + }, + ) + StorageIndexerUpdateIndexDurationSeconds = prometheus.NewHistogram( prometheus.HistogramOpts{ Namespace: metricsNamespace, diff --git a/metrics/middleware.go b/metrics/middleware.go index c4098d201..1a69d5f1f 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -27,6 +27,7 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(CursorUpdatesTotal) prometheus.MustRegister(StorageIndexerGetDurationSeconds) prometheus.MustRegister(StorageIndexerUpdateIndexDurationSeconds) + prometheus.MustRegister(StorageIndexerUpdateIndexErrorsTotal) prometheus.MustRegister(StorageRequestsTotal) return func(next http.Handler) http.Handler { diff --git a/storage/indexer.go b/storage/indexer.go index 353ff79cb..f7279581b 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -134,11 +134,13 @@ func (i *Indexer) updateIndex(ctx context.Context) error { bucketName, rootStoragePath, err := extractBucketNameFromURL(i.options.PackageStorageBucketInternal) if err != nil { + metrics.StorageIndexerUpdateIndexErrorsTotal.Inc() return errors.Wrapf(err, "can't extract bucket name from URL (url: %s)", i.options.PackageStorageBucketInternal) } storageCursor, err := loadCursor(ctx, i.storageClient, bucketName, rootStoragePath) if err != nil { + metrics.StorageIndexerUpdateIndexErrorsTotal.Inc() return errors.Wrap(err, "can't load latest cursor") } @@ -150,12 +152,14 @@ func (i *Indexer) updateIndex(ctx context.Context) error { anIndex, err := loadSearchIndexAll(ctx, i.storageClient, bucketName, rootStoragePath, *storageCursor) if err != nil { + metrics.StorageIndexerUpdateIndexErrorsTotal.Inc() return errors.Wrapf(err, "can't load the search-index-all index content") } logger.Info("Downloaded new search-index-all index", zap.String("index.packages.size", fmt.Sprintf("%d", len(anIndex.Packages)))) refreshedList, err := i.transformSearchIndexAllToPackages(*anIndex) if err != nil { + metrics.StorageIndexerUpdateIndexErrorsTotal.Inc() return errors.Wrap(err, "can't transform the search-index-all") } From 7d9ff68e3930405ab933f63d5330350bdd9192e8 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 18:44:08 +0200 Subject: [PATCH 23/29] Update Dockerfile to ensure metrics are accesbile from outside --- Dockerfile | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Dockerfile b/Dockerfile index a9325dd94..2fb3c8a09 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,5 +35,8 @@ ENTRYPOINT ["./package-registry"] # Make sure it's accessible from outside the container ENV EPR_ADDRESS=0.0.0.0:8080 +# Make sure metrics endpoint is accessible from outside the container +ENV EPR_METRICS_ADDRESS=0.0.0.0:9000 + HEALTHCHECK --interval=1s --retries=30 CMD curl --silent --fail localhost:8080/health || exit 1 From 14ad680ca9cbf6cfd1a1c1a74d5c0ea2773c169d Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Molins Date: Wed, 6 Jul 2022 18:57:55 +0200 Subject: [PATCH 24/29] Update README.md Co-authored-by: Jaime Soriano Pastor --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0bc2b6ebe..9c456fd42 100644 --- a/README.md +++ b/README.md @@ -237,6 +237,7 @@ It will be listening in the given address. You can read more about this profiler and the available endpoints in the [pprof documentation](https://pkg.go.dev/net/http/pprof). ## Metrics + Package registry is instrumented to expose Prometheus metrics. These metrics are exposed under the `/metrics` endpoint. By default this endpoint runs `localhost:9000`. These metrics can be scraped like: ``` From 4913a1fb3bc115a00cd7fd8caf55456e85bf1c63 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 19:42:19 +0200 Subject: [PATCH 25/29] Handler error of GetPathTemplate --- metrics/middleware.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/metrics/middleware.go b/metrics/middleware.go index 1a69d5f1f..51d4a41a8 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -35,7 +35,10 @@ func MetricsMiddleware() mux.MiddlewareFunc { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { route := mux.CurrentRoute(req) - path, _ := route.GetPathTemplate() + path, err := route.GetPathTemplate() + if err != nil { + path = "unknown" + } labels := prometheus.Labels{"path": path} handler = promhttp.InstrumentHandlerCounter(httpRequestsTotal.MustCurryWith(labels), handler) From bc44972facc2be79ea58ecced86c257cd08e27e1 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 19:43:48 +0200 Subject: [PATCH 26/29] Add function to get hostname --- main.go | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 8815e71e0..6732fb5b2 100644 --- a/main.go +++ b/main.go @@ -149,11 +149,17 @@ func initHttpProf(logger *zap.Logger) { }() } -func initMetricsServer(logger *zap.Logger) { +func getHostname() string { hostname, err := os.Hostname() if err != nil { - hostname = defaultInstanceName + return defaultInstanceName } + return hostname +} + +func initMetricsServer(logger *zap.Logger) { + hostname := getHostname() + metrics.ServiceInfo.With(prometheus.Labels{"version": version, "instance": hostname}).Set(1) logger.Info("Starting http metrics in " + metricsAddress) From 8492d330da2b72a20dfe540231c839f79a21e734 Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 19:57:10 +0200 Subject: [PATCH 27/29] Disable by default metrics endpoint --- Dockerfile | 3 --- README.md | 13 ++++++------- main.go | 11 ++++++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Dockerfile b/Dockerfile index 2fb3c8a09..a9325dd94 100644 --- a/Dockerfile +++ b/Dockerfile @@ -35,8 +35,5 @@ ENTRYPOINT ["./package-registry"] # Make sure it's accessible from outside the container ENV EPR_ADDRESS=0.0.0.0:8080 -# Make sure metrics endpoint is accessible from outside the container -ENV EPR_METRICS_ADDRESS=0.0.0.0:9000 - HEALTHCHECK --interval=1s --retries=30 CMD curl --silent --fail localhost:8080/health || exit 1 diff --git a/README.md b/README.md index 9c456fd42..dfeda39c9 100644 --- a/README.md +++ b/README.md @@ -238,14 +238,13 @@ You can read more about this profiler and the available endpoints in the [pprof ## Metrics -Package registry is instrumented to expose Prometheus metrics. These metrics are exposed under the `/metrics` endpoint. -By default this endpoint runs `localhost:9000`. These metrics can be scraped like: -``` -curl http://localhost:9000 -``` +Package registry can be instrumented to expose Prometheus metrics under `/metrics` endpoint. +By default this endpoint is disabled. + +To enable this instrumentation, the required address (host and port) where this endpoint needs +to run must be set using the parameter `metrics-address` (or the `EPR_METRICS_ADDRESS` environment variable). +For example: -The host and port where this endpoint runs can be configured by means of the parameter `-metrics-address` -(or `EPR_METRICS_ADDRESS` environment variable): ``` package-registry --metrics-address 0.0.0.0:9000 ``` diff --git a/main.go b/main.go index 6732fb5b2..6b8201b2e 100644 --- a/main.go +++ b/main.go @@ -68,7 +68,7 @@ var ( func init() { flag.BoolVar(&printVersionInfo, "version", false, "Print Elastic Package Registry version") flag.StringVar(&address, "address", "localhost:8080", "Address of the package-registry service.") - flag.StringVar(&metricsAddress, "metrics-address", "localhost:9000", "Address to expose the Prometheus metrics.") + flag.StringVar(&metricsAddress, "metrics-address", "", "Address to expose the Prometheus metrics.") flag.StringVar(&tlsCertFile, "tls-cert", "", "Path of the TLS certificate.") flag.StringVar(&tlsKeyFile, "tls-key", "", "Path of the TLS key.") flag.StringVar(&configPath, "config", "config.yml", "Path to the configuration file.") @@ -158,6 +158,10 @@ func getHostname() string { } func initMetricsServer(logger *zap.Logger) { + if metricsAddress == "" { + return + } + hostname := getHostname() metrics.ServiceInfo.With(prometheus.Labels{"version": version, "instance": hostname}).Set(1) @@ -320,7 +324,6 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router staticHandler := staticHandler(indexer, config.CacheTimeCatchAll) router := mux.NewRouter().StrictSlash(true) - router.HandleFunc("/", indexHandlerFunc) router.HandleFunc("/index.json", indexHandlerFunc) router.HandleFunc("/search", searchHandler(indexer, config.CacheTimeSearch)) @@ -332,7 +335,9 @@ func getRouter(logger *zap.Logger, config *Config, indexer Indexer) (*mux.Router router.HandleFunc(packageIndexRouterPath, packageIndexHandler) router.HandleFunc(staticRouterPath, staticHandler) router.Use(util.LoggingMiddleware(logger)) - router.Use(metrics.MetricsMiddleware()) + if metricsAddress != "" { + router.Use(metrics.MetricsMiddleware()) + } router.NotFoundHandler = http.Handler(notFoundHandler(fmt.Errorf("404 page not found"))) return router, nil } From f05309e012e2945890b3c5d485c4678a844636ee Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Wed, 6 Jul 2022 20:22:43 +0200 Subject: [PATCH 28/29] Adjust metrics name related to storage indexer processes --- metrics/metrics.go | 16 ++++++++-------- metrics/middleware.go | 4 ++-- storage/indexer.go | 2 +- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/metrics/metrics.go b/metrics/metrics.go index b8c67e0a2..130bed8c7 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -26,27 +26,27 @@ var ( Help: "A gauge for number of indexed packages", }) - CursorUpdatesTotal = prometheus.NewCounter( + StorageRequestsTotal = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: metricsNamespace, - Name: "cursor_updates_total", - Help: "A counter for updates of the cursor", + Name: "storage_requests_total", + Help: "A counter for requests performed to the storage", }, + []string{"location", "component"}, ) - StorageRequestsTotal = prometheus.NewCounterVec( + StorageIndexerUpdateIndexSuccessTotal = prometheus.NewCounter( prometheus.CounterOpts{ Namespace: metricsNamespace, - Name: "storage_requests_total", - Help: "A counter for requests performed to the storage", + Name: "storage_indexer_update_index_success_total", + Help: "A counter for updates of the cursor", }, - []string{"location", "component"}, ) StorageIndexerUpdateIndexErrorsTotal = prometheus.NewCounter( prometheus.CounterOpts{ Namespace: metricsNamespace, - Name: "storage_indexer_update_index_errors_total", + Name: "storage_indexer_update_index_error_total", Help: "A counter for all the update index processes that finished with error", }, ) diff --git a/metrics/middleware.go b/metrics/middleware.go index 51d4a41a8..3e7d3740d 100644 --- a/metrics/middleware.go +++ b/metrics/middleware.go @@ -24,11 +24,11 @@ func MetricsMiddleware() mux.MiddlewareFunc { prometheus.MustRegister(httpResponseSizeBytes) prometheus.MustRegister(NumberIndexedPackages) - prometheus.MustRegister(CursorUpdatesTotal) + prometheus.MustRegister(StorageRequestsTotal) prometheus.MustRegister(StorageIndexerGetDurationSeconds) prometheus.MustRegister(StorageIndexerUpdateIndexDurationSeconds) + prometheus.MustRegister(StorageIndexerUpdateIndexSuccessTotal) prometheus.MustRegister(StorageIndexerUpdateIndexErrorsTotal) - prometheus.MustRegister(StorageRequestsTotal) return func(next http.Handler) http.Handler { handler := next diff --git a/storage/indexer.go b/storage/indexer.go index f7279581b..909bed777 100644 --- a/storage/indexer.go +++ b/storage/indexer.go @@ -167,7 +167,7 @@ func (i *Indexer) updateIndex(ctx context.Context) error { defer i.m.Unlock() i.cursor = storageCursor.Current i.packageList = refreshedList - metrics.CursorUpdatesTotal.Inc() + metrics.StorageIndexerUpdateIndexSuccessTotal.Inc() metrics.NumberIndexedPackages.Set(float64(len(i.packageList))) return nil } From 584bd06d475d1f669f41f4d45b8cd38c84bfb3de Mon Sep 17 00:00:00 2001 From: Mario Rodriguez Date: Thu, 7 Jul 2022 10:04:58 +0200 Subject: [PATCH 29/29] Updated metric help messages --- metrics/metrics.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/metrics/metrics.go b/metrics/metrics.go index 130bed8c7..aafc95a62 100644 --- a/metrics/metrics.go +++ b/metrics/metrics.go @@ -14,7 +14,7 @@ var ServiceInfo = prometheus.NewGaugeVec( prometheus.GaugeOpts{ Namespace: metricsNamespace, Name: "service_info", - Help: "Version information about this binary", + Help: "Version information about this binary.", }, []string{"version", "instance"}, ) @@ -23,14 +23,14 @@ var ( NumberIndexedPackages = prometheus.NewGauge(prometheus.GaugeOpts{ Namespace: metricsNamespace, Name: "number_indexed_packages", - Help: "A gauge for number of indexed packages", + Help: "A gauge for number of indexed packages.", }) StorageRequestsTotal = prometheus.NewCounterVec( prometheus.CounterOpts{ Namespace: metricsNamespace, Name: "storage_requests_total", - Help: "A counter for requests performed to the storage", + Help: "A counter for requests performed to the storage.", }, []string{"location", "component"}, ) @@ -39,7 +39,7 @@ var ( prometheus.CounterOpts{ Namespace: metricsNamespace, Name: "storage_indexer_update_index_success_total", - Help: "A counter for updates of the cursor", + Help: "A counter for updates of the cursor.", }, ) @@ -47,7 +47,7 @@ var ( prometheus.CounterOpts{ Namespace: metricsNamespace, Name: "storage_indexer_update_index_error_total", - Help: "A counter for all the update index processes that finished with error", + Help: "A counter for all the update index processes that finished with error.", }, ) @@ -55,7 +55,7 @@ var ( prometheus.HistogramOpts{ Namespace: metricsNamespace, Name: "storage_indexer_update_index_duration_seconds", - Help: "A histogram of update index processes run by the indexer", + Help: "A histogram of latencies for update index processes run by the indexer.", }, ) @@ -63,7 +63,7 @@ var ( prometheus.HistogramOpts{ Namespace: metricsNamespace, Name: "storage_indexer_get_duration_seconds", - Help: "A histogram of package search processes.", + Help: "A histogram of latencies for get processes run by the indexer.", }, ) )