Skip to content

Commit 3103d0c

Browse files
authored
tsplot: Create settable aggregation options. (#12)
Expose ability to set all possible alignment and reduction options on the queries aggregation settings. All Set_* methods are generated using scripts/codegen.go.
1 parent d90e5a1 commit 3103d0c

File tree

8 files changed

+296
-30
lines changed

8 files changed

+296
-30
lines changed

.github/workflows/ci.yml

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,6 @@ jobs:
88
- uses: actions/setup-go@v2
99
with:
1010
go-version: '^1.16.2'
11-
- run: go test -v ./tsplot/...
12-
tscli:
13-
runs-on: ubuntu-latest
14-
steps:
15-
- uses: actions/checkout@v2
16-
- uses: actions/setup-go@v2
17-
with:
18-
go-version: '^1.16.2'
19-
- run: go test -v ./tscli/...
20-
11+
- run: make test
12+
- run: make build
13+
- run: make codegendiff

Makefile

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,36 @@
1+
BINDIR ?= ./bin
2+
SCRIPTDIR := ./scripts
3+
SHELL := /bin/bash
4+
GOBIN := $(shell which go 2> /dev/null)
5+
ifeq ($(GOBIN),)
6+
GOBIN := /usr/local/go/bin/go
7+
endif
8+
19
.PHONY: test
210
test:
3-
go test -v ./...
11+
$(GOBIN) test -v ./...
412

513
.PHONY: build
6-
build:
14+
build: $(BINDIR)/codegen
715
make -C tscli
816

17+
$(BINDIR)/codegen: $(SCRIPTDIR)/codegen.go
18+
@mkdir -p $(BINDIR)
19+
GOOS=linux GOARCH=amd64 $(GOBIN) build -o $(BINDIR)/codegen $<
20+
921
.PHONY: install
1022
install: build
11-
cp ./bin/tscli /usr/local/bin/
23+
cp $(BINDIR)/tscli /usr/local/bin/
1224

1325
.PHONY: clean
1426
clean:
15-
rm -rf ./bin
27+
rm -rf $(BINDIR)
28+
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)

scripts/codegen.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package main
2+
/*
3+
codegen.go creates Set_* API methods for Google Cloud Monitoring aggregation alignment and reduction options.
4+
Leverages the exported variables in the Google monitoring/v3 package (common.pb.go) to ensure that all
5+
alignment and reduction options are covered.
6+
*/
7+
8+
import (
9+
"flag"
10+
"log"
11+
"os"
12+
"text/template"
13+
14+
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
15+
)
16+
17+
var rawTpl = `/*
18+
DO NOT EDIT
19+
Generated by codegen.go
20+
https://github.com/googleapis/go-genproto/blob/0135a39c27378c1b903c75204eff61a060be5eb7/googleapis/monitoring/v3/common.pb.go
21+
*/
22+
package tsplot
23+
{{range $name, $value := .Aligners}}
24+
func (mq *MetricQuery) Set_{{$name}}() {
25+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner({{$value}}))
26+
}
27+
{{end -}}
28+
{{range $name, $value := .Reducers}}
29+
func (mq *MetricQuery) Set_{{$name}}() {
30+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer({{$value}}))
31+
}
32+
{{end}}
33+
`
34+
35+
func main() {
36+
outFileName := flag.String("output", "./tsplot/set_aggregation_opts.go", "Output path of generated file.")
37+
toStdout := flag.Bool("stdout", false, "Toggle output to STDOUT.")
38+
flag.Parse()
39+
40+
var outFile *os.File
41+
var fileErr error
42+
43+
if *toStdout {
44+
outFile = os.Stdout
45+
} else {
46+
outFile, fileErr = os.OpenFile(*outFileName, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0644)
47+
if fileErr != nil {
48+
log.Print(fileErr)
49+
}
50+
defer outFile.Close()
51+
}
52+
53+
values := struct {
54+
Aligners map[string]int32
55+
Reducers map[string]int32
56+
}{
57+
Aligners: monitoringpb.Aggregation_Aligner_value,
58+
Reducers: monitoringpb.Aggregation_Reducer_value,
59+
}
60+
61+
t := template.New("codegen")
62+
pt := template.Must(t.Parse(rawTpl))
63+
err := pt.Execute(outFile, values)
64+
if err != nil {
65+
log.Fatal(err)
66+
}
67+
}

tscli/Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
BINDIR ?= ../bin
2+
SHELL := /bin/bash
3+
GOBIN := $(shell which go 2> /dev/null)
4+
ifeq ($(GOBIN),)
5+
GOBIN := /usr/local/go/bin/go
6+
endif
27

38
default: build
49

