Skip to content

Commit

Permalink
add log options to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeknox committed Feb 24, 2024
1 parent 2518bbf commit 7a9592f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 9 deletions.
2 changes: 1 addition & 1 deletion internal/cli/clean.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func genCleanCommand() *cli.Command {

homeDir := getHomeDir()
targetDir := filepath.Join(homeDir, ".run-o11y-run")
err := files.ExtractFiles(targetDir)
err := files.ExtractFiles(targetDir, files.ServicesConfig{})
if err != nil {
fmt.Println("Error extracting files:", err)
return err
Expand Down
16 changes: 15 additions & 1 deletion internal/cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ func genStartCommand() *cli.Command {
Usage: "apply the :latest tag to all images. Caution: This may result in breaking changes to the setup",
Value: false,
},
&cli.BoolFlag{
Name: "local-log-files",
Usage: "include all local log files in the collector",
Value: false,
},
&cli.StringFlag{
Name: "follow-log-file",
Usage: "regex of log files to track in the collector",
},
},
Action: func(c *cli.Context) error {
fmt.Println("✨ Starting...")
Expand All @@ -57,7 +66,12 @@ func genStartCommand() *cli.Command {

homeDir := getHomeDir()
targetDir := filepath.Join(homeDir, ".run-o11y-run")
err := files.ExtractFiles(targetDir)
svcConfig := files.ServicesConfig{
LocalLogFiles: c.Bool("local-log-files"),
LogFilePath: c.String("follow-log-file"),
LogFiles: (c.Bool("local-log-files") || c.String("follow-log-file") != ""),
}
err := files.ExtractFiles(targetDir, svcConfig)
if err != nil {
fmt.Println("Error extracting files:", err)
return err
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func genStopCommand() *cli.Command {

homeDir := getHomeDir()
targetDir := filepath.Join(homeDir, ".run-o11y-run")
err := files.ExtractFiles(targetDir)
err := files.ExtractFiles(targetDir, files.ServicesConfig{})
if err != nil {
fmt.Println("Error extracting files:", err)
return err
Expand Down
9 changes: 5 additions & 4 deletions internal/files/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
var files embed.FS

type ServicesConfig struct {
LocalLogFiles bool
LocalLogFiles bool // follow local log files
LogFilePath string // follow specified log file
LogFiles bool // true if LocalLogFiles or LogFilePath
}

// ExtractFiles extracts the files from the embedded filesystem to the target directory
func ExtractFiles(targetDir string) error {
func ExtractFiles(targetDir string, svcConfig ServicesConfig) error {
err := os.MkdirAll(targetDir, os.ModePerm)
if err != nil {
return err
Expand All @@ -38,13 +40,12 @@ func ExtractFiles(targetDir string) error {
return err
}
if filepath.Ext(path) == ".tmpl" {
servicesConfig := ServicesConfig{LocalLogFiles: false}
serviceTemplate, err := template.New("serviceTemplateConfigFile").Parse(string(data))
if err != nil {
return err
}
var hydratedContent bytes.Buffer
err = serviceTemplate.Execute(&hydratedContent, servicesConfig)
err = serviceTemplate.Execute(&hydratedContent, svcConfig)
if err != nil {
return err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/files/files/grafana/shared/otel-collector.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
receivers:
filelog:
{{- if .LocalLogFiles }}
{{- if .LogFiles }}
include:
{{- if .LocalLogFiles }}
- /app/*.log
- /var/log/*.log
{{- end }}
{{- if .LogFilePath }}
- {{ .LogFilePath }}
{{- end }}
{{- end }}
include_file_name: true
include_file_path: true
Expand Down Expand Up @@ -41,7 +46,7 @@ exporters:
service:
pipelines:
logs:
{{- if .LocalLogFiles }}
{{- if .LogFiles }}
receivers: [filelog, otlp, syslog]
{{- else }}
receivers: [otlp, syslog]
Expand Down

0 comments on commit 7a9592f

Please sign in to comment.