From 6bdf8c8594c074f14930ed75794780e81ce5eabc Mon Sep 17 00:00:00 2001 From: Egor Lyutov Date: Wed, 4 Sep 2019 13:59:40 +0500 Subject: [PATCH 1/2] added to service register Address service field --- .gitignore | 1 + README.adoc | 2 ++ config/structs.go | 7 ++--- discovery/consul.go | 9 ++++--- example-config.hcl | 1 + go.mod | 3 ++- go.sum | 63 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 go.sum diff --git a/.gitignore b/.gitignore index 8b736cb..4f0cdcb 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /.idea/ /prometheus-nginxlog-exporter +.history \ No newline at end of file diff --git a/README.adoc b/README.adoc index e24df8d..e69d2e2 100644 --- a/README.adoc +++ b/README.adoc @@ -86,6 +86,7 @@ consul { service { id = "nginx-exporter" name = "nginx-exporter" + address = "192.168.3.1" tags = ["foo", "bar"] } } @@ -129,6 +130,7 @@ consul: service: id: "nginx-exporter" name: "nginx-exporter" + address = "192.168.3.1" tags: ["foo", "bar"] namespaces: diff --git a/config/structs.go b/config/structs.go index fa69da3..72628ca 100644 --- a/config/structs.go +++ b/config/structs.go @@ -46,9 +46,10 @@ type ConsulConfig struct { // ConsulServiceConfig describes the Consul service that the exporter should use type ConsulServiceConfig struct { - ID string - Name string - Tags []string + ID string + Name string + Address string + Tags []string } // StabilityWarnings tests if the Config or any of its sub-objects uses any diff --git a/discovery/consul.go b/discovery/consul.go index bf88bfa..f173c4a 100644 --- a/discovery/consul.go +++ b/discovery/consul.go @@ -44,10 +44,11 @@ func NewConsulRegistrator(cfg *config.Config) (*ConsulRegistrator, error) { // RegisterConsul registers the exporter instance at Consul func (r *ConsulRegistrator) RegisterConsul() error { registration := api.AgentServiceRegistration{ - ID: r.serviceID, - Port: r.config.Listen.Port, - Name: getDefault(r.config.Consul.Service.Name, "nginx-exporter"), - Tags: r.config.Consul.Service.Tags, + ID: r.serviceID, + Address: getDefault(r.config.Consul.Service.Address, r.config.Listen.Address), + Port: r.config.Listen.Port, + Name: getDefault(r.config.Consul.Service.Name, "nginx-exporter"), + Tags: r.config.Consul.Service.Tags, } err := r.client.Agent().ServiceRegister(®istration) diff --git a/example-config.hcl b/example-config.hcl index 23d4c37..eb3bc85 100644 --- a/example-config.hcl +++ b/example-config.hcl @@ -11,6 +11,7 @@ consul { service { id = "nginx-exporter" name = "nginx-exporter" + address = "192.168.3.1" tags = ["foo", "bar"] } } diff --git a/go.mod b/go.mod index 6a65882..be147c2 100644 --- a/go.mod +++ b/go.mod @@ -19,7 +19,8 @@ require ( github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5 github.com/smartystreets/goconvey v0.0.0-20190306220146-200a235640ff // indirect github.com/stretchr/testify v1.3.0 - golang.org/x/sys v0.0.0-20161023150541-c200b10b5d5e // indirect + golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect + golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 // indirect gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect gopkg.in/fsnotify.v1 v1.4.7 // indirect gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..83d9295 --- /dev/null +++ b/go.sum @@ -0,0 +1,63 @@ +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/beorn7/perks v0.0.0-20160229213445-3ac7bf7a47d1 h1:OnJHjoVbY69GG4gclp0ngXfywigLhR6rrgUxmxQRWO4= +github.com/beorn7/perks v0.0.0-20160229213445-3ac7bf7a47d1/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo= +github.com/golang/protobuf v0.0.0-20160614223140-0c1f6d65b5a1 h1:wWfHFGZjHKDnUW3NaVqmOco2gy0/E4mGS7Bvt7eGTig= +github.com/golang/protobuf v0.0.0-20160614223140-0c1f6d65b5a1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY= +github.com/hashicorp/consul v0.0.0-20150921174127-de080672fee9 h1:18FDJdsMcd1+EVmUZkqbQSacgKUFx5HxQ88eZlQeR64= +github.com/hashicorp/consul v0.0.0-20150921174127-de080672fee9/go.mod h1:mFrjN1mfidgJfYP1xrJCF+AfRhr6Eaqhb2+sfyn/OOI= +github.com/hashicorp/go-msgpack v0.5.3 h1:zKjpN5BK/P5lMYrLmBHdBULWbJ0XpYR+7NGzqkZzoD4= +github.com/hashicorp/go-msgpack v0.5.3/go.mod h1:ahLV/dePpqEmjfWmKiqvPkv/twdG7iPBM1vqhUKIvfM= +github.com/hashicorp/golang-lru v0.5.1 h1:0hERBMJE1eitiLkihrMvRVBYAkpHzc/J3QdDN+dAcgU= +github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= +github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= +github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ= +github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI= +github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +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/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.0.0-20160916180340-5636dc67ae77 h1:AsRF8OgdR5mSdUww9ELov+ybjPcwiRfYmQycUkMRR0k= +github.com/prometheus/client_golang v0.0.0-20160916180340-5636dc67ae77/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= +github.com/prometheus/client_model v0.0.0-20150212101744-fa8ad6fec335 h1:0E/5GnGmzoDCtmzTycjGDWW33H0UBmAhR0h+FC8hWLs= +github.com/prometheus/client_model v0.0.0-20150212101744-fa8ad6fec335/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo= +github.com/prometheus/common v0.0.0-20160623151427-4402f4e5ea79 h1:SvLlpwM0nouIVroZjoDw3sMhOvtX2sSbXaUp5BqDjZA= +github.com/prometheus/common v0.0.0-20160623151427-4402f4e5ea79/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro= +github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9 h1:ex32PG6WhE5zviWS08vcXTwX2IkaH9zpeYZZvrmj3/U= +github.com/prometheus/procfs v0.0.0-20160411190841-abf152e5f3e9/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk= +github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5 h1:nIAK+9DnhpSebWeiIqvPr0rqSDC3j9r1I2bp0OJAYVE= +github.com/satyrius/gonx v1.3.1-0.20180709120835-47c52b995fe5/go.mod h1:+r8KNe5d2tjkZU+DfhERo0G6KxkGih+1qYF6tqLHwvk= +github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc= +github.com/smartystreets/goconvey v0.0.0-20190306220146-200a235640ff/go.mod h1:KSQcGKpxUMHk3nbYzs/tIBAM2iDooCn0BmttHOJEbLs= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 h1:Gv7RPwsi3eZ2Fgewe3CBsuOebPwO27PoXzRpJPsvSSM= +golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297 h1:k7pJ2yAPLPgbskkFdhRCsA77k2fySZ1zf2zCjvQCiIM= +golang.org/x/net v0.0.0-20190827160401-ba9fcec4b297/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/sys v0.0.0-20161023150541-c200b10b5d5e/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-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4= +gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ= +gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= +gopkg.in/yaml.v2 v2.0.0-20180104195722-0e4404da7122 h1:SZqIK7cl+kTgWR0pJmEXSmltitRTJZ0fR2rwlRymaEM= +gopkg.in/yaml.v2 v2.0.0-20180104195722-0e4404da7122/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74= From 578a9588c815779a41cddcbca98ef1e3185e9b65 Mon Sep 17 00:00:00 2001 From: Martin Helmich Date: Sun, 26 Apr 2020 11:47:38 +0200 Subject: [PATCH 2/2] Fall back to agent address if not specified --- discovery/consul.go | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/discovery/consul.go b/discovery/consul.go index f173c4a..003c611 100644 --- a/discovery/consul.go +++ b/discovery/consul.go @@ -1,7 +1,9 @@ package discovery -import "github.com/martin-helmich/prometheus-nginxlog-exporter/config" -import "github.com/hashicorp/consul/api" +import ( + "github.com/hashicorp/consul/api" + "github.com/martin-helmich/prometheus-nginxlog-exporter/config" +) // ConsulRegistrator is a helper struct that handles Consul service registration type ConsulRegistrator struct { @@ -45,7 +47,7 @@ func NewConsulRegistrator(cfg *config.Config) (*ConsulRegistrator, error) { func (r *ConsulRegistrator) RegisterConsul() error { registration := api.AgentServiceRegistration{ ID: r.serviceID, - Address: getDefault(r.config.Consul.Service.Address, r.config.Listen.Address), + Address: r.config.Consul.Service.Address, Port: r.config.Listen.Port, Name: getDefault(r.config.Consul.Service.Name, "nginx-exporter"), Tags: r.config.Consul.Service.Tags,