Skip to content

Commit 151f1a0

Browse files
committed
make options passing by value
1 parent 73e12c7 commit 151f1a0

File tree

4 files changed

+25
-35
lines changed

4 files changed

+25
-35
lines changed

monitoring/exporter/stackdriver/mock_check_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ func mockAddToBundler(bndler *bundler.Bundler, item interface{}, _ int) error {
127127

128128
// newTestExp creates an exporter which saves error to errStorage. Caller should not set
129129
// opts.OnError.
130-
func newTestExp(t *testing.T, opts *Options) *Exporter {
130+
func newTestExp(t *testing.T, opts Options) *Exporter {
131131
opts.OnError = testOnError
132132
exp, err := NewExporter(ctx, opts)
133133
if err != nil {
@@ -140,7 +140,7 @@ func newTestExp(t *testing.T, opts *Options) *Exporter {
140140

141141
// newTestProjData creates a projectData object to test behavior of projectData.uploadRowData. Other
142142
// uses are not recommended. As newTestExp, all errors are saved to errStorage.
143-
func newTestProjData(t *testing.T, opts *Options) *projectData {
143+
func newTestProjData(t *testing.T, opts Options) *projectData {
144144
return newTestExp(t, opts).newProjectData(project1)
145145
}
146146

monitoring/exporter/stackdriver/stackdriver.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,7 @@ type Exporter struct {
4747
// TODO(lawrencechung): If possible, find a way to not storing ctx in the struct.
4848
ctx context.Context
4949
client *monitoring.MetricClient
50-
opts *Options
51-
52-
// copy of some option values which may be modified by exporter.
53-
getProjectID func(*RowData) (string, error)
54-
onError func(error, ...*RowData)
55-
makeResource func(*RowData) (*mrpb.MonitoredResource, error)
50+
opts Options
5651

5752
// mu protects access to projDataMap
5853
mu sync.Mutex
@@ -146,7 +141,7 @@ var (
146141
// NewExporter creates an Exporter object. Once a call to NewExporter is made, any fields in opts
147142
// must not be modified at all. ctx will also be used throughout entire exporter operation when
148143
// making RPC call.
149-
func NewExporter(ctx context.Context, opts *Options) (*Exporter, error) {
144+
func NewExporter(ctx context.Context, opts Options) (*Exporter, error) {
150145
client, err := newMetricClient(ctx, opts.ClientOptions...)
151146
if err != nil {
152147
return nil, fmt.Errorf("failed to create a metric client: %v", err)
@@ -159,19 +154,14 @@ func NewExporter(ctx context.Context, opts *Options) (*Exporter, error) {
159154
projDataMap: make(map[string]*projectData),
160155
}
161156

162-
// We don't want to modify user-supplied options, so save default options directly in
163-
// exporter.
164-
e.getProjectID = defaultGetProjectID
165-
if opts.GetProjectID != nil {
166-
e.getProjectID = opts.GetProjectID
157+
if e.opts.GetProjectID == nil {
158+
e.opts.GetProjectID = defaultGetProjectID
167159
}
168-
e.onError = defaultOnError
169-
if opts.OnError != nil {
170-
e.onError = opts.OnError
160+
if e.opts.OnError == nil {
161+
e.opts.OnError = defaultOnError
171162
}
172-
e.makeResource = defaultMakeResource
173-
if opts.MakeResource != nil {
174-
e.makeResource = opts.MakeResource
163+
if e.opts.MakeResource == nil {
164+
e.opts.MakeResource = defaultMakeResource
175165
}
176166

177167
return e, nil
@@ -206,12 +196,12 @@ var RowDataNotApplicableError = errors.New("row data is not applicable to the ex
206196

207197
// exportRowData exports a single row data.
208198
func (e *Exporter) exportRowData(rd *RowData) {
209-
projID, err := e.getProjectID(rd)
199+
projID, err := e.opts.GetProjectID(rd)
210200
if err != nil {
211201
// We ignore non-applicable RowData.
212202
if err != RowDataNotApplicableError {
213203
newErr := fmt.Errorf("failed to get project ID on row data with view %s: %v", rd.View.Name, err)
214-
e.onError(newErr, rd)
204+
e.opts.OnError(newErr, rd)
215205
}
216206
return
217207
}
@@ -222,7 +212,7 @@ func (e *Exporter) exportRowData(rd *RowData) {
222212
go pd.uploadRowData(rd)
223213
default:
224214
newErr := fmt.Errorf("failed to add row data with view %s to bundle for project %s: %v", rd.View.Name, projID, err)
225-
e.onError(newErr, rd)
215+
e.opts.OnError(newErr, rd)
226216
}
227217
}
228218

monitoring/exporter/stackdriver/stackdriver_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func testProjectClassifyNoError(t *testing.T) {
7676
}
7777
}
7878

79-
exp := newTestExp(t, &Options{GetProjectID: getProjectID})
79+
exp := newTestExp(t, Options{GetProjectID: getProjectID})
8080
exp.ExportView(viewData1)
8181
exp.ExportView(viewData2)
8282

@@ -122,7 +122,7 @@ func testProjectClassifyError(t *testing.T) {
122122
}
123123
}
124124

125-
exp := newTestExp(t, &Options{GetProjectID: getProjectID})
125+
exp := newTestExp(t, Options{GetProjectID: getProjectID})
126126
exp.ExportView(viewData1)
127127
exp.ExportView(viewData2)
128128

@@ -171,7 +171,7 @@ func testDefaultProjectClassify(t *testing.T) {
171171
Rows: []*view.Row{view3row3},
172172
}
173173

174-
exp := newTestExp(t, &Options{})
174+
exp := newTestExp(t, Options{})
175175
exp.ExportView(viewData1)
176176
exp.ExportView(viewData2)
177177
exp.ExportView(viewData3)
@@ -194,7 +194,7 @@ func testDefaultProjectClassify(t *testing.T) {
194194
// testUploadNoError tests that all RowData objects passed to uploadRowData() are grouped by
195195
// slice of length MaxTimeSeriesPerUpload, and passed to createTimeSeries().
196196
func testUploadNoError(t *testing.T) {
197-
pd := newTestProjData(t, &Options{})
197+
pd := newTestProjData(t, Options{})
198198
rd := []*RowData{
199199
{view1, startTime1, endTime1, view1row1},
200200
{view1, startTime1, endTime1, view1row2},
@@ -220,7 +220,7 @@ func testUploadTimeSeriesMakeError(t *testing.T) {
220220
}
221221
return defaultMakeResource(rd)
222222
}
223-
pd := newTestProjData(t, &Options{MakeResource: makeResource})
223+
pd := newTestProjData(t, Options{MakeResource: makeResource})
224224
rd := []*RowData{
225225
{view1, startTime1, endTime1, view1row1},
226226
{view1, startTime1, endTime1, view1row2},
@@ -255,7 +255,7 @@ func testUploadTimeSeriesMakeError(t *testing.T) {
255255
// testUploadTimeSeriesMakeError tests that exporter can handle error on metric client's time
256256
// series create RPC call.
257257
func testUploadWithMetricClientError(t *testing.T) {
258-
pd := newTestProjData(t, &Options{})
258+
pd := newTestProjData(t, Options{})
259259
timeSeriesResults = append(timeSeriesResults, invalidDataError)
260260
rd := []*RowData{
261261
{view1, startTime1, endTime1, view1row1},
@@ -298,7 +298,7 @@ func testMakeResource(t *testing.T) {
298298
return nil, unrecognizedDataError
299299
}
300300
}
301-
pd := newTestProjData(t, &Options{MakeResource: makeResource})
301+
pd := newTestProjData(t, Options{MakeResource: makeResource})
302302
rd := []*RowData{
303303
{view1, startTime1, endTime1, view1row1},
304304
{view1, startTime1, endTime1, view1row2},
@@ -318,7 +318,7 @@ func testMakeResource(t *testing.T) {
318318
// testMakeLabel tests that exporter can correctly handle label manipulation process, including
319319
// merging default label with tags, and removing unexported labels.
320320
func testMakeLabel(t *testing.T) {
321-
opts := &Options{
321+
opts := Options{
322322
DefaultLabels: map[string]string{
323323
label1name: value7,
324324
label4name: value8,

monitoring/exporter/stackdriver/upload.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (pd *projectData) uploadRowData(bundle interface{}) {
4444
for _, rd := range rds {
4545
ts, err := exp.makeTS(rd)
4646
if err != nil {
47-
exp.onError(err, rd)
47+
exp.opts.OnError(err, rd)
4848
continue
4949
}
5050
// Time series created. We update both uploadTs and uploadRds.
@@ -68,7 +68,7 @@ func (e *Exporter) makeTS(rd *RowData) (*mpb.TimeSeries, error) {
6868
if pt.Value == nil {
6969
return nil, fmt.Errorf("inconsistent data found in view %s", rd.View.Name)
7070
}
71-
resource, err := e.makeResource(rd)
71+
resource, err := e.opts.MakeResource(rd)
7272
if err != nil {
7373
return nil, fmt.Errorf("failed to construct resource of view %s: %v", rd.View.Name, err)
7474
}
@@ -102,7 +102,7 @@ func (e *Exporter) makeLabels(tags []tag.Tag) map[string]string {
102102
}
103103

104104
// uploadTimeSeries uploads timeSeries. ts and rds must contain matching data, and ts must not be
105-
// empty. When uploading fails, this function calls exporter's onError() directly, not propagating
105+
// empty. When uploading fails, this function calls exporter's OnError() directly, not propagating
106106
// errors to the caller.
107107
func (pd *projectData) uploadTimeSeries(ts []*mpb.TimeSeries, rds []*RowData) {
108108
exp := pd.parent
@@ -113,6 +113,6 @@ func (pd *projectData) uploadTimeSeries(ts []*mpb.TimeSeries, rds []*RowData) {
113113
if err := createTimeSeries(exp.client, exp.ctx, req); err != nil {
114114
newErr := fmt.Errorf("RPC call to create time series failed for project %s: %v", pd.projectID, err)
115115
// We pass all row data not successfully uploaded.
116-
exp.onError(newErr, rds...)
116+
exp.opts.OnError(newErr, rds...)
117117
}
118118
}

0 commit comments

Comments
 (0)