Skip to content

Commit

Permalink
all: fix CR issues
Browse files Browse the repository at this point in the history
  • Loading branch information
seilagamo committed Nov 7, 2023
1 parent 3e8e54d commit c4cf556
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 392 deletions.
22 changes: 5 additions & 17 deletions cmd/lava/internal/run/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ package run
import (
"errors"
"fmt"
"log/slog"
"net/url"
"os"
"path/filepath"
"time"
Expand Down Expand Up @@ -42,9 +40,8 @@ output is disabled in the following cases:
}

var (
cfgfile = CmdRun.Flag.String("c", "lava.yaml", "config file")
forceColor = CmdRun.Flag.Bool("forcecolor", false, "force colorized output")
metricsFile = CmdRun.Flag.String("m", "", "metrics output file")
cfgfile = CmdRun.Flag.String("c", "lava.yaml", "config file")
forceColor = CmdRun.Flag.Bool("forcecolor", false, "force colorized output")
)

func init() {
Expand Down Expand Up @@ -74,14 +71,6 @@ func run(args []string) error {
}
metrics.Collect("lava_version", cfg.LavaVersion)
metrics.Collect("targets", cfg.Targets)
if os.Getenv("GITHUB_SERVER_URL") != "" && os.Getenv("GITHUB_REPOSITORY") != "" {
ghRepo, err := url.JoinPath(os.Getenv("GITHUB_SERVER_URL"), os.Getenv("GITHUB_REPOSITORY"))
if err != nil {
slog.Warn("error collecting the GitHub repo URI")
} else {
metrics.Collect("repository_uri", ghRepo)
}
}

base.LogLevel.Set(cfg.LogLevel)
er, err := engine.Run(cfg.ChecktypesURLs, cfg.Targets, cfg.AgentConfig)
Expand All @@ -101,12 +90,11 @@ func run(args []string) error {
}

metrics.Collect("exit_code", exitCode)
finishTime := time.Now()
duration := finishTime.Sub(executionTime)
duration := time.Now().Sub(executionTime)

Check failure on line 93 in cmd/lava/internal/run/run.go

View workflow job for this annotation

GitHub Actions / Lint

S1012: should use `time.Since` instead of `time.Now().Sub` (gosimple)
metrics.Collect("duration", duration.String())

if *metricsFile != "" {
if err = metrics.Write(*metricsFile); err != nil {
if cfg.ReportConfig.Metrics != "" {
if err = metrics.WriteFile(cfg.ReportConfig.Metrics); err != nil {
return fmt.Errorf("write metrics: %w", err)
}
}
Expand Down
6 changes: 4 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ type ReportConfig struct {
// instance, accepted risks, false positives, etc.
Exclusions []Exclusion `yaml:"exclusions"`

// Metrics decides is a metrics report is printed.
Metrics bool `yaml:"metrics"`
// Metrics is the file where the metrics will be writen.

Check failure on line 152 in internal/config/config.go

View workflow job for this annotation

GitHub Actions / Lint

`writen` is a misspelling of `written` (misspell)
// If Metrics is an empty string (or not specified in the yaml file) then,
// the metrics report is not saved.
Metrics string `yaml:"metrics"`
}

// Target represents the target of a scan.
Expand Down
70 changes: 70 additions & 0 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright 2023 Adevinta

// Package metrics collects Lava execution metrics.
package metrics

import (
"encoding/json"
"fmt"
"io"
"os"
"sync"
)

// DefaultCollector is the default [Collector].
var DefaultCollector = NewCollector()

// Collector represents a metrics collector.
type Collector struct {
mutex sync.Mutex
metrics map[string]any
}

// NewCollector returns a new metrics collector.
func NewCollector() *Collector {
return &Collector{
metrics: make(map[string]any),
}
}

// Collect records a metric with the provided name and value.
func (c *Collector) Collect(name string, value any) {
c.mutex.Lock()
defer c.mutex.Unlock()

c.metrics[name] = value
}

// Write writes the metrics to the specified [io.Writer].
func (c *Collector) Write(w io.Writer) error {
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
if err := enc.Encode(c.metrics); err != nil {
return fmt.Errorf("encode JSON: %w", err)
}
return nil
}

// Collect records a metric with the provided name and value using
// [DefaultCollector].
func Collect(name string, value any) {
DefaultCollector.Collect(name, value)
}

// Write writes the collected metrics to the specified [io.Writer]
// using [DefaultCollector].
func Write(w io.Writer) error {
return DefaultCollector.Write(w)
}

// WriteFile writes the collected metrics into the specified file
// using [DefaultCollector].
func WriteFile(file string) error {
f, err := os.Create(file)
if err != nil {
return fmt.Errorf("create file: %w", err)
}
defer f.Close()

return Write(f)
}
98 changes: 0 additions & 98 deletions internal/metrics/metrics_collector.go

This file was deleted.

Loading

0 comments on commit c4cf556

Please sign in to comment.