Skip to content

Commit 9ad41c0

Browse files
committed
collector: user-friendly output for 'add' command showing detected log format and config
1 parent 5bf0149 commit 9ad41c0

File tree

1 file changed

+45
-0
lines changed
  • apps/collector/cmd/tracer-collector

1 file changed

+45
-0
lines changed

apps/collector/cmd/tracer-collector/main.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,18 +222,63 @@ func handleAdd(ctx context.Context, path string, cfg config.Config, addCmd *flag
222222
}
223223
name, spec := inferSourceSpec(remaining[0])
224224

225+
fmt.Printf("Adding log source: %s\n", remaining[0])
226+
if spec.Type == "file" {
227+
fmt.Printf(" Type: file path\n")
228+
fmt.Printf(" Path: %s\n", spec.Params["path"])
229+
} else {
230+
fmt.Printf(" Type: systemd unit\n")
231+
fmt.Printf(" Unit: %s\n", spec.Params["unit"])
232+
}
233+
225234
if err := formatguesser.Add(ctx, &cfg, name, spec, formatguesser.AddRequest{
226235
Override: *override,
227236
AllowMixed: *ignoreFormatChange,
228237
}); err != nil {
229238
log.Fatalf("Failed to guess the source format, error: %v", err)
230239
}
240+
241+
// Find the app we just added/updated
242+
var addedApp *config.AppSpec
243+
for i := range cfg.Apps {
244+
if cfg.Apps[i].Name == name || (cfg.Apps[i].Source.Type == spec.Type &&
245+
cfg.Apps[i].Source.Params["unit"] == spec.Params["unit"] &&
246+
cfg.Apps[i].Source.Params["path"] == spec.Params["path"]) {
247+
addedApp = &cfg.Apps[i]
248+
break
249+
}
250+
}
251+
252+
if addedApp == nil {
253+
log.Fatalf("Failed to find the added app in config")
254+
}
255+
256+
parserSpec := cfg.Parsers[addedApp.ParserID]
257+
258+
fmt.Printf("\nDetected log format:\n")
259+
fmt.Printf(" Parser: %s\n", parserSpec.Type)
260+
fmt.Printf(" Timestamp field: %s\n", parserSpec.Params["timestamp_field"])
261+
fmt.Printf(" Timestamp format: %s\n", parserSpec.Params["timestamp_format"])
262+
fmt.Printf(" Level field: %s\n", parserSpec.Params["level_field"])
263+
fmt.Printf(" Message field: %s\n", parserSpec.Params["message_field"])
264+
if traceField := parserSpec.Params["trace_id_field"]; traceField != "" {
265+
fmt.Printf(" Trace ID field: %s\n", traceField)
266+
}
267+
231268
if strings.TrimSpace(*healthURL) != "" {
269+
fmt.Printf("\nAttaching health check: %s\n", strings.TrimSpace(*healthURL))
232270
attachHTTPHealth(&cfg, name, spec, strings.TrimSpace(*healthURL))
233271
}
272+
234273
if err := cfg.Persist(path); err != nil {
235274
log.Fatalf("Failed to write the file config, error: %v", err)
236275
}
276+
277+
fmt.Printf("\nConfiguration saved to: %s\n", path)
278+
fmt.Printf("\nAdded app configuration:\n")
279+
fmt.Printf(" Name: %s\n", addedApp.Name)
280+
fmt.Printf(" Parser ID: %s\n", addedApp.ParserID)
281+
fmt.Printf(" Source Type: %s\n", addedApp.Source.Type)
237282
}
238283

239284
func handleSet(path string, cfg config.Config, setCmd *flag.FlagSet) {

0 commit comments

Comments
 (0)