Skip to content

Commit

Permalink
improve log enriching
Browse files Browse the repository at this point in the history
  • Loading branch information
leg100 committed Apr 8, 2024
1 parent def5f71 commit 4f532b2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 16 deletions.
6 changes: 3 additions & 3 deletions internal/logging/enricher.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ type enricher struct {
enrichers []Enricher
}

// Enricher implementations add attributes to log records.
// Enricher implementations update a log record with further info
type Enricher interface {
AddLogAttributes(args ...any) []any
EnrichLogRecord(args ...any) []any
}

func (e *enricher) AddEnricher(enricher Enricher) {
Expand All @@ -17,7 +17,7 @@ func (e *enricher) AddEnricher(enricher Enricher) {

func (e *enricher) enrich(args ...any) []any {
for _, en := range e.enrichers {
args = en.AddLogAttributes(args...)
args = en.EnrichLogRecord(args...)
}
return args
}
46 changes: 39 additions & 7 deletions internal/module/log_enricher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package module

import "github.com/leg100/pug/internal/resource"
import (
"github.com/leg100/pug/internal/resource"
)

// logEnricher adds module related attributes to log records where pertinent.
type logEnricher struct {
Expand All @@ -12,23 +14,53 @@ type moduleResource interface {
Module() *resource.Resource
}

// AddLogAttributes checks if one of the log args is a resource that
// belongs to a module, and if so, adds the module path to the log record.
func (e *logEnricher) AddLogAttributes(attrs ...any) []any {
for _, arg := range attrs {
func (e *logEnricher) EnrichLogRecord(args ...any) []any {
args = e.addModulePath(args...)
args = e.replaceIDWithModule(args...)
return args
}

// addModulePath checks if one of the log args is a resource that
// belongs to a module, and if so, adds the module to the args
func (e *logEnricher) addModulePath(args ...any) []any {
for _, arg := range args {
res, ok := arg.(moduleResource)
if !ok {
// does not belong to a module
continue
}
modResource := res.Module()
if modResource == nil {
// can belong to a module but not in this instance
continue
}
mod, err := e.table.Get(modResource.ID)
if err != nil {
// module with id does not exist
continue
}
return append(args, "module", mod)
}
return args
}

// replaceIDWithModule checks if one of the arguments is a module ID, and
// if so, replaces it with a module instance.
func (e *logEnricher) replaceIDWithModule(args ...any) []any {
for i, arg := range args {
id, ok := arg.(resource.ID)
if !ok {
// Not an id
continue
}
mod, err := e.table.Get(id)
if err != nil {
// Not a module id
continue
}
return append(attrs, "module", mod.Path)
// replace id with module
args[i] = mod
return args
}
return attrs
return args
}
3 changes: 2 additions & 1 deletion internal/state/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,8 @@ func (s *Service) Reload(workspaceID resource.ID) (*task.Task, error) {
}

task, err := s.createTask(workspaceID, task.CreateOptions{
Command: []string{"show", "-json"},
Command: []string{"show"},
Args: []string{"-json"},
JSON: true,
AfterError: func(t *task.Task) {
s.logger.Error("reloading state", "error", t.Err, "workspace", ws)
Expand Down
39 changes: 34 additions & 5 deletions internal/workspace/log_enricher.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package workspace

import "github.com/leg100/pug/internal/resource"
import (
"github.com/leg100/pug/internal/resource"
)

// logEnricher adds module related attributes to log records where pertinent.
type logEnricher struct {
Expand All @@ -12,9 +14,15 @@ type workspaceResource interface {
Workspace() *resource.Resource
}

// AddLogAttributes checks if one of the log args is a resource that belongs to
// a workspace, and if so, adds the workspace name to the log record.
func (e *logEnricher) AddLogAttributes(args ...any) []any {
func (e *logEnricher) EnrichLogRecord(args ...any) []any {
args = e.addWorkspaceName(args...)
args = e.replaceIDWithWorkspace(args...)
return args
}

// addWorkspaceName checks if one of the log args is a resource that belongs to
// a workspace, and if so, adds the workspace to the args
func (e *logEnricher) addWorkspaceName(args ...any) []any {
for _, arg := range args {
res, ok := arg.(workspaceResource)
if !ok {
Expand All @@ -28,7 +36,28 @@ func (e *logEnricher) AddLogAttributes(args ...any) []any {
if err != nil {
continue
}
return append(args, "workspace", ws.Name)
return append(args, "workspace", ws)
}
return args
}

// replaceIDWithWorkspace checks if one of the arguments is a workspace ID, and
// if so, replaces it with a workspace instance.
func (e *logEnricher) replaceIDWithWorkspace(args ...any) []any {
for i, arg := range args {
id, ok := arg.(resource.ID)
if !ok {
// Not an id
continue
}
ws, err := e.table.Get(id)
if err != nil {
// Not a workspace id
continue
}
// replace id with workspace
args[i] = ws
return args
}
return args
}

0 comments on commit 4f532b2

Please sign in to comment.