Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use slog #19

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/sirupsen/logrus"
)

// BuildRequest defines a request to the build service
Expand Down Expand Up @@ -38,21 +37,26 @@ type BuildResponse struct {
// APIServerConfig defines the configuration for the APIServer
type APIServerConfig struct {
BuildService BuildService
Log *logrus.Logger
Log *slog.Logger
}

// APIServer defines a k6build API server
type APIServer struct {
srv BuildService
log *logrus.Logger
log *slog.Logger
}

// NewAPIServer creates a new build service API server
// TODO: add logger
func NewAPIServer(config APIServerConfig) *APIServer {
log := config.Log
if log == nil {
log = &logrus.Logger{Out: io.Discard}
log = slog.New(
slog.NewTextHandler(
io.Discard,
&slog.HandlerOptions{},
),
)
}
return &APIServer{
srv: config.BuildService,
Expand Down Expand Up @@ -82,7 +86,7 @@ func (a *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

a.log.Debugf("processing request %s", req.String())
a.log.Debug("processing", "request", req.String())

artifact, err := a.srv.Build( //nolint:contextcheck
context.Background(),
Expand All @@ -96,7 +100,7 @@ func (a *APIServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}

a.log.Debugf("returning artifact %s", artifact.String())
a.log.Debug("returning", "artifact", artifact.String())

resp.Artifact = artifact
w.WriteHeader(http.StatusOK)
Expand Down
15 changes: 10 additions & 5 deletions cache_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"

"github.com/sirupsen/logrus"
)

// CacheServerResponse is the response to a cache server request
Expand All @@ -21,21 +20,27 @@ type CacheServerResponse struct {
type CacheServer struct {
baseURL string
cache Cache
log *logrus.Logger
log *slog.Logger
}

// CacheServerConfig defines the configuration for the APIServer
type CacheServerConfig struct {
BaseURL string
Cache Cache
Log *logrus.Logger
Log *slog.Logger
}

// NewCacheServer returns a CacheServer backed by a cache
func NewCacheServer(config CacheServerConfig) http.Handler {
log := config.Log

if log == nil {
log = &logrus.Logger{Out: io.Discard}
log = slog.New(
slog.NewTextHandler(
io.Discard,
&slog.HandlerOptions{},
),
)
}
cacheSrv := &CacheServer{
baseURL: config.BaseURL,
Expand Down
25 changes: 15 additions & 10 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package cmd

import (
"fmt"
"log/slog"
"net/http"
"os"

"github.com/grafana/k6build"
"github.com/grafana/k6catalog"
"github.com/grafana/k6foundry"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

Expand All @@ -34,7 +34,6 @@ func NewServer() *cobra.Command { //nolint:funlen
copyGoEnv bool
port int
verbose bool
log *logrus.Logger
logLevel string
)

Expand All @@ -48,22 +47,28 @@ func NewServer() *cobra.Command { //nolint:funlen
// this is needed to prevent cobra to print errors reported by subcommands in the stderr
SilenceErrors: true,
RunE: func(cmd *cobra.Command, _ []string) error {
ll, err := logrus.ParseLevel(logLevel)
// set log
ll, err := k6build.ParseLogLevel(logLevel)
if err != nil {
return fmt.Errorf("parsing log level %w", err)
}
log = &logrus.Logger{
Out: os.Stderr,
Formatter: new(logrus.TextFormatter),
Level: ll,
}

log := slog.New(
slog.NewTextHandler(
os.Stderr,
&slog.HandlerOptions{
Level: ll,
},
),
)

catalog, err := k6catalog.NewCatalogFromJSON(catalog)
if err != nil {
return fmt.Errorf("creating catalog %w", err)
}

builderOpts := k6foundry.NativeBuilderOpts{
Logger: log,
GoOpts: k6foundry.GoOpts{
Env: buildEnv,
CopyGoEnv: copyGoEnv,
Expand Down Expand Up @@ -118,10 +123,10 @@ func NewServer() *cobra.Command { //nolint:funlen
srv.Handle("/cache/", http.StripPrefix("/cache", cacheSrv))

listerAddr := fmt.Sprintf("localhost:%d", port)
log.Infof("starting server at %s", listerAddr)
log.Info("starting server", "address", listerAddr)
err = http.ListenAndServe(listerAddr, srv) //nolint:gosec
if err != nil {
log.Infof("server ended with error %s", err.Error())
log.Info("server ended", "error", err.Error())
}
log.Info("ending server")

Expand Down
4 changes: 1 addition & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,16 @@ go 1.22.2
require (
github.com/grafana/clireadme v0.1.0
github.com/grafana/k6catalog v0.1.0
github.com/grafana/k6foundry v0.1.4
github.com/grafana/k6foundry v0.2.0
github.com/spf13/cobra v1.8.0
)

require (
github.com/Masterminds/semver v1.5.0 // indirect
github.com/google/go-cmp v0.6.0
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/sirupsen/logrus v1.9.3
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/mod v0.18.0 // indirect
golang.org/x/sys v0.21.0 // indirect
)

retract v0.0.0 // premature publishing
21 changes: 4 additions & 17 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,37 +1,24 @@
github.com/Masterminds/semver v1.5.0 h1:H65muMkzWKEuNDnfl9d70GUjFniHKHRbFPGBuZ3QEww=
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/grafana/clireadme v0.1.0 h1:KYEYSnYdSzmHf3bufaK6fQZ5j4dzvM/T+G6Ba+qNnAM=
github.com/grafana/clireadme v0.1.0/go.mod h1:Wy4KIG2ZBGMYAYyF9l7qAy+yoJVasqk/txsRgoRI3gc=
github.com/grafana/k6catalog v0.1.0 h1:jLmbmB3EUJ+zyQG3hWy6dWbtMjvTkvJNx1d4LX8it6I=
github.com/grafana/k6catalog v0.1.0/go.mod h1:8R9eXAh2nb69+drkj0rZ4aemso0jcwCbPP6Q3E5LqCw=
github.com/grafana/k6foundry v0.1.4 h1:6kPQeU1nSJiV6Y42sVRESuVfpgt/uiCwoYD729hRypM=
github.com/grafana/k6foundry v0.1.4/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM=
github.com/grafana/k6foundry v0.2.0 h1:+aE5wuCP0XNGNsxM7UiPj9hyw4RdWeW929PuGwLWIlg=
github.com/grafana/k6foundry v0.2.0/go.mod h1:b6n4InFgXl+3yPobmlyJfcJmLozU9CI9IIUuq8YqEiM=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/mod v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/mod v0.18.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
17 changes: 17 additions & 0 deletions log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package k6build

import (
"fmt"
"log/slog"
)

// ParseLogLevel parses the level from a string
func ParseLogLevel(levelString string) (slog.Level, error) {
var level slog.Level
err := level.UnmarshalText([]byte(levelString))
if err != nil {
return level, fmt.Errorf("parsing log level from string: %w", err)
}

return level, nil
}
Loading