Skip to content

Commit d5ad67e

Browse files
authored
Minor Improvements: Log Level and Metrics Documentation (#84)
* control log level via env var Signed-off-by: Eric Pickard <piceri@github.com> * rename deptracker_post_record_no_attestation to deptracker_post_record_unknown_artifact Signed-off-by: Eric Pickard <piceri@github.com> * add docs for deptracker_post_record_unknown_artifact_cache_hit Signed-off-by: Eric Pickard <piceri@github.com> * remove trailing space Signed-off-by: Eric Pickard <piceri@github.com> --------- Signed-off-by: Eric Pickard <piceri@github.com>
1 parent 5d8c7c3 commit d5ad67e

5 files changed

Lines changed: 51 additions & 26 deletions

File tree

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Two modes of authentication are supported:
6565

6666
| Variable | Description | Default |
6767
|------------------------|--------------------------------------------|------------------------------------------------------|
68+
| `LOG_LEVEL` | log level: DEBUG, INFO, WARN, ERROR | `INFO` |
6869
| `ORG` | GitHub organization name | (required) |
6970
| `BASE_URL` | API base URL | `api.github.com` |
7071
| `DN_TEMPLATE` | Deployment name template | `{{namespace}}/{{deploymentName}}/{{containerName}}` |
@@ -192,9 +193,12 @@ The metrics exposed beyond the default Prometheus metrics are:
192193
record uploads.
193194
* `deptracker_post_record_rate_limited`: the number of post attempts
194195
that were rate limited.
195-
* `deptracker_post_record_no_attestation`: the number of attempts
196+
* `deptracker_post_record_unknown_artifact`: the number of attempts
196197
that resulted in no matching attestation for the container digest
197-
(404 "no artifacts found" responses).
198+
(404 "no artifacts found" responses) and an entry in the unknown
199+
artifact cache.
200+
* `deptracker_post_record_unknown_artifact_cache_hit`: the number of
201+
attempts to create new records prevented by the unknown artifact cache.
198202
* `deptracker_post_record_soft_fail`: the number of recoverable failed
199203
attempts to upload the deployment record.
200204
* `deptracker_post_record_hard_fail`: the number of failures to

cmd/deployment-tracker/main.go

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"net/http"
1111
"os"
1212
"os/signal"
13+
"strings"
1314
"syscall"
1415
"time"
1516

@@ -65,8 +66,13 @@ func main() {
6566

6667
// init logging
6768
log.SetFlags(log.LstdFlags | log.Lshortfile | log.LUTC)
68-
opts := slog.HandlerOptions{Level: slog.LevelInfo}
69+
logLevelStr := getEnvOrDefault("LOG_LEVEL", "INFO")
70+
level, msg := parseLogLevel(logLevelStr)
71+
opts := slog.HandlerOptions{Level: level}
6972
slog.SetDefault(slog.New(slog.NewJSONHandler(os.Stdout, &opts)))
73+
if msg != "" {
74+
slog.Warn(msg)
75+
}
7076

7177
var ghAppPrivateKey []byte
7278
if b64Key := os.Getenv("GH_APP_PRIVATE_KEY"); b64Key != "" {
@@ -220,3 +226,18 @@ func createK8sConfig(kubeconfig string) (*rest.Config, error) {
220226
}
221227
return clientcmd.BuildConfigFromFlags("", homeDir+"/.kube/config")
222228
}
229+
230+
func parseLogLevel(logLevel string) (slog.Level, string) {
231+
switch strings.ToUpper(logLevel) {
232+
case "DEBUG":
233+
return slog.LevelDebug, ""
234+
case "INFO":
235+
return slog.LevelInfo, ""
236+
case "WARN":
237+
return slog.LevelWarn, ""
238+
case "ERROR":
239+
return slog.LevelError, ""
240+
default:
241+
return slog.LevelInfo, fmt.Sprintf("%s is an unsupported log level (DEBUG, WARN, INFO, ERROR), using INFO...", logLevel)
242+
}
243+
}

pkg/deploymentrecord/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func (c *Client) PostOne(ctx context.Context, record *DeploymentRecord) error {
265265
switch {
266266
case resp.StatusCode == 404:
267267
// No artifact found - do not retry
268-
dtmetrics.PostDeploymentRecordNoAttestation.Inc()
268+
dtmetrics.PostDeploymentRecordUnknownArtifact.Inc()
269269
slog.Debug("no artifact attestation found, no record created",
270270
"attempt", attempt,
271271
"status_code", resp.StatusCode,

pkg/deploymentrecord/client_test.go

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ func testRecord() *DeploymentRecord {
298298
func allCounters() []prometheus.Counter {
299299
return []prometheus.Counter{
300300
dtmetrics.PostDeploymentRecordOk,
301-
dtmetrics.PostDeploymentRecordNoAttestation,
301+
dtmetrics.PostDeploymentRecordUnknownArtifact,
302302
dtmetrics.PostDeploymentRecordRateLimited,
303303
dtmetrics.PostDeploymentRecordSoftFail,
304304
dtmetrics.PostDeploymentRecordHardFail,
@@ -308,19 +308,19 @@ func allCounters() []prometheus.Counter {
308308

309309
func TestPostOne(t *testing.T) {
310310
tests := []struct {
311-
name string
312-
record *DeploymentRecord
313-
retries int
314-
handler http.HandlerFunc
315-
wantErr bool
316-
errType any // expected error type for errors.As
317-
errContain string
318-
wantOk float64
319-
wantNoAttestation float64
320-
wantRateLimited float64
321-
wantSoftFail float64
322-
wantHardFail float64
323-
wantClientError float64
311+
name string
312+
record *DeploymentRecord
313+
retries int
314+
handler http.HandlerFunc
315+
wantErr bool
316+
errType any // expected error type for errors.As
317+
errContain string
318+
wantOk float64
319+
wantUnknownArtifact float64
320+
wantRateLimited float64
321+
wantSoftFail float64
322+
wantHardFail float64
323+
wantClientError float64
324324
}{
325325
{
326326
name: "success on 200",
@@ -354,10 +354,10 @@ func TestPostOne(t *testing.T) {
354354
w.WriteHeader(http.StatusNotFound)
355355
_, _ = w.Write([]byte(`{"message":"no artifacts found"}`))
356356
},
357-
wantErr: true,
358-
errType: &NoArtifactError{},
359-
errContain: "sha256:abc123",
360-
wantNoAttestation: 1,
357+
wantErr: true,
358+
errType: &NoArtifactError{},
359+
errContain: "sha256:abc123",
360+
wantUnknownArtifact: 1,
361361
},
362362
{
363363
name: "400 returns ClientError",
@@ -555,15 +555,15 @@ func TestPostOne(t *testing.T) {
555555
// Assert all metric deltas
556556
wantDeltas := []float64{
557557
tt.wantOk,
558-
tt.wantNoAttestation,
558+
tt.wantUnknownArtifact,
559559
tt.wantRateLimited,
560560
tt.wantSoftFail,
561561
tt.wantHardFail,
562562
tt.wantClientError,
563563
}
564564
names := []string{
565565
"PostDeploymentRecordOk",
566-
"PostDeploymentRecordNoAttestation",
566+
"PostDeploymentRecordUnknownArtifact",
567567
"PostDeploymentRecordRateLimited",
568568
"PostDeploymentRecordSoftFail",
569569
"PostDeploymentRecordHardFail",

pkg/dtmetrics/prom.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ var (
5050
)
5151

5252
//nolint: revive
53-
PostDeploymentRecordNoAttestation = promauto.NewCounter(
53+
PostDeploymentRecordUnknownArtifact = promauto.NewCounter(
5454
prometheus.CounterOpts{
55-
Name: "deptracker_post_record_no_attestation",
55+
Name: "deptracker_post_record_unknown_artifact",
5656
Help: "The total number of post attempts that resulted in no matching attestation for the container digest (404 'no artifacts found' responses)",
5757
},
5858
)

0 commit comments

Comments
 (0)