Skip to content

Commit 047e30c

Browse files
authored
v2: Ditch Query helpers. (#17)
This PR focuses around simplification of the tsplot package. Moving away from something that was rather perscriptive in the type of Query that you could make and towards a package that can accept a Time Series or Time Series Itterator from the Google Metrics API and return a rendered graph of those Time Series.
1 parent b97b9fd commit 047e30c

File tree

15 files changed

+218
-641
lines changed

15 files changed

+218
-641
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ jobs:
77
- uses: actions/checkout@v2
88
- uses: actions/setup-go@v2
99
with:
10-
go-version: '^1.16.2'
10+
go-version: '^1.20'
1111
- run: make test
1212
- run: make build
13-
- run: make codegendiff

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea/*
2+
.DS_store

Makefile

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,9 @@ test:
1111
$(GOBIN) test -v ./...
1212

1313
.PHONY: build
14-
build: $(BINDIR)/codegen
14+
build:
1515
make -C tscli
1616

17-
$(BINDIR)/codegen: $(SCRIPTDIR)/codegen.go
18-
@mkdir -p $(BINDIR)
19-
GOOS=linux GOARCH=amd64 $(GOBIN) build -o $(BINDIR)/codegen $<
20-
2117
.PHONY: install
2218
install: build
2319
cp $(BINDIR)/tscli /usr/local/bin/
@@ -26,11 +22,3 @@ install: build
2622
clean:
2723
rm -rf $(BINDIR)
2824

29-
CODEGENFILE="set_aggregation_opts.go"
30-
.PHONY: codegen
31-
codegen:
32-
$(BINDIR)/codegen -output ./tsplot/$(CODEGENFILE)
33-
34-
.PHONY: codegendiff
35-
codegendiff:
36-
diff ./tsplot/$(CODEGENFILE) <($(BINDIR)/codegen -stdout)

README.md

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2,84 +2,44 @@
22
[![Go Report Card](https://goreportcard.com/badge/github.com/bitly/tsplot)](https://goreportcard.com/report/github.com/bitly/tsplot)
33
[![Go Reference](https://pkg.go.dev/badge/github.com/bitly/tsplot.svg)](https://pkg.go.dev/github.com/bitly/tsplot)
44

5-
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.
6-
7-
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
8-
Slack bots. There is no facility in the Google API that provides a PNG of already graphed data.
9-
105
## Authentication
116
This package makes no effort to assist in authentication to the Google APIs.
127
Instead, it will expect the caller to supply an authenticated client.
138

149
More information on authentication can be found in the official [Google Cloud documentation](https://cloud.google.com/docs/authentication).
15-
16-
## Query
17-
tsplot helps to facilitate easy querying of the Google Cloud Monitoring API for time series matching the supplied criteria.
18-
In addition it provides methods of overriding certain aspects of the query.
19-
20-
For example, the following code snippet will return a single time series for the following metric descriptor: `custom.googleapis.com/opencensus/fishnet/queuereader_fishnet/messages_total`.
2110
```
2211
func main() {
2312
24-
... snip ...
13+
... snip ...
2514
2615
start := time.Now().Add(-1 * time.Hour)
2716
end := time.Now()
28-
mq := &tsplot.NewMetricQuery(
29-
"bitly-gcp-prod", // GCP project
30-
"custom.googleapis.com/opencensus/fishent/queuereader_fishnet/messages_total", // metric descriptor
31-
&start, // start of time window
32-
&end, // end of time window
33-
)
34-
35-
// disable cross series reducer (MEAN reduction is default)
36-
query.Set_REDUCE_NONE()
37-
38-
// set different alignment window. (Default is 1 minute)
39-
query.SetAlignmentPeriod(time.Minute * 2)
40-
41-
tsi, err := mq.PerformWithClient(client) // client is provided by user
42-
if err != nil {
43-
fmt.Printf("error performing query: %v\n", err)
44-
}
45-
}
46-
```
47-
48-
## Plotting
49-
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.
50-
51-
The example below creates a new graph containing a singular time series, plots it, and saves the resulting plot to disk.
52-
```
53-
func main() {
54-
55-
... snip ...
56-
57-
ts := tsplot.TimeSeries{}
58-
59-
// optionally iterate over returned time series
60-
timeSeries, _ := tsi.Next()
61-
ts[metric] = ts.GetPoints()
62-
63-
// create the plot with some formatting options
64-
p, err := ts.Plot([]tsplot.PlotOption{
65-
tsplot.WithXAxisName("UTC"),
66-
tsplot.WIthGrid(colornames.Darkgrey),
67-
tsplot.WithTitle(metric)}...)
6817
69-
if err != nil {
70-
return err
18+
// create new request
19+
request := monitoringpb.ListTimeSeriesRequest{
20+
Name: fmt.Sprintf("projects/%s", project),
21+
Filter: query,
22+
Interval: &monitoringpb.TimeInterval{
23+
EndTime: timestamppb.New(et),
24+
StartTime: timestamppb.New(st),
25+
},
26+
Aggregation: nil,
27+
SecondaryAggregation: nil,
28+
View: monitoringpb.ListTimeSeriesRequest_FULL,
7129
}
72-
73-
// optionally save the plot to disk
74-
p.Save(8*vg.Inch, 4*vg.Inch, "./my-graph.png")
30+
31+
// execute the request and get the response from Google APIs
32+
tsi := GoogleCloudMonitoringClient.ListTimeSeries(context.Background(), request)
33+
34+
// Create the plot from the GAPI TimeSeries
35+
plot, _ := tsplot.NewPlotFromTimeSeriesIterator(tsi, "", nil)
36+
37+
// Save the new plot to disk.
38+
plot.Save(8*vg.Inch, 4*vg.Inch, "my_plot.png")
7539
}
7640
```
7741

7842
### Example generated graphs:
7943
Query across multiple time series with mean reducer:
8044
![graph1](sample/1.png)
8145

82-
### Graph Color Scheme
83-
I'm not a UX designer, but I have selected colors that I find higher contrast
84-
and easier to see. I am basing this completely off my colorblindness which is
85-
unique to me. Improvements to the color palette used are welcome.

go.mod

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/bitly/tsplot
22

3-
go 1.14
3+
go 1.20
44

55
require (
66
cloud.google.com/go v0.80.0
@@ -11,3 +11,25 @@ require (
1111
google.golang.org/genproto v0.0.0-20210401141331-865547bb08e2
1212
google.golang.org/protobuf v1.26.0
1313
)
14+
15+
require (
16+
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af // indirect
17+
github.com/fogleman/gg v1.3.0 // indirect
18+
github.com/go-fonts/liberation v0.1.1 // indirect
19+
github.com/go-latex/latex v0.0.0-20210118124228-b3d85cf34e07 // indirect
20+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
21+
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
22+
github.com/golang/protobuf v1.5.1 // indirect
23+
github.com/google/go-cmp v0.5.5 // indirect
24+
github.com/googleapis/gax-go/v2 v2.0.5 // indirect
25+
github.com/inconshreveable/mousetrap v1.0.0 // indirect
26+
github.com/phpdave11/gofpdf v1.4.2 // indirect
27+
github.com/spf13/pflag v1.0.5 // indirect
28+
go.opencensus.io v0.23.0 // indirect
29+
golang.org/x/net v0.0.0-20210316092652-d523dce5a7f4 // indirect
30+
golang.org/x/oauth2 v0.0.0-20210313182246-cd4f82c27b84 // indirect
31+
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4 // indirect
32+
golang.org/x/text v0.3.5 // indirect
33+
google.golang.org/appengine v1.6.7 // indirect
34+
google.golang.org/grpc v1.36.1 // indirect
35+
)

go.sum

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,6 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
526526
gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
527527
gonum.org/v1/gonum v0.8.2 h1:CCXrcPKiGGotvnN6jfUsKk4rRqm7q09/YbKb5xCEvtM=
528528
gonum.org/v1/gonum v0.8.2/go.mod h1:oe/vMfY3deqTw+1EZJhuvEW2iwGF1bW9wwu7XCu0+v0=
529-
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0 h1:OE9mWmgKkjJyEmDAAtGMPjXu+YNeGvK9VTSHY6+Qihc=
530529
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
531530
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
532531
gonum.org/v1/plot v0.9.0 h1:3sEo36Uopv1/SA/dMFFaxXoL5XyikJ9Sf2Vll/k6+2E=

scripts/codegen.go

Lines changed: 0 additions & 67 deletions
This file was deleted.

tscli/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ default: build
1010
.PHONY: build
1111
build:
1212
@mkdir -p $(BINDIR)
13-
GOOS=linux GOARCH=amd64 $(GOBIN) build -o $(BINDIR)/tscli
13+
$(GOBIN) build -o $(BINDIR)/tscli

0 commit comments

Comments
 (0)