From b91a6cec5c9b5d3f330caa40b21e1679636b3e10 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 10 Jan 2022 22:24:20 +0100 Subject: [PATCH 01/23] MF-1308 - Use IETF Health Check standard Signed-off-by: Manuel Imperiale --- auth/api/http/transport.go | 3 +- bootstrap/api/transport.go | 2 +- certs/api/transport.go | 2 +- coap/api/transport.go | 2 +- consumers/notifiers/api/transport.go | 2 +- consumers/writers/api/transport.go | 3 +- health.go | 49 ++++++++++++++++++++++++++++ http/api/transport.go | 2 +- lora/api/api.go | 2 +- opcua/api/transport.go | 2 +- pkg/sdk/go/version.go | 12 +++---- provision/api/transport.go | 2 +- readers/api/transport.go | 2 +- things/api/things/http/transport.go | 2 +- twins/api/http/transport.go | 2 +- users/api/transport.go | 2 +- version.go | 31 ------------------ 17 files changed, 71 insertions(+), 51 deletions(-) create mode 100644 health.go delete mode 100644 version.go diff --git a/auth/api/http/transport.go b/auth/api/http/transport.go index 046988895a..ed87ee6fbb 100644 --- a/auth/api/http/transport.go +++ b/auth/api/http/transport.go @@ -15,12 +15,13 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) +// MakeHandler returns a HTTP handler for API endpoints. func MakeHandler(svc auth.Service, tracer opentracing.Tracer) http.Handler { mux := bone.New() mux = keys.MakeHandler(svc, mux, tracer) mux = groups.MakeHandler(svc, mux, tracer) mux = policies.MakeHandler(svc, mux, tracer) - mux.GetFunc("/version", mainflux.Version("auth")) + mux.GetFunc("/health", mainflux.Health("auth")) mux.Handle("/metrics", promhttp.Handler()) return mux } diff --git a/bootstrap/api/transport.go b/bootstrap/api/transport.go index b16834edd4..69feac18a5 100644 --- a/bootstrap/api/transport.go +++ b/bootstrap/api/transport.go @@ -100,7 +100,7 @@ func MakeHandler(svc bootstrap.Service, reader bootstrap.ConfigReader) http.Hand encodeResponse, opts...)) - r.GetFunc("/version", mainflux.Version("bootstrap")) + r.GetFunc("/health", mainflux.Health("bootstrap")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/certs/api/transport.go b/certs/api/transport.go index 1fdf8ea6a6..935b7f4216 100644 --- a/certs/api/transport.go +++ b/certs/api/transport.go @@ -68,7 +68,7 @@ func MakeHandler(svc certs.Service) http.Handler { )) r.Handle("/metrics", promhttp.Handler()) - r.GetFunc("/version", mainflux.Version("certs")) + r.GetFunc("/health", mainflux.Health("certs")) return r } diff --git a/coap/api/transport.go b/coap/api/transport.go index 25a1454713..d76e31c2d4 100644 --- a/coap/api/transport.go +++ b/coap/api/transport.go @@ -43,7 +43,7 @@ var ( //MakeHTTPHandler creates handler for version endpoint. func MakeHTTPHandler() http.Handler { b := bone.New() - b.GetFunc("/version", mainflux.Version(protocol)) + b.GetFunc("/health", mainflux.Health(protocol)) b.Handle("/metrics", promhttp.Handler()) return b diff --git a/consumers/notifiers/api/transport.go b/consumers/notifiers/api/transport.go index 979fef0c1f..d9638758ee 100644 --- a/consumers/notifiers/api/transport.go +++ b/consumers/notifiers/api/transport.go @@ -59,7 +59,7 @@ func MakeHandler(svc notifiers.Service, tracer opentracing.Tracer) http.Handler opts..., )) - mux.GetFunc("/version", mainflux.Version("notifier")) + mux.GetFunc("/health", mainflux.Health("notifier")) mux.Handle("/metrics", promhttp.Handler()) return mux diff --git a/consumers/writers/api/transport.go b/consumers/writers/api/transport.go index df5915e5a8..f88e27080d 100644 --- a/consumers/writers/api/transport.go +++ b/consumers/writers/api/transport.go @@ -1,6 +1,7 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test // +build !test package api @@ -16,7 +17,7 @@ import ( // MakeHandler returns a HTTP API handler with version and metrics. func MakeHandler(svcName string) http.Handler { r := bone.New() - r.GetFunc("/version", mainflux.Version(svcName)) + r.GetFunc("/health", mainflux.Health(svcName)) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/health.go b/health.go new file mode 100644 index 0000000000..4addfec3aa --- /dev/null +++ b/health.go @@ -0,0 +1,49 @@ +// Copyright (c) Mainflux +// SPDX-License-Identifier: Apache-2.0 + +package mainflux + +import ( + "encoding/json" + "net/http" + + "github.com/nelkinda/http-go/header" +) + +const ( + version string = "0.12.1" + contentType string = "application/json" + svcStatus string = "pass" +) + +// HealthInfo contains version endpoint response. +type HealthInfo struct { + // Status contains service status. + Status string `json:"status"` + + // Version contains service current version. + Version string `json:"version"` + + // Description contains service description. + Description string `json:"description"` +} + +// Health exposes an HTTP handler for retrieving service health. +func Health(service string) http.HandlerFunc { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Add(header.ContentType, contentType) + if r.Method != http.MethodGet && r.Method != http.MethodHead { + w.WriteHeader(http.StatusMethodNotAllowed) + return + } + + res := HealthInfo{ + Status: svcStatus, + Description: service, + Version: version, + } + + w.WriteHeader(http.StatusOK) + _ = json.NewEncoder(w).Encode(res) + }) +} diff --git a/http/api/transport.go b/http/api/transport.go index 916da603e8..7654436a99 100644 --- a/http/api/transport.go +++ b/http/api/transport.go @@ -57,7 +57,7 @@ func MakeHandler(svc adapter.Service, tracer opentracing.Tracer) http.Handler { opts..., )) - r.GetFunc("/version", mainflux.Version("http")) + r.GetFunc("/health", mainflux.Health("http")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/lora/api/api.go b/lora/api/api.go index 297b1bfdb3..c6082677df 100644 --- a/lora/api/api.go +++ b/lora/api/api.go @@ -14,7 +14,7 @@ import ( // MakeHandler returns a HTTP handler for API endpoints. func MakeHandler() http.Handler { r := bone.New() - r.GetFunc("/version", mainflux.Version("lora-adapter")) + r.GetFunc("/health", mainflux.Health("lora-adapter")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/opcua/api/transport.go b/opcua/api/transport.go index 733a7cfd87..724afdd437 100644 --- a/opcua/api/transport.go +++ b/opcua/api/transport.go @@ -43,7 +43,7 @@ func MakeHandler(svc opcua.Service) http.Handler { opts..., )) - r.GetFunc("/version", mainflux.Version("opcua-adapter")) + r.GetFunc("/health", mainflux.Health("opcua-adapter")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/pkg/sdk/go/version.go b/pkg/sdk/go/version.go index fcf1c3d45d..1951c37c0e 100644 --- a/pkg/sdk/go/version.go +++ b/pkg/sdk/go/version.go @@ -12,12 +12,12 @@ import ( "github.com/mainflux/mainflux/pkg/errors" ) -type version struct { - Value string `json:"version"` +type health struct { + Version string `json:"version"` } func (sdk mfSDK) Version() (string, error) { - url := fmt.Sprintf("%s/version", sdk.thingsURL) + url := fmt.Sprintf("%s/health", sdk.thingsURL) resp, err := sdk.client.Get(url) if err != nil { @@ -34,10 +34,10 @@ func (sdk mfSDK) Version() (string, error) { return "", errors.Wrap(ErrFetchVersion, errors.New(resp.Status)) } - var ver version - if err := json.Unmarshal(body, &ver); err != nil { + var h health + if err := json.Unmarshal(body, &h); err != nil { return "", err } - return ver.Value, nil + return h.Version, nil } diff --git a/provision/api/transport.go b/provision/api/transport.go index 3fcc1987ff..e13db665d2 100644 --- a/provision/api/transport.go +++ b/provision/api/transport.go @@ -47,7 +47,7 @@ func MakeHandler(svc provision.Service) http.Handler { )) r.Handle("/metrics", promhttp.Handler()) - r.GetFunc("/version", mainflux.Version("provision")) + r.GetFunc("/health", mainflux.Health("provision")) return r } diff --git a/readers/api/transport.go b/readers/api/transport.go index 0f85330ab7..07452b6734 100644 --- a/readers/api/transport.go +++ b/readers/api/transport.go @@ -62,7 +62,7 @@ func MakeHandler(svc readers.MessageRepository, tc mainflux.ThingsServiceClient, opts..., )) - mux.GetFunc("/version", mainflux.Version(svcName)) + mux.GetFunc("/health", mainflux.Health(svcName)) mux.Handle("/metrics", promhttp.Handler()) return mux diff --git a/things/api/things/http/transport.go b/things/api/things/http/transport.go index 2b9ae59358..fcd2baf554 100644 --- a/things/api/things/http/transport.go +++ b/things/api/things/http/transport.go @@ -198,7 +198,7 @@ func MakeHandler(tracer opentracing.Tracer, svc things.Service) http.Handler { opts..., )) - r.GetFunc("/version", mainflux.Version("things")) + r.GetFunc("/health", mainflux.Health("things")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/twins/api/http/transport.go b/twins/api/http/transport.go index 7e0b66f6e9..de5c607e55 100644 --- a/twins/api/http/transport.go +++ b/twins/api/http/transport.go @@ -81,7 +81,7 @@ func MakeHandler(tracer opentracing.Tracer, svc twins.Service) http.Handler { opts..., )) - r.GetFunc("/version", mainflux.Version("twins")) + r.GetFunc("/health", mainflux.Health("twins")) r.Handle("/metrics", promhttp.Handler()) return r diff --git a/users/api/transport.go b/users/api/transport.go index 90efeedc3c..0b3cc88211 100644 --- a/users/api/transport.go +++ b/users/api/transport.go @@ -109,7 +109,7 @@ func MakeHandler(svc users.Service, tracer opentracing.Tracer) http.Handler { opts..., )) - mux.GetFunc("/version", mainflux.Version("users")) + mux.GetFunc("/health", mainflux.Health("users")) mux.Handle("/metrics", promhttp.Handler()) return mux diff --git a/version.go b/version.go deleted file mode 100644 index 3f16d52066..0000000000 --- a/version.go +++ /dev/null @@ -1,31 +0,0 @@ -// Copyright (c) Mainflux -// SPDX-License-Identifier: Apache-2.0 - -package mainflux - -import ( - "encoding/json" - "net/http" -) - -const version string = "0.12.1" - -// VersionInfo contains version endpoint response. -type VersionInfo struct { - // Service contains service name. - Service string `json:"service"` - - // Version contains service current version value. - Version string `json:"version"` -} - -// Version exposes an HTTP handler for retrieving service version. -func Version(service string) http.HandlerFunc { - return http.HandlerFunc(func(rw http.ResponseWriter, _ *http.Request) { - res := VersionInfo{service, version} - - data, _ := json.Marshal(res) - - rw.Write(data) - }) -} From a12d4fc15565facea9b23c70158b52e870d2c31f Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 10 Jan 2022 22:28:47 +0100 Subject: [PATCH 02/23] Add nginx health endpoint Signed-off-by: Manuel Imperiale --- docker/nginx/nginx-key.conf | 2 +- docker/nginx/nginx-x509.conf | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docker/nginx/nginx-key.conf b/docker/nginx/nginx-key.conf index 2238c3afeb..61d6f897c5 100644 --- a/docker/nginx/nginx-key.conf +++ b/docker/nginx/nginx-key.conf @@ -89,7 +89,7 @@ http { proxy_pass http://auth:${MF_AUTH_HTTP_PORT}; } - location /version { + location /health { include snippets/proxy-headers.conf; proxy_pass http://things:${MF_THINGS_HTTP_PORT}; } diff --git a/docker/nginx/nginx-x509.conf b/docker/nginx/nginx-x509.conf index e3e4ea3dca..89e95a60cc 100644 --- a/docker/nginx/nginx-x509.conf +++ b/docker/nginx/nginx-x509.conf @@ -97,7 +97,7 @@ http { proxy_pass http://auth:${MF_AUTH_HTTP_PORT}; } - location /version { + location /health { include snippets/proxy-headers.conf; proxy_pass http://things:${MF_THINGS_HTTP_PORT}; } From c7f534baa1876ac74034fd53f36d48f8ebc9e3a7 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Tue, 11 Jan 2022 10:06:40 +0100 Subject: [PATCH 03/23] Rm github.com/nelkinda dependency Signed-off-by: Manuel Imperiale --- bootstrap/api/logging.go | 2 -- bootstrap/api/metrics.go | 2 -- coap/api/logging.go | 2 -- coap/api/metrics.go | 2 -- consumers/writers/api/logging.go | 2 -- consumers/writers/api/transport.go | 3 --- health.go | 11 +++++------ http/api/logging.go | 2 -- http/api/metrics.go | 2 -- readers/api/logging.go | 2 -- readers/api/metrics.go | 2 -- things/api/logging.go | 2 -- things/api/metrics.go | 2 -- twins/api/logging.go | 2 -- twins/api/metrics.go | 2 -- 15 files changed, 5 insertions(+), 35 deletions(-) diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index b99028ef89..7668c511b3 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/bootstrap/api/metrics.go b/bootstrap/api/metrics.go index 79c47c1fb1..0b507cbec8 100644 --- a/bootstrap/api/metrics.go +++ b/bootstrap/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/coap/api/logging.go b/coap/api/logging.go index 3ba3b1c469..4952dad861 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/coap/api/metrics.go b/coap/api/metrics.go index 47dae727c3..30bd2b5287 100644 --- a/coap/api/metrics.go +++ b/coap/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/consumers/writers/api/logging.go b/consumers/writers/api/logging.go index 7c2e5eb25f..49ae620db9 100644 --- a/consumers/writers/api/logging.go +++ b/consumers/writers/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/consumers/writers/api/transport.go b/consumers/writers/api/transport.go index f88e27080d..8805d02aa0 100644 --- a/consumers/writers/api/transport.go +++ b/consumers/writers/api/transport.go @@ -1,9 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -//go:build !test -// +build !test - package api import ( diff --git a/health.go b/health.go index 4addfec3aa..9d53e2b23f 100644 --- a/health.go +++ b/health.go @@ -6,14 +6,13 @@ package mainflux import ( "encoding/json" "net/http" - - "github.com/nelkinda/http-go/header" ) const ( - version string = "0.12.1" - contentType string = "application/json" - svcStatus string = "pass" + version string = "0.12.1" + contentType = "Content-Type" + contentTypeJSON string = "application/json" + svcStatus string = "pass" ) // HealthInfo contains version endpoint response. @@ -31,7 +30,7 @@ type HealthInfo struct { // Health exposes an HTTP handler for retrieving service health. func Health(service string) http.HandlerFunc { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - w.Header().Add(header.ContentType, contentType) + w.Header().Add(contentType, contentTypeJSON) if r.Method != http.MethodGet && r.Method != http.MethodHead { w.WriteHeader(http.StatusMethodNotAllowed) return diff --git a/http/api/logging.go b/http/api/logging.go index fa0587a4b3..797401ba9e 100644 --- a/http/api/logging.go +++ b/http/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/http/api/metrics.go b/http/api/metrics.go index 1aa25320b3..ac869e3484 100644 --- a/http/api/metrics.go +++ b/http/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/readers/api/logging.go b/readers/api/logging.go index 43154686b6..5e1a6f59aa 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/readers/api/metrics.go b/readers/api/metrics.go index 1ce4684a57..3519398137 100644 --- a/readers/api/metrics.go +++ b/readers/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/things/api/logging.go b/things/api/logging.go index b1ed7d6f00..750c2a8cbe 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/things/api/metrics.go b/things/api/metrics.go index d5da0d5972..3257a0b942 100644 --- a/things/api/metrics.go +++ b/things/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/twins/api/logging.go b/twins/api/logging.go index 497e232773..0c198764f8 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( diff --git a/twins/api/metrics.go b/twins/api/metrics.go index cf2b739690..40a0f6a0f8 100644 --- a/twins/api/metrics.go +++ b/twins/api/metrics.go @@ -1,8 +1,6 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 -// +build !test - package api import ( From 794e871de101eff239e2ac64da3c53e74562f9ee Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Tue, 11 Jan 2022 12:03:05 +0100 Subject: [PATCH 04/23] Check error Signed-off-by: Manuel Imperiale --- health.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/health.go b/health.go index 9d53e2b23f..6f28aa0385 100644 --- a/health.go +++ b/health.go @@ -42,7 +42,10 @@ func Health(service string) http.HandlerFunc { Version: version, } + if err := json.NewEncoder(w).Encode(res); err != nil { + w.WriteHeader(http.StatusInternalServerError) + } + w.WriteHeader(http.StatusOK) - _ = json.NewEncoder(w).Encode(res) }) } From 247d5b4f026ad47fdc1d9f510813bdb8489f9dcb Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Tue, 11 Jan 2022 13:20:20 +0100 Subject: [PATCH 05/23] Replace Version by Health in the CLI and SDK Signed-off-by: Manuel Imperiale --- cli/README.md | 6 +++--- cli/{version.go => health.go} | 12 ++++++------ cmd/cli/main.go | 4 ++-- coap/api/transport.go | 2 +- health.go | 2 +- pkg/sdk/go/README.md | 4 ++-- pkg/sdk/go/{version.go => health.go} | 19 ++++++++----------- .../go/{version_test.go => health_test.go} | 8 ++++---- pkg/sdk/go/sdk.go | 10 ++++++---- vendor/github.com/spf13/cobra/user_guide.md | 4 ++-- 10 files changed, 35 insertions(+), 36 deletions(-) rename cli/{version.go => health.go} (51%) rename pkg/sdk/go/{version.go => health.go} (59%) rename pkg/sdk/go/{version_test.go => health_test.go} (73%) diff --git a/cli/README.md b/cli/README.md index 8fdaf69db7..19832909cb 100644 --- a/cli/README.md +++ b/cli/README.md @@ -7,9 +7,9 @@ make cli ## Usage ### Service -#### Get the version of Mainflux services +#### Get Mainflux Things services Health Check ```bash -mainflux-cli version +mainflux-cli health ``` ### Users management @@ -239,4 +239,4 @@ mainflux-cli groups members #### List groups that user belongs to ```bash mainflux-cli groups membership -``` \ No newline at end of file +``` diff --git a/cli/version.go b/cli/health.go similarity index 51% rename from cli/version.go rename to cli/health.go index eafce28c6d..419c1a5e2f 100644 --- a/cli/version.go +++ b/cli/health.go @@ -5,14 +5,14 @@ package cli import "github.com/spf13/cobra" -// NewVersionCmd returns version command. -func NewVersionCmd() *cobra.Command { +// NewHealthCmd returns health check command. +func NewHealthCmd() *cobra.Command { return &cobra.Command{ - Use: "version", - Short: "Mainflux services version", - Long: `Mainflux services version: get version of Mainflux Things Service`, + Use: "health", + Short: "Health Check", + Long: `Mainflux Things service Health Check`, Run: func(cmd *cobra.Command, args []string) { - v, err := sdk.Version() + v, err := sdk.Health() if err != nil { logError(err) return diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 6ca4843fd5..ff6ecc7ae6 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -41,7 +41,7 @@ func main() { } // API commands - versionCmd := cli.NewVersionCmd() + healthCmd := cli.NewHealthCmd() usersCmd := cli.NewUsersCmd() thingsCmd := cli.NewThingsCmd() groupsCmd := cli.NewGroupsCmd() @@ -52,7 +52,7 @@ func main() { certsCmd := cli.NewCertsCmd() // Root Commands - rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(healthCmd) rootCmd.AddCommand(usersCmd) rootCmd.AddCommand(groupsCmd) rootCmd.AddCommand(thingsCmd) diff --git a/coap/api/transport.go b/coap/api/transport.go index d76e31c2d4..301a13ebaf 100644 --- a/coap/api/transport.go +++ b/coap/api/transport.go @@ -40,7 +40,7 @@ var ( service coap.Service ) -//MakeHTTPHandler creates handler for version endpoint. +// MakeHandler returns a HTTP handler for API endpoints. func MakeHTTPHandler() http.Handler { b := bone.New() b.GetFunc("/health", mainflux.Health(protocol)) diff --git a/health.go b/health.go index 6f28aa0385..6b1c7e277c 100644 --- a/health.go +++ b/health.go @@ -20,7 +20,7 @@ type HealthInfo struct { // Status contains service status. Status string `json:"status"` - // Version contains service current version. + // Version contains current service version. Version string `json:"version"` // Description contains service description. diff --git a/pkg/sdk/go/README.md b/pkg/sdk/go/README.md index 7f5ff1a64c..4924b8985b 100644 --- a/pkg/sdk/go/README.md +++ b/pkg/sdk/go/README.md @@ -77,6 +77,6 @@ func (sdk mfSDK) UpdateChannel(channel Channel, token string) error func (sdk mfSDK) UpdateThing(thing Thing, token string) error UpdateThing - updates thing by ID -func (sdk mfSDK) Version() (string, error) - Version - server health check +func (sdk mfSDK) Health() (mainflux.Health, error) + Health - things service health check ``` diff --git a/pkg/sdk/go/version.go b/pkg/sdk/go/health.go similarity index 59% rename from pkg/sdk/go/version.go rename to pkg/sdk/go/health.go index 1951c37c0e..ef64c894dd 100644 --- a/pkg/sdk/go/version.go +++ b/pkg/sdk/go/health.go @@ -9,35 +9,32 @@ import ( "io/ioutil" "net/http" + "github.com/mainflux/mainflux" "github.com/mainflux/mainflux/pkg/errors" ) -type health struct { - Version string `json:"version"` -} - -func (sdk mfSDK) Version() (string, error) { +func (sdk mfSDK) Health() (mainflux.HealthInfo, error) { url := fmt.Sprintf("%s/health", sdk.thingsURL) resp, err := sdk.client.Get(url) if err != nil { - return "", err + return mainflux.HealthInfo{}, err } defer resp.Body.Close() body, err := ioutil.ReadAll(resp.Body) if err != nil { - return "", err + return mainflux.HealthInfo{}, err } if resp.StatusCode != http.StatusOK { - return "", errors.Wrap(ErrFetchVersion, errors.New(resp.Status)) + return mainflux.HealthInfo{}, errors.Wrap(ErrFetchHealth, errors.New(resp.Status)) } - var h health + var h mainflux.HealthInfo if err := json.Unmarshal(body, &h); err != nil { - return "", err + return mainflux.HealthInfo{}, err } - return h.Version, nil + return h, nil } diff --git a/pkg/sdk/go/version_test.go b/pkg/sdk/go/health_test.go similarity index 73% rename from pkg/sdk/go/version_test.go rename to pkg/sdk/go/health_test.go index 8788659322..706b0290ee 100644 --- a/pkg/sdk/go/version_test.go +++ b/pkg/sdk/go/health_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestVersion(t *testing.T) { +func TestHealth(t *testing.T) { svc := newThingsService(map[string]string{token: email}) ts := newThingsServer(svc) defer ts.Close() @@ -24,14 +24,14 @@ func TestVersion(t *testing.T) { empty bool err error }{ - "get version": { + "get things service version": { empty: false, err: nil, }, } for desc, tc := range cases { - ver, err := mainfluxSDK.Version() + h, err := mainfluxSDK.Health() assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err)) - assert.Equal(t, tc.empty, ver == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, ver)) + assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, h.Version)) } } diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index c1a5a2acb9..a89026f65d 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -7,6 +7,8 @@ import ( "crypto/tls" "errors" "net/http" + + "github.com/mainflux/mainflux" ) const ( @@ -52,8 +54,8 @@ var ( // was passed. ErrInvalidContentType = errors.New("Unknown Content Type") - // ErrFetchVersion indicates that fetching of version failed. - ErrFetchVersion = errors.New("failed to fetch version") + // ErrFetchHealth indicates that fetching of health check failed. + ErrFetchHealth = errors.New("failed to fetch health check") // ErrFailedWhitelist failed to whitelist configs ErrFailedWhitelist = errors.New("failed to whitelist") @@ -224,8 +226,8 @@ type SDK interface { // SetContentType sets message content type. SetContentType(ct ContentType) error - // Version returns used mainflux version. - Version() (string, error) + // Health returns things service health check. + Health() (mainflux.HealthInfo, error) // AddBootstrap add bootstrap configuration AddBootstrap(token string, cfg BootstrapConfig) (string, error) diff --git a/vendor/github.com/spf13/cobra/user_guide.md b/vendor/github.com/spf13/cobra/user_guide.md index 311abce284..bede809382 100644 --- a/vendor/github.com/spf13/cobra/user_guide.md +++ b/vendor/github.com/spf13/cobra/user_guide.md @@ -175,10 +175,10 @@ import ( ) func init() { - rootCmd.AddCommand(versionCmd) + rootCmd.AddCommand(healthCmd) } -var versionCmd = &cobra.Command{ +var healthCmd = &cobra.Command{ Use: "version", Short: "Print the version number of Hugo", Long: `All software has versions. This is Hugo's`, From f439983199fa9816b57bc1246cd2ed4c7d65d9f2 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Tue, 11 Jan 2022 13:24:14 +0100 Subject: [PATCH 06/23] Fix typo Signed-off-by: Manuel Imperiale --- consumers/writers/api/transport.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/consumers/writers/api/transport.go b/consumers/writers/api/transport.go index 8805d02aa0..1230726a78 100644 --- a/consumers/writers/api/transport.go +++ b/consumers/writers/api/transport.go @@ -11,7 +11,7 @@ import ( "github.com/prometheus/client_golang/prometheus/promhttp" ) -// MakeHandler returns a HTTP API handler with version and metrics. +// MakeHandler returns a HTTP API handler with health check and metrics. func MakeHandler(svcName string) http.Handler { r := bone.New() r.GetFunc("/health", mainflux.Health(svcName)) From 24e8cf571d23d33efb5f980e54a930b6f6e4ccdf Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 13 Jan 2022 11:39:43 +0100 Subject: [PATCH 07/23] Use new build flag go:build Signed-off-by: Manuel Imperiale --- auth/api/metrics.go | 2 ++ bootstrap/api/logging.go | 2 ++ bootstrap/api/metrics.go | 2 ++ certs/api/logging.go | 2 ++ certs/api/metrics.go | 2 ++ coap/api/logging.go | 2 ++ coap/api/metrics.go | 2 ++ consumers/notifiers/api/logging.go | 2 ++ consumers/notifiers/api/metrics.go | 2 ++ consumers/writers/api/logging.go | 2 ++ consumers/writers/api/metrics.go | 2 ++ http/api/logging.go | 2 ++ http/api/metrics.go | 2 ++ lora/api/logging.go | 2 ++ lora/api/metrics.go | 2 ++ opcua/api/logging.go | 2 ++ opcua/api/metrics.go | 2 ++ provision/api/logging.go | 5 +++++ readers/api/logging.go | 2 ++ readers/api/metrics.go | 2 ++ things/api/logging.go | 2 ++ things/api/metrics.go | 2 ++ twins/api/logging.go | 2 ++ twins/api/metrics.go | 2 ++ users/api/logging.go | 2 ++ users/api/metrics.go | 2 ++ 26 files changed, 55 insertions(+) diff --git a/auth/api/metrics.go b/auth/api/metrics.go index 25e602fcef..c07bd94289 100644 --- a/auth/api/metrics.go +++ b/auth/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/bootstrap/api/logging.go b/bootstrap/api/logging.go index 7668c511b3..49c903b736 100644 --- a/bootstrap/api/logging.go +++ b/bootstrap/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/bootstrap/api/metrics.go b/bootstrap/api/metrics.go index 0b507cbec8..49398d41f7 100644 --- a/bootstrap/api/metrics.go +++ b/bootstrap/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/certs/api/logging.go b/certs/api/logging.go index c86b9794ef..2f4ed7040c 100644 --- a/certs/api/logging.go +++ b/certs/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/certs/api/metrics.go b/certs/api/metrics.go index 1649198211..09668e394b 100644 --- a/certs/api/metrics.go +++ b/certs/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/coap/api/logging.go b/coap/api/logging.go index 4952dad861..6bad55c305 100644 --- a/coap/api/logging.go +++ b/coap/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/coap/api/metrics.go b/coap/api/metrics.go index 30bd2b5287..69526b3cd8 100644 --- a/coap/api/metrics.go +++ b/coap/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/consumers/notifiers/api/logging.go b/consumers/notifiers/api/logging.go index a459ac86f1..2ced56f7a6 100644 --- a/consumers/notifiers/api/logging.go +++ b/consumers/notifiers/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/consumers/notifiers/api/metrics.go b/consumers/notifiers/api/metrics.go index 1d3052b6dd..0462bca402 100644 --- a/consumers/notifiers/api/metrics.go +++ b/consumers/notifiers/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/consumers/writers/api/logging.go b/consumers/writers/api/logging.go index 49ae620db9..39bddb07f8 100644 --- a/consumers/writers/api/logging.go +++ b/consumers/writers/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/consumers/writers/api/metrics.go b/consumers/writers/api/metrics.go index 8021f1734c..cbc09c5d08 100644 --- a/consumers/writers/api/metrics.go +++ b/consumers/writers/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/http/api/logging.go b/http/api/logging.go index 797401ba9e..b4e5957a75 100644 --- a/http/api/logging.go +++ b/http/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/http/api/metrics.go b/http/api/metrics.go index ac869e3484..46ef9ef6a0 100644 --- a/http/api/metrics.go +++ b/http/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/lora/api/logging.go b/lora/api/logging.go index 6db7ab5b8a..f17894203b 100644 --- a/lora/api/logging.go +++ b/lora/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/lora/api/metrics.go b/lora/api/metrics.go index 7f65f73c3d..7e703ad36a 100644 --- a/lora/api/metrics.go +++ b/lora/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/opcua/api/logging.go b/opcua/api/logging.go index fe82d18d4e..ba32a7f848 100644 --- a/opcua/api/logging.go +++ b/opcua/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/opcua/api/metrics.go b/opcua/api/metrics.go index 4222cbc363..1061932d4d 100644 --- a/opcua/api/metrics.go +++ b/opcua/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/provision/api/logging.go b/provision/api/logging.go index e20893d49c..bc63a7ac7a 100644 --- a/provision/api/logging.go +++ b/provision/api/logging.go @@ -1,3 +1,8 @@ +// Copyright (c) Mainflux +// SPDX-License-Identifier: Apache-2.0 + +//go:build !test + package api import ( diff --git a/readers/api/logging.go b/readers/api/logging.go index 5e1a6f59aa..185383c8cb 100644 --- a/readers/api/logging.go +++ b/readers/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/readers/api/metrics.go b/readers/api/metrics.go index 3519398137..e6b8b6fa37 100644 --- a/readers/api/metrics.go +++ b/readers/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/things/api/logging.go b/things/api/logging.go index 750c2a8cbe..cf0ddb1c07 100644 --- a/things/api/logging.go +++ b/things/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/things/api/metrics.go b/things/api/metrics.go index 3257a0b942..5aad2998fd 100644 --- a/things/api/metrics.go +++ b/things/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/twins/api/logging.go b/twins/api/logging.go index 0c198764f8..dafe88d1f2 100644 --- a/twins/api/logging.go +++ b/twins/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/twins/api/metrics.go b/twins/api/metrics.go index 40a0f6a0f8..d58fafda77 100644 --- a/twins/api/metrics.go +++ b/twins/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/users/api/logging.go b/users/api/logging.go index e6c6e50eb4..8fdd62515e 100644 --- a/users/api/logging.go +++ b/users/api/logging.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( diff --git a/users/api/metrics.go b/users/api/metrics.go index 028d168a81..8865e68d14 100644 --- a/users/api/metrics.go +++ b/users/api/metrics.go @@ -1,6 +1,8 @@ // Copyright (c) Mainflux // SPDX-License-Identifier: Apache-2.0 +//go:build !test + package api import ( From 848982a74b2b0b0e07531550facb46c22bd2f98d Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 13 Jan 2022 11:47:27 +0100 Subject: [PATCH 08/23] Revert wrong renaming Signed-off-by: Manuel Imperiale --- vendor/github.com/spf13/cobra/user_guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/vendor/github.com/spf13/cobra/user_guide.md b/vendor/github.com/spf13/cobra/user_guide.md index bede809382..311abce284 100644 --- a/vendor/github.com/spf13/cobra/user_guide.md +++ b/vendor/github.com/spf13/cobra/user_guide.md @@ -175,10 +175,10 @@ import ( ) func init() { - rootCmd.AddCommand(healthCmd) + rootCmd.AddCommand(versionCmd) } -var healthCmd = &cobra.Command{ +var versionCmd = &cobra.Command{ Use: "version", Short: "Print the version number of Hugo", Long: `All software has versions. This is Hugo's`, From 4c06deea95b97717453dfd23fe25dc139a510dcf Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 13 Jan 2022 12:21:56 +0100 Subject: [PATCH 09/23] sdk health test Signed-off-by: Manuel Imperiale --- health.go | 6 +++--- pkg/sdk/go/health_test.go | 9 ++++++++- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/health.go b/health.go index 6b1c7e277c..a0402a9a36 100644 --- a/health.go +++ b/health.go @@ -38,14 +38,14 @@ func Health(service string) http.HandlerFunc { res := HealthInfo{ Status: svcStatus, - Description: service, + Description: service + " service", Version: version, } + w.WriteHeader(http.StatusOK) + if err := json.NewEncoder(w).Encode(res); err != nil { w.WriteHeader(http.StatusInternalServerError) } - - w.WriteHeader(http.StatusOK) }) } diff --git a/pkg/sdk/go/health_test.go b/pkg/sdk/go/health_test.go index 706b0290ee..25d603c342 100644 --- a/pkg/sdk/go/health_test.go +++ b/pkg/sdk/go/health_test.go @@ -8,6 +8,11 @@ import ( "github.com/stretchr/testify/assert" ) +const ( + thingsDescription = "things service" + thingsStatus = "pass" +) + func TestHealth(t *testing.T) { svc := newThingsService(map[string]string{token: email}) ts := newThingsServer(svc) @@ -24,7 +29,7 @@ func TestHealth(t *testing.T) { empty bool err error }{ - "get things service version": { + "get things service health check": { empty: false, err: nil, }, @@ -33,5 +38,7 @@ func TestHealth(t *testing.T) { h, err := mainfluxSDK.Health() assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err)) assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, h.Version)) + assert.Equal(t, thingsDescription, h.Description, fmt.Sprintf("%s: expected non-empty result description, got %s", desc, h.Description)) + assert.Equal(t, thingsStatus, h.Status, fmt.Sprintf("%s: expected non-empty result status, got %s", desc, h.Status)) } } From ecdefa13c6977de90f34dd2270961b6bbb17fa97 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 13 Jan 2022 15:33:01 +0100 Subject: [PATCH 10/23] Add /health endpoint to openapi doc Signed-off-by: Manuel Imperiale --- api/auth.yml | 33 +++++++++++++++++++++++++++++++-- api/bootstrap.yml | 28 ++++++++++++++++++++++++++++ api/certs.yml | 36 ++++++++++++++++++++++++++++++++---- api/consumers-notifiers.yml | 28 ++++++++++++++++++++++++++++ api/http.yml | 37 +++++++++++++++++++++++++++++++++++-- api/provision.yml | 30 +++++++++++++++++++++++++++++- api/readers.yml | 29 ++++++++++++++++++++++++++++- api/things.yml | 29 +++++++++++++++++++++++++++++ api/twins.yml | 28 ++++++++++++++++++++++++++++ api/users.yml | 31 ++++++++++++++++++++++++++++++- 10 files changed, 298 insertions(+), 11 deletions(-) diff --git a/api/auth.yml b/api/auth.yml index b3c14b82ae..e6c4145cb3 100644 --- a/api/auth.yml +++ b/api/auth.yml @@ -87,7 +87,7 @@ paths: summary: Gets all groups. description: | Gets all groups up to a max level of hierarchy that can be fetched in one - request ( max level = 5). Result can be filtered by metadata. Groups will + request ( max level = 5). Result can be filtered by metadata. Groups will be returned as JSON array or JSON tree. tags: - auth @@ -154,7 +154,7 @@ paths: summary: Deletes group. description: | Deletes group. If group is parent and descendant groups do not have any members - child groups will be deleted. Group cannot be deleted if has members or if + child groups will be deleted. Group cannot be deleted if has members or if any descendant group has members. tags: - auth @@ -354,6 +354,17 @@ paths: description: Missing or invalid content type. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" + components: schemas: Key: @@ -542,6 +553,18 @@ components: uniqueItems: true items: type: string + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. parameters: Authorization: @@ -736,3 +759,9 @@ components: application/json: schema: $ref: "#/components/schemas/MembershipPage" + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/bootstrap.yml b/api/bootstrap.yml index 0cb9cae36d..3f9e1010cc 100644 --- a/api/bootstrap.yml +++ b/api/bootstrap.yml @@ -224,6 +224,16 @@ paths: description: Missing or invalid access token provided. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: schemas: @@ -329,6 +339,18 @@ components: - mainflux_key - mainflux_channels - content + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. parameters: Authorization: @@ -514,3 +536,9 @@ components: $ref: "#/components/schemas/BootstrapConfig" ServiceError: description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/certs.yml b/api/certs.yml index 8e20d31bab..37dfec0b78 100644 --- a/api/certs.yml +++ b/api/certs.yml @@ -10,7 +10,7 @@ paths: summary: Creates a certificate for thing description: Creates a certificate for thing tags: - - Thing to proxy + - certs parameters: - $ref: "#/components/parameters/Authorization" requestBody: @@ -28,7 +28,7 @@ paths: description: | Retrieves a certificate for a given cert ID. tags: - - configs + - certs parameters: - $ref: "#/components/parameters/Authorization" - $ref: "#/components/parameters/CertID" @@ -45,7 +45,7 @@ paths: description: | Revokes a certificate for a given cert ID. tags: - - configs + - certs parameters: - $ref: "#/components/parameters/Authorization" - $ref: "#/components/parameters/CertID" @@ -63,7 +63,7 @@ paths: description: | Retrieves a list of certificates' serial IDs for a given thing ID. tags: - - configs + - certs parameters: - $ref: "#/components/parameters/Authorization" - $ref: "#/components/parameters/ThingID" @@ -75,6 +75,16 @@ paths: Failed to retrieve corresponding certificates. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: parameters: @@ -174,6 +184,18 @@ components: revocation_time: type: string description: Certificate revocation time + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. requestBodies: CertReq: @@ -228,3 +250,9 @@ components: application/json: schema: $ref: "#/components/schemas/Revoke" + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/consumers-notifiers.yml b/api/consumers-notifiers.yml index 209351ae50..41b1719b9d 100644 --- a/api/consumers-notifiers.yml +++ b/api/consumers-notifiers.yml @@ -79,6 +79,16 @@ paths: description: Missing or invalid access token provided. "500": $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: securitySchemes: @@ -126,6 +136,18 @@ components: limit: type: integer description: Maximum number of items to return in one page. + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. parameters: Id: @@ -204,3 +226,9 @@ components: $ref: "#/components/schemas/Page" ServiceError: description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/http.yml b/api/http.yml index a511d14972..3b05f9f4da 100644 --- a/api/http.yml +++ b/api/http.yml @@ -30,8 +30,18 @@ paths: description: Message discarded due to invalid channel id. "415": description: Message discarded due to invalid or missing content type. - "500": - description: Unexpected server-side error occurred. + '500': + $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: schemas: @@ -104,6 +114,19 @@ components: type: apiKey in: header name: Authorization + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. + parameters: ID: name: id @@ -127,3 +150,13 @@ components: application/json: schema: $ref: "#/components/schemas/SenMLArray" + +responses: + ServiceError: + description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/provision.yml b/api/provision.yml index ce02b818d3..05f99dfcf4 100644 --- a/api/provision.yml +++ b/api/provision.yml @@ -40,9 +40,18 @@ paths: description: Unauthorized. '500': description: Unexpected server-side error ocurred. + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: - parameters: Authorization: name: Authorization @@ -52,6 +61,19 @@ components: type: string format: jwt required: false + schemas: + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. requestBodies: ProvisionReq: @@ -78,3 +100,9 @@ components: application/json: schema: type: object + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/readers.yml b/api/readers.yml index 8c6cacf1c7..09d971981c 100644 --- a/api/readers.yml +++ b/api/readers.yml @@ -37,6 +37,16 @@ paths: description: Missing or invalid access token provided. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: schemas: @@ -95,6 +105,18 @@ components: updateTime: type: number description: Time of updating measurement. + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. parameters: Authorization: @@ -210,6 +232,11 @@ components: application/json: schema: $ref: "#/components/schemas/MessagesPage" - ServiceError: description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/things.yml b/api/things.yml index e36088a668..dd3b05599d 100644 --- a/api/things.yml +++ b/api/things.yml @@ -621,6 +621,17 @@ paths: description: Database can't process request. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" + components: schemas: Key: @@ -798,6 +809,18 @@ components: description: Policies items: type: string + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. parameters: Authorization: @@ -1096,3 +1119,9 @@ components: schema: type: string format: byte + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/twins.yml b/api/twins.yml index ebe5bdd349..a84695c02d 100644 --- a/api/twins.yml +++ b/api/twins.yml @@ -140,6 +140,16 @@ paths: description: Twin does not exist. '500': $ref: '#/components/responses/ServiceError' + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: parameters: @@ -326,6 +336,18 @@ components: description: Maximum number of items to return in one page. required: - twins + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. requestBodies: TwinReq: @@ -365,3 +387,9 @@ components: $ref: '#/components/schemas/StatesPage' ServiceError: description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" diff --git a/api/users.yml b/api/users.yml index 53e9a6a208..b213b15247 100644 --- a/api/users.yml +++ b/api/users.yml @@ -25,7 +25,7 @@ paths: '415': description: Missing or invalid content type. '500': - $ref: "#/components/responses/ServiceError" + $ref: "#/components/responses/ServiceError" get: summary: Retrieves users description: | @@ -219,6 +219,16 @@ paths: description: Missing or invalid content type. '500': $ref: "#/components/responses/ServiceError" + /health: + get: + summary: Retrieves service health check info. + tags: + - health + responses: + '200': + $ref: "#/components/responses/HealthRes" + '500': + $ref: "#/components/responses/ServiceError" components: securitySchemes: @@ -301,6 +311,19 @@ components: error: type: string description: Error message + HealthInfo: + type: object + properties: + status: + type: string + description: Service status. + version: + type: string + description: Service version. + description: + type: string + description: Service description. + parameters: Authorization: name: Authorization @@ -452,3 +475,9 @@ components: $ref: "#/components/schemas/UsersPage" ServiceError: description: Unexpected server-side error occurred. + HealthRes: + description: Service Health Check. + content: + application/json: + schema: + $ref: "#/components/schemas/HealthInfo" From f92c1478349835056b85fb5b75593eb661bc7388 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 13 Jan 2022 20:45:08 +0100 Subject: [PATCH 11/23] Use const for description message Signed-off-by: Manuel Imperiale --- health.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/health.go b/health.go index a0402a9a36..615bc80abb 100644 --- a/health.go +++ b/health.go @@ -9,10 +9,11 @@ import ( ) const ( - version string = "0.12.1" - contentType = "Content-Type" - contentTypeJSON string = "application/json" - svcStatus string = "pass" + version = "0.12.1" + contentType = "Content-Type" + contentTypeJSON = "application/json" + svcStatus = "pass" + description = " service" ) // HealthInfo contains version endpoint response. @@ -38,7 +39,7 @@ func Health(service string) http.HandlerFunc { res := HealthInfo{ Status: svcStatus, - Description: service + " service", + Description: service + description, Version: version, } From 85d862527494b920a742684f8148bc11033ba32e Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 17 Jan 2022 18:02:51 +0100 Subject: [PATCH 12/23] Add version and build time during build Signed-off-by: Manuel Imperiale --- Makefile | 7 ++++++- health.go | 14 ++++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index e08379e647..4af3387705 100644 --- a/Makefile +++ b/Makefile @@ -11,8 +11,13 @@ DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES)) CGO_ENABLED ?= 0 GOARCH ?= amd64 +VERSION = 0.12.1 +TIME = $(shell date +%F_%T) + define compile_service - CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) go build -mod=vendor -ldflags "-s -w" -o ${BUILD_DIR}/mainflux-$(1) cmd/$(1)/main.go + CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \ + go build -mod=vendor -ldflags "-s -w -X 'github.com/mainflux/mainflux.Version=$(VERSION)' -X 'github.com/mainflux/mainflux.BuildTime=$(TIME)'" \ + -o ${BUILD_DIR}/mainflux-$(1) cmd/$(1)/main.go endef define make_docker diff --git a/health.go b/health.go index 615bc80abb..f3fbe4738b 100644 --- a/health.go +++ b/health.go @@ -9,13 +9,19 @@ import ( ) const ( - version = "0.12.1" contentType = "Content-Type" contentTypeJSON = "application/json" svcStatus = "pass" description = " service" ) +var ( + // Version represents the service version. + Version = "0.12.1" + // BuildTime represents the service build time. + BuildTime = "1970-01-01_00:00:00" +) + // HealthInfo contains version endpoint response. type HealthInfo struct { // Status contains service status. @@ -26,6 +32,9 @@ type HealthInfo struct { // Description contains service description. Description string `json:"description"` + + // BuildTime contains service build time. + BuildTime string `json:"build_time"` } // Health exposes an HTTP handler for retrieving service health. @@ -40,7 +49,8 @@ func Health(service string) http.HandlerFunc { res := HealthInfo{ Status: svcStatus, Description: service + description, - Version: version, + Version: Version, + BuildTime: BuildTime, } w.WriteHeader(http.StatusOK) From c9d31110241807714688df2b3f08fdb306a883e1 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Tue, 18 Jan 2022 19:08:35 +0100 Subject: [PATCH 13/23] Time format Signed-off-by: Manuel Imperiale --- Makefile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4f224ba73a..2f5baa55e6 100644 --- a/Makefile +++ b/Makefile @@ -11,12 +11,11 @@ DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES)) CGO_ENABLED ?= 0 GOARCH ?= amd64 -VERSION = 0.12.1 TIME = $(shell date +%F_%T) define compile_service CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \ - go build -mod=vendor -ldflags "-s -w -X 'github.com/mainflux/mainflux.Version=$(VERSION)' -X 'github.com/mainflux/mainflux.BuildTime=$(TIME)'" \ + go build -mod=vendor -ldflags "-s -w -X 'github.com/mainflux/mainflux.BuildTime=$(TIME)'" \ -o ${BUILD_DIR}/mainflux-$(1) cmd/$(1)/main.go endef From 57cca9677721025da055c47957fc3e869e0325aa Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 09:43:30 +0100 Subject: [PATCH 14/23] Add version and commit using git and build args Signed-off-by: Manuel Imperiale --- Makefile | 10 +++++++--- docker/Dockerfile | 3 +++ health.go | 6 ++++-- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 2f5baa55e6..fb0578d76f 100644 --- a/Makefile +++ b/Makefile @@ -11,11 +11,12 @@ DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES)) CGO_ENABLED ?= 0 GOARCH ?= amd64 -TIME = $(shell date +%F_%T) - define compile_service CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \ - go build -mod=vendor -ldflags "-s -w -X 'github.com/mainflux/mainflux.BuildTime=$(TIME)'" \ + go build -mod=vendor -ldflags "-s -w \ + -X 'github.com/mainflux/mainflux.BuildTime=$(TIME)' \ + -X 'github.com/mainflux/mainflux.Version=$(VERSION)' \ + -X 'github.com/mainflux/mainflux.Commit=$(COMMIT)'" \ -o ${BUILD_DIR}/mainflux-$(1) cmd/$(1)/main.go endef @@ -27,6 +28,9 @@ define make_docker --build-arg SVC=$(svc) \ --build-arg GOARCH=$(GOARCH) \ --build-arg GOARM=$(GOARM) \ + --build-arg VERSION=$(shell git describe --abbrev=0 --tags) \ + --build-arg COMMIT=$(shell git rev-parse --short HEAD) \ + --build-arg TIME=$(shell date +%F_%T) \ --tag=$(MF_DOCKER_IMAGE_NAME_PREFIX)/$(svc) \ -f docker/Dockerfile . endef diff --git a/docker/Dockerfile b/docker/Dockerfile index 08f32fc6ab..1cc06b87e1 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -2,6 +2,9 @@ FROM golang:1.17-alpine AS builder ARG SVC ARG GOARCH ARG GOARM +ARG VERSION +ARG COMMIT +ARG TIME WORKDIR /go/src/github.com/mainflux/mainflux COPY . . diff --git a/health.go b/health.go index f3fbe4738b..df272bd575 100644 --- a/health.go +++ b/health.go @@ -17,7 +17,9 @@ const ( var ( // Version represents the service version. - Version = "0.12.1" + Version = "0.0.0" + // Commit represents the commit SHA. + Commit = "00000000" // BuildTime represents the service build time. BuildTime = "1970-01-01_00:00:00" ) @@ -49,7 +51,7 @@ func Health(service string) http.HandlerFunc { res := HealthInfo{ Status: svcStatus, Description: service + description, - Version: Version, + Version: Version + ":" + Commit, BuildTime: BuildTime, } From 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 10:15:24 +0100 Subject: [PATCH 15/23] Add comments Signed-off-by: Manuel Imperiale --- Makefile | 2 +- health.go | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index fb0578d76f..c1a05ab016 100644 --- a/Makefile +++ b/Makefile @@ -29,7 +29,7 @@ define make_docker --build-arg GOARCH=$(GOARCH) \ --build-arg GOARM=$(GOARM) \ --build-arg VERSION=$(shell git describe --abbrev=0 --tags) \ - --build-arg COMMIT=$(shell git rev-parse --short HEAD) \ + --build-arg COMMIT=$(shell git rev-parse HEAD) \ --build-arg TIME=$(shell date +%F_%T) \ --tag=$(MF_DOCKER_IMAGE_NAME_PREFIX)/$(svc) \ -f docker/Dockerfile . diff --git a/health.go b/health.go index df272bd575..19276ee986 100644 --- a/health.go +++ b/health.go @@ -17,10 +17,16 @@ const ( var ( // Version represents the service version. + // It's meant to be set using go build ldflags: + // -ldflags "-X 'github.com/mainflux/mainflux.Version=0.0.0'" Version = "0.0.0" - // Commit represents the commit SHA. - Commit = "00000000" - // BuildTime represents the service build time. + // Commit represents the git commit SHA used. + // It's meant to be set using go build ldflags: + // -ldflags "-X 'github.com/mainflux/mainflux.Commit=ffffffff'" + Commit = "ffffffff" + // BuildTime contains service build time. + // It's meant to be set using go build ldflags: + // -ldflags "-X 'github.com/mainflux/mainflux.BuildTime=1970-01-01_00:00:00'" BuildTime = "1970-01-01_00:00:00" ) @@ -32,6 +38,9 @@ type HealthInfo struct { // Version contains current service version. Version string `json:"version"` + // Commit represents the service build time. + Commit string `json:"commit"` + // Description contains service description. Description string `json:"description"` @@ -50,8 +59,9 @@ func Health(service string) http.HandlerFunc { res := HealthInfo{ Status: svcStatus, + Version: Version, + Commit: Commit, Description: service + description, - Version: Version + ":" + Commit, BuildTime: BuildTime, } From 2ba2345641a0a59a8996418e62c7e94cb7791ed0 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 10:29:32 +0100 Subject: [PATCH 16/23] Add tests Signed-off-by: Manuel Imperiale --- pkg/sdk/go/health_test.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/go/health_test.go b/pkg/sdk/go/health_test.go index 25d603c342..31e16696d9 100644 --- a/pkg/sdk/go/health_test.go +++ b/pkg/sdk/go/health_test.go @@ -4,6 +4,7 @@ import ( "fmt" "testing" + "github.com/mainflux/mainflux" sdk "github.com/mainflux/mainflux/pkg/sdk/go" "github.com/stretchr/testify/assert" ) @@ -37,8 +38,10 @@ func TestHealth(t *testing.T) { for desc, tc := range cases { h, err := mainfluxSDK.Health() assert.Equal(t, tc.err, err, fmt.Sprintf("%s: expected error %s, got %s", desc, tc.err, err)) - assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty result version, got %s", desc, h.Version)) - assert.Equal(t, thingsDescription, h.Description, fmt.Sprintf("%s: expected non-empty result description, got %s", desc, h.Description)) - assert.Equal(t, thingsStatus, h.Status, fmt.Sprintf("%s: expected non-empty result status, got %s", desc, h.Status)) + assert.Equal(t, thingsStatus, h.Status, fmt.Sprintf("%s: expected %s status, got %s", desc, thingsStatus, h.Status)) + assert.Equal(t, tc.empty, h.Version == "", fmt.Sprintf("%s: expected non-empty version", desc)) + assert.Equal(t, mainflux.Commit, h.Commit, fmt.Sprintf("%s: expected non-empty commit", desc)) + assert.Equal(t, thingsDescription, h.Description, fmt.Sprintf("%s: expected proper description, got %s", desc, h.Description)) + assert.Equal(t, mainflux.BuildTime, h.BuildTime, fmt.Sprintf("%s: expected default epoch date, got %s", desc, h.BuildTime)) } } From 4b87bfcdd0d4a51435be2a46edc5a0f4e3031c90 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 10:47:03 +0100 Subject: [PATCH 17/23] Add missing api properties Signed-off-by: Manuel Imperiale --- api/auth.yml | 8 ++++++++ api/bootstrap.yml | 8 ++++++++ api/certs.yml | 8 ++++++++ api/consumers-notifiers.yml | 8 ++++++++ api/http.yml | 8 ++++++++ api/provision.yml | 8 ++++++++ api/readers.yml | 8 ++++++++ api/things.yml | 8 ++++++++ api/twins.yml | 8 ++++++++ api/users.yml | 8 ++++++++ 10 files changed, 80 insertions(+) diff --git a/api/auth.yml b/api/auth.yml index e6c4145cb3..168d8d4820 100644 --- a/api/auth.yml +++ b/api/auth.yml @@ -559,12 +559,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Authorization: diff --git a/api/bootstrap.yml b/api/bootstrap.yml index 3f9e1010cc..1679540463 100644 --- a/api/bootstrap.yml +++ b/api/bootstrap.yml @@ -345,12 +345,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Authorization: diff --git a/api/certs.yml b/api/certs.yml index 37dfec0b78..5c5130f2b5 100644 --- a/api/certs.yml +++ b/api/certs.yml @@ -190,12 +190,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. requestBodies: CertReq: diff --git a/api/consumers-notifiers.yml b/api/consumers-notifiers.yml index 41b1719b9d..26ab9cd826 100644 --- a/api/consumers-notifiers.yml +++ b/api/consumers-notifiers.yml @@ -142,12 +142,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Id: diff --git a/api/http.yml b/api/http.yml index 3b05f9f4da..51776fd295 100644 --- a/api/http.yml +++ b/api/http.yml @@ -120,12 +120,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: ID: diff --git a/api/provision.yml b/api/provision.yml index 05f99dfcf4..79c2a1a748 100644 --- a/api/provision.yml +++ b/api/provision.yml @@ -68,12 +68,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. requestBodies: ProvisionReq: diff --git a/api/readers.yml b/api/readers.yml index 09d971981c..2c5ae331d9 100644 --- a/api/readers.yml +++ b/api/readers.yml @@ -111,12 +111,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Authorization: diff --git a/api/things.yml b/api/things.yml index dd3b05599d..3870de6449 100644 --- a/api/things.yml +++ b/api/things.yml @@ -815,12 +815,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Authorization: diff --git a/api/twins.yml b/api/twins.yml index a84695c02d..30936281e2 100644 --- a/api/twins.yml +++ b/api/twins.yml @@ -342,12 +342,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. requestBodies: TwinReq: diff --git a/api/users.yml b/api/users.yml index b213b15247..0a68792c69 100644 --- a/api/users.yml +++ b/api/users.yml @@ -317,12 +317,20 @@ components: status: type: string description: Service status. + enum: + - "pass" version: type: string description: Service version. + commit: + type: string + description: Service commit. description: type: string description: Service description. + build_time: + type: string + description: Service build time. parameters: Authorization: From 19be83bd74f9c25efdd8de61b096a8a44085ea1e Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 11:09:38 +0100 Subject: [PATCH 18/23] Fix api Signed-off-by: Manuel Imperiale --- api/auth.yml | 9 +++++++-- api/bootstrap.yml | 9 +++++++-- api/certs.yml | 9 +++++++-- api/consumers-notifiers.yml | 8 ++++++-- api/http.yml | 9 +++++++-- api/provision.yml | 9 +++++++-- api/readers.yml | 8 ++++++-- api/things.yml | 9 +++++++-- api/twins.yml | 9 +++++++-- api/users.yml | 9 +++++++-- health.go | 6 +++--- 11 files changed, 71 insertions(+), 23 deletions(-) diff --git a/api/auth.yml b/api/auth.yml index 168d8d4820..eb0a1433e9 100644 --- a/api/auth.yml +++ b/api/auth.yml @@ -560,19 +560,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - auth service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Authorization: diff --git a/api/bootstrap.yml b/api/bootstrap.yml index 1679540463..3dd272a60a 100644 --- a/api/bootstrap.yml +++ b/api/bootstrap.yml @@ -346,19 +346,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - bootstrap service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Authorization: diff --git a/api/certs.yml b/api/certs.yml index 5c5130f2b5..814caa4f83 100644 --- a/api/certs.yml +++ b/api/certs.yml @@ -191,19 +191,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - certs service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 requestBodies: CertReq: diff --git a/api/consumers-notifiers.yml b/api/consumers-notifiers.yml index 26ab9cd826..ca64def565 100644 --- a/api/consumers-notifiers.yml +++ b/api/consumers-notifiers.yml @@ -143,19 +143,23 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + example: postgresdb-writer service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Id: diff --git a/api/http.yml b/api/http.yml index 51776fd295..551f824714 100644 --- a/api/http.yml +++ b/api/http.yml @@ -121,19 +121,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - http-adapter service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: ID: diff --git a/api/provision.yml b/api/provision.yml index 79c2a1a748..d2bd94b724 100644 --- a/api/provision.yml +++ b/api/provision.yml @@ -69,19 +69,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - provision service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 requestBodies: ProvisionReq: diff --git a/api/readers.yml b/api/readers.yml index 2c5ae331d9..76ad186a68 100644 --- a/api/readers.yml +++ b/api/readers.yml @@ -112,19 +112,23 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + example: postgresdb-reader service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Authorization: diff --git a/api/things.yml b/api/things.yml index 3870de6449..7c732c2278 100644 --- a/api/things.yml +++ b/api/things.yml @@ -816,19 +816,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - things service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Authorization: diff --git a/api/twins.yml b/api/twins.yml index 30936281e2..c231176644 100644 --- a/api/twins.yml +++ b/api/twins.yml @@ -343,19 +343,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - twins service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 requestBodies: TwinReq: diff --git a/api/users.yml b/api/users.yml index 0a68792c69..8856c12de8 100644 --- a/api/users.yml +++ b/api/users.yml @@ -318,19 +318,24 @@ components: type: string description: Service status. enum: - - "pass" + - pass version: type: string description: Service version. + example: 0.0.1 commit: type: string - description: Service commit. + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 description: type: string description: Service description. + enum: + - users service build_time: type: string description: Service build time. + example: 1970-01-01_00:00:00 parameters: Authorization: diff --git a/health.go b/health.go index 19276ee986..87d875469f 100644 --- a/health.go +++ b/health.go @@ -16,15 +16,15 @@ const ( ) var ( - // Version represents the service version. + // Version represents the last service git tag in git history. // It's meant to be set using go build ldflags: // -ldflags "-X 'github.com/mainflux/mainflux.Version=0.0.0'" Version = "0.0.0" - // Commit represents the git commit SHA used. + // Commit represents the service git commit hash. // It's meant to be set using go build ldflags: // -ldflags "-X 'github.com/mainflux/mainflux.Commit=ffffffff'" Commit = "ffffffff" - // BuildTime contains service build time. + // BuildTime represetns the service build time. // It's meant to be set using go build ldflags: // -ldflags "-X 'github.com/mainflux/mainflux.BuildTime=1970-01-01_00:00:00'" BuildTime = "1970-01-01_00:00:00" From 3517aeb9d7da7ae20840ee6a37c16dcb69cba2b6 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 13:20:09 +0100 Subject: [PATCH 19/23] Use ./schemas/HealthInfo.yml as Signed-off-by: Manuel Imperiale --- api/auth.yml | 27 +-------------------------- api/bootstrap.yml | 27 +-------------------------- api/certs.yml | 27 +-------------------------- api/consumers-notifiers.yml | 26 +------------------------- api/http.yml | 27 +-------------------------- api/provision.yml | 28 +--------------------------- api/readers.yml | 26 +------------------------- api/schemas/HealthInfo.yml | 24 ++++++++++++++++++++++++ api/things.yml | 27 +-------------------------- api/twins.yml | 27 +-------------------------- api/users.yml | 27 +-------------------------- 11 files changed, 34 insertions(+), 259 deletions(-) create mode 100644 api/schemas/HealthInfo.yml diff --git a/api/auth.yml b/api/auth.yml index eb0a1433e9..0b8882a2a4 100644 --- a/api/auth.yml +++ b/api/auth.yml @@ -553,31 +553,6 @@ components: uniqueItems: true items: type: string - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - auth service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Authorization: @@ -777,4 +752,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/bootstrap.yml b/api/bootstrap.yml index 3dd272a60a..81173834b7 100644 --- a/api/bootstrap.yml +++ b/api/bootstrap.yml @@ -339,31 +339,6 @@ components: - mainflux_key - mainflux_channels - content - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - bootstrap service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Authorization: @@ -554,4 +529,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/certs.yml b/api/certs.yml index 814caa4f83..a7e2fedcc8 100644 --- a/api/certs.yml +++ b/api/certs.yml @@ -184,31 +184,6 @@ components: revocation_time: type: string description: Certificate revocation time - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - certs service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 requestBodies: CertReq: @@ -268,4 +243,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/consumers-notifiers.yml b/api/consumers-notifiers.yml index ca64def565..2afc750fcf 100644 --- a/api/consumers-notifiers.yml +++ b/api/consumers-notifiers.yml @@ -136,30 +136,6 @@ components: limit: type: integer description: Maximum number of items to return in one page. - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - example: postgresdb-writer service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Id: @@ -243,4 +219,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/http.yml b/api/http.yml index 551f824714..88f3294522 100644 --- a/api/http.yml +++ b/api/http.yml @@ -114,31 +114,6 @@ components: type: apiKey in: header name: Authorization - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - http-adapter service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: ID: @@ -172,4 +147,4 @@ responses: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/provision.yml b/api/provision.yml index d2bd94b724..8097c3f579 100644 --- a/api/provision.yml +++ b/api/provision.yml @@ -61,32 +61,6 @@ components: type: string format: jwt required: false - schemas: - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - provision service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 requestBodies: ProvisionReq: @@ -118,4 +92,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/readers.yml b/api/readers.yml index 76ad186a68..7e6bd4b86c 100644 --- a/api/readers.yml +++ b/api/readers.yml @@ -105,30 +105,6 @@ components: updateTime: type: number description: Time of updating measurement. - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - example: postgresdb-reader service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Authorization: @@ -251,4 +227,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/schemas/HealthInfo.yml b/api/schemas/HealthInfo.yml new file mode 100644 index 0000000000..29e9f461b9 --- /dev/null +++ b/api/schemas/HealthInfo.yml @@ -0,0 +1,24 @@ +type: object +properties: + status: + type: string + description: Service status. + enum: + - pass + version: + type: string + description: Service version. + example: 0.0.1 + commit: + type: string + description: Service commit hash. + example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 + description: + type: string + description: Service description. + enum: + - auth service + build_time: + type: string + description: Service build time. + example: 1970-01-01_00:00:00 diff --git a/api/things.yml b/api/things.yml index 7c732c2278..89ceb1859c 100644 --- a/api/things.yml +++ b/api/things.yml @@ -809,31 +809,6 @@ components: description: Policies items: type: string - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - things service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Authorization: @@ -1137,4 +1112,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/twins.yml b/api/twins.yml index c231176644..4233e16960 100644 --- a/api/twins.yml +++ b/api/twins.yml @@ -336,31 +336,6 @@ components: description: Maximum number of items to return in one page. required: - twins - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - twins service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 requestBodies: TwinReq: @@ -405,4 +380,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" diff --git a/api/users.yml b/api/users.yml index 8856c12de8..994162d51c 100644 --- a/api/users.yml +++ b/api/users.yml @@ -311,31 +311,6 @@ components: error: type: string description: Error message - HealthInfo: - type: object - properties: - status: - type: string - description: Service status. - enum: - - pass - version: - type: string - description: Service version. - example: 0.0.1 - commit: - type: string - description: Service commit hash. - example: 7d6f4dc4f7f0c1fa3dc24eddfb18bb5073ff4f62 - description: - type: string - description: Service description. - enum: - - users service - build_time: - type: string - description: Service build time. - example: 1970-01-01_00:00:00 parameters: Authorization: @@ -493,4 +468,4 @@ components: content: application/json: schema: - $ref: "#/components/schemas/HealthInfo" + $ref: "./schemas/HealthInfo.yml" From a4b7c0fbc8b5eec6ca53edaba019825fd7741dd2 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 13:23:44 +0100 Subject: [PATCH 20/23] Fix example Signed-off-by: Manuel Imperiale --- api/schemas/HealthInfo.yml | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/schemas/HealthInfo.yml b/api/schemas/HealthInfo.yml index 29e9f461b9..4d4c062c4e 100644 --- a/api/schemas/HealthInfo.yml +++ b/api/schemas/HealthInfo.yml @@ -16,8 +16,7 @@ properties: description: type: string description: Service description. - enum: - - auth service + example: service build_time: type: string description: Service build time. From 2cc816a31ffc2f029dc9db5669ffc7897c659a47 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Wed, 19 Jan 2022 18:23:49 +0100 Subject: [PATCH 21/23] Use content type application/health+json Signed-off-by: Manuel Imperiale --- health.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/health.go b/health.go index 87d875469f..7835667e67 100644 --- a/health.go +++ b/health.go @@ -10,7 +10,7 @@ import ( const ( contentType = "Content-Type" - contentTypeJSON = "application/json" + contentTypeJSON = "application/health+json" svcStatus = "pass" description = " service" ) From d3c3638f54fbf56b2c2bb23804813746358e5ca7 Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Thu, 20 Jan 2022 10:58:05 +0100 Subject: [PATCH 22/23] Set Makefile variables only if empty Signed-off-by: Manuel Imperiale --- Makefile | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Makefile b/Makefile index c1a05ab016..a5143c886d 100644 --- a/Makefile +++ b/Makefile @@ -10,6 +10,9 @@ DOCKERS = $(addprefix docker_,$(SERVICES)) DOCKERS_DEV = $(addprefix docker_dev_,$(SERVICES)) CGO_ENABLED ?= 0 GOARCH ?= amd64 +VERSION ?= $(shell git describe --abbrev=0 --tags) +COMMIT ?= $(shell git rev-parse HEAD) +TIME ?= $(shell date +%F_%T) define compile_service CGO_ENABLED=$(CGO_ENABLED) GOOS=$(GOOS) GOARCH=$(GOARCH) GOARM=$(GOARM) \ @@ -28,9 +31,9 @@ define make_docker --build-arg SVC=$(svc) \ --build-arg GOARCH=$(GOARCH) \ --build-arg GOARM=$(GOARM) \ - --build-arg VERSION=$(shell git describe --abbrev=0 --tags) \ - --build-arg COMMIT=$(shell git rev-parse HEAD) \ - --build-arg TIME=$(shell date +%F_%T) \ + --build-arg VERSION=$(VERSION) \ + --build-arg COMMIT=$(COMMIT) \ + --build-arg TIME=$(TIME) \ --tag=$(MF_DOCKER_IMAGE_NAME_PREFIX)/$(svc) \ -f docker/Dockerfile . endef From 42de5efcb9a8234d9247e3e3990cbcd0cda53cad Mon Sep 17 00:00:00 2001 From: Manuel Imperiale Date: Mon, 24 Jan 2022 20:47:50 +0100 Subject: [PATCH 23/23] Fix typo Signed-off-by: Manuel Imperiale --- health.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/health.go b/health.go index 7835667e67..a142440f23 100644 --- a/health.go +++ b/health.go @@ -38,7 +38,7 @@ type HealthInfo struct { // Version contains current service version. Version string `json:"version"` - // Commit represents the service build time. + // Commit represents the git hash commit. Commit string `json:"commit"` // Description contains service description.