-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathworker.go
236 lines (201 loc) · 6.63 KB
/
worker.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
package main
import (
"bytes"
"context"
"errors"
"fmt"
"net/url"
"os"
githubstats "github.com/ossf/scorecard/v4/clients/githubrepo/stats"
"github.com/ossf/scorecard/v4/cron/data"
"github.com/ossf/scorecard/v4/cron/monitoring"
"github.com/ossf/scorecard/v4/cron/worker"
"github.com/ossf/scorecard/v4/stats"
"go.opencensus.io/stats/view"
"go.uber.org/zap"
"github.com/ossf/criticality_score/v2/cmd/collect_signals/vcs"
"github.com/ossf/criticality_score/v2/internal/collector"
"github.com/ossf/criticality_score/v2/internal/scorer"
"github.com/ossf/criticality_score/v2/internal/signalio"
)
const (
collectionDateColumnName = "collection_date"
commitIDColumnName = "worker_commit_id"
)
type collectWorker struct {
logger *zap.Logger
exporter monitoring.Exporter
c *collector.Collector
s *scorer.Scorer
scoreColumnName string
csvBucketURL string
}
// Process implements the worker.Worker interface.
func (w *collectWorker) Process(ctx context.Context, req *data.ScorecardBatchRequest, bucketURL string) error {
filename := worker.ResultFilename(req)
jobTime := req.GetJobTime().AsTime()
jobID := jobTime.Format("20060102_150405")
// Prepare the logger with identifiers for the shard and job.
logger := w.logger.With(
zap.Int32("shard_id", req.GetShardNum()),
zap.Time("job_time", jobTime),
zap.String("filename", filename),
)
logger.Info("Processing shard")
// Prepare the output writer
extras := []string{}
if w.s != nil {
extras = append(extras, w.scoreColumnName)
}
extras = append(extras, collectionDateColumnName)
if commitID := vcs.CommitID(); commitID != vcs.MissingCommitID {
extras = append(extras, commitIDColumnName)
}
var jsonOutput bytes.Buffer
jsonOut := signalio.JSONWriter(&jsonOutput)
var csvOutput bytes.Buffer
csvOut := signalio.CSVWriter(&csvOutput, w.c.EmptySets(), extras...)
// Iterate through the repos in this shard.
for _, repo := range req.GetRepos() {
rawURL := repo.GetUrl()
if rawURL == "" {
logger.Warn("Skipping empty repo URL")
continue
}
// Create a logger for this repo.
repoLogger := logger.With(zap.String("repo", rawURL))
repoLogger.Info("Processing repo")
// Parse the URL to ensure it is a URL.
u, err := url.Parse(rawURL)
if err != nil {
// TODO: record a metric
repoLogger.With(zap.Error(err)).Warn("Failed to parse repo URL")
continue
}
ss, err := w.c.Collect(ctx, u, jobID)
if err != nil {
if errors.Is(err, collector.ErrUncollectableRepo) {
repoLogger.With(zap.Error(err)).Warn("Repo is uncollectable")
continue
}
return fmt.Errorf("failed during signal collection: %w", err)
}
// If scoring is enabled, prepare the extra data to be output.
extras := []signalio.Field{}
if w.s != nil {
f := signalio.Field{
Key: w.scoreColumnName,
Value: fmt.Sprintf("%.5f", w.s.Score(ss)),
}
extras = append(extras, f)
}
// Ensure the collection date is included with each record for paritioning.
extras = append(extras, signalio.Field{
Key: collectionDateColumnName,
Value: jobTime,
})
// Ensure the commit ID is included with each record for helping
// identify which Git commit is associated with this record.
if commitID := vcs.CommitID(); commitID != vcs.MissingCommitID {
extras = append(extras, signalio.Field{
Key: commitIDColumnName,
Value: commitID,
})
}
// Write the signals to storage.
if err := jsonOut.WriteSignals(ss, extras...); err != nil {
return fmt.Errorf("failed writing signals: %w", err)
}
if err := csvOut.WriteSignals(ss, extras...); err != nil {
return fmt.Errorf("failed writing signals: %w", err)
}
}
// Write to the csv bucket if it is set.
if w.csvBucketURL != "" {
if err := data.WriteToBlobStore(ctx, w.csvBucketURL, filename, csvOutput.Bytes()); err != nil {
return fmt.Errorf("error writing csv to blob store: %w", err)
}
}
// Write to the canonical bucket last. The presence of the file indicates
// the job was completed. See scorecard's worker package for details.
if err := data.WriteToBlobStore(ctx, bucketURL, filename, jsonOutput.Bytes()); err != nil {
return fmt.Errorf("error writing json to blob store: %w", err)
}
logger.Info("Shard written successfully")
return nil
}
// Close is called to clean up resources used by the worker.
func (w *collectWorker) Close() {
w.exporter.StopMetricsExporter()
}
// PostProcess implements the worker.Worker interface.
func (w *collectWorker) PostProcess() {
w.exporter.Flush()
}
func getScorer(logger *zap.Logger, scoringEnabled bool, scoringConfigFile string) (*scorer.Scorer, error) {
logger.Debug("Creating scorer")
if !scoringEnabled {
logger.Info("Scoring: disabled")
return nil, nil
}
if scoringConfigFile == "" {
logger.Info("Scoring: using default config")
return scorer.FromDefaultConfig(), nil
}
logger.With(zap.String("filename", scoringConfigFile)).Info("Scoring: using config file")
f, err := os.Open(scoringConfigFile)
if err != nil {
return nil, fmt.Errorf("opening config: %w", err)
}
defer f.Close()
s, err := scorer.FromConfig(scorer.NameFromFilepath(scoringConfigFile), f)
if err != nil {
return nil, fmt.Errorf("from config: %w", err)
}
return s, nil
}
func getMetricsExporter() (monitoring.Exporter, error) {
exporter, err := monitoring.GetExporter()
if err != nil {
return nil, fmt.Errorf("getting monitoring exporter: %w", err)
}
if err := exporter.StartMetricsExporter(); err != nil {
return nil, fmt.Errorf("starting exporter: %w", err)
}
if err := view.Register(
&stats.CheckRuntime,
&stats.CheckErrorCount,
&stats.OutgoingHTTPRequests,
&githubstats.GithubTokens); err != nil {
return nil, fmt.Errorf("opencensus view register: %w", err)
}
return exporter, nil
}
func NewWorker(ctx context.Context, logger *zap.Logger, scoringEnabled bool, scoringConfigFile, scoringColumn, csvBucketURL string, collectOpts []collector.Option) (*collectWorker, error) {
logger.Info("Initializing worker")
c, err := collector.New(ctx, logger, collectOpts...)
if err != nil {
return nil, fmt.Errorf("collector: %w", err)
}
s, err := getScorer(logger, scoringEnabled, scoringConfigFile)
if err != nil {
return nil, fmt.Errorf("scorer: %w", err)
}
// If we have the scorer, and the column isn't overridden, use the scorer's
// name.
if s != nil && scoringColumn == "" {
scoringColumn = s.Name()
}
exporter, err := getMetricsExporter()
if err != nil {
return nil, fmt.Errorf("metrics exporter: %w", err)
}
return &collectWorker{
logger: logger,
c: c,
s: s,
scoreColumnName: scoringColumn,
exporter: exporter,
csvBucketURL: csvBucketURL,
}, nil
}