Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
6 changes: 6 additions & 0 deletions api/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ import (

const svcName = "agent"

func healthEndpoint(svc agent.Service) endpoint.Endpoint {
return func(_ context.Context, _ any) (any, error) {
return healthRes{Metrics: svc.Health()}, nil
}
}

func pubEndpoint(svc agent.Service) endpoint.Endpoint {
return func(_ context.Context, request any) (any, error) {
req := request.(pubReq)
Expand Down
21 changes: 21 additions & 0 deletions api/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import (

"github.com/absmach/agent"
"github.com/absmach/agent/pkg/devicemgr"
"github.com/absmach/agent/pkg/health"
"github.com/absmach/magistrala"
)

var (
_ magistrala.Response = (*publishRes)(nil)
_ magistrala.Response = (*execRes)(nil)
_ magistrala.Response = (*healthRes)(nil)
_ magistrala.Response = (*addConfigRes)(nil)
_ magistrala.Response = (*viewConfigRes)(nil)
_ magistrala.Response = (*viewServicesRes)(nil)
Expand Down Expand Up @@ -244,3 +246,22 @@ func (res otaStatusRes) Headers() map[string]string {
func (res otaStatusRes) Empty() bool {
return false
}

type healthRes struct {
*health.Metrics
}

func (res healthRes) Code() int {
if res.Metrics == nil {
return http.StatusServiceUnavailable
}
return http.StatusOK
}

func (res healthRes) Headers() map[string]string {
return map[string]string{}
}

func (res healthRes) Empty() bool {
return res.Metrics == nil
}
7 changes: 7 additions & 0 deletions api/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,13 @@ func MakeHandler(svc agent.Service, logger *slog.Logger, stream *logstream.Strea
})
r.Handle("/ui/*", http.StripPrefix("/ui", agentui.Handler()))

r.Get("/api/health", kithttp.NewServer(
healthEndpoint(svc),
decodeRequest,
EncodeResponse,
opts...,
).ServeHTTP)

return r
}

Expand Down
9 changes: 9 additions & 0 deletions middleware/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/absmach/agent"
"github.com/absmach/agent/pkg/devicemgr"
"github.com/absmach/agent/pkg/health"
"github.com/go-chi/chi/v5/middleware"
)

Expand Down Expand Up @@ -224,6 +225,14 @@ func (lm *loggingMiddleware) OTA(ctx context.Context, url, sha256hex string, siz
return lm.svc.OTA(ctx, url, sha256hex, size)
}

func (lm *loggingMiddleware) Health() *health.Metrics {
defer func(begin time.Time) {
lm.logger.Info("Retrieve health completed successfully.", slog.String("duration", time.Since(begin).String()))
}(time.Now())

return lm.svc.Health()
}

func (lm *loggingMiddleware) NodeRed(cmdStr string) (resp string, err error) {
defer func(begin time.Time) {
args := []any{
Expand Down
10 changes: 10 additions & 0 deletions middleware/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/absmach/agent"
"github.com/absmach/agent/pkg/devicemgr"
"github.com/absmach/agent/pkg/health"
"github.com/go-kit/kit/metrics"
)

Expand Down Expand Up @@ -149,6 +150,15 @@ func (ms *metricsMiddleware) OTA(ctx context.Context, url, sha256hex string, siz
return ms.svc.OTA(ctx, url, sha256hex, size)
}

func (ms *metricsMiddleware) Health() *health.Metrics {
defer func(begin time.Time) {
ms.counter.With("method", "health").Add(1)
ms.latency.With("method", "health").Observe(time.Since(begin).Seconds())
}(time.Now())

return ms.svc.Health()
}

func (ms *metricsMiddleware) NodeRed(cmdStr string) (string, error) {
defer func(begin time.Time) {
ms.counter.With("method", "nodered").Add(1)
Expand Down
269 changes: 0 additions & 269 deletions mocks/pub_sub.go

This file was deleted.

Loading
Loading