/
controller.go
204 lines (164 loc) · 6 KB
/
controller.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package controller
import (
"context"
"github.com/google/uuid"
"github.com/pkg/errors"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
log "github.com/sirupsen/logrus"
"github.com/vmihailenco/taskq/v4"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
semconv "go.opentelemetry.io/otel/semconv/v1.7.0"
"google.golang.org/grpc"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/config"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/gitlab"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/ratelimit"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/schemas"
"github.com/mvisonneau/gitlab-ci-pipelines-exporter/pkg/store"
)
const tracerName = "gitlab-ci-pipelines-exporter"
// Controller holds the necessary clients to run the app and handle requests.
type Controller struct {
Config config.Config
Redis *redis.Client
Gitlab *gitlab.Client
Store store.Store
TaskController TaskController
// UUID is used to identify this controller/process amongst others when
// the exporter is running in cluster mode, leveraging Redis.
UUID uuid.UUID
}
// New creates a new controller.
func New(ctx context.Context, cfg config.Config, version string) (c Controller, err error) {
c.Config = cfg
c.UUID = uuid.New()
if err = configureTracing(ctx, cfg.OpenTelemetry.GRPCEndpoint); err != nil {
return
}
if err = c.configureRedis(ctx, cfg.Redis.URL); err != nil {
return
}
c.TaskController = NewTaskController(ctx, c.Redis, cfg.Gitlab.MaximumJobsQueueSize)
c.registerTasks()
c.Store = store.New(ctx, c.Redis, c.Config.Projects)
if err = c.configureGitlab(cfg.Gitlab, version); err != nil {
return
}
// Start the scheduler
c.Schedule(ctx, cfg.Pull, cfg.GarbageCollect)
return
}
func (c *Controller) registerTasks() {
for n, h := range map[schemas.TaskType]interface{}{
schemas.TaskTypeGarbageCollectEnvironments: c.TaskHandlerGarbageCollectEnvironments,
schemas.TaskTypeGarbageCollectMetrics: c.TaskHandlerGarbageCollectMetrics,
schemas.TaskTypeGarbageCollectProjects: c.TaskHandlerGarbageCollectProjects,
schemas.TaskTypeGarbageCollectRefs: c.TaskHandlerGarbageCollectRefs,
schemas.TaskTypePullEnvironmentMetrics: c.TaskHandlerPullEnvironmentMetrics,
schemas.TaskTypePullEnvironmentsFromProject: c.TaskHandlerPullEnvironmentsFromProject,
schemas.TaskTypePullEnvironmentsFromProjects: c.TaskHandlerPullEnvironmentsFromProjects,
schemas.TaskTypePullMetrics: c.TaskHandlerPullMetrics,
schemas.TaskTypePullProject: c.TaskHandlerPullProject,
schemas.TaskTypePullProjectsFromWildcard: c.TaskHandlerPullProjectsFromWildcard,
schemas.TaskTypePullProjectsFromWildcards: c.TaskHandlerPullProjectsFromWildcards,
schemas.TaskTypePullRefMetrics: c.TaskHandlerPullRefMetrics,
schemas.TaskTypePullRefsFromProject: c.TaskHandlerPullRefsFromProject,
schemas.TaskTypePullRefsFromProjects: c.TaskHandlerPullRefsFromProjects,
} {
_, _ = c.TaskController.TaskMap.Register(string(n), &taskq.TaskConfig{
Handler: h,
RetryLimit: 1,
})
}
}
func (c *Controller) unqueueTask(ctx context.Context, tt schemas.TaskType, uniqueID string) {
if err := c.Store.UnqueueTask(ctx, tt, uniqueID); err != nil {
log.WithContext(ctx).
WithFields(log.Fields{
"task_type": tt,
"task_unique_id": uniqueID,
}).
WithError(err).
Warn("unqueuing task")
}
}
func configureTracing(ctx context.Context, grpcEndpoint string) error {
if len(grpcEndpoint) == 0 {
log.Debug("opentelemetry.grpc_endpoint is not configured, skipping open telemetry support")
return nil
}
log.WithFields(log.Fields{
"opentelemetry_grpc_endpoint": grpcEndpoint,
}).Info("opentelemetry gRPC endpoint provided, initializing connection..")
traceClient := otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(grpcEndpoint),
otlptracegrpc.WithDialOption(grpc.WithBlock()))
traceExp, err := otlptrace.New(ctx, traceClient)
if err != nil {
return err
}
res, err := resource.New(ctx,
resource.WithFromEnv(),
resource.WithProcess(),
resource.WithTelemetrySDK(),
resource.WithHost(),
resource.WithAttributes(
semconv.ServiceNameKey.String("gitlab-ci-pipelines-exporter"),
),
)
if err != nil {
return err
}
bsp := sdktrace.NewBatchSpanProcessor(traceExp)
tracerProvider := sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithResource(res),
sdktrace.WithSpanProcessor(bsp),
)
otel.SetTracerProvider(tracerProvider)
return nil
}
func (c *Controller) configureGitlab(cfg config.Gitlab, version string) (err error) {
var rl ratelimit.Limiter
if c.Redis != nil {
rl = ratelimit.NewRedisLimiter(c.Redis, cfg.MaximumRequestsPerSecond)
} else {
rl = ratelimit.NewLocalLimiter(cfg.MaximumRequestsPerSecond, cfg.BurstableRequestsPerSecond)
}
c.Gitlab, err = gitlab.NewClient(gitlab.ClientConfig{
URL: cfg.URL,
Token: cfg.Token,
DisableTLSVerify: !cfg.EnableTLSVerify,
UserAgentVersion: version,
RateLimiter: rl,
ReadinessURL: cfg.HealthURL,
})
return
}
func (c *Controller) configureRedis(ctx context.Context, url string) (err error) {
ctx, span := otel.Tracer(tracerName).Start(ctx, "controller:configureRedis")
defer span.End()
if len(url) <= 0 {
log.Debug("redis url is not configured, skipping configuration & using local driver")
return
}
log.Info("redis url configured, initializing connection..")
var opt *redis.Options
if opt, err = redis.ParseURL(url); err != nil {
return
}
c.Redis = redis.NewClient(opt)
if err = redisotel.InstrumentTracing(c.Redis); err != nil {
return
}
if _, err := c.Redis.Ping(ctx).Result(); err != nil {
return errors.Wrap(err, "connecting to redis")
}
log.Info("connected to redis")
return
}