Skip to content

Commit 2497dfa

Browse files
authored
feat(cdn): add metrics (#5181)
1 parent 6f14f3b commit 2497dfa

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

engine/api/api.go

+3
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,9 @@ func (a *API) Serve(ctx context.Context) error {
867867
Db: a.mustDB(),
868868
Cache: a.Cache,
869869
}
870+
if err := cdsService.InitMetrics(); err != nil {
871+
return sdk.WithStack(err)
872+
}
870873
cdsService.RunTcpLogServer(ctx)
871874

872875
log.Info(ctx, "Starting CDS API HTTP Server on %s:%d", a.Config.HTTP.Addr, a.Config.HTTP.Port)

engine/cdn/cdn_log.go

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
gocache "github.com/patrickmn/go-cache"
1313

14+
"github.com/ovh/cds/engine/api/observability"
1415
"github.com/ovh/cds/engine/api/services"
1516
"github.com/ovh/cds/engine/api/worker"
1617
"github.com/ovh/cds/engine/api/workflow"
@@ -44,10 +45,12 @@ func (s *Service) RunTcpLogServer(ctx context.Context) {
4445
for {
4546
conn, err := listener.Accept()
4647
if err != nil {
48+
observability.Record(ctx, Errors, 1)
4749
log.Error(ctx, "unable to accept connection: %v", err)
4850
return
4951
}
5052
sdk.GoRoutine(ctx, "cdn-logServer", func(ctx context.Context) {
53+
observability.Record(ctx, Hits, 1)
5154
s.handleConnection(ctx, conn)
5255
})
5356
}
@@ -69,6 +72,7 @@ func (s *Service) handleConnection(ctx context.Context, conn net.Conn) {
6972
bytes = bytes[:len(bytes)-1]
7073

7174
if err := s.handleLogMessage(ctx, bytes); err != nil {
75+
observability.Record(ctx, Errors, 1)
7276
log.Error(ctx, "cdn.log> %v", err)
7377
continue
7478
}
@@ -94,8 +98,10 @@ func (s *Service) handleLogMessage(ctx context.Context, messageReceived []byte)
9498

9599
switch {
96100
case signature.Worker != nil:
101+
observability.Record(ctx, WorkerLogReceived, 1)
97102
return s.handleWorkerLog(ctx, signature.Worker.WorkerID, sig, m)
98103
case signature.Service != nil:
104+
observability.Record(ctx, ServiceLogReceived, 1)
99105
return s.handleServiceLog(ctx, signature.Service.HatcheryID, signature.Service.HatcheryName, signature.Service.WorkerName, sig, m)
100106
default:
101107
return sdk.WithStack(sdk.ErrWrongRequest)

engine/cdn/status_handler.go

+50
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,23 @@ package cdn
33
import (
44
"context"
55
"net/http"
6+
"sync"
67

8+
"go.opencensus.io/stats"
9+
"go.opencensus.io/tag"
10+
11+
"github.com/ovh/cds/engine/api/observability"
712
"github.com/ovh/cds/engine/service"
813
"github.com/ovh/cds/sdk"
14+
"github.com/ovh/cds/sdk/log"
15+
)
16+
17+
var (
18+
onceMetrics sync.Once
19+
Errors *stats.Int64Measure
20+
Hits *stats.Int64Measure
21+
WorkerLogReceived *stats.Int64Measure
22+
ServiceLogReceived *stats.Int64Measure
923
)
1024

1125
func (s *Service) statusHandler() service.Handler {
@@ -22,3 +36,39 @@ func (s *Service) Status(ctx context.Context) sdk.MonitoringStatus {
2236
m.Lines = append(m.Lines, sdk.MonitoringStatusLine{Component: "CDN", Value: status, Status: status})
2337
return m
2438
}
39+
40+
func (s *Service) InitMetrics() error {
41+
var err error
42+
onceMetrics.Do(func() {
43+
Errors = stats.Int64(
44+
"cdn/tcp/router_errors",
45+
"number of errors",
46+
stats.UnitDimensionless)
47+
Hits = stats.Int64(
48+
"cdn/tcp/router_hits",
49+
"number of hits",
50+
stats.UnitDimensionless)
51+
WorkerLogReceived = stats.Int64(
52+
"cdn/tcp/worker/log/count",
53+
"Number of worker log received",
54+
stats.UnitDimensionless)
55+
ServiceLogReceived = stats.Int64(
56+
"cdn/tcp/service/log/count",
57+
"Number of service log received",
58+
stats.UnitDimensionless)
59+
60+
tagServiceType := observability.MustNewKey(observability.TagServiceType)
61+
tagServiceName := observability.MustNewKey(observability.TagServiceName)
62+
63+
err = observability.RegisterView(
64+
observability.NewViewCount("cdn/tcp/router/router_errors", Errors, []tag.Key{tagServiceType, tagServiceName}),
65+
observability.NewViewCount("cdn/tcp/router/router_hits", Hits, []tag.Key{tagServiceType, tagServiceName}),
66+
observability.NewViewCount("cdn/tcp/worker/log/count", WorkerLogReceived, []tag.Key{tagServiceType, tagServiceName}),
67+
observability.NewViewCount("cdn/tcp/service/log/count", ServiceLogReceived, []tag.Key{tagServiceType, tagServiceName}),
68+
)
69+
})
70+
71+
log.Debug("cdn> Stats initialized")
72+
73+
return err
74+
}

0 commit comments

Comments
 (0)