Skip to content
This repository was archived by the owner on Oct 3, 2023. It is now read-only.

Commit 93f882d

Browse files
authored
Remove the batching version for proto exporter (#187)
* Remove the batching version for proto exporter * Do not hold a reference to the metric descriptor when not needed
1 parent b0cbe77 commit 93f882d

5 files changed

Lines changed: 164 additions & 277 deletions

File tree

equivalence_test.go

Lines changed: 128 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -160,71 +160,69 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
160160

161161
// Generate the view.Data.
162162
var vdl []*view.Data
163-
for i := 0; i < 10; i++ {
164-
vdl = append(vdl,
165-
&view.Data{
166-
Start: startTime,
167-
End: startTime.Add(time.Duration(1+i) * time.Second),
168-
View: &view.View{
169-
Name: "ocagent.io/calls",
170-
Description: "The number of the various calls",
171-
Aggregation: view.Count(),
172-
Measure: mLatencyMs,
173-
},
174-
Rows: []*view.Row{
175-
{
176-
Data: &view.CountData{Value: int64(4 * (i + 2))},
177-
},
178-
},
163+
vdl = append(vdl,
164+
&view.Data{
165+
Start: startTime,
166+
End: startTime.Add(time.Duration(1) * time.Second),
167+
View: &view.View{
168+
Name: "ocagent.io/calls",
169+
Description: "The number of the various calls",
170+
Aggregation: view.Count(),
171+
Measure: mLatencyMs,
179172
},
180-
&view.Data{
181-
Start: startTime,
182-
End: startTime.Add(time.Duration(2+i) * time.Second),
183-
View: &view.View{
184-
Name: "ocagent.io/latency",
185-
Description: "The latency of the various methods",
186-
Aggregation: view.Distribution(100, 500, 1000, 2000, 4000, 8000, 16000),
187-
Measure: mLatencyMs,
173+
Rows: []*view.Row{
174+
{
175+
Data: &view.CountData{Value: int64(8)},
188176
},
189-
Rows: []*view.Row{
190-
{
191-
Data: &view.DistributionData{
192-
Count: 1,
193-
Min: 100,
194-
Max: 500,
195-
Mean: 125.9,
196-
CountPerBucket: []int64{0, 1, 0, 0, 0, 0, 0},
197-
},
177+
},
178+
},
179+
&view.Data{
180+
Start: startTime,
181+
End: startTime.Add(time.Duration(2) * time.Second),
182+
View: &view.View{
183+
Name: "ocagent.io/latency",
184+
Description: "The latency of the various methods",
185+
Aggregation: view.Distribution(100, 500, 1000, 2000, 4000, 8000, 16000),
186+
Measure: mLatencyMs,
187+
},
188+
Rows: []*view.Row{
189+
{
190+
Data: &view.DistributionData{
191+
Count: 1,
192+
Min: 100,
193+
Max: 500,
194+
Mean: 125.9,
195+
CountPerBucket: []int64{0, 1, 0, 0, 0, 0, 0},
198196
},
199197
},
200198
},
201-
&view.Data{
202-
Start: startTime,
203-
End: startTime.Add(time.Duration(3+i) * time.Second),
204-
View: &view.View{
205-
Name: "ocagent.io/connections",
206-
Description: "The count of various connections instantaneously",
207-
Aggregation: view.LastValue(),
208-
Measure: mConnections,
209-
},
210-
Rows: []*view.Row{
211-
{Data: &view.LastValueData{Value: 99}},
212-
},
199+
},
200+
&view.Data{
201+
Start: startTime,
202+
End: startTime.Add(time.Duration(3) * time.Second),
203+
View: &view.View{
204+
Name: "ocagent.io/connections",
205+
Description: "The count of various connections instantaneously",
206+
Aggregation: view.LastValue(),
207+
Measure: mConnections,
213208
},
214-
&view.Data{
215-
Start: startTime,
216-
End: startTime.Add(time.Duration(1+i) * time.Second),
217-
View: &view.View{
218-
Name: "ocagent.io/uptime",
219-
Description: "The total uptime at any instance",
220-
Aggregation: view.Sum(),
221-
Measure: mTimeMs,
222-
},
223-
Rows: []*view.Row{
224-
{Data: &view.SumData{Value: 199903.97}},
225-
},
226-
})
227-
}
209+
Rows: []*view.Row{
210+
{Data: &view.LastValueData{Value: 99}},
211+
},
212+
},
213+
&view.Data{
214+
Start: startTime,
215+
End: startTime.Add(time.Duration(1) * time.Second),
216+
View: &view.View{
217+
Name: "ocagent.io/uptime",
218+
Description: "The total uptime at any instance",
219+
Aggregation: view.Sum(),
220+
Measure: mTimeMs,
221+
},
222+
Rows: []*view.Row{
223+
{Data: &view.SumData{Value: 199903.97}},
224+
},
225+
})
228226

229227
for _, vd := range vdl {
230228
// Export the view.Data to the Stackdriver backend.
@@ -249,105 +247,104 @@ func TestEquivalenceStatsVsMetricsUploads(t *testing.T) {
249247

250248
// Generate the proto Metrics.
251249
var metricPbs []*metricspb.Metric
252-
for i := 0; i < 10; i++ {
253-
metricPbs = append(metricPbs,
254-
&metricspb.Metric{
255-
MetricDescriptor: &metricspb.MetricDescriptor{
256-
Name: "ocagent.io/calls",
257-
Description: "The number of the various calls",
258-
Unit: "1",
259-
Type: metricspb.MetricDescriptor_CUMULATIVE_INT64,
260-
},
261-
Timeseries: []*metricspb.TimeSeries{
262-
{
263-
StartTimestamp: startTimePb,
264-
Points: []*metricspb.Point{
265-
{
266-
Timestamp: &timestamp.Timestamp{Seconds: int64(1001 + i)},
267-
Value: &metricspb.Point_Int64Value{Int64Value: int64(4 * (i + 2))},
268-
},
250+
metricPbs = append(metricPbs,
251+
&metricspb.Metric{
252+
MetricDescriptor: &metricspb.MetricDescriptor{
253+
Name: "ocagent.io/calls",
254+
Description: "The number of the various calls",
255+
Unit: "1",
256+
Type: metricspb.MetricDescriptor_CUMULATIVE_INT64,
257+
},
258+
Timeseries: []*metricspb.TimeSeries{
259+
{
260+
StartTimestamp: startTimePb,
261+
Points: []*metricspb.Point{
262+
{
263+
Timestamp: &timestamp.Timestamp{Seconds: int64(1001)},
264+
Value: &metricspb.Point_Int64Value{Int64Value: int64(8)},
269265
},
270266
},
271267
},
272268
},
273-
&metricspb.Metric{
274-
MetricDescriptor: &metricspb.MetricDescriptor{
275-
Name: "ocagent.io/latency",
276-
Description: "The latency of the various methods",
277-
Unit: "ms",
278-
Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION,
279-
},
280-
Timeseries: []*metricspb.TimeSeries{
281-
{
282-
StartTimestamp: startTimePb,
283-
Points: []*metricspb.Point{
284-
{
285-
Timestamp: &timestamp.Timestamp{Seconds: int64(1002 + i)},
286-
Value: &metricspb.Point_DistributionValue{
287-
DistributionValue: &metricspb.DistributionValue{
288-
Count: 1,
289-
Sum: 125.9,
290-
BucketOptions: &metricspb.DistributionValue_BucketOptions{
291-
Type: &metricspb.DistributionValue_BucketOptions_Explicit_{
292-
Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{Bounds: []float64{100, 500, 1000, 2000, 4000, 8000, 16000}},
293-
},
269+
},
270+
&metricspb.Metric{
271+
MetricDescriptor: &metricspb.MetricDescriptor{
272+
Name: "ocagent.io/latency",
273+
Description: "The latency of the various methods",
274+
Unit: "ms",
275+
Type: metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION,
276+
},
277+
Timeseries: []*metricspb.TimeSeries{
278+
{
279+
StartTimestamp: startTimePb,
280+
Points: []*metricspb.Point{
281+
{
282+
Timestamp: &timestamp.Timestamp{Seconds: int64(1002)},
283+
Value: &metricspb.Point_DistributionValue{
284+
DistributionValue: &metricspb.DistributionValue{
285+
Count: 1,
286+
Sum: 125.9,
287+
BucketOptions: &metricspb.DistributionValue_BucketOptions{
288+
Type: &metricspb.DistributionValue_BucketOptions_Explicit_{
289+
Explicit: &metricspb.DistributionValue_BucketOptions_Explicit{Bounds: []float64{100, 500, 1000, 2000, 4000, 8000, 16000}},
294290
},
295-
Buckets: []*metricspb.DistributionValue_Bucket{{Count: 0}, {Count: 1}, {Count: 0}, {Count: 0}, {Count: 0}, {Count: 0}, {Count: 0}},
296291
},
292+
Buckets: []*metricspb.DistributionValue_Bucket{{Count: 0}, {Count: 1}, {Count: 0}, {Count: 0}, {Count: 0}, {Count: 0}, {Count: 0}},
297293
},
298294
},
299295
},
300296
},
301297
},
302298
},
303-
&metricspb.Metric{
304-
MetricDescriptor: &metricspb.MetricDescriptor{
305-
Name: "ocagent.io/connections",
306-
Description: "The count of various connections instantaneously",
307-
Unit: "1",
308-
Type: metricspb.MetricDescriptor_GAUGE_INT64,
309-
},
310-
Timeseries: []*metricspb.TimeSeries{
311-
{
312-
StartTimestamp: startTimePb,
313-
Points: []*metricspb.Point{
314-
{
315-
Timestamp: &timestamp.Timestamp{Seconds: int64(1003 + i)},
316-
Value: &metricspb.Point_Int64Value{Int64Value: 99},
317-
},
299+
},
300+
&metricspb.Metric{
301+
MetricDescriptor: &metricspb.MetricDescriptor{
302+
Name: "ocagent.io/connections",
303+
Description: "The count of various connections instantaneously",
304+
Unit: "1",
305+
Type: metricspb.MetricDescriptor_GAUGE_INT64,
306+
},
307+
Timeseries: []*metricspb.TimeSeries{
308+
{
309+
StartTimestamp: startTimePb,
310+
Points: []*metricspb.Point{
311+
{
312+
Timestamp: &timestamp.Timestamp{Seconds: int64(1003)},
313+
Value: &metricspb.Point_Int64Value{Int64Value: 99},
318314
},
319315
},
320316
},
321317
},
322-
&metricspb.Metric{
323-
MetricDescriptor: &metricspb.MetricDescriptor{
324-
Name: "ocagent.io/uptime",
325-
Description: "The total uptime at any instance",
326-
Unit: "ms",
327-
Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE,
328-
},
329-
Timeseries: []*metricspb.TimeSeries{
330-
{
331-
StartTimestamp: startTimePb,
332-
Points: []*metricspb.Point{
333-
{
334-
Timestamp: &timestamp.Timestamp{Seconds: int64(1001 + i)},
335-
Value: &metricspb.Point_DoubleValue{DoubleValue: 199903.97},
336-
},
318+
},
319+
&metricspb.Metric{
320+
MetricDescriptor: &metricspb.MetricDescriptor{
321+
Name: "ocagent.io/uptime",
322+
Description: "The total uptime at any instance",
323+
Unit: "ms",
324+
Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE,
325+
},
326+
Timeseries: []*metricspb.TimeSeries{
327+
{
328+
StartTimestamp: startTimePb,
329+
Points: []*metricspb.Point{
330+
{
331+
Timestamp: &timestamp.Timestamp{Seconds: int64(1001)},
332+
Value: &metricspb.Point_DoubleValue{DoubleValue: 199903.97},
337333
},
338334
},
339335
},
340-
})
341-
}
336+
},
337+
})
342338

343339
// Export the proto Metrics to the Stackdriver backend.
344-
se.ExportMetricsProto(context.Background(), nil, nil, metricPbs)
340+
se.ExportMetricsProtoSync(context.Background(), nil, nil, metricPbs)
345341
se.Flush()
346342

347343
var stackdriverTimeSeriesFromMetrics []*monitoringpb.CreateTimeSeriesRequest
348344
server.forEachStackdriverTimeSeries(func(sdt *monitoringpb.CreateTimeSeriesRequest) {
349345
stackdriverTimeSeriesFromMetrics = append(stackdriverTimeSeriesFromMetrics, sdt)
350346
})
347+
351348
var stackdriverMetricDescriptorsFromMetrics []*monitoringpb.CreateMetricDescriptorRequest
352349
server.forEachStackdriverMetricDescriptor(func(sdmd *monitoringpb.CreateMetricDescriptorRequest) {
353350
stackdriverMetricDescriptorsFromMetrics = append(stackdriverMetricDescriptorsFromMetrics, sdmd)

metrics.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"errors"
2525
"fmt"
26+
2627
"github.com/golang/protobuf/proto"
2728
"github.com/golang/protobuf/ptypes/any"
2829
"github.com/golang/protobuf/ptypes/timestamp"
@@ -210,24 +211,18 @@ func (se *statsExporter) createMetricDescriptorFromMetric(ctx context.Context, m
210211
return err
211212
}
212213

213-
var md *googlemetricpb.MetricDescriptor
214214
if builtinMetric(inMD.Type) {
215-
gmrdesc := &monitoringpb.GetMetricDescriptorRequest{
216-
Name: inMD.Name,
217-
}
218-
md, err = getMetricDescriptor(ctx, se.c, gmrdesc)
215+
se.metricDescriptors[name] = true
219216
} else {
220-
221217
cmrdesc := &monitoringpb.CreateMetricDescriptorRequest{
222218
Name: fmt.Sprintf("projects/%s", se.o.ProjectID),
223219
MetricDescriptor: inMD,
224220
}
225-
md, err = createMetricDescriptor(ctx, se.c, cmrdesc)
226-
}
227-
228-
if err == nil {
229-
// Now record the metric as having been created.
230-
se.metricDescriptors[name] = md
221+
_, err = createMetricDescriptor(ctx, se.c, cmrdesc)
222+
if err == nil {
223+
// Now record the metric as having been created.
224+
se.metricDescriptors[name] = true
225+
}
231226
}
232227

233228
return err

0 commit comments

Comments
 (0)