Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MF-1308 - Use IETF Health Check standard #1541

Merged
merged 30 commits into from
Jan 24, 2022
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
b91a6ce
MF-1308 - Use IETF Health Check standard
manuio Jan 10, 2022
a12d4fc
Add nginx health endpoint
manuio Jan 10, 2022
c7f534b
Rm github.com/nelkinda dependency
manuio Jan 11, 2022
794e871
Check error
manuio Jan 11, 2022
247d5b4
Replace Version by Health in the CLI and SDK
manuio Jan 11, 2022
f439983
Fix typo
manuio Jan 11, 2022
24e8cf5
Use new build flag go:build
manuio Jan 13, 2022
775dd7c
Fix conflicts
manuio Jan 13, 2022
848982a
Revert wrong renaming
manuio Jan 13, 2022
4c06dee
sdk health test
manuio Jan 13, 2022
ecdefa1
Add /health endpoint to openapi doc
manuio Jan 13, 2022
f92c147
Use const for description message
manuio Jan 13, 2022
784d43f
Merge branch 'master' into health
manuio Jan 15, 2022
85d8625
Add version and build time during build
manuio Jan 17, 2022
a285f83
Merge branch 'health' of http://github.com/manuio/mainflux into health
manuio Jan 17, 2022
0772600
Merge branch 'master' into health
manuio Jan 18, 2022
438eef9
Merge branch 'master' into health
manuio Jan 18, 2022
c9d3111
Time format
manuio Jan 18, 2022
57cca96
Add version and commit using git and build args
manuio Jan 19, 2022
7d6f4dc
Add comments
manuio Jan 19, 2022
2ba2345
Add tests
manuio Jan 19, 2022
4b87bfc
Add missing api properties
manuio Jan 19, 2022
19be83b
Fix api
manuio Jan 19, 2022
3517aeb
Use ./schemas/HealthInfo.yml as
manuio Jan 19, 2022
a4b7c0f
Fix example
manuio Jan 19, 2022
36727bb
Merge branch 'master' into health
manuio Jan 19, 2022
2cc816a
Use content type application/health+json
manuio Jan 19, 2022
d47bdc6
Merge branch 'health' of http://github.com/manuio/mainflux into health
manuio Jan 19, 2022
d3c3638
Set Makefile variables only if empty
manuio Jan 20, 2022
42de5ef
Fix typo
manuio Jan 24, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion auth/api/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions auth/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 1 addition & 1 deletion bootstrap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions certs/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 2 additions & 0 deletions certs/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 1 addition & 1 deletion certs/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -239,4 +239,4 @@ mainflux-cli groups members <group_id> <user_auth_token>
#### List groups that user belongs to
```bash
mainflux-cli groups membership <user_id> <user_auth_token>
```
```
12 changes: 6 additions & 6 deletions cli/version.go → cli/health.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func main() {
}

// API commands
versionCmd := cli.NewVersionCmd()
healthCmd := cli.NewHealthCmd()
usersCmd := cli.NewUsersCmd()
thingsCmd := cli.NewThingsCmd()
groupsCmd := cli.NewGroupsCmd()
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion coap/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 1 addition & 1 deletion coap/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
4 changes: 2 additions & 2 deletions coap/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ 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("/version", mainflux.Version(protocol))
b.GetFunc("/health", mainflux.Health(protocol))
b.Handle("/metrics", promhttp.Handler())

return b
Expand Down
2 changes: 2 additions & 0 deletions consumers/notifiers/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 2 additions & 0 deletions consumers/notifiers/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 1 addition & 1 deletion consumers/notifiers/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion consumers/writers/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 2 additions & 0 deletions consumers/writers/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
6 changes: 2 additions & 4 deletions consumers/writers/api/transport.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test

package api

import (
Expand All @@ -13,10 +11,10 @@ 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("/version", mainflux.Version(svcName))
r.GetFunc("/health", mainflux.Health(svcName))
r.Handle("/metrics", promhttp.Handler())

return r
Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/nginx-key.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand Down
2 changes: 1 addition & 1 deletion docker/nginx/nginx-x509.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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};
}
Expand Down
51 changes: 51 additions & 0 deletions health.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

package mainflux

import (
"encoding/json"
"net/http"
)

const (
version string = "0.12.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a way that version is automatically set by release process
something like explained here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You forgot the link :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mteodor explained where?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contentType = "Content-Type"
contentTypeJSON string = "application/json"
svcStatus string = "pass"
)

// HealthInfo contains version endpoint response.
type HealthInfo struct {
// Status contains service status.
Status string `json:"status"`

// Version contains current service 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(contentType, contentTypeJSON)
if r.Method != http.MethodGet && r.Method != http.MethodHead {
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

res := HealthInfo{
Status: svcStatus,
Description: service + " service",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" service" as constant?

Version: version,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be good to have timestamp of build so that when debugging deployment by querying the health you can confirm that right version is deployed

Copy link
Contributor Author

@manuio manuio Jan 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be covered by @blokovi proposition. There is an issue about: https://github.com/mainflux/mainflux/issues/1543

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mteodor there is a way to put timestamp and Git hash during the compilation phase. We had this before. I think it would make sense to add it back.

}

w.WriteHeader(http.StatusOK)

if err := json.NewEncoder(w).Encode(res); err != nil {
w.WriteHeader(http.StatusInternalServerError)
}
})
}
2 changes: 1 addition & 1 deletion http/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 1 addition & 1 deletion http/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

// +build !test
//go:build !test

package api

Expand Down
2 changes: 1 addition & 1 deletion http/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lora/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions lora/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 2 additions & 0 deletions lora/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 2 additions & 0 deletions opcua/api/logging.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 2 additions & 0 deletions opcua/api/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright (c) Mainflux
// SPDX-License-Identifier: Apache-2.0

//go:build !test

package api

import (
Expand Down
2 changes: 1 addition & 1 deletion opcua/api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/sdk/go/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Loading