Skip to content

Commit e0c015a

Browse files
committed
Support switching the sample sink on CLI
1 parent 70b38e1 commit e0c015a

File tree

5 files changed

+74
-15
lines changed

5 files changed

+74
-15
lines changed

tracker/backend/ingestor/go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ require (
1818
github.com/aws/aws-sdk-go-v2/internal/configsources v1.1.34 // indirect
1919
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28 // indirect
2020
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.35 // indirect
21+
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.26.2 // indirect
2122
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.28 // indirect
2223
github.com/aws/aws-sdk-go-v2/service/sso v1.12.12 // indirect
2324
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.14.12 // indirect
2425
github.com/aws/aws-sdk-go-v2/service/sts v1.19.2 // indirect
2526
github.com/aws/smithy-go v1.13.5 // indirect
2627
github.com/bufbuild/connect-go v1.9.0 // indirect
28+
github.com/jmespath/go-jmespath v0.4.0 // indirect
2729
golang.org/x/sys v0.1.0 // indirect
2830
google.golang.org/protobuf v1.31.0 // indirect
2931
)

tracker/backend/ingestor/go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28 h1:srIVS45eQuewqz6fKK
7171
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.4.28/go.mod h1:7VRpKQQedkfIEXb4k52I7swUnZP0wohVajJMRn3vsUw=
7272
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.35 h1:LWA+3kDM8ly001vJ1X1waCuLJdtTl48gwkPKWy9sosI=
7373
github.com/aws/aws-sdk-go-v2/internal/ini v1.3.35/go.mod h1:0Eg1YjxE0Bhn56lx+SHJwCzhW+2JGtizsrx+lCqrfm0=
74+
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.26.2 h1:PWGu2JhCb/XJlJ7SSFJq76pxk4xWsN76nZxh7TzMHx0=
75+
github.com/aws/aws-sdk-go-v2/service/cloudwatch v1.26.2/go.mod h1:2KOZkkzMDZCo/aLzPhys06mHNkiU74u85aMJA3PLRvg=
7476
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.28 h1:bkRyG4a929RCnpVSTvLM2j/T4ls015ZhhYApbmYs15s=
7577
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.9.28/go.mod h1:jj7znCIg05jXlaGBlFMGP8+7UN3VtCkRBG2spnmRQkU=
7678
github.com/aws/aws-sdk-go-v2/service/sso v1.12.12 h1:nneMBM2p79PGWBQovYO/6Xnc2ryRMw3InnDJq1FHkSY=
@@ -265,6 +267,7 @@ github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/
265267
github.com/iris-contrib/httpexpect/v2 v2.3.1/go.mod h1:ICTf89VBKSD3KB0fsyyHviKF8G8hyepP0dOXJPWz3T0=
266268
github.com/iris-contrib/jade v1.1.4/go.mod h1:EDqR+ur9piDl6DUgs6qRrlfzmlx/D5UybogqrXvJTBE=
267269
github.com/iris-contrib/schema v0.0.6/go.mod h1:iYszG0IOsuIsfzjymw1kMzTL8YQcCWlm65f3wX8J5iA=
270+
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
268271
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
269272
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
270273
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=

tracker/backend/ingestor/handler/handler.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,33 +11,29 @@ import (
1111
"github.com/sirupsen/logrus"
1212
)
1313

14-
func NewBuildReportHandler(cfg aws.Config) *BuildReportHandler {
14+
func NewBuildReportHandler(store SampleStorageFunc) *BuildReportHandler {
1515
hdlr := &BuildReportHandler{
16-
Config: cfg,
17-
storeSample: func(ctx context.Context, sample PackageSample) error {
18-
logrus.WithField("sample", sample).Info("package sample")
19-
return nil
20-
},
16+
storeSample: store,
2117
}
2218
return hdlr
2319
}
2420

2521
type BuildReportHandler struct {
2622
Config aws.Config
27-
storeSample func(ctx context.Context, sample PackageSample) error
23+
storeSample SampleStorageFunc
2824

2925
v1connect.UnimplementedReporterServiceHandler
3026
}
3127

3228
// BuildFinished implements v1connect.ReporterServiceHandler
3329
func (handler *BuildReportHandler) BuildFinished(ctx context.Context, req *connect_go.Request[v1.BuildFinishedRequest]) (*connect_go.Response[v1.EmptyResponse], error) {
34-
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).Info("BuildFinished")
30+
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).Debug("BuildFinished")
3531
return &connect_go.Response[v1.EmptyResponse]{Msg: &v1.EmptyResponse{}}, nil
3632
}
3733

3834
// BuildStarted implements v1connect.ReporterServiceHandler
3935
func (handler *BuildReportHandler) BuildStarted(ctx context.Context, req *connect_go.Request[v1.BuildStartedRequest]) (*connect_go.Response[v1.EmptyResponse], error) {
40-
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("status", req.Msg.Status).Info("BuildStarted")
36+
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("status", req.Msg.Status).Debug("BuildStarted")
4137
return &connect_go.Response[v1.EmptyResponse]{Msg: &v1.EmptyResponse{}}, nil
4238
}
4339

@@ -56,25 +52,26 @@ func (handler *BuildReportHandler) PackageBuildFinished(ctx context.Context, req
5652
Time: time.Now(),
5753
Status: status,
5854
DirtyWorkingCopy: req.Msg.Package.DirtyWorkingCopy,
55+
Type: req.Msg.Package.Type,
5956
}
6057
err := handler.storeSample(ctx, sample)
6158
if err != nil {
6259
return nil, err
6360
}
6461