510
.PHONY: build
611
build:
712
@mkdir -p $(BINDIR)
8-
GOOS=linux GOARCH=amd64 /usr/local/go/bin/go build -o $(BINDIR)/tscli
13+
GOOS=linux GOARCH=amd64 $(GOBIN) build -o $(BINDIR)/tscli

tscli/main.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,18 +114,20 @@ func executeQuery(cmd *cobra.Command, args []string) error {
114114
st := parseTime(startTime)
115115
et := parseTime(endTime)
116116

117-
query := tsplot.MetricQuery{
118-
Project: project,
119-
MetricDescriptor: fmt.Sprintf("custom.googleapis.com/opencensus/%s/%s/%s", app, service, metric),
120-
StartTime: &st,
121-
EndTime: &et,
122-
}
117+
query := tsplot.NewMetricQuery(
118+
project,
119+
fmt.Sprintf("custom.googleapis.com/opencensus/%s/%s/%s", app, service, metric),
120+
&st,
121+
&et,
122+
)
123123

124124
if queryOverride != "" {
125125
query.SetQueryFilter(queryOverride)
126126
}
127127

128-
query.SetReduce(reduce)
128+
if !reduce {
129+
query.Set_REDUCE_NONE()
130+
}
129131

130132
tsi, err := query.PerformWithClient(GoogleCloudMonitoringClient)
131133
if err != nil {

tsplot/options.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package tsplot
22

33
import (
4+
"image/color"
5+
"time"
6+
47
"gonum.org/v1/plot"
58
"gonum.org/v1/plot/plotter"
6-
"image/color"
9+
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
10+
"google.golang.org/protobuf/types/known/durationpb"
711
)
812

913
// PlotOption defines the type used to configure the underlying *plot.Plot.
@@ -91,3 +95,28 @@ func ApplyDefaultHighContrast(p *plot.Plot) {
9195
opt(p)
9296
}
9397
}
98+
99+
// aggregationOption defines the type used to configure the underlying *monitoringpb.Aggregation.
100+
// A function that returns aggregationOption can be used to set options on the *monitoringpb.Aggregation.
101+
type aggregationOption func(agg *monitoringpb.Aggregation)
102+
103+
// withAlignmentPeriod sets the duration of the aggregation's alignment period.
104+
func withAlignmentPeriod(d time.Duration) aggregationOption {
105+
return func(agg *monitoringpb.Aggregation) {
106+
agg.AlignmentPeriod = durationpb.New(d)
107+
}
108+
}
109+
110+
// withPerSeriesAligner sets the alignment method used for the time series.
111+
func withPerSeriesAligner(aligner monitoringpb.Aggregation_Aligner) aggregationOption {
112+
return func(agg *monitoringpb.Aggregation) {
113+
agg.PerSeriesAligner = aligner
114+
}
115+
}
116+
117+
// withCrossSeriesReducer sets the reduction method used for the time series.
118+
func withCrossSeriesReducer(reducer monitoringpb.Aggregation_Reducer) aggregationOption {
119+
return func(agg *monitoringpb.Aggregation) {
120+
agg.CrossSeriesReducer = reducer
121+
}
122+
}

tsplot/query.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,18 +29,28 @@ type MetricQuery struct {
2929
EndTime *time.Time
3030

3131
queryFilter string
32-
reduce bool
32+
aggregation *[]aggregationOption
33+
}
34+
35+
// NewMetricQuery creates a new MetricQuery type with the aggregation opts initialized.
36+
func NewMetricQuery(project, metric string, startTime, endTime *time.Time) *MetricQuery {
37+
return &MetricQuery{
38+
Project: project,
39+
MetricDescriptor: metric,
40+
StartTime: startTime,
41+
EndTime: endTime,
42+
aggregation: &[]aggregationOption{},
43+
}
3344
}
3445

3546
// SetQueryFilter provides a hook to modify the metric query filter.
3647
func (mq *MetricQuery) SetQueryFilter(queryFilter string) {
3748
mq.queryFilter = queryFilter
3849
}
3950

40-
// SetReduce sets the boolean reduce value on *MetricQuery structure. This controls whether or not cross series reduction is
41-
// performed on multiple time series.
42-
func (mq *MetricQuery) SetReduce(b bool) {
43-
mq.reduce = b
51+
// SetAlignmentPeriod sets the alignment duration.
52+
func (mq *MetricQuery) SetAlignmentPeriod(d time.Duration) {
53+
*mq.aggregation = append(*mq.aggregation, withAlignmentPeriod(d))
4454
}
4555

4656
// request builds and returns a *monitoringpb.ListTimeSeriesRequest.
@@ -88,8 +98,8 @@ func (mq *MetricQuery) request() (*monitoringpb.ListTimeSeriesRequest, error) {
8898
View: monitoringpb.ListTimeSeriesRequest_FULL,
8999
}
90100

91-
if !mq.reduce {
92-
tsreq.Aggregation.CrossSeriesReducer = 0
101+
for _, opt := range *mq.aggregation {
102+
opt(tsreq.Aggregation)
93103
}
94104

95105
return &tsreq, nil

tsplot/set_aggregation_opts.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
DO NOT EDIT
3+
Generated by codegen.go
4+
https://github.com/googleapis/go-genproto/blob/0135a39c27378c1b903c75204eff61a060be5eb7/googleapis/monitoring/v3/common.pb.go
5+
*/
6+
package tsplot
7+
8+
func (mq *MetricQuery) Set_ALIGN_COUNT() {
9+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(13))
10+
}
11+
12+
func (mq *MetricQuery) Set_ALIGN_COUNT_FALSE() {
13+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(24))
14+
}
15+
16+
func (mq *MetricQuery) Set_ALIGN_COUNT_TRUE() {
17+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(16))
18+
}
19+
20+
func (mq *MetricQuery) Set_ALIGN_DELTA() {
21+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(1))
22+
}
23+
24+
func (mq *MetricQuery) Set_ALIGN_FRACTION_TRUE() {
25+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(17))
26+
}
27+
28+
func (mq *MetricQuery) Set_ALIGN_INTERPOLATE() {
29+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(3))
30+
}
31+
32+
func (mq *MetricQuery) Set_ALIGN_MAX() {
33+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(11))
34+
}
35+
36+
func (mq *MetricQuery) Set_ALIGN_MEAN() {
37+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(12))
38+
}
39+
40+
func (mq *MetricQuery) Set_ALIGN_MIN() {
41+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(10))
42+
}
43+
44+
func (mq *MetricQuery) Set_ALIGN_NEXT_OLDER() {
45+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(4))
46+
}
47+
48+
func (mq *MetricQuery) Set_ALIGN_NONE() {
49+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(0))
50+
}
51+
52+
func (mq *MetricQuery) Set_ALIGN_PERCENTILE_05() {
53+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(21))
54+
}
55+
56+
func (mq *MetricQuery) Set_ALIGN_PERCENTILE_50() {
57+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(20))
58+
}
59+
60+
func (mq *MetricQuery) Set_ALIGN_PERCENTILE_95() {
61+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(19))
62+
}
63+
64+
func (mq *MetricQuery) Set_ALIGN_PERCENTILE_99() {
65+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(18))
66+
}
67+
68+
func (mq *MetricQuery) Set_ALIGN_PERCENT_CHANGE() {
69+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(23))
70+
}
71+
72+
func (mq *MetricQuery) Set_ALIGN_RATE() {
73+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(2))
74+
}
75+
76+
func (mq *MetricQuery) Set_ALIGN_STDDEV() {
77+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(15))
78+
}
79+
80+
func (mq *MetricQuery) Set_ALIGN_SUM() {
81+
*mq.aggregation = append(*mq.aggregation, withPerSeriesAligner(14))
82+
}
83+
84+
func (mq *MetricQuery) Set_REDUCE_COUNT() {
85+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(6))
86+
}
87+
88+
func (mq *MetricQuery) Set_REDUCE_COUNT_FALSE() {
89+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(15))
90+
}
91+
92+
func (mq *MetricQuery) Set_REDUCE_COUNT_TRUE() {
93+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(7))
94+
}
95+
96+
func (mq *MetricQuery) Set_REDUCE_FRACTION_TRUE() {
97+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(8))
98+
}
99+
100+
func (mq *MetricQuery) Set_REDUCE_MAX() {
101+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(3))
102+
}
103+
104+
func (mq *MetricQuery) Set_REDUCE_MEAN() {
105+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(1))
106+
}
107+
108+
func (mq *MetricQuery) Set_REDUCE_MIN() {
109+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(2))
110+
}
111+
112+
func (mq *MetricQuery) Set_REDUCE_NONE() {
113+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(0))
114+
}
115+
116+
func (mq *MetricQuery) Set_REDUCE_PERCENTILE_05() {
117+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(12))
118+
}
119+
120+
func (mq *MetricQuery) Set_REDUCE_PERCENTILE_50() {
121+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(11))
122+
}
123+
124+
func (mq *MetricQuery) Set_REDUCE_PERCENTILE_95() {
125+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(10))
126+
}
127+
128+
func (mq *MetricQuery) Set_REDUCE_PERCENTILE_99() {
129+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(9))
130+
}
131+
132+
func (mq *MetricQuery) Set_REDUCE_STDDEV() {
133+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(5))
134+
}
135+
136+
func (mq *MetricQuery) Set_REDUCE_SUM() {
137+
*mq.aggregation = append(*mq.aggregation, withCrossSeriesReducer(4))
138+
}
139+

0 commit comments

Comments
 (0)