diff --git a/tscli/main.go b/tscli/main.go index 380381d..e97c5fa 100644 --- a/tscli/main.go +++ b/tscli/main.go @@ -113,13 +113,19 @@ func executeQuery(cmd *cobra.Command, args []string) error { // todo: these need to be settable as they are not uniformly useful across all metric types. PerSeriesAligner: monitoringpb.Aggregation_ALIGN_RATE, CrossSeriesReducer: monitoringpb.Aggregation_REDUCE_MEAN, + GroupByFields: []string{"metric.labels.classification"}, }, View: monitoringpb.ListTimeSeriesRequest_FULL, } tsi := GoogleCloudMonitoringClient.ListTimeSeries(context.Background(), request) // todo: handle multiple time series - ts, err := tsi.Next() + plot, err := tsplot.NewPlotFromTimeSeriesIterator(tsi) + if err != nil { + log.Fatal(err) + } + + /*ts, err := tsi.Next() if err != nil { log.Fatal(err) } @@ -128,6 +134,7 @@ func executeQuery(cmd *cobra.Command, args []string) error { if err != nil { log.Fatal(err) } + */ saveFile := fmt.Sprintf("%s/test.png", "/tmp/") return plot.Save(8*vg.Inch, 4*vg.Inch, saveFile) diff --git a/tsplot/plot.go b/tsplot/plot.go index fb1a0b2..411eacc 100644 --- a/tsplot/plot.go +++ b/tsplot/plot.go @@ -1,9 +1,11 @@ package tsplot import ( + monitoring "cloud.google.com/go/monitoring/apiv3/v2" "fmt" "gonum.org/v1/plot" "gonum.org/v1/plot/plotter" + "google.golang.org/api/iterator" "google.golang.org/genproto/googleapis/api/metric" monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" ) @@ -70,9 +72,8 @@ func NewPlotFromTimeSeries(ts *monitoringpb.TimeSeries, opts ...PlotOption) (*pl return p, nil } -/* -func NewPlotFromTimeSeriesIterator(tsi monitoring.TimeSeriesIterator) (*plot.Plot, error) { - var ts *monitoringpb.TimeSeries +func NewPlotFromTimeSeriesIterator(tsi *monitoring.TimeSeriesIterator) (*plot.Plot, error) { + p := plot.New() for { timeSeries, err := tsi.Next() if err != nil { @@ -81,12 +82,14 @@ func NewPlotFromTimeSeriesIterator(tsi monitoring.TimeSeriesIterator) (*plot.Plo } return nil, err } - ts = timeSeries + line, _ := createLine(timeSeries.GetPoints()) + p.Y.Max = 400 // todo: make dynamic according to data + p.Add(line) } - return nil, nil + return p, nil } -*/ + // createLine creates a line from data points. func createLine(dataPoints []*monitoringpb.Point) (*plotter.Line, error) { var XYs plotter.XYs