forked from mtusi2026/mtusi2026lab6
feat(http): lab6 add OTel spans, metrics, JSON logs and tracing readiness
This commit is contained in:
@@ -7,9 +7,11 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"go.opentelemetry.io/otel/codes"
|
||||
|
||||
"high-loud-apps-labs/internal/domain"
|
||||
"high-loud-apps-labs/internal/httpapi/dto"
|
||||
"high-loud-apps-labs/internal/telemetry"
|
||||
)
|
||||
|
||||
type HealthCheck func(ctx context.Context) error
|
||||
@@ -33,12 +35,16 @@ func (h *HealthHandler) Live(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
ctx, span := telemetry.Tracer("httpapi").Start(c.Request.Context(), "health.ready")
|
||||
defer span.End()
|
||||
|
||||
if h.ready != nil && !h.ready.Load() {
|
||||
span.SetStatus(codes.Error, "shutting down")
|
||||
respondError(c, &domain.UnavailableError{Reason: "shutting down"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, 2*time.Second)
|
||||
defer cancel()
|
||||
|
||||
for _, check := range h.checks {
|
||||
@@ -46,6 +52,7 @@ func (h *HealthHandler) Ready(c *gin.Context) {
|
||||
continue
|
||||
}
|
||||
if err := check(ctx); err != nil {
|
||||
span.SetStatus(codes.Error, "зависимость недоступна")
|
||||
respondError(c, &domain.UnavailableError{Reason: "зависимость недоступна"})
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user