From e4a6e6b5e0fe3d60376bef16282d1286f23bbfaa Mon Sep 17 00:00:00 2001 From: Vladimir Garvardt Date: Fri, 22 Sep 2017 15:44:34 +0200 Subject: [PATCH 1/2] Added MongoDB check --- Gopkg.lock | 33 +++++++++++++++++++++++++++++++++ Gopkg.toml | 38 ++++++++++++++++++++++++++++++++++++++ README.md | 1 + checks/mongo/check.go | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 110 insertions(+) create mode 100644 Gopkg.lock create mode 100644 Gopkg.toml create mode 100644 checks/mongo/check.go diff --git a/Gopkg.lock b/Gopkg.lock new file mode 100644 index 0000000..328a6c3 --- /dev/null +++ b/Gopkg.lock @@ -0,0 +1,33 @@ +# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. + + +[[projects]] + name = "github.com/garyburd/redigo" + packages = ["internal","redis"] + revision = "433969511232c397de61b1442f9fd49ec06ae9ba" + version = "v1.1.0" + +[[projects]] + branch = "master" + name = "github.com/lib/pq" + packages = [".","oid"] + revision = "23da1db4f16d9658a86ae9b717c245fc078f10f1" + +[[projects]] + branch = "master" + name = "github.com/streadway/amqp" + packages = ["."] + revision = "2cbfe40c9341ad63ba23e53013b3ddc7989d801c" + +[[projects]] + branch = "v2" + name = "gopkg.in/mgo.v2" + packages = [".","bson","internal/json","internal/sasl","internal/scram"] + revision = "3f83fa5005286a7fe593b055f0d7771a7dce4655" + +[solve-meta] + analyzer-name = "dep" + analyzer-version = 1 + inputs-digest = "fc205481720ab78f3ed99173e5f33005ac460b7983f35f9fee34e7610d322892" + solver-name = "gps-cdcl" + solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml new file mode 100644 index 0000000..4ec5285 --- /dev/null +++ b/Gopkg.toml @@ -0,0 +1,38 @@ + +# Gopkg.toml example +# +# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md +# for detailed Gopkg.toml documentation. +# +# required = ["github.com/user/thing/cmd/thing"] +# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] +# +# [[constraint]] +# name = "github.com/user/project" +# version = "1.0.0" +# +# [[constraint]] +# name = "github.com/user/project2" +# branch = "dev" +# source = "github.com/myfork/project2" +# +# [[override]] +# name = "github.com/x/y" +# version = "2.4.0" + + +[[constraint]] + name = "github.com/garyburd/redigo" + version = "1.1.0" + +[[constraint]] + branch = "master" + name = "github.com/lib/pq" + +[[constraint]] + branch = "master" + name = "github.com/streadway/amqp" + +[[constraint]] + branch = "v2" + name = "gopkg.in/mgo.v2" diff --git a/README.md b/README.md index f48026b..187dc4f 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ * PostgreSQL * Redis * HTTP + * MongoDB ## Usage diff --git a/checks/mongo/check.go b/checks/mongo/check.go new file mode 100644 index 0000000..082f158 --- /dev/null +++ b/checks/mongo/check.go @@ -0,0 +1,38 @@ +package mongo + +import "gopkg.in/mgo.v2" + +// Config is the MongoDB checker configuration settings container. +type Config struct { + // DSN is the MongoDB instance connection DSN. Required. + DSN string + // LogFunc is the callback function for errors logging during check. + // If not set logging is skipped. + LogFunc func(err error, details string, extra ...interface{}) +} + +// New creates new MongoDB health check that verifies the following: +// - connection establishing +// - doing the ping command +func New(config Config) func() error { + if config.LogFunc == nil { + config.LogFunc = func(err error, details string, extra ...interface{}) {} + } + + return func() error { + session, err := mgo.Dial(config.DSN) + if err != nil { + config.LogFunc(err, "MongoDB health check failed during connect") + return err + } + defer session.Close() + + err = session.Ping() + if err != nil { + config.LogFunc(err, "MongoDB health check failed during ping") + return err + } + + return nil + } +} From d96a4dfcf55b3b15fdf1f31481d7f2333e1ea20f Mon Sep 17 00:00:00 2001 From: Vladimir Garvardt Date: Fri, 22 Sep 2017 15:46:26 +0200 Subject: [PATCH 2/2] Init config defaults outside of checker function --- Gopkg.lock | 33 --------------------------------- Gopkg.toml | 38 -------------------------------------- checks/http/check.go | 14 +++++++------- checks/postgres/check.go | 8 ++++---- checks/rabbitmq/check.go | 39 +++++++++++++++++++-------------------- checks/redis/check.go | 8 ++++---- 6 files changed, 34 insertions(+), 106 deletions(-) delete mode 100644 Gopkg.lock delete mode 100644 Gopkg.toml diff --git a/Gopkg.lock b/Gopkg.lock deleted file mode 100644 index 328a6c3..0000000 --- a/Gopkg.lock +++ /dev/null @@ -1,33 +0,0 @@ -# This file is autogenerated, do not edit; changes may be undone by the next 'dep ensure'. - - -[[projects]] - name = "github.com/garyburd/redigo" - packages = ["internal","redis"] - revision = "433969511232c397de61b1442f9fd49ec06ae9ba" - version = "v1.1.0" - -[[projects]] - branch = "master" - name = "github.com/lib/pq" - packages = [".","oid"] - revision = "23da1db4f16d9658a86ae9b717c245fc078f10f1" - -[[projects]] - branch = "master" - name = "github.com/streadway/amqp" - packages = ["."] - revision = "2cbfe40c9341ad63ba23e53013b3ddc7989d801c" - -[[projects]] - branch = "v2" - name = "gopkg.in/mgo.v2" - packages = [".","bson","internal/json","internal/sasl","internal/scram"] - revision = "3f83fa5005286a7fe593b055f0d7771a7dce4655" - -[solve-meta] - analyzer-name = "dep" - analyzer-version = 1 - inputs-digest = "fc205481720ab78f3ed99173e5f33005ac460b7983f35f9fee34e7610d322892" - solver-name = "gps-cdcl" - solver-version = 1 diff --git a/Gopkg.toml b/Gopkg.toml deleted file mode 100644 index 4ec5285..0000000 --- a/Gopkg.toml +++ /dev/null @@ -1,38 +0,0 @@ - -# Gopkg.toml example -# -# Refer to https://github.com/golang/dep/blob/master/docs/Gopkg.toml.md -# for detailed Gopkg.toml documentation. -# -# required = ["github.com/user/thing/cmd/thing"] -# ignored = ["github.com/user/project/pkgX", "bitbucket.org/user/project/pkgA/pkgY"] -# -# [[constraint]] -# name = "github.com/user/project" -# version = "1.0.0" -# -# [[constraint]] -# name = "github.com/user/project2" -# branch = "dev" -# source = "github.com/myfork/project2" -# -# [[override]] -# name = "github.com/x/y" -# version = "2.4.0" - - -[[constraint]] - name = "github.com/garyburd/redigo" - version = "1.1.0" - -[[constraint]] - branch = "master" - name = "github.com/lib/pq" - -[[constraint]] - branch = "master" - name = "github.com/streadway/amqp" - -[[constraint]] - branch = "v2" - name = "gopkg.in/mgo.v2" diff --git a/checks/http/check.go b/checks/http/check.go index eae91c8..8b96ce2 100644 --- a/checks/http/check.go +++ b/checks/http/check.go @@ -24,15 +24,15 @@ type Config struct { // - getting response status from defined URL // - verifying that status code is less than 500 func New(config Config) func() error { - return func() error { - if config.LogFunc == nil { - config.LogFunc = func(err error, details string, extra ...interface{}) {} - } + if config.LogFunc == nil { + config.LogFunc = func(err error, details string, extra ...interface{}) {} + } - if config.RequestTimeout == 0 { - config.RequestTimeout = time.Second * 5 - } + if config.RequestTimeout == 0 { + config.RequestTimeout = time.Second * 5 + } + return func() error { req, err := http.NewRequest(http.MethodGet, config.URL, nil) if err != nil { config.LogFunc(err, "Creating the request for the health check failed") diff --git a/checks/postgres/check.go b/checks/postgres/check.go index a16dde5..4ece946 100644 --- a/checks/postgres/check.go +++ b/checks/postgres/check.go @@ -32,11 +32,11 @@ type Config struct { // - selecting inserted row // - deleting inserted row func New(config Config) func() error { - return func() error { - if config.LogFunc == nil { - config.LogFunc = func(err error, details string, extra ...interface{}) {} - } + if config.LogFunc == nil { + config.LogFunc = func(err error, details string, extra ...interface{}) {} + } + return func() error { db, err := sql.Open("postgres", config.DSN) if err != nil { config.LogFunc(err, "PostgreSQL health check failed during connect") diff --git a/checks/rabbitmq/check.go b/checks/rabbitmq/check.go index a7d80f3..09403f3 100644 --- a/checks/rabbitmq/check.go +++ b/checks/rabbitmq/check.go @@ -41,32 +41,31 @@ type Config struct { // - publishing a message to the exchange with the defined routing key // - consuming published message func New(config Config) func() error { - return func() error { - if config.LogFunc == nil { - config.LogFunc = func(err error, details string, extra ...interface{}) {} - } + if config.LogFunc == nil { + config.LogFunc = func(err error, details string, extra ...interface{}) {} + } - if config.Exchange == "" { - config.Exchange = defaultExchange - } + if config.Exchange == "" { + config.Exchange = defaultExchange + } - if config.RoutingKey == "" { - host, err := os.Hostname() - if nil != err { - config.LogFunc(err, "RabbitMQ health check failed on settings default value for unset routing key") - return err - } - config.RoutingKey = host + if config.RoutingKey == "" { + host, err := os.Hostname() + if nil != err { + config.RoutingKey = "-unknown-" } + config.RoutingKey = host + } - if config.Queue == "" { - config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey) - } + if config.Queue == "" { + config.Queue = fmt.Sprintf("%s.%s", config.Exchange, config.RoutingKey) + } - if config.ConsumeTimeout == 0 { - config.ConsumeTimeout = time.Second * 3 - } + if config.ConsumeTimeout == 0 { + config.ConsumeTimeout = time.Second * 3 + } + return func() error { conn, err := amqp.Dial(config.DSN) if err != nil { config.LogFunc(err, "RabbitMQ health check failed on dial phase") diff --git a/checks/redis/check.go b/checks/redis/check.go index 9fbf95b..bd4d024 100644 --- a/checks/redis/check.go +++ b/checks/redis/check.go @@ -20,11 +20,11 @@ type Config struct { // - connection establishing // - doing the PING command and verifying the response func New(config Config) func() error { - return func() error { - if config.LogFunc == nil { - config.LogFunc = func(err error, details string, extra ...interface{}) {} - } + if config.LogFunc == nil { + config.LogFunc = func(err error, details string, extra ...interface{}) {} + } + return func() error { pool := &redis.Pool{ MaxIdle: 1, IdleTimeout: 10 * time.Second,