|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "strings" |
| 7 | + |
| 8 | + "github.com/spf13/cobra" |
| 9 | + |
| 10 | + "github.com/hookdeck/hookdeck-cli/pkg/hookdeck" |
| 11 | + "github.com/hookdeck/hookdeck-cli/pkg/validators" |
| 12 | +) |
| 13 | + |
| 14 | +// printMetricsResponse prints data as JSON or a human-readable table. |
| 15 | +func printMetricsResponse(data hookdeck.MetricsResponse, output string) error { |
| 16 | + if output == "json" { |
| 17 | + bytes, err := json.MarshalIndent(data, "", " ") |
| 18 | + if err != nil { |
| 19 | + return fmt.Errorf("failed to marshal metrics: %w", err) |
| 20 | + } |
| 21 | + fmt.Println(string(bytes)) |
| 22 | + return nil |
| 23 | + } |
| 24 | + if len(data) == 0 { |
| 25 | + fmt.Println("No data points.") |
| 26 | + return nil |
| 27 | + } |
| 28 | + for i, pt := range data { |
| 29 | + tb := "<none>" |
| 30 | + if pt.TimeBucket != nil { |
| 31 | + tb = *pt.TimeBucket |
| 32 | + } |
| 33 | + fmt.Printf("time_bucket: %s\n", tb) |
| 34 | + if len(pt.Dimensions) > 0 { |
| 35 | + for k, v := range pt.Dimensions { |
| 36 | + fmt.Printf(" %s: %v\n", k, v) |
| 37 | + } |
| 38 | + } |
| 39 | + if len(pt.Metrics) > 0 { |
| 40 | + for k, v := range pt.Metrics { |
| 41 | + fmt.Printf(" %s: %v\n", k, v) |
| 42 | + } |
| 43 | + } |
| 44 | + if i < len(data)-1 { |
| 45 | + fmt.Println("---") |
| 46 | + } |
| 47 | + } |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +const granularityHelp = `Time bucket size. Format: <number><unit> (e.g. 1h, 5m, 1d). |
| 52 | +Units: s (seconds), m (minutes), h (hours), d (days), w (weeks), M (months).` |
| 53 | + |
| 54 | +// metricsCommonFlags holds the common flags for all metrics subcommands. |
| 55 | +// Used by addMetricsCommonFlags and to build hookdeck.MetricsQueryParams. |
| 56 | +type metricsCommonFlags struct { |
| 57 | + start string |
| 58 | + end string |
| 59 | + granularity string |
| 60 | + measures string |
| 61 | + dimensions string |
| 62 | + sourceID string |
| 63 | + destinationID string |
| 64 | + connectionID string |
| 65 | + status string |
| 66 | + issueID string |
| 67 | + output string |
| 68 | +} |
| 69 | + |
| 70 | +// addMetricsCommonFlags adds common metrics flags to cmd and binds them to f. |
| 71 | +// For subcommands that take a required resource id as an argument (e.g. events-by-issue <issue-id>), |
| 72 | +// pass skipIssueID true so --issue-id is not added as a flag. |
| 73 | +func addMetricsCommonFlags(cmd *cobra.Command, f *metricsCommonFlags) { |
| 74 | + addMetricsCommonFlagsEx(cmd, f, false) |
| 75 | +} |
| 76 | + |
| 77 | +func addMetricsCommonFlagsEx(cmd *cobra.Command, f *metricsCommonFlags, skipIssueID bool) { |
| 78 | + cmd.Flags().StringVar(&f.start, "start", "", "Start of time range (ISO 8601 date-time, required)") |
| 79 | + cmd.Flags().StringVar(&f.end, "end", "", "End of time range (ISO 8601 date-time, required)") |
| 80 | + cmd.Flags().StringVar(&f.granularity, "granularity", "", granularityHelp) |
| 81 | + cmd.Flags().StringVar(&f.measures, "measures", "", "Comma-separated list of measures to return") |
| 82 | + cmd.Flags().StringVar(&f.dimensions, "dimensions", "", "Comma-separated list of dimensions") |
| 83 | + cmd.Flags().StringVar(&f.sourceID, "source-id", "", "Filter by source ID") |
| 84 | + cmd.Flags().StringVar(&f.destinationID, "destination-id", "", "Filter by destination ID") |
| 85 | + cmd.Flags().StringVar(&f.connectionID, "connection-id", "", "Filter by connection ID") |
| 86 | + cmd.Flags().StringVar(&f.status, "status", "", "Filter by status (e.g. SUCCESSFUL, FAILED)") |
| 87 | + if !skipIssueID { |
| 88 | + cmd.Flags().StringVar(&f.issueID, "issue-id", "", "Filter by issue ID") |
| 89 | + } |
| 90 | + cmd.Flags().StringVar(&f.output, "output", "", "Output format (json)") |
| 91 | + _ = cmd.MarkFlagRequired("start") |
| 92 | + _ = cmd.MarkFlagRequired("end") |
| 93 | +} |
| 94 | + |
| 95 | +// metricsParamsFromFlags builds hookdeck.MetricsQueryParams from common flags. |
| 96 | +// Measures and dimensions are split from comma-separated strings. |
| 97 | +func metricsParamsFromFlags(f *metricsCommonFlags) hookdeck.MetricsQueryParams { |
| 98 | + var measures, dimensions []string |
| 99 | + if f.measures != "" { |
| 100 | + for _, s := range strings.Split(f.measures, ",") { |
| 101 | + if t := strings.TrimSpace(s); t != "" { |
| 102 | + measures = append(measures, t) |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + if f.dimensions != "" { |
| 107 | + for _, s := range strings.Split(f.dimensions, ",") { |
| 108 | + if t := strings.TrimSpace(s); t != "" { |
| 109 | + dimensions = append(dimensions, t) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + return hookdeck.MetricsQueryParams{ |
| 114 | + Start: f.start, |
| 115 | + End: f.end, |
| 116 | + Granularity: f.granularity, |
| 117 | + Measures: measures, |
| 118 | + Dimensions: dimensions, |
| 119 | + SourceID: f.sourceID, |
| 120 | + DestinationID: f.destinationID, |
| 121 | + ConnectionID: f.connectionID, |
| 122 | + Status: f.status, |
| 123 | + IssueID: f.issueID, |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +type metricsCmd struct { |
| 128 | + cmd *cobra.Command |
| 129 | +} |
| 130 | + |
| 131 | +func newMetricsCmd() *metricsCmd { |
| 132 | + mc := &metricsCmd{} |
| 133 | + |
| 134 | + mc.cmd = &cobra.Command{ |
| 135 | + Use: "metrics", |
| 136 | + Args: validators.NoArgs, |
| 137 | + Short: ShortBeta("Query Event Gateway metrics"), |
| 138 | + Long: LongBeta(`Query metrics for events, requests, attempts, queue depth, pending events, events by issue, and transformations. |
| 139 | +Requires --start and --end (ISO 8601 date-time). Use subcommands to choose the metric type.`), |
| 140 | + } |
| 141 | + |
| 142 | + mc.cmd.AddCommand(newMetricsEventsCmd().cmd) |
| 143 | + mc.cmd.AddCommand(newMetricsRequestsCmd().cmd) |
| 144 | + mc.cmd.AddCommand(newMetricsAttemptsCmd().cmd) |
| 145 | + mc.cmd.AddCommand(newMetricsQueueDepthCmd().cmd) |
| 146 | + mc.cmd.AddCommand(newMetricsPendingCmd().cmd) |
| 147 | + mc.cmd.AddCommand(newMetricsEventsByIssueCmd().cmd) |
| 148 | + mc.cmd.AddCommand(newMetricsTransformationsCmd().cmd) |
| 149 | + |
| 150 | + return mc |
| 151 | +} |
| 152 | + |
| 153 | +func addMetricsCmdTo(parent *cobra.Command) { |
| 154 | + parent.AddCommand(newMetricsCmd().cmd) |
| 155 | +} |
0 commit comments