Skip to content

Commit

Permalink
Merge pull request #59 from slosive/better-logging
Browse files Browse the repository at this point in the history
Add more logging to go/sloth parser
  • Loading branch information
tfadeyi committed Jul 1, 2023
2 parents 648c3c8 + 5168f2f commit 2314f4b
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
"github.com/spf13/cobra"
)

const header = `# Code generated by SLOsive's sloscribe CLI: https://github.com/tfadeyi/slosive.
const header = `# Code generated by SLOsive's sloscribe CLI: https://github.com/slosive/sloscribe.
# DO NOT EDIT.`

func specInitCmd(common *commonoptions.Options) *cobra.Command {
Expand Down
6 changes: 0 additions & 6 deletions gomod2nix.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ schema = 3
[mod."github.com/modern-go/reflect2"]
version = "v1.0.2"
hash = "sha256-+W9EIW7okXIXjWEgOaMh58eLvBZ7OshW2EhaIpNLSBU="
[mod."github.com/pelletier/go-toml/v2"]
version = "v2.0.7"
hash = "sha256-N/bMEQkqdlkmRN0bKbdifEquKHEPaxltX8LTmhOkUGc="
[mod."github.com/pmezard/go-difflib"]
version = "v1.0.0"
hash = "sha256-/FtmHnaGjdvEIKAJtrUfEhV7EVo5A/eYrtdnUkuxLDA="
Expand All @@ -61,9 +58,6 @@ schema = 3
[mod."github.com/stretchr/testify"]
version = "v1.8.4"
hash = "sha256-MoOmRzbz9QgiJ+OOBo5h5/LbilhJfRUryvzHJmXAWjo="
[mod."github.com/tfadeyi/go-aloe"]
version = "v0.0.2"
hash = "sha256-ysaV7IF6m9p2MYzy15YfCLnpgliSzhj1fDxCdEA1T7g="
[mod."golang.org/x/net"]
version = "v0.8.0"
hash = "sha256-2cOtqa7aJ5mn64kZ+8+PVjJ4uGbhpXTpC1vm/+iaZzM="
Expand Down
13 changes: 8 additions & 5 deletions internal/parser/specification/sloth/language/golang/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ import "github.com/slosive/sloscribe/internal/parser/specification/sloth/languag


<a name="Options"></a>
## type [Options](<https://github.com/slosive/sloscribe/blob/main/internal/parser/specification/sloth/language/golang/parser.go#L33-L39>)
## type [Options](<https://github.com/slosive/sloscribe/blob/main/internal/parser/specification/sloth/language/golang/parser.go#L38-L47>)

Options contains the configuration options available to the Parser

```go
type Options struct {
Logger *logging.Logger
SourceFile string
Logger *logging.Logger
// SourceFile is the path to the target file to be parsed, i.e: -f file.go
SourceFile string
// SourceContent is the reader to the content to be parsed
SourceContent io.ReadCloser
InputDirectories []string
Kubernetes bool
// Kubernetes tells the parser to parser the sloth annotations and output a kubernetes specification of the service
Kubernetes bool
}
```

<a name="NewOptions"></a>
### func [NewOptions](<https://github.com/slosive/sloscribe/blob/main/internal/parser/specification/sloth/language/golang/parser.go#L41>)
### func [NewOptions](<https://github.com/slosive/sloscribe/blob/main/internal/parser/specification/sloth/language/golang/parser.go#L49>)

```go
func NewOptions() *Options
Expand Down
88 changes: 64 additions & 24 deletions internal/parser/specification/sloth/language/golang/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,30 @@ import (
)

type parser struct {
specs map[string]any
current any
sourceFile string
// specs contains references to all the service specifications that have been parsed
specs map[string]any
// current references the current service specification being parsed
current any
// sourceFile is the path to the target file to be parsed, i.e: -f file.go
sourceFile string
// sourceContent is the reader to the content to be parsed
sourceContent io.ReadCloser
includedDirs []string
logger *logging.Logger
kubernetes bool
// kubernetes tells the parser to parser the sloth annotations and output a kubernetes specification of the service
kubernetes bool
}

// Options contains the configuration options available to the Parser
type Options struct {
Logger *logging.Logger
SourceFile string
Logger *logging.Logger
// SourceFile is the path to the target file to be parsed, i.e: -f file.go
SourceFile string
// SourceContent is the reader to the content to be parsed
SourceContent io.ReadCloser
InputDirectories []string
Kubernetes bool
// Kubernetes tells the parser to parser the sloth annotations and output a kubernetes specification of the service
Kubernetes bool
}

func NewOptions() *Options {
Expand Down Expand Up @@ -134,9 +142,12 @@ func (p *parser) parseK8SlothAnnotations(comments ...*ast.CommentGroup) error {
}
}

p.logger.Debug("current service being parsed", "service", p.current.(*k8sloth.PrometheusServiceLevel).Spec.Service)
p.logger.Debug("Current service being parsed", "service", p.current.(*k8sloth.PrometheusServiceLevel).Spec.Service)
for _, comment := range comments {
p.logger.Debug("parser parsing", "comment", strings.TrimSpace(comment.Text()))
if !strings.HasPrefix(strings.TrimSpace(comment.Text()), "@sloth") {
continue
}
p.logger.Debug("Parsing", "comment", strings.TrimSpace(comment.Text()))
// partialServiceSpec contains the partially parsed sloth Specification for a given comment group
// this means the parsed spec will only contain data for the fields that are present in the comments, making the spec only partially accurate
partialServiceSpec, err := grammar.Eval(strings.TrimSpace(comment.Text()))
Expand Down Expand Up @@ -256,10 +267,13 @@ func (p *parser) parseSlothAnnotations(comments ...*ast.CommentGroup) error {
}
}

p.logger.Debug("current service being parsed", "service", p.current.(*sloth.Spec).Service)
p.logger.Debug("Current service being parsed", "service", p.current.(*sloth.Spec).Service)

for _, comment := range comments {
p.logger.Debug("parser parsing", "comment", strings.TrimSpace(comment.Text()))
if !strings.HasPrefix(strings.TrimSpace(comment.Text()), "@sloth") {
continue
}
p.logger.Debug("Parsing", "comment", strings.TrimSpace(comment.Text()))
// partialServiceSpec contains the partially parsed sloth Specification for a given comment group
// this means the parsed spec will only contain data for the fields that are present in the comments, making the spec only partially accurate
partialServiceSpec, err := grammar.Eval(strings.TrimSpace(comment.Text()))
Expand Down Expand Up @@ -322,6 +336,7 @@ func (p *parser) Parse(ctx context.Context) (map[string]any, error) {
// error hard as we can't extract more data for the spec
return nil, err
}
p.logger.Debug("Parsing source code", "file", file.Name)
if p.kubernetes {
if err := p.parseK8SlothAnnotations(file.Comments...); err != nil {
return nil, err
Expand All @@ -331,6 +346,7 @@ func (p *parser) Parse(ctx context.Context) (map[string]any, error) {
return nil, err
}
}
p.logger.Debug("Parsed source code", "file", file.Name)
return p.specs, nil
}

Expand Down Expand Up @@ -365,26 +381,30 @@ func (p *parser) Parse(ctx context.Context) (map[string]any, error) {
for _, pkg := range applicationPackages {
// Prioritise parsing the main.go if present in the package
for filename, file := range pkg.Files {
if !strings.Contains(filename, "main.go") {
continue
}

if p.kubernetes {
if err := p.parseK8SlothAnnotations(file.Comments...); err != nil {
return nil, err
}
} else {
if err := p.parseSlothAnnotations(file.Comments...); err != nil {
p.warn(err)
continue
if strings.Contains(filename, "main.go") {
p.logger.Debug("Parsing source code", "package", pkg.Name, "file", filename)
if p.kubernetes {
if err := p.parseK8SlothAnnotations(file.Comments...); err != nil {
p.warn(err)
break
}
} else {
if err := p.parseSlothAnnotations(file.Comments...); err != nil {
p.warn(err)
break
}
}
p.logger.Debug("Parsed source code", "package", pkg.Name, "file", filename)
break
}
}

// parse the rest of the files, skipping main.go
for filename, file := range pkg.Files {
if strings.Contains(filename, "main.go") {
continue
}
p.logger.Debug("Parsing source code", "package", pkg.Name, "file", filename)
// handle signals with context
select {
case <-ctx.Done():
Expand All @@ -394,17 +414,22 @@ func (p *parser) Parse(ctx context.Context) (map[string]any, error) {

if p.kubernetes {
if err := p.parseK8SlothAnnotations(file.Comments...); err != nil {
return nil, err
p.warn(err)
continue
}
} else {
if err := p.parseSlothAnnotations(file.Comments...); err != nil {
p.warn(err)
continue
}
}
p.logger.Debug("Parsed source code", "package", pkg.Name, "file", filename)
}
}

// print statistics
p.stats()

return p.specs, nil
}

Expand All @@ -413,3 +438,18 @@ func (p *parser) warn(err error, keyValues ...interface{}) {
p.logger.Warn(err, keyValues...)
}
}

func (p *parser) stats() {
p.logger.Info("Found", "services", len(p.specs))
allSLOs := 0
for _, spec := range p.specs {
if p.kubernetes {
s := spec.(*k8sloth.PrometheusServiceLevel)
allSLOs += len(s.Spec.SLOs)
} else {
s := spec.(*sloth.Spec)
allSLOs += len(s.SLOs)
}
}
p.logger.Info("Found", "SLOs", allSLOs)
}

0 comments on commit 2314f4b

Please sign in to comment.