From 2497dfa69955087a9039af5b683d37aa6c780494 Mon Sep 17 00:00:00 2001 From: Guiheux Steven Date: Fri, 15 May 2020 08:52:38 +0200 Subject: [PATCH] feat(cdn): add metrics (#5181) --- engine/api/api.go | 3 +++ engine/cdn/cdn_log.go | 6 +++++ engine/cdn/status_handler.go | 50 ++++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) diff --git a/engine/api/api.go b/engine/api/api.go index ce93263c7a..6bc42a8d68 100644 --- a/engine/api/api.go +++ b/engine/api/api.go @@ -867,6 +867,9 @@ func (a *API) Serve(ctx context.Context) error { Db: a.mustDB(), Cache: a.Cache, } + if err := cdsService.InitMetrics(); err != nil { + return sdk.WithStack(err) + } cdsService.RunTcpLogServer(ctx) log.Info(ctx, "Starting CDS API HTTP Server on %s:%d", a.Config.HTTP.Addr, a.Config.HTTP.Port) diff --git a/engine/cdn/cdn_log.go b/engine/cdn/cdn_log.go index 14e4f7a5b0..2be656b232 100644 --- a/engine/cdn/cdn_log.go +++ b/engine/cdn/cdn_log.go @@ -11,6 +11,7 @@ import ( gocache "github.com/patrickmn/go-cache" + "github.com/ovh/cds/engine/api/observability" "github.com/ovh/cds/engine/api/services" "github.com/ovh/cds/engine/api/worker" "github.com/ovh/cds/engine/api/workflow" @@ -44,10 +45,12 @@ func (s *Service) RunTcpLogServer(ctx context.Context) { for { conn, err := listener.Accept() if err != nil { + observability.Record(ctx, Errors, 1) log.Error(ctx, "unable to accept connection: %v", err) return } sdk.GoRoutine(ctx, "cdn-logServer", func(ctx context.Context) { + observability.Record(ctx, Hits, 1) s.handleConnection(ctx, conn) }) } @@ -69,6 +72,7 @@ func (s *Service) handleConnection(ctx context.Context, conn net.Conn) { bytes = bytes[:len(bytes)-1] if err := s.handleLogMessage(ctx, bytes); err != nil { + observability.Record(ctx, Errors, 1) log.Error(ctx, "cdn.log> %v", err) continue } @@ -94,8 +98,10 @@ func (s *Service) handleLogMessage(ctx context.Context, messageReceived []byte) switch { case signature.Worker != nil: + observability.Record(ctx, WorkerLogReceived, 1) return s.handleWorkerLog(ctx, signature.Worker.WorkerID, sig, m) case signature.Service != nil: + observability.Record(ctx, ServiceLogReceived, 1) return s.handleServiceLog(ctx, signature.Service.HatcheryID, signature.Service.HatcheryName, signature.Service.WorkerName, sig, m) default: return sdk.WithStack(sdk.ErrWrongRequest) diff --git a/engine/cdn/status_handler.go b/engine/cdn/status_handler.go index 294a431058..a177549a52 100644 --- a/engine/cdn/status_handler.go +++ b/engine/cdn/status_handler.go @@ -3,9 +3,23 @@ package cdn import ( "context" "net/http" + "sync" + "go.opencensus.io/stats" + "go.opencensus.io/tag" + + "github.com/ovh/cds/engine/api/observability" "github.com/ovh/cds/engine/service" "github.com/ovh/cds/sdk" + "github.com/ovh/cds/sdk/log" +) + +var ( + onceMetrics sync.Once + Errors *stats.Int64Measure + Hits *stats.Int64Measure + WorkerLogReceived *stats.Int64Measure + ServiceLogReceived *stats.Int64Measure ) func (s *Service) statusHandler() service.Handler { @@ -22,3 +36,39 @@ func (s *Service) Status(ctx context.Context) sdk.MonitoringStatus { m.Lines = append(m.Lines, sdk.MonitoringStatusLine{Component: "CDN", Value: status, Status: status}) return m } + +func (s *Service) InitMetrics() error { + var err error + onceMetrics.Do(func() { + Errors = stats.Int64( + "cdn/tcp/router_errors", + "number of errors", + stats.UnitDimensionless) + Hits = stats.Int64( + "cdn/tcp/router_hits", + "number of hits", + stats.UnitDimensionless) + WorkerLogReceived = stats.Int64( + "cdn/tcp/worker/log/count", + "Number of worker log received", + stats.UnitDimensionless) + ServiceLogReceived = stats.Int64( + "cdn/tcp/service/log/count", + "Number of service log received", + stats.UnitDimensionless) + + tagServiceType := observability.MustNewKey(observability.TagServiceType) + tagServiceName := observability.MustNewKey(observability.TagServiceName) + + err = observability.RegisterView( + observability.NewViewCount("cdn/tcp/router/router_errors", Errors, []tag.Key{tagServiceType, tagServiceName}), + observability.NewViewCount("cdn/tcp/router/router_hits", Hits, []tag.Key{tagServiceType, tagServiceName}), + observability.NewViewCount("cdn/tcp/worker/log/count", WorkerLogReceived, []tag.Key{tagServiceType, tagServiceName}), + observability.NewViewCount("cdn/tcp/service/log/count", ServiceLogReceived, []tag.Key{tagServiceType, tagServiceName}), + ) + }) + + log.Debug("cdn> Stats initialized") + + return err +}