WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Commit 0a7b8f7

Browse files
authored
Merge branch 'main' into INS-172-Update-GitLab-Detector-Regex
2 parents 6e95b65 + 674f626 commit 0a7b8f7

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

pkg/sources/docker/docker.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,15 @@ func (s *Source) Init(ctx context.Context, name string, jobId sources.JobID, sou
6969
s.verify = verify
7070
s.concurrency = concurrency
7171

72+
jobIDStr := fmt.Sprint(s.jobId)
73+
7274
// Reset metrics for this source at initialization time.
73-
dockerImagesScanned.WithLabelValues(s.name).Set(0)
74-
dockerLayersScanned.WithLabelValues(s.name).Set(0)
75+
dockerImagesScanned.WithLabelValues(s.name, jobIDStr).Set(0)
76+
dockerLayersScanned.WithLabelValues(s.name, jobIDStr).Set(0)
77+
dockerLayersEnumerated.WithLabelValues(s.name, jobIDStr).Set(0)
78+
dockerHistoryEntriesEnumerated.WithLabelValues(s.name, jobIDStr).Set(0)
79+
dockerImagesEnumerated.WithLabelValues(s.name, jobIDStr).Set(0)
80+
dockerHistoryEntriesScanned.WithLabelValues(s.name, jobIDStr).Set(0)
7581

7682
if err := anypb.UnmarshalTo(connection, &s.conn, proto.UnmarshalOptions{}); err != nil {
7783
return fmt.Errorf("error unmarshalling connection: %w", err)
@@ -115,6 +121,7 @@ type layerInfo struct {
115121
// Chunks emits data over a channel that is decoded and scanned for secrets.
116122
func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ ...sources.ChunkingTarget) error {
117123
ctx = context.WithValues(ctx, "source_type", s.Type(), "source_name", s.name)
124+
jobIDStr := fmt.Sprint(s.jobId)
118125

119126
workers := new(errgroup.Group)
120127
workers.SetLimit(s.concurrency)
@@ -153,21 +160,23 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ .
153160
imageCtx.Logger().Error(err, "error getting image layers")
154161
continue
155162
}
163+
dockerLayersEnumerated.WithLabelValues(s.name, jobIDStr).Add(float64(len(layers)))
156164

157165
// Get history entries and associate them with layers
158166
historyEntries, err := getHistoryEntries(imageCtx, imgInfo, layers)
159167
if err != nil {
160168
imageCtx.Logger().Error(err, "error getting image history entries")
161169
continue
162170
}
171+
dockerHistoryEntriesEnumerated.WithLabelValues(s.name, jobIDStr).Add(float64(len(historyEntries)))
163172

164173
// Scan each history entry for secrets in build commands
165174
for _, historyEntry := range historyEntries {
166175
if err := s.processHistoryEntry(imageCtx, historyEntry, chunksChan); err != nil {
167176
imageCtx.Logger().Error(err, "error processing history entry")
168177
continue
169178
}
170-
dockerHistoryEntriesScanned.WithLabelValues(s.name).Inc()
179+
dockerHistoryEntriesScanned.WithLabelValues(s.name, jobIDStr).Inc()
171180
}
172181

173182
imageCtx.Logger().V(2).Info("scanning image layers")
@@ -179,7 +188,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ .
179188
imageCtx.Logger().Error(err, "error processing layer")
180189
return nil
181190
}
182-
dockerLayersScanned.WithLabelValues(s.name).Inc()
191+
dockerLayersScanned.WithLabelValues(s.name, jobIDStr).Inc()
183192

184193
return nil
185194
})
@@ -190,7 +199,7 @@ func (s *Source) Chunks(ctx context.Context, chunksChan chan *sources.Chunk, _ .
190199
continue
191200
}
192201

193-
dockerImagesScanned.WithLabelValues(s.name).Inc()
202+
dockerImagesScanned.WithLabelValues(s.name, jobIDStr).Inc()
194203
}
195204

196205
return nil
@@ -502,7 +511,7 @@ func (s *Source) remoteOpts() ([]remote.Option, error) {
502511
}
503512

504513
var opts []remote.Option
505-
opts = append(opts, remote.WithTransport(defaultTransport))
514+
opts = append(opts, remote.WithTransport(common.NewInstrumentedTransport(common.NewCustomTransport(defaultTransport))))
506515

507516
// Configure authentication based on credential type
508517
switch s.conn.GetCredential().(type) {

pkg/sources/docker/docker_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,9 @@ func TestQuayRegistry(t *testing.T) {
109109
close(chunksChan)
110110
wg.Wait()
111111

112-
assert.Equal(t, 945, chunkCounter)
112+
assert.Equal(t, 944, chunkCounter)
113113
assert.Equal(t, 941, layerCounter)
114-
assert.Equal(t, 4, historyCounter)
114+
assert.Equal(t, 3, historyCounter)
115115
}
116116

117117
func TestGHCRRegistry(t *testing.T) {

pkg/sources/docker/metrics.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,48 @@ var (
1414
Name: "docker_layers_scanned",
1515
Help: "Total number of Docker layers scanned.",
1616
},
17-
[]string{"source_name"})
17+
[]string{"source_name", "job_id"})
18+
19+
dockerLayersEnumerated = promauto.NewGaugeVec(
20+
prometheus.GaugeOpts{
21+
Namespace: common.MetricsNamespace,
22+
Subsystem: common.MetricsSubsystem,
23+
Name: "docker_layers_enumerated",
24+
Help: "Total number of Docker layers enumerated.",
25+
},
26+
[]string{"source_name", "job_id"})
1827

1928
dockerHistoryEntriesScanned = promauto.NewGaugeVec(prometheus.GaugeOpts{
2029
Namespace: common.MetricsNamespace,
2130
Subsystem: common.MetricsSubsystem,
2231
Name: "docker_history_entries_scanned",
2332
Help: "Total number of Docker image history entries scanned.",
2433
},
25-
[]string{"source_name"})
34+
[]string{"source_name", "job_id"})
35+
36+
dockerHistoryEntriesEnumerated = promauto.NewGaugeVec(prometheus.GaugeOpts{
37+
Namespace: common.MetricsNamespace,
38+
Subsystem: common.MetricsSubsystem,
39+
Name: "docker_history_entries_enumerated",
40+
Help: "Total number of Docker history entries enumerated.",
41+
},
42+
[]string{"source_name", "job_id"})
2643

2744
dockerImagesScanned = promauto.NewGaugeVec(prometheus.GaugeOpts{
2845
Namespace: common.MetricsNamespace,
2946
Subsystem: common.MetricsSubsystem,
3047
Name: "docker_images_scanned",
3148
Help: "Total number of Docker images scanned.",
3249
},
33-
[]string{"source_name"})
50+
[]string{"source_name", "job_id"})
51+
52+
dockerImagesEnumerated = promauto.NewGaugeVec(prometheus.GaugeOpts{
53+
Namespace: common.MetricsNamespace,
54+
Subsystem: common.MetricsSubsystem,
55+
Name: "docker_images_enumerated",
56+
Help: "Total number of Docker images enumerated.",
57+
},
58+
[]string{"source_name", "job_id"})
3459

3560
dockerListImagesAPIDuration = promauto.NewHistogramVec(
3661
prometheus.HistogramOpts{

0 commit comments

Comments
 (0)