-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
98 lines (86 loc) · 2.32 KB
/
Copy pathmain.go
File metadata and controls
98 lines (86 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"strings"
"github.com/mongodb/ftdc"
"github.com/spf13/cobra"
"go.mongodb.org/mongo-driver/v2/bson"
)
var (
outputFormat string
)
var rootCmd = &cobra.Command{
Use: "ftdc-reader [path-to-ftdc-file]",
Short: "A CLI tool to parse MongoDB FTDC files.",
Long: `A CLI tool to parse MongoDB FTDC files.`,
Example: "ftdc-reader diagnostics.data/metrics.2023-10-27T10-15-00Z-00000 -o JSON",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
filePath := args[0]
return parseFTDC(filePath, outputFormat)
},
}
func init() {
rootCmd.PersistentFlags().StringVarP(&outputFormat, "outputFormat", "o", "JSON", "Output format. Options: JSON, BSON")
}
type metricOutput struct {
Key string `json:"key" bson:"key"`
Values []int64 `json:"values" bson:"values"`
}
func parseFTDC(filePath string, format string) error {
ctx := context.Background()
file, err := os.Open(filePath)
if err != nil {
return fmt.Errorf("failed to open file '%s': %w", filePath, err)
}
defer file.Close()
iterator := ftdc.ReadChunks(ctx, file)
// Use a switch to handle different output formats.
switch strings.ToUpper(format) {
case "JSON":
encoder := json.NewEncoder(os.Stdout)
for iterator.Next() {
chunk := iterator.Chunk()
for _, metric := range chunk.Metrics {
data := metricOutput{
Key: metric.Key(),
Values: metric.Values,
}
if err := encoder.Encode(data); err != nil {
return fmt.Errorf("failed to encode metric to JSON: %w", err)
}
}
}
case "BSON":
for iterator.Next() {
chunk := iterator.Chunk()
for _, metric := range chunk.Metrics {
data := metricOutput{
Key: metric.Key(),
Values: metric.Values,
}
bsonBytes, err := bson.Marshal(data)
if err != nil {
return fmt.Errorf("failed to marshal metric to BSON: %w", err)
}
if _, err := os.Stdout.Write(bsonBytes); err != nil {
return fmt.Errorf("failed to write BSON to stdout: %w", err)
}
}
}
default:
return fmt.Errorf("unsupported output format '%s'. Supported formats are JSON, BSON", format)
}
if err := iterator.Err(); err != nil {
return fmt.Errorf("iterator error during parsing: %w", err)
}
return nil
}
func main() {
if err := rootCmd.Execute(); err != nil {
os.Exit(1)
}
}