Skip to content

Commit

Permalink
Convert logging in build.assets/tooling to use slog (#50805)
Browse files Browse the repository at this point in the history
  • Loading branch information
rosstimothy authored Jan 8, 2025
1 parent cf01dc4 commit 1bc68f7
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
7 changes: 4 additions & 3 deletions build.assets/tooling/cmd/protoc-gen-eventschema/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,26 @@ package main
// inspect what is happening inside the plugin.

import (
"context"
"io"
"log/slog"
"os"

"github.com/gogo/protobuf/proto"
"github.com/gogo/protobuf/protoc-gen-gogo/generator"
plugin "github.com/gogo/protobuf/protoc-gen-gogo/plugin"
"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
)

const pluginInputPathEnvironment = "TELEPORT_PROTOC_READ_FILE"

func readRequest() (*plugin.CodeGeneratorRequest, error) {
inputPath := os.Getenv(pluginInputPathEnvironment)
if inputPath == "" {
log.Error(trace.BadParameter("When built with the 'debug' tag, the input path must be set through the environment variable: %s", pluginInputPathEnvironment))
slog.ErrorContext(context.Background(), "When built with the 'debug' tag, the input path must be set through the TELEPORT_PROTOC_READ_FILE environment variable")
os.Exit(-1)
}
log.Infof("This is a debug build, the protoc request is read from the file: '%s'", inputPath)
slog.InfoContext(context.Background(), "This is a debug build, the protoc request is read from provided file", "file", inputPath)

req, err := readRequestFromFile(inputPath)
if err != nil {
Expand Down
14 changes: 8 additions & 6 deletions build.assets/tooling/cmd/protoc-gen-eventschema/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,23 @@
package main

import (
"context"
"log/slog"
"os"

log "github.com/sirupsen/logrus"
)

func main() {
log.SetLevel(log.DebugLevel)
log.SetOutput(os.Stderr)
logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelDebug}))
slog.SetDefault(logger)

ctx := context.Background()
req, err := readRequest()
if err != nil {
log.WithError(err).Error("Failed to read request")
logger.ErrorContext(ctx, "Failed to read request", "error", err)
os.Exit(-1)
}
if err := handleRequest(req); err != nil {
log.WithError(err).Error("Failed to generate schema")
logger.ErrorContext(ctx, "Failed to generate schema", "error", err)
os.Exit(-1)
}
}
19 changes: 12 additions & 7 deletions build.assets/tooling/cmd/render-helm-ref/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ package main

import (
"bufio"
"context"
"encoding/json"
"flag"
"fmt"
"log/slog"
"os"
"regexp"
"strings"

"github.com/gravitational/trace"
log "github.com/sirupsen/logrus"
"gopkg.in/yaml.v3"
"helm.sh/helm/v3/pkg/chart/loader"
)
Expand All @@ -52,14 +53,15 @@ func main() {
flag.StringVar(&outputPath, "output", "-", "Path of the generated markdown reference, '-' means stdout.")
flag.Parse()

ctx := context.Background()
if chartPath == "" {
log.Error(trace.BadParameter("chart path must be specified"))
slog.ErrorContext(ctx, "chart path must be specified")
os.Exit(1)
}

reference, err := parseAndRender(chartPath)
if err != nil {
log.Errorf("failed parsing chart and rendering reference: %s", err)
slog.ErrorContext(ctx, "failed parsing chart and rendering reference", "error", err)
os.Exit(1)
}

Expand All @@ -69,10 +71,10 @@ func main() {
}
err = os.WriteFile(outputPath, reference, 0o644)
if err != nil {
log.Errorf("failed writing file: %s", err)
slog.ErrorContext(ctx, "failed writing file", "error", err)
os.Exit(1)
}
log.Infof("File %s successfully written", outputPath)
slog.InfoContext(ctx, "File successfully written", "file_path", outputPath)
}

func parseAndRender(chartPath string) ([]byte, error) {
Expand Down Expand Up @@ -106,7 +108,10 @@ func parseAndRender(chartPath string) ([]byte, error) {
if value.Kind != "" && value.Default == "" {
defaultValue, err := getDefaultForValue(value.Name, chrt.Values)
if err != nil {
log.Warnf("failed to get default for value %s, error: %s", value.Name, err)
slog.WarnContext(context.Background(), "failed to look up default value",
"value", value.Name,
"error", err,
)
} else {
value.Default = string(defaultValue)
}
Expand Down Expand Up @@ -227,7 +232,7 @@ func cleanLine(line string) string {
return ""
}
if line2[0] != '#' {
log.Warnf("Misformatted line: %s", line)
slog.WarnContext(context.Background(), "Misformatted line", "line", line)
return ""
}
return line2[2:]
Expand Down
2 changes: 0 additions & 2 deletions build.assets/tooling/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/gogo/protobuf v1.3.2
github.com/google/go-github/v41 v41.0.0
github.com/gravitational/trace v1.4.0
github.com/sirupsen/logrus v1.9.3
github.com/stretchr/testify v1.10.0
github.com/waigani/diffparser v0.0.0-20190828052634-7391f219313d
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
Expand Down Expand Up @@ -48,7 +47,6 @@ require (
github.com/xhit/go-str2duration/v2 v2.1.0 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/tools v0.26.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
Expand Down
5 changes: 0 additions & 5 deletions build.assets/tooling/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -881,8 +881,6 @@ github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfF
github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk=
github.com/shopspring/decimal v1.4.0 h1:bxl37RwXBklmTi0C79JfXCEBD1cqqHt0bbgBAGFp81k=
github.com/shopspring/decimal v1.4.0/go.mod h1:gawqmDU56v4yIKSwfBSFip1HdCCXN8/+DMd9qYNcwME=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/spf13/afero v1.3.3/go.mod h1:5KUK8ByomD5Ti5Artl0RtHeI5pTF7MIDuXL3yY520V4=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down Expand Up @@ -1183,7 +1181,6 @@ golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220610221304-9f5ed59c137d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220615213510-4f61da869c0c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220624220833-87e55d714810/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220728004956-3c1f35247d10/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
Expand All @@ -1197,8 +1194,6 @@ golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.28.0 h1:Fksou7UEQUWlKvIdsqzJmUmCX3cZuD2+P3XyyzwMhlA=
golang.org/x/sys v0.28.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc=
Expand Down

0 comments on commit 1bc68f7

Please sign in to comment.