Skip to content

Commit bafd691

Browse files
authored
perf(bigtable): parallelize the exportTimeSeries function (#13004)
Fix Bigtable test timeout in TestBatchingExport The `TestBatchingExport/1199_metrics` test was timing out after 45 minutes. This was caused by the `exportTimeSeries` function sending metric batches sequentially. With 1199 metrics, this resulted in 6 separate, synchronous gRPC calls, which caused a hang. This change parallelizes the `exportTimeSeries` function to send metric batches concurrently using goroutines. This resolves the timeout and improves performance. Fixes: #12920
1 parent 2b75879 commit bafd691

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

bigtable/metrics_monitoring_exporter.go

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,40 @@ func (me *monitoringExporter) exportTimeSeries(ctx context.Context, rm *otelmetr
142142

143143
name := fmt.Sprintf("projects/%s", me.projectID)
144144

145-
errs := []error{err}
145+
var wg sync.WaitGroup
146+
errs := make(chan error, len(tss)/sendBatchSize+1)
147+
if err != nil {
148+
errs <- err
149+
}
150+
146151
for i := 0; i < len(tss); i += sendBatchSize {
147152
j := i + sendBatchSize
148-
if j >= len(tss) {
153+
if j > len(tss) {
149154
j = len(tss)
150155
}
151156

152-
req := &monitoringpb.CreateTimeSeriesRequest{
153-
Name: name,
154-
TimeSeries: tss[i:j],
155-
}
156-
errs = append(errs, me.client.CreateServiceTimeSeries(ctx, req))
157+
wg.Add(1)
158+
go func(i, j int) {
159+
defer wg.Done()
160+
req := &monitoringpb.CreateTimeSeriesRequest{
161+
Name: name,
162+
TimeSeries: tss[i:j],
163+
}
164+
if err := me.client.CreateServiceTimeSeries(ctx, req); err != nil {
165+
errs <- err
166+
}
167+
}(i, j)
168+
}
169+
170+
wg.Wait()
171+
close(errs)
172+
173+
var allErrors []error
174+
for err := range errs {
175+
allErrors = append(allErrors, err)
157176
}
158177

159-
return errors.Join(errs...)
178+
return errors.Join(allErrors...)
160179
}
161180

162181
// recordToMetricAndMonitoredResourcePbs converts data from records to Metric and Monitored resource proto type for Cloud Monitoring.

0 commit comments

Comments
 (0)