diff --git a/README.md b/README.md index 27397fb..69c9646 100644 --- a/README.md +++ b/README.md @@ -2,11 +2,6 @@ [![Go Report Card](https://goreportcard.com/badge/github.com/bitly/tsplot)](https://goreportcard.com/report/github.com/bitly/tsplot) [![Go Reference](https://pkg.go.dev/badge/github.com/bitly/tsplot.svg)](https://pkg.go.dev/github.com/bitly/tsplot) -This package provides a method of querying for raw time series data from the GCM APIs and additionally plotting that data for use in other applications. - -This came to be due to what we consider a small limitation in the Google APIs which require us to re-draw graphs to include them in other applications such as -Slack bots. There is no facility in the Google API that provides a PNG of already graphed data. - ## Authentication This package makes no effort to assist in authentication to the Google APIs. Instead, it will expect the caller to supply an authenticated client. @@ -16,60 +11,31 @@ More information on authentication can be found in the official [Google Cloud do func main() { ... snip ... - + start := time.Now().Add(-1 * time.Hour) end := time.Now() - - // create new request - request := tsplot.GCMTimeSeriesRequester{ListTimeSeriesRequest: monitoringpb.ListTimeSeriesRequest{ - Name: fmt.Sprintf("projects/%s", project), - Filter: query, - Interval: &monitoringpb.TimeInterval{ - EndTime: timestamppb.New(et), - StartTime: timestamppb.New(st), - }, - Aggregation: nil, - SecondaryAggregation: nil, - View: monitoringpb.ListTimeSeriesRequest_FULL, - }} - - // disable cross series reducer (MEAN reduction is default) - request.Set_REDUCE_NONE() - - // set different alignment window. (Default is 1 minute) - request.SetAlignmentPeriod(time.Minute * 5) - - tsi := request.Do(client) // client is provided by user -} -``` -## Plotting -To plot the data, tsplot leverages the open source package [gonum/plot](github.com/gonum/plot) to create a graph and plot the data for a given time series. - -The example below creates a new graph containing a singular time series, plots it, and saves the resulting plot to disk. -``` -func main() { - - ... snip ... - - ts := tsplot.TimeSeries{} + // create new request + request := monitoringpb.ListTimeSeriesRequest{ + Name: fmt.Sprintf("projects/%s", project), + Filter: query, + Interval: &monitoringpb.TimeInterval{ + EndTime: timestamppb.New(et), + StartTime: timestamppb.New(st), + }, + Aggregation: nil, + SecondaryAggregation: nil, + View: monitoringpb.ListTimeSeriesRequest_FULL, + } - // optionally iterate over returned time series - timeSeries, _ := tsi.Next() - ts[metric] = ts.GetPoints() + // execute the request and get the response from Google APIs + tsi := GoogleCloudMonitoringClient.ListTimeSeries(context.Background(), request) - // create the plot with some formatting options - p, err := ts.Plot([]tsplot.PlotOption{ - tsplot.WithXAxisName("UTC"), - tsplot.WIthGrid(colornames.Darkgrey), - tsplot.WithTitle(metric)}...) - - if err != nil { - return err - } + // Create the plot from the GAPI TimeSeries + plot, _ := tsplot.NewPlotFromTimeSeriesIterator(tsi) - // optionally save the plot to disk - p.Save(8*vg.Inch, 4*vg.Inch, "./my-graph.png") + // Save the new plot to disk. + plot.Save(8*vg.Inch, 4*vg.Inch, "my_plot.png") } ``` diff --git a/scripts/codegen.go b/scripts/codegen.go deleted file mode 100644 index 837d8e9..0000000 --- a/scripts/codegen.go +++ /dev/null @@ -1,101 +0,0 @@ -package main - -/* -codegen.go creates Set_* API methods for Google Cloud Monitoring aggregation alignment and reduction options. -Leverages the exported variables in the Google monitoring/v3 package (common.pb.go) to ensure that all -alignment and reduction options are covered. -*/ - -import ( - "flag" - "log" - "os" - "text/template" - - monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" -) - -var rawTpl = `package tsplot -/* -DO NOT EDIT -Generated by codegen.go -https://github.com/googleapis/go-genproto/blob/0135a39c27378c1b903c75204eff61a060be5eb7/googleapis/monitoring/v3/common.pb.go -*/ - -import ( - "time" - - monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3" - "google.golang.org/protobuf/types/known/durationpb" -) - -// aggregationOption defines the type used to configure the underlying *monitoringpb.Aggregation. -// A function that returns aggregationOption can be used to set options on the *monitoringpb.Aggregation. -type aggregationOption func(agg *monitoringpb.Aggregation) - -// withAlignmentPeriod sets the duration of the aggregation's alignment period. -func withAlignmentPeriod(d time.Duration) aggregationOption { - return func(agg *monitoringpb.Aggregation) { - agg.AlignmentPeriod = durationpb.New(d) - } -} - -// withPerSeriesAligner sets the alignment method used for the time series. -func withPerSeriesAligner(aligner monitoringpb.Aggregation_Aligner) aggregationOption { - return func(agg *monitoringpb.Aggregation) { - agg.PerSeriesAligner = aligner - } -} - -// withCrossSeriesReducer sets the reduction method used for the time series. -func withCrossSeriesReducer(reducer monitoringpb.Aggregation_Reducer) aggregationOption { - return func(agg *monitoringpb.Aggregation) { - agg.CrossSeriesReducer = reducer - } -} - -{{range $name, $value := .Aligners}} -func (tsr *GCMTimeSeriesRequester) Set_{{$name}}() { - *tsr.aggregation = append(*tsr.aggregation, withPerSeriesAligner({{$value}})) -} -{{end -}} -{{range $name, $value := .Reducers}} -func (tsr *GCMTimeSeriesRequester) Set_{{$name}}() { - *tsr.aggregation = append(*tsr.aggregation, withCrossSeriesReducer({{$value}})) -} -{{end}} -` - -func main() { - outFileName := flag.String("output", "./tsplot/set_aggregation_opts.go", "Output path of generated file.") - toStdout := flag.Bool("stdout", false, "Toggle output to STDOUT.") - flag.Parse() - - var outFile *os.File - var fileErr error - - if *toStdout { - outFile = os.Stdout - } else { - outFile, fileErr = os.OpenFile(*outFileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644) - if fileErr != nil { - log.Print(fileErr) - } - defer outFile.Close() - } - - values := struct { - Aligners map[string]int32 - Reducers map[string]int32 - }{ - Aligners: monitoringpb.Aggregation_Aligner_value, - Reducers: monitoringpb.Aggregation_Reducer_value, - } - - t := template.New("codegen") - pt := template.Must(t.Parse(rawTpl)) - err := pt.Execute(outFile, values) - if err != nil { - log.Fatal(err) - } -}