Skip to content

Commit

Permalink
feat(cdn): add metrics (#5181)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored May 15, 2020
1 parent 6f14f3b commit 2497dfa
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 0 deletions.
3 changes: 3 additions & 0 deletions engine/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions engine/cdn/cdn_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
})
}
Expand All @@ -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
}
Expand All @@ -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)
Expand Down
50 changes: 50 additions & 0 deletions engine/cdn/status_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}

0 comments on commit 2497dfa

Please sign in to comment.