-
Notifications
You must be signed in to change notification settings - Fork 80
/
setlayerscanned.go
75 lines (67 loc) · 1.69 KB
/
setlayerscanned.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
package postgres
import (
"context"
"fmt"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/quay/zlog"
"github.com/quay/claircore"
"github.com/quay/claircore/indexer"
)
var (
setLayerScannedCounter = promauto.NewCounterVec(
prometheus.CounterOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "setlayerscanned_total",
Help: "Total number of database queries issued in the SetLayerScanned method.",
},
[]string{"query"},
)
setLayerScannedDuration = promauto.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "claircore",
Subsystem: "indexer",
Name: "setlayerscanned_duration_seconds",
Help: "The duration of all queries issued in the SetLayerScanned method",
},
[]string{"query"},
)
)
func (s *IndexerStore) SetLayerScanned(ctx context.Context, hash claircore.Digest, vs indexer.VersionedScanner) error {
ctx = zlog.ContextWithValues(ctx, "scanner", vs.Name())
const query = `
WITH
scanner
AS (
SELECT
id
FROM
scanner
WHERE
name = $2 AND version = $3 AND kind = $4
),
layer AS (SELECT id FROM layer WHERE hash = $1)
INSERT
INTO
scanned_layer (layer_id, scanner_id)
VALUES
(
(SELECT id AS layer_id FROM layer),
(SELECT id AS scanner_id FROM scanner)
)
ON CONFLICT
(layer_id, scanner_id)
DO
NOTHING;
`
start := time.Now()
_, err := s.pool.Exec(ctx, query, hash, vs.Name(), vs.Version(), vs.Kind())
if err != nil {
return fmt.Errorf("error setting layer scanned: %w", err)
}
setLayerScannedCounter.WithLabelValues("query").Add(1)
setLayerScannedDuration.WithLabelValues("query").Observe(time.Since(start).Seconds())
return nil
}