diff options
| author | Paul Buetow <paul@buetow.org> | 2026-02-07 16:32:10 +0200 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-02-07 16:32:10 +0200 |
| commit | 3fd46f3977fb650974e5e936cba362c787c00637 (patch) | |
| tree | b49111ddd0b7af4a007bca6a304dba10efcd88ff /internal/parser/parser.go | |
reimport this PoC
Diffstat (limited to 'internal/parser/parser.go')
| -rw-r--r-- | internal/parser/parser.go | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/parser/parser.go b/internal/parser/parser.go new file mode 100644 index 0000000..860baa3 --- /dev/null +++ b/internal/parser/parser.go @@ -0,0 +1,56 @@ +package parser + +import ( + "context" + "fmt" + "io" + "os" + + "epimetheus/internal/metrics" +) + +// Parser defines the interface for metric parsers. +type Parser interface { + Parse(ctx context.Context, reader io.Reader) ([]metrics.Sample, error) +} + +// ParseFile parses metrics from a file. +func ParseFile(ctx context.Context, filename, format string) ([]metrics.Sample, error) { + file, err := os.Open(filename) + if err != nil { + return nil, fmt.Errorf("failed to open file: %w", err) + } + defer file.Close() + + return parseWithFormat(ctx, file, format) +} + +// ParseStdin parses metrics from standard input. +func ParseStdin(ctx context.Context, format string) ([]metrics.Sample, error) { + return parseWithFormat(ctx, os.Stdin, format) +} + +// parseWithFormat parses metrics using the specified format. +func parseWithFormat(ctx context.Context, reader io.Reader, format string) ([]metrics.Sample, error) { + var parser Parser + + switch format { + case "csv": + parser = NewCSVParser() + case "json": + parser = NewJSONParser() + default: + return nil, fmt.Errorf("unsupported format: %s (use csv or json)", format) + } + + samples, err := parser.Parse(ctx, reader) + if err != nil { + return nil, fmt.Errorf("failed to parse metrics: %w", err) + } + + if len(samples) == 0 { + return nil, fmt.Errorf("no valid samples found") + } + + return samples, nil +} |