65-
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("dur", req.Msg.DurationMs).WithField("success", req.Msg.Error == "").Info("PackageBuildFinished")
62+
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("dur", req.Msg.DurationMs).WithField("success", req.Msg.Error == "").Debug("PackageBuildFinished")
6663
return &connect_go.Response[v1.EmptyResponse]{Msg: &v1.EmptyResponse{}}, nil
6764
}
6865

6966
// PackageBuildLog implements v1connect.ReporterServiceHandler
7067
func (handler *BuildReportHandler) PackageBuildLog(ctx context.Context, req *connect_go.Request[v1.PackageBuildLogRequest]) (*connect_go.Response[v1.EmptyResponse], error) {
71-
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.PackageName).Info("PackageBuildLog")
68+
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.PackageName).Debug("PackageBuildLog")
7269
return &connect_go.Response[v1.EmptyResponse]{Msg: &v1.EmptyResponse{}}, nil
7370
}
7471

7572
// PackageBuildStarted implements v1connect.ReporterServiceHandler
7673
func (handler *BuildReportHandler) PackageBuildStarted(ctx context.Context, req *connect_go.Request[v1.PackageBuildStartedRequest]) (*connect_go.Response[v1.EmptyResponse], error) {
77-
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("dirtyWorkingCopy", req.Msg.Package.DirtyWorkingCopy).Info("PackageBuildStarted")
74+
logrus.WithField("session", req.Msg.SessionId).WithField("pkg", req.Msg.Package.Name).WithField("dirtyWorkingCopy", req.Msg.Package.DirtyWorkingCopy).Debug("PackageBuildStarted")
7875
return &connect_go.Response[v1.EmptyResponse]{Msg: &v1.EmptyResponse{}}, nil
7976
}
8077

tracker/backend/ingestor/handler/sample.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
package handler
22

3-
import "time"
3+
import (
4+
context "context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/aws/aws-sdk-go-v2/aws"
9+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
10+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch/types"
11+
v1 "github.com/gitpod-io/leeway/pkg/remotereporter/api/gen/v1"
12+
"github.com/sirupsen/logrus"
13+
)
414

515
type PackageSample struct {
616
FullName string
717
DirtyWorkingCopy bool
18+
Type v1.PackageType
819
Status PackageStatus
920
Time time.Time
1021
BuildDuration time.Duration
@@ -17,3 +28,33 @@ const (
1728
PackageStatusFailed PackageStatus = "failed"
1829
PackageStatusFailedTests PackageStatus = "failed_tests"
1930
)
31+
32+
type SampleStorageFunc func(ctx context.Context, sample PackageSample) error
33+
34+
func PrintSample(ctx context.Context, sample PackageSample) error {
35+
logrus.WithField("sample", sample).Info("package sample")
36+
return nil
37+
}
38+
39+
func PutCloudwatchMetric(cw *cloudwatch.Client) SampleStorageFunc {
40+
return func(ctx context.Context, sample PackageSample) error {
41+
_, err := cw.PutMetricData(ctx, &cloudwatch.PutMetricDataInput{
42+
Namespace: aws.String("leeway"),
43+
MetricData: []types.MetricDatum{
44+
{
45+
Timestamp: aws.Time(sample.Time),
46+
Value: aws.Float64(sample.BuildDuration.Seconds()),
47+
Unit: types.StandardUnitSeconds,
48+
MetricName: aws.String("package_build_duration"),
49+
Dimensions: []types.Dimension{
50+
{Name: aws.String("name"), Value: aws.String(sample.FullName)},
51+
{Name: aws.String("status"), Value: aws.String(string(sample.Status))},
52+
{Name: aws.String("type"), Value: aws.String(sample.Type.String())},
53+
{Name: aws.String("dirtyWorkingCopy"), Value: aws.String(strconv.FormatBool(sample.DirtyWorkingCopy))},
54+
},
55+
},
56+
},
57+
})
58+
return err
59+
}
60+
}

tracker/backend/ingestor/main.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/aws/aws-lambda-go/lambda"
1111
"github.com/aws/aws-sdk-go-v2/config"
12+
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
1213
"github.com/awslabs/aws-lambda-go-api-proxy/httpadapter"
1314
grpcreflect "github.com/bufbuild/connect-grpcreflect-go"
1415
"github.com/sirupsen/logrus"
@@ -18,20 +19,35 @@ import (
1819
)
1920

2021
var (
21-
listen = flag.String("listen", ":8080", "address to listen on when not running as lambda")
22+
listen = flag.String("listen", ":8080", "address to listen on when not running as lambda")
23+
verbose = flag.Bool("verbose", false, "enable verbose logging")
24+
sampleSink = flag.String("sample-sink", "console", "where to write samples to. Valid values are: console, cloudwatch")
2225
)
2326

2427
func main() {
2528
flag.Parse()
2629

30+
if *verbose {
31+
logrus.SetLevel(logrus.DebugLevel)
32+
}
33+
2734
cfg, err := config.LoadDefaultConfig(context.TODO())
2835
if err != nil {
2936
log.Fatal(err)
3037
}
3138

3239
mux := http.NewServeMux()
3340

34-
mux.Handle(v1connect.NewReporterServiceHandler(handler.NewBuildReportHandler(cfg)))
41+
var store handler.SampleStorageFunc
42+
switch *sampleSink {
43+
case "console":
44+
store = handler.PrintSample
45+
case "cloudwatch":
46+
store = handler.PutCloudwatchMetric(cloudwatch.NewFromConfig(cfg))
47+
default:
48+
logrus.Fatalf("unsupported --sample-sink: %s", *sampleSink)
49+
}
50+
mux.Handle(v1connect.NewReporterServiceHandler(handler.NewBuildReportHandler(store)))
3551

3652
reflector := grpcreflect.NewStaticReflector(
3753
v1connect.ReporterServiceName,

0 commit comments

Comments
 (0)