@@ -51,6 +51,10 @@ import (
5151)
5252
5353const (
54+ // The number of timeserieses to send to GCM in a single request. This
55+ // is a hard limit in the GCM API, so we never want to exceed 200.
56+ sendBatchSize = 200
57+
5458 cloudMonitoringMetricDescriptorNameFormat = "workload.googleapis.com/%s"
5559)
5660
@@ -221,31 +225,34 @@ func (me *metricExporter) createMetricDescriptorIfNeeded(ctx context.Context, md
221225// exportTimeSeries create TimeSeries from the records in cps.
222226// res should be the common resource among all TimeSeries, such as instance id, application name and so on.
223227func (me * metricExporter ) exportTimeSeries (ctx context.Context , rm metricdata.ResourceMetrics ) error {
224- tss := []* monitoringpb.TimeSeries {}
225- mr := me .resourceToMonitoredResourcepb (rm .Resource )
226- var aggError error
228+ tss , aggErr := me .recordsToTspbs (rm )
229+ if len (tss ) == 0 {
230+ return aggErr
231+ }
227232
228- extraLabels := me .extraLabelsFromResource (rm .Resource )
229- for _ , scope := range rm .ScopeMetrics {
230- for _ , metrics := range scope .Metrics {
231- ts , err := me .recordToTspb (metrics , mr , scope .Scope , extraLabels )
232- aggError = multierr .Append (aggError , err )
233- tss = append (tss , ts ... )
233+ name := fmt .Sprintf ("projects/%s" , me .o .projectID )
234+
235+ var createErrors []error
236+ for i := 0 ; i < len (tss ); i += sendBatchSize {
237+ j := i + sendBatchSize
238+ if j >= len (tss ) {
239+ j = len (tss )
234240 }
235- }
236241
237- if len (tss ) == 0 {
238- return aggError
239- }
242+ // TODO: When this exporter is rewritten, support writing to multiple
243+ // projects based on the "gcp.project.id" resource.
244+ req := & monitoringpb.CreateTimeSeriesRequest {
245+ Name : name ,
246+ TimeSeries : tss [i :j ],
247+ }
240248
241- // TODO: When this exporter is rewritten, support writing to multiple
242- // projects based on the "gcp.project.id" resource.
243- req := & monitoringpb.CreateTimeSeriesRequest {
244- Name : fmt .Sprintf ("projects/%s" , me .o .projectID ),
245- TimeSeries : tss ,
249+ err := me .client .CreateTimeSeries (ctx , req )
250+ if err != nil {
251+ createErrors = append (createErrors , err )
252+ }
246253 }
247254
248- return multierr .Append (aggError , me . client . CreateTimeSeries ( ctx , req ))
255+ return multierr .Append (aggErr , multierr . Combine ( createErrors ... ))
249256}
250257
251258func (me * metricExporter ) extraLabelsFromResource (res * resource.Resource ) * attribute.Set {
@@ -495,6 +502,28 @@ func (me *metricExporter) recordToTspb(m metricdata.Metrics, mr *monitoredrespb.
495502 return tss , aggErr
496503}
497504
505+ func (me * metricExporter ) recordsToTspbs (rm metricdata.ResourceMetrics ) ([]* monitoringpb.TimeSeries , error ) {
506+ mr := me .resourceToMonitoredResourcepb (rm .Resource )
507+ extraLabels := me .extraLabelsFromResource (rm .Resource )
508+
509+ var (
510+ tss []* monitoringpb.TimeSeries
511+ errors []error
512+ )
513+ for _ , scope := range rm .ScopeMetrics {
514+ for _ , metrics := range scope .Metrics {
515+ ts , err := me .recordToTspb (metrics , mr , scope .Scope , extraLabels )
516+ if err != nil {
517+ errors = append (errors , err )
518+ }
519+
520+ tss = append (tss , ts ... )
521+ }
522+ }
523+
524+ return tss , multierr .Combine (errors ... )
525+ }
526+
498527func sanitizeUTF8 (s string ) string {
499528 return strings .ToValidUTF8 (s , "�" )
500529}
0 commit comments