Skip to content

Commit 0aea812

Browse files
committed
monitor: replaced basic RPC with gRPC
1 parent 5ceabea commit 0aea812

File tree

15 files changed

+1055
-360
lines changed

15 files changed

+1055
-360
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ and this project adheres to [0ver](https://0ver.org) (more or less).
2323
- Upgraded most dependencies to their lastest versions
2424
- Fixed child pipeline jobs not found whilst looking up through bridges (#345)
2525
- `gitlab_ci_pipeline_job_queued_duration_seconds` & `gitlab_ci_pipeline_queued_duration_seconds` will now be leveraging the value returned through the GitLab API instead of computing it with (startedAt - createdAt)
26+
- Refactored the RPC layer used for CLI monitoring with gRPC
2627

2728
## [v0.5.3] - 2022-02-11
2829

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ release: ## Build & release the binaries (stable)
3434
goreleaser release --rm-dist
3535
find dist -type f -name "*.snap" -exec snapcraft upload --release stable,edge '{}' \;
3636

37+
.PHONY: protoc
38+
protoc: setup ## Generate golang from .proto files
39+
@command -v protoc 2>&1 >/dev/null || (echo "protoc needs to be available in PATH: https://github.com/protocolbuffers/protobuf/releases"; false)
40+
@command -v protoc-gen-go 2>&1 >/dev/null || go install google.golang.org/grpc/cmd/[email protected]
41+
protoc \
42+
--go_out=. --go_opt=paths=source_relative \
43+
--go-grpc_out=. --go-grpc_opt=paths=source_relative \
44+
pkg/monitor/protobuf/monitor.proto
45+
3746
.PHONY: prerelease
3847
prerelease: setup ## Build & prerelease the binaries (edge)
3948
@\

examples/opentelemetry/gitlab-ci-pipelines-exporter.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ gitlab:
1313
redis:
1414
url: redis://redis:6379
1515

16-
1716
# Example public projects to monitor
1817
projects:
1918
- name: gitlab-org/gitlab-runner

internal/cmd/run.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111

1212
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/controller"
13-
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/monitor/rpc"
13+
monitoringServer "github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/monitor/server"
1414
log "github.com/sirupsen/logrus"
1515
"github.com/urfave/cli/v2"
1616
)
@@ -32,14 +32,13 @@ func Run(cliCtx *cli.Context) (int, error) {
3232

3333
// Start the monitoring RPC server
3434
go func(c *controller.Controller) {
35-
rpc.ServeUNIX(
36-
rpc.NewServer(
37-
c.Gitlab,
38-
c.Config,
39-
c.Store,
40-
c.TaskController.TaskSchedulingMonitoring,
41-
),
35+
s := monitoringServer.NewServer(
36+
c.Gitlab,
37+
c.Config,
38+
c.Store,
39+
c.TaskController.TaskSchedulingMonitoring,
4240
)
41+
s.Serve()
4342
}(&c)
4443

4544
// Graceful shutdowns

pkg/controller/scheduler.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -455,11 +455,8 @@ func (c *Controller) ScheduleTaskWithTicker(ctx context.Context, tt schemas.Task
455455

456456
return
457457
case <-ticker.C:
458-
switch tt {
459-
default:
460-
c.ScheduleTask(ctx, tt, "_")
461-
c.TaskController.monitorNextTaskScheduling(tt, intervalSeconds)
462-
}
458+
c.ScheduleTask(ctx, tt, "_")
459+
c.TaskController.monitorNextTaskScheduling(tt, intervalSeconds)
463460
}
464461
}
465462
}(ctx)

pkg/monitor/client/client.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"net/url"
6+
7+
pb "github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/monitor/protobuf"
8+
log "github.com/sirupsen/logrus"
9+
"google.golang.org/grpc"
10+
"google.golang.org/grpc/credentials/insecure"
11+
)
12+
13+
// Client ..
14+
type Client struct {
15+
pb.MonitorClient
16+
}
17+
18+
// NewClient ..
19+
func NewClient(ctx context.Context, endpoint *url.URL) *Client {
20+
log.WithField("endpoint", endpoint.String()).Debug("establishing gRPC connection to the server..")
21+
22+
conn, err := grpc.DialContext(
23+
ctx,
24+
endpoint.String(),
25+
grpc.WithTransportCredentials(insecure.NewCredentials()),
26+
)
27+
if err != nil {
28+
log.WithField("endpoint", endpoint.String()).WithField("error", err).Fatal("could not connect to the server")
29+
}
30+
31+
log.Debug("gRPC connection established")
32+
33+
return &Client{
34+
MonitorClient: pb.NewMonitorClient(conn),
35+
}
36+
}

pkg/monitor/monitor.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package monitor
2+
3+
import "time"
4+
5+
type TaskSchedulingStatus struct {
6+
Last time.Time
7+
Next time.Time
8+
}

0 commit comments

Comments
 (0)