Skip to content

Commit 4535ee9

Browse files
committed
fix temp
1 parent a3fb647 commit 4535ee9

File tree

6 files changed

+242
-77
lines changed

6 files changed

+242
-77
lines changed

monitoring/exporter/data_test.go renamed to monitoring/exporter/stackdriver/data_test.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
package exporter
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
216

317
import (
418
"context"
@@ -14,11 +28,6 @@ import (
1428

1529
// This file defines various data needed for testing.
1630

17-
func init() {
18-
// For testing convenience, we reduce maximum time series that metric client accepts.
19-
MaxTimeSeriesPerUpload = 3
20-
}
21-
2231
const (
2332
label1name = "key_1"
2433
label2name = "key_2"
@@ -37,6 +46,8 @@ const (
3746
metric1desc = "this is metric 1"
3847
metric2name = "metric_2"
3948
metric2desc = "this is metric 2"
49+
metric3name = "metric_3"
50+
metric3desc = "this is metric 3"
4051

4152
project1 = "project-1"
4253
project2 = "project-2"
@@ -50,9 +61,10 @@ var (
5061
// This error is used for unexpected error.
5162
unrecognizedDataError = errors.New("unrecognized data")
5263

53-
key1 = getKey(label1name)
54-
key2 = getKey(label2name)
55-
key3 = getKey(label3name)
64+
key1 = getKey(label1name)
65+
key2 = getKey(label2name)
66+
key3 = getKey(label3name)
67+
projectKey = getKey(ProjectKeyName)
5668

5769
view1 = &view.View{
5870
Name: metric1name,
@@ -68,6 +80,13 @@ var (
6880
Measure: stats.Int64(metric2name, metric2desc, stats.UnitDimensionless),
6981
Aggregation: view.Sum(),
7082
}
83+
view3 = &view.View{
84+
Name: metric3name,
85+
Description: metric3desc,
86+
TagKeys: []tag.Key{projectKey},
87+
Measure: stats.Int(metric3name, metric3desc, stats.UnitDimensionless),
88+
Aggregation: view.Sum(),
89+
}
7190

7291
// To make verification easy, we require all valid rows should int64 values and all of them
7392
// must be distinct.
@@ -91,6 +110,14 @@ var (
91110
Tags: []tag.Tag{{key1, value4}, {key2, value5}, {key3, value6}},
92111
Data: &view.SumData{Value: 5},
93112
}
113+
view3row1 = &view.Row{
114+
Tags: []tag.Tag{{ProjectKeyName, project1}},
115+
Data: &view.SumData{Value: 6},
116+
}
117+
view3row2 = &view.Row{
118+
Tags: []tag.Tag{{ProjectKeyName, project1}},
119+
Data: &view.SumData{Value: 7},
120+
}
94121
// This Row does not have valid Data field, so is invalid.
95122
invalidRow = &view.Row{Data: nil}
96123

monitoring/exporter/mock_check_test.go renamed to monitoring/exporter/stackdriver/mock_check_test.go

Lines changed: 79 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,108 @@
1-
package exporter
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
216

317
import (
418
"context"
519
"fmt"
20+
"os"
621
"strings"
722
"testing"
823
"time"
924

25+
monitoring "cloud.google.com/go/monitoring/apiv3"
1026
gax "github.com/googleapis/gax-go"
1127
"google.golang.org/api/option"
28+
"google.golang.org/api/support/bundler"
1229
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
1330
)
1431

1532
// This file defines various mocks for testing, and checking functions for mocked data. We mock
1633
// metric client and bundler because their actions involves RPC calls or non-deterministic behavior.
1734

18-
// We define mock Client.
35+
var (
36+
// bundlerMark records
37+
bundlerMark map[string]*bundler.Bundler
38+
// projAddedRds saves all
39+
projAddedRds map[string][]*RowData
40+
41+
// timeSeriesReqs saves all incoming requests for creating time series.
42+
timeSerieReqs []*monitoringpb.CreateTimeSeriesRequest
43+
// timeSeriesResults holds predefined error values to be returned by mockCreateTimeSerie()
44+
// calls. Each errors in timeSeriesResults are returned per each mockCreateTimeSeries()
45+
// call. If all errors in timeSeriesResults are used, all other mockCreateTimeSeries calls
46+
// will return nil.
47+
timeSeriesResults []error
48+
)
49+
50+
func init() {
51+
// For testing convenience, we reduce maximum time series that metric client accepts.
52+
MaxTimeSeriesPerUpload = 3
53+
54+
newMetricClient = mockNewMetricClient
55+
createTimeSeries = mockCreateTimeSeries
56+
57+
}
58+
59+
func mockNewMetricClient(_ context.Context, _ ...option.ClientOption) (*monitoring.MetricClient, error) {
60+
return nil, nil
61+
}
62+
63+
func mockCreateTimeSeries(_ *monitoing.MetricClient, _ context.Context, req *monitoringpb.CreateTimeSeriesRequest, _ ...gax.CallOption) error {
64+
createTimeSeriesReq = append(createTimeSeriesReq, req)
65+
// Check returnErrs and if not empty, return the first error from it.
66+
if len(createTimereturnErrs) == 0 {
67+
return nil
68+
}
69+
err := data.returnErrs[0]
70+
// Delete the returning error.
71+
data.returnErrs = data.returnErrs[1:]
72+
return err
73+
}
74+
75+
func mockNewBundler(projectID string, _ interface{}, _ int) *bundler.Bundler {
76+
bndler := &bundler.Bundler{}
77+
bndler2proj[bndler] = projectID
78+
return bndlr
79+
}
1980

20-
type mockMetricClientData struct {
21-
// returnErrs holds predefined error values to return. Each errors in returnErrs are
22-
// returned per each CreateTimeSeries() call. If all errors in returnErrs are used, all
23-
// other CreateTimeSeries calls will return nil.
24-
returnErrs []error
25-
// reqs saves all incoming requests.
26-
reqs []*monitoringpb.CreateTimeSeriesRequest
81+
func addToBundler(bndler *bundler.Bundler, item interface{}, _ int) error {
82+
rds := &projAddRds[bndler2proj[bndler]]
83+
*rds = append(*rds, iterm.(*RowData))
84+
return nil
2785
}
2886

87+
func TestMain(m *testing.M) {
88+
89+
// Clear all data that stores behavior of mocked functions.
90+
createTimeSeriesReq = nil
91+
createTimeSeriesReturnErrs = nil
92+
93+
os.Exit(m.Run())
94+
}
95+
96+
// We define mock Client.
97+
2998
func (data *mockMetricClientData) createTimeSeries(_ *monitoring.MetricClient, _ context.Context, req *monitoringpb.CreateTimeSeriesRequest, opts ...gax.CallOption) error {
3099
data.reqs = append(data.reqs, req)
31100
// Check returnErrs and if not empty, return the first error from it.
32101
if len(data.returnErrs) == 0 {
33102
return nil
34103
}
35104
err := data.returnErrs[0]
36-
// delete the returning error.
105+
// Delete the returning error.
37106
data.returnErrs = data.returnErrs[1:]
38107
return err
39108
}

monitoring/exporter/project_data.go renamed to monitoring/exporter/stackdriver/project_data.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
1-
package exporter
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
216

317
import (
418
"fmt"
519

620
"go.opencensus.io/tag"
721
"google.golang.org/api/support/bundler"
822
metricpb "google.golang.org/genproto/googleapis/api/metric"
9-
monitoringpb "google.golang.org/genproto/googleapis/monitoring/v3"
23+
mpb "google.golang.org/genproto/googleapis/monitoring/v3"
1024
)
1125

12-
// maximum number of time series that stackdriver accepts. Only test may change this value.
26+
// MaxTimeSeriePerUpload is the maximum number of time series that stackdriver accepts. Only test
27+
// may change this value.
1328
var MaxTimeSeriesPerUpload = 200
1429

1530
// projectData contain per-project data in exporter. It should be created by newProjectData()
1631
type projectData struct {
17-
parent *StatsExporter
32+
parent *Exporter
1833
projectID string
1934
// We make bundler for each project because call to monitoring RPC can be grouped only in
2035
// project level
2136
bndler *bundler.Bundler
2237
}
2338

24-
var (
25-
newBundler = bundler.NewBundler
26-
bundlerAdd = (*bundler.Bundler).Add
27-
)
28-
29-
func (e *StatsExporter) newProjectData(projectID string) *projectData {
39+
func (e *Exporter) newProjectData(projectID string) *projectData {
3040
pd := &projectData{
3141
parent: e,
3242
projectID: projectID,
3343
}
3444

35-
pd.bndler = newBundler((*RowData)(nil), pd.uploadRowData)
45+
pd.bndler = newBundler(projectID, (*RowData)(nil), pd.uploadRowData)
3646
// Set options for bundler if they are provided by users.
3747
if 0 < e.opts.BundleDelayThreshold {
3848
pd.bndler.DelayThreshold = e.opts.BundleDelayThreshold
@@ -53,10 +63,10 @@ func (pd *projectData) uploadRowData(bundle interface{}) {
5363
// remainingRds are RowData that has not been processed at all.
5464
var reqRds, remainingRds []*RowData
5565
for ; len(rds) != 0; rds = remainingRds {
56-
var req *monitoringpb.CreateTimeSeriesRequest
66+
var req *mpb.CreateTimeSeriesRequest
5767
req, reqRds, remainingRds = pd.makeReq(rds)
5868
if req == nil {
59-
// no need to perform RPC call for empty set of requests.
69+
// No need to perform RPC call for empty set of requests.
6070
continue
6171
}
6272
if err := createTimeSeries(exp.client, exp.ctx, req); err != nil {
@@ -77,9 +87,9 @@ func (pd *projectData) uploadRowData(bundle interface{}) {
7787
//
7888
// Some rows in rds may fail while converting them to time series, and in that case makeReq() calls
7989
// exporter's onError() directly, not propagating errors to the caller.
80-
func (pd *projectData) makeReq(rds []*RowData) (req *monitoringpb.CreateTimeSeriesRequest, reqRds, remainingRds []*RowData) {
90+
func (pd *projectData) makeReq(rds []*RowData) (req *mpb.CreateTimeSeriesRequest, reqRds, remainingRds []*RowData) {
8191
exp := pd.parent
82-
timeSeries := []*monitoringpb.TimeSeries{}
92+
timeSeries := []*mpb.TimeSeries{}
8393

8494
var i int
8595
var rd *RowData
@@ -97,13 +107,13 @@ func (pd *projectData) makeReq(rds []*RowData) (req *monitoringpb.CreateTimeSeri
97107
continue
98108
}
99109

100-
ts := &monitoringpb.TimeSeries{
110+
ts := &mpb.TimeSeries{
101111
Metric: &metricpb.Metric{
102112
Type: rd.View.Name,
103113
Labels: exp.makeLabels(rd.Row.Tags),
104114
},
105115
Resource: resource,
106-
Points: []*monitoringpb.Point{pt},
116+
Points: []*mpb.Point{pt},
107117
}
108118
// Growing timeseries and reqRds are done at same time.
109119
timeSeries = append(timeSeries, ts)
@@ -119,7 +129,7 @@ func (pd *projectData) makeReq(rds []*RowData) (req *monitoringpb.CreateTimeSeri
119129
if len(timeSeries) == 0 {
120130
req = nil
121131
} else {
122-
req = &monitoringpb.CreateTimeSeriesRequest{
132+
req = &mpb.CreateTimeSeriesRequest{
123133
Name: fmt.Sprintf("projects/%s", pd.projectID),
124134
TimeSeries: timeSeries,
125135
}
@@ -128,7 +138,7 @@ func (pd *projectData) makeReq(rds []*RowData) (req *monitoringpb.CreateTimeSeri
128138
}
129139

130140
// makeLables constructs label that's ready for being uploaded to stackdriver.
131-
func (e *StatsExporter) makeLabels(tags []tag.Tag) map[string]string {
141+
func (e *Exporter) makeLabels(tags []tag.Tag) map[string]string {
132142
opts := e.opts
133143
labels := make(map[string]string, len(opts.DefaultLabels)+len(tags))
134144
for key, val := range opts.DefaultLabels {

monitoring/exporter/row_data_to_point.go renamed to monitoring/exporter/stackdriver/row_data_to_point.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,18 @@
1-
package exporter
1+
// Copyright 2018 Google Inc. All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package stackdriver
216

317
import (
418
"time"

0 commit comments

Comments
 (0)