From 742235166bf8459679846c54642ca0bc98201484 Mon Sep 17 00:00:00 2001 From: Johannes Edmeier Date: Sun, 26 Nov 2023 18:38:35 +0100 Subject: [PATCH] refa: use discovery-kit-sdk --- extinstance/instance_discovery.go | 61 +++++++++++++++++-------------- go.mod | 16 +++----- go.sum | 26 +++++++++++-- main.go | 26 ++----------- 4 files changed, 66 insertions(+), 63 deletions(-) diff --git a/extinstance/instance_discovery.go b/extinstance/instance_discovery.go index 23f4277..6beb7bf 100644 --- a/extinstance/instance_discovery.go +++ b/extinstance/instance_discovery.go @@ -4,34 +4,43 @@ package extinstance import ( + "context" "github.com/steadybit/discovery-kit/go/discovery_kit_api" "github.com/steadybit/discovery-kit/go/discovery_kit_commons" + "github.com/steadybit/discovery-kit/go/discovery_kit_sdk" "github.com/steadybit/extension-kit/extbuild" - "github.com/steadybit/extension-kit/exthttp" "github.com/steadybit/extension-kit/extutil" "github.com/steadybit/extension-prometheus/config" + "time" ) -func RegisterInstanceDiscoveryHandlers() { - exthttp.RegisterHttpHandler("/prometheus/instance/discovery", exthttp.GetterAsHandler(getPrometheusInstanceDiscoveryDescription)) - exthttp.RegisterHttpHandler("/prometheus/instance/discovery/target-description", exthttp.GetterAsHandler(getPrometheusInstanceTargetDescription)) - exthttp.RegisterHttpHandler("/prometheus/instance/discovery/attribute-descriptions", exthttp.GetterAsHandler(getPrometheusInstanceAttributeDescriptions)) - exthttp.RegisterHttpHandler("/prometheus/instance/discovery/discovered-targets", exthttp.GetterAsHandler(getPrometheusInstanceDiscoveryResults)) +type instanceDiscovery struct { } -func getPrometheusInstanceDiscoveryDescription() discovery_kit_api.DiscoveryDescription { +var ( + _ discovery_kit_sdk.TargetDescriber = (*instanceDiscovery)(nil) + _ discovery_kit_sdk.AttributeDescriber = (*instanceDiscovery)(nil) +) + +func NewInstanceDiscovery() discovery_kit_sdk.TargetDiscovery { + discovery := &instanceDiscovery{} + return discovery_kit_sdk.NewCachedTargetDiscovery(discovery, + discovery_kit_sdk.WithRefreshTargetsNow(), + discovery_kit_sdk.WithRefreshTargetsInterval(context.Background(), 30*time.Second), + ) +} + +func (d *instanceDiscovery) Describe() discovery_kit_api.DiscoveryDescription { return discovery_kit_api.DiscoveryDescription{ Id: PrometheusInstanceTargetId, RestrictTo: extutil.Ptr(discovery_kit_api.LEADER), Discover: discovery_kit_api.DescribingEndpointReferenceWithCallInterval{ - Method: "GET", - Path: "/prometheus/instance/discovery/discovered-targets", CallInterval: extutil.Ptr("30s"), }, } } -func getPrometheusInstanceTargetDescription() discovery_kit_api.TargetDescription { +func (d *instanceDiscovery) DescribeTarget() discovery_kit_api.TargetDescription { return discovery_kit_api.TargetDescription{ Id: PrometheusInstanceTargetId, Label: discovery_kit_api.PluralLabel{One: "Prometheus instance", Other: "Prometheus instances"}, @@ -53,27 +62,25 @@ func getPrometheusInstanceTargetDescription() discovery_kit_api.TargetDescriptio } } -func getPrometheusInstanceAttributeDescriptions() discovery_kit_api.AttributeDescriptions { - return discovery_kit_api.AttributeDescriptions{ - Attributes: []discovery_kit_api.AttributeDescription{ - { - Attribute: "prometheus.instance.name", - Label: discovery_kit_api.PluralLabel{ - One: "Prometheus instance name", - Other: "Prometheus instance names", - }, - }, { - Attribute: "prometheus.instance.url", - Label: discovery_kit_api.PluralLabel{ - One: "Prometheus instance URL", - Other: "Prometheus instance URLs", - }, +func (d *instanceDiscovery) DescribeAttributes() []discovery_kit_api.AttributeDescription { + return []discovery_kit_api.AttributeDescription{ + { + Attribute: "prometheus.instance.name", + Label: discovery_kit_api.PluralLabel{ + One: "Prometheus instance name", + Other: "Prometheus instance names", + }, + }, { + Attribute: "prometheus.instance.url", + Label: discovery_kit_api.PluralLabel{ + One: "Prometheus instance URL", + Other: "Prometheus instance URLs", }, }, } } -func getPrometheusInstanceDiscoveryResults() discovery_kit_api.DiscoveredTargets { +func (d *instanceDiscovery) DiscoverTargets(_ context.Context) ([]discovery_kit_api.Target, error) { targets := make([]discovery_kit_api.Target, len(Instances)) for i, instance := range Instances { @@ -88,5 +95,5 @@ func getPrometheusInstanceDiscoveryResults() discovery_kit_api.DiscoveredTargets } } - return discovery_kit_api.DiscoveredTargets{Targets: discovery_kit_commons.ApplyAttributeExcludes(targets, config.Config.DiscoveryAttributesExcludesInstance)} + return discovery_kit_commons.ApplyAttributeExcludes(targets, config.Config.DiscoveryAttributesExcludesInstance), nil } diff --git a/go.mod b/go.mod index 7ba5b0a..3dd8b9a 100644 --- a/go.mod +++ b/go.mod @@ -6,21 +6,16 @@ module github.com/steadybit/extension-prometheus go 1.21 require ( + github.com/kelseyhightower/envconfig v1.4.0 github.com/prometheus/client_golang v1.17.0 github.com/prometheus/common v0.45.0 github.com/rs/zerolog v1.31.0 github.com/steadybit/action-kit/go/action_kit_api/v2 v2.8.0 github.com/steadybit/action-kit/go/action_kit_sdk v1.1.8 github.com/steadybit/discovery-kit/go/discovery_kit_api v1.5.0 - github.com/steadybit/extension-kit v1.8.11 -) - -// Workaround for https://github.com/testcontainers/testcontainers-go/issues/1359 -replace github.com/docker/docker v23.0.5+incompatible => github.com/docker/docker v23.0.7-0.20230730020554-801e90549aac+incompatible - -require ( - github.com/kelseyhightower/envconfig v1.4.0 github.com/steadybit/discovery-kit/go/discovery_kit_commons v0.1.0 + github.com/steadybit/discovery-kit/go/discovery_kit_sdk v0.0.0-20231124155843-e1eb7f18dbd1 + github.com/steadybit/extension-kit v1.8.11 github.com/stretchr/testify v1.8.4 github.com/testcontainers/testcontainers-go v0.26.0 ) @@ -113,8 +108,8 @@ require ( github.com/shirou/gopsutil/v3 v3.23.10 // indirect github.com/shoenig/go-m1cpu v0.1.6 // indirect github.com/sirupsen/logrus v1.9.3 // indirect - github.com/tdewolff/minify/v2 v2.20.6 // indirect - github.com/tdewolff/parse/v2 v2.7.4 // indirect + github.com/tdewolff/minify/v2 v2.20.7 // indirect + github.com/tdewolff/parse/v2 v2.7.5 // indirect github.com/tklauser/go-sysconf v0.3.12 // indirect github.com/tklauser/numcpus v0.6.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect @@ -125,6 +120,7 @@ require ( github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/yosssi/ace v0.0.5 // indirect github.com/yusufpapurcu/wmi v1.2.3 // indirect + github.com/zmwangx/debounce v1.0.0 // indirect golang.org/x/arch v0.6.0 // indirect golang.org/x/crypto v0.15.0 // indirect golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect diff --git a/go.sum b/go.sum index 139e9fa..0a1c221 100644 --- a/go.sum +++ b/go.sum @@ -107,6 +107,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= github.com/go-playground/validator/v10 v10.16.0 h1:x+plE831WK4vaKHO/jpgUGsvLKIqRRkz6M78GuJAfGE= github.com/go-playground/validator/v10 v10.16.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU= +github.com/go-resty/resty/v2 v2.10.0 h1:Qla4W/+TMmv0fOeeRqzEpXPLfTUnR5HZ1+lGs+CkiCo= +github.com/go-resty/resty/v2 v2.10.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A= github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/gobwas/glob v0.2.3 h1:A4xDbljILXROh+kObIiy5kIaPYD8e96x1tgBhUI5J+Y= @@ -135,6 +137,8 @@ github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17 github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4= github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= +github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= github.com/gorilla/css v1.0.1 h1:ntNaBIghp6JmvWnxbZKANoLyuXTPZ4cAMlo6RyhlbO8= github.com/gorilla/css v1.0.1/go.mod h1:BvnYkspnSzMmwRK+b8/xgNPLiIuNZr6vbZBTPQ2A3b0= github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= @@ -153,6 +157,8 @@ github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2E github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/kataras/blocks v0.0.8 h1:MrpVhoFTCR2v1iOOfGng5VJSILKeZZI+7NGfxEh3SUM= github.com/kataras/blocks v0.0.8/go.mod h1:9Jm5zx6BB+06NwA+OhTbHW1xkMOYxahnqTN5DveZ2Yg= @@ -286,6 +292,10 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/smartystreets/assertions v1.13.1 h1:Ef7KhSmjZcK6AVf9YbJdvPYG9avaF0ZxudX+ThRdWfU= +github.com/smartystreets/assertions v1.13.1/go.mod h1:cXr/IwVfSo/RbCSPhoAPv73p3hlSdrBH/b3SdnW/LMY= +github.com/smartystreets/goconvey v1.8.0 h1:Oi49ha/2MURE0WexF052Z0m+BNSGirfjg5RL+JXWq3w= +github.com/smartystreets/goconvey v1.8.0/go.mod h1:EdX8jtrTIj26jmjCOVNMVSIYAtgexqXKHOXW2Dx9JLg= github.com/spkg/bom v0.0.0-20160624110644-59b7046e48ad/go.mod h1:qLr4V1qq6nMqFKkMo8ZTx3f+BZEkzsRUY10Xsm2mwU0= github.com/steadybit/action-kit/go/action_kit_api/v2 v2.8.0 h1:mo+wf8we5LVnxgUuJvvN/DDVA42uZt6RLKCBJtkWL/4= github.com/steadybit/action-kit/go/action_kit_api/v2 v2.8.0/go.mod h1:q9fyUhgf3LnhMRCzZ9tBjj8w2le5SUJi0vHw6A3PIKw= @@ -295,11 +305,17 @@ github.com/steadybit/discovery-kit/go/discovery_kit_api v1.5.0 h1:kRDryl2fvjXiaV github.com/steadybit/discovery-kit/go/discovery_kit_api v1.5.0/go.mod h1:vAMfjAw7zpu55b8fdl0QRa4NszbuK4Pg8/nj1iHIO68= github.com/steadybit/discovery-kit/go/discovery_kit_commons v0.1.0 h1:g4PjxmmUdHKJQ4FJ9evrIHKm5qgN2r3Rd8AJjiZZ2kI= github.com/steadybit/discovery-kit/go/discovery_kit_commons v0.1.0/go.mod h1:Gg/S2hwxxZsYoXaFvyLkpqHmir2GVrN8nE7FWki9afQ= +github.com/steadybit/discovery-kit/go/discovery_kit_sdk v0.0.0-20231124155843-e1eb7f18dbd1 h1:ADr2i/62VluOGUAC71qljjZDJBLvdeN/Oy+4sYIqUs4= +github.com/steadybit/discovery-kit/go/discovery_kit_sdk v0.0.0-20231124155843-e1eb7f18dbd1/go.mod h1:Wef4foA1tlNP8oMKeGRWDp8iF7lipH6DLtYF6Dfc1uw= +github.com/steadybit/discovery-kit/go/discovery_kit_test v1.1.0 h1:hUfIZGRLpPC+ZACzmo8fWV9UOOmFQWA1egmlqO7LLKg= +github.com/steadybit/discovery-kit/go/discovery_kit_test v1.1.0/go.mod h1:DvvMXdE+SjOLDhTBhYkFupnEGQ0ZepP1ONR1xUIzqO4= github.com/steadybit/extension-kit v1.8.11 h1:7X76rOpC8DXiS6R83rr70eLO09sDu0DHOzSmGEIBCeo= github.com/steadybit/extension-kit v1.8.11/go.mod h1:vT1jG8WMZuImhNDCoLX19ckmCuPOmX/HXd2YegTcP/Y= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= +github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0= +github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0= 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.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= @@ -310,10 +326,10 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww= -github.com/tdewolff/minify/v2 v2.20.6 h1:R4+Iw1ZqJxrqH52WWHtCpukMuhmO/EasY8YlDiSxphw= -github.com/tdewolff/minify/v2 v2.20.6/go.mod h1:9t0EY9xySGt1vrP8iscmJfywQwDCQyQBYN6ge+9GwP0= -github.com/tdewolff/parse/v2 v2.7.4 h1:zrUn2CFg9+5llbUZcsycctFlNRyV1D5gFBZRxuGzdzk= -github.com/tdewolff/parse/v2 v2.7.4/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= +github.com/tdewolff/minify/v2 v2.20.7 h1:NUkuzJ9dvQUNJjSdmmrfELa/ZpnMdyMR/ZKU2bw7N/E= +github.com/tdewolff/minify/v2 v2.20.7/go.mod h1:bj2NpP3zoUhsPzE4oM4JYwuUyVCU/uMaCYZ6/riEjIo= +github.com/tdewolff/parse/v2 v2.7.5 h1:RdcN3Ja6zAMSvnxxO047xRoWexX3RrXKi3H6EQHzXto= +github.com/tdewolff/parse/v2 v2.7.5/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52 h1:gAQliwn+zJrkjAHVcBEYW/RFvd2St4yYimisvozAYlA= github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= github.com/testcontainers/testcontainers-go v0.26.0 h1:uqcYdoOHBy1ca7gKODfBd9uTHVK3a7UL848z09MVZ0c= @@ -356,6 +372,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yusufpapurcu/wmi v1.2.3 h1:E1ctvB7uKFMOJw3fdOW32DwGE9I7t++CRUEMKvFoFiw= github.com/yusufpapurcu/wmi v1.2.3/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= +github.com/zmwangx/debounce v1.0.0 h1:Dyf+WfLESjc2bqFKHgI1dZTW9oh6CJm8SBDkhXrwLB4= +github.com/zmwangx/debounce v1.0.0/go.mod h1:U+/QHt+bSMdUh8XKOb6U+MQV5Ew4eS8M3ua5WJ7Ns6I= golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8= golang.org/x/arch v0.6.0 h1:S0JTfE48HbRj80+4tbvZDYsJ3tGv6BUU3XxyZ7CirAc= golang.org/x/arch v0.6.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= diff --git a/main.go b/main.go index b7825d2..35f89c2 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "github.com/steadybit/action-kit/go/action_kit_api/v2" "github.com/steadybit/action-kit/go/action_kit_sdk" "github.com/steadybit/discovery-kit/go/discovery_kit_api" + "github.com/steadybit/discovery-kit/go/discovery_kit_sdk" "github.com/steadybit/extension-kit/extbuild" "github.com/steadybit/extension-kit/exthealth" "github.com/steadybit/extension-kit/exthttp" @@ -33,7 +34,7 @@ func main() { config.ValidateConfiguration() exthttp.RegisterHttpHandler("/", exthttp.GetterAsHandler(getExtensionList)) - extinstance.RegisterInstanceDiscoveryHandlers() + discovery_kit_sdk.Register(extinstance.NewInstanceDiscovery()) action_kit_sdk.RegisterAction(extmetric.NewMetricCheckAction()) action_kit_sdk.InstallSignalHandler() @@ -51,26 +52,7 @@ type ExtensionListResponse struct { func getExtensionList() ExtensionListResponse { return ExtensionListResponse{ - ActionList: action_kit_sdk.GetActionList(), - DiscoveryList: discovery_kit_api.DiscoveryList{ - Discoveries: []discovery_kit_api.DescribingEndpointReference{ - { - Method: "GET", - Path: "/prometheus/instance/discovery", - }, - }, - TargetTypes: []discovery_kit_api.DescribingEndpointReference{ - { - Method: "GET", - Path: "/prometheus/instance/discovery/target-description", - }, - }, - TargetAttributes: []discovery_kit_api.DescribingEndpointReference{ - { - Method: "GET", - Path: "/prometheus/instance/discovery/attribute-descriptions", - }, - }, - }, + ActionList: action_kit_sdk.GetActionList(), + DiscoveryList: discovery_kit_sdk.GetDiscoveryList(), } }