@@ -67,14 +67,14 @@ func (se *statsExporter) PushMetricsProto(ctx context.Context, node *commonpb.No
6767 if metric .GetMetricDescriptor ().GetType () == metricspb .MetricDescriptor_SUMMARY {
6868 summaryMtcs := se .convertSummaryMetrics (metric )
6969 for _ , summaryMtc := range summaryMtcs {
70- if err := se .createMetricDescriptorWithTimeout (ctx , summaryMtc ); err != nil {
70+ if err := se .createMetricDescriptorFromMetricProto (ctx , summaryMtc ); err != nil {
7171 mb .recordDroppedTimeseries (len (summaryMtc .GetTimeseries ()), err )
7272 continue
7373 }
7474 se .protoMetricToTimeSeries (ctx , mappedRsc , summaryMtc , mb )
7575 }
7676 } else {
77- if err := se .createMetricDescriptorWithTimeout (ctx , metric ); err != nil {
77+ if err := se .createMetricDescriptorFromMetricProto (ctx , metric ); err != nil {
7878 mb .recordDroppedTimeseries (len (metric .GetTimeseries ()), err )
7979 continue
8080 }
@@ -236,8 +236,7 @@ func (se *statsExporter) protoMetricToTimeSeries(ctx context.Context, mappedRsc
236236 mb .recordDroppedTimeseries (len (metric .GetTimeseries ()), errNilMetricOrMetricDescriptor )
237237 }
238238
239- metricName := metric .GetMetricDescriptor ().GetName ()
240- metricType := se .metricTypeFromProto (metricName )
239+ metricType := se .metricTypeFromProto (metric .GetMetricDescriptor ().GetName ())
241240 metricLabelKeys := metric .GetMetricDescriptor ().GetLabelKeys ()
242241 metricKind , valueType := protoMetricDescriptorTypeToMetricKind (metric )
243242 labelKeys := make ([]string , 0 , len (metricLabelKeys ))
@@ -246,6 +245,11 @@ func (se *statsExporter) protoMetricToTimeSeries(ctx context.Context, mappedRsc
246245 }
247246
248247 for _ , protoTimeSeries := range metric .Timeseries {
248+ if len (protoTimeSeries .Points ) == 0 {
249+ // No points to send just move forward.
250+ continue
251+ }
252+
249253 sdPoints , err := se .protoTimeSeriesToMonitoringPoints (protoTimeSeries , metricKind )
250254 if err != nil {
251255 mb .recordDroppedTimeseries (1 , err )
@@ -273,17 +277,21 @@ func (se *statsExporter) protoMetricToTimeSeries(ctx context.Context, mappedRsc
273277}
274278
275279func labelsPerTimeSeries (defaults map [string ]labelValue , labelKeys []string , labelValues []* metricspb.LabelValue ) (map [string ]string , error ) {
280+ if len (labelKeys ) != len (labelValues ) {
281+ return nil , fmt .Errorf ("length mismatch: len(labelKeys)=%d len(labelValues)=%d" , len (labelKeys ), len (labelValues ))
282+ }
283+
284+ if len (defaults )+ len (labelKeys ) == 0 {
285+ // No labels for this metric
286+ return nil , nil
287+ }
288+
276289 labels := make (map [string ]string )
277290 // Fill in the defaults firstly, irrespective of if the labelKeys and labelValues are mismatched.
278291 for key , label := range defaults {
279292 labels [key ] = label .val
280293 }
281294
282- // Perform this sanity check now.
283- if len (labelKeys ) != len (labelValues ) {
284- return labels , fmt .Errorf ("length mismatch: len(labelKeys)=%d len(labelValues)=%d" , len (labelKeys ), len (labelValues ))
285- }
286-
287295 for i , labelKey := range labelKeys {
288296 labelValue := labelValues [i ]
289297 if ! labelValue .GetHasValue () {
@@ -295,21 +303,15 @@ func labelsPerTimeSeries(defaults map[string]labelValue, labelKeys []string, lab
295303 return labels , nil
296304}
297305
298- func (se * statsExporter ) createMetricDescriptorWithTimeout (ctx context.Context , metric * metricspb.Metric ) error {
299- ctx , cancel := newContextWithTimeout (ctx , se .o .Timeout )
300- defer cancel ()
301-
302- return se .protoCreateMetricDescriptor (ctx , metric )
303- }
304-
305- // createMetricDescriptor creates a metric descriptor from the OpenCensus proto metric
306- // and then creates it remotely using Stackdriver's API.
307- func (se * statsExporter ) protoCreateMetricDescriptor (ctx context.Context , metric * metricspb.Metric ) error {
306+ func (se * statsExporter ) createMetricDescriptorFromMetricProto (ctx context.Context , metric * metricspb.Metric ) error {
308307 // Skip create metric descriptor if configured
309308 if se .o .SkipCMD {
310309 return nil
311310 }
312311
312+ ctx , cancel := newContextWithTimeout (ctx , se .o .Timeout )
313+ defer cancel ()
314+
313315 se .protoMu .Lock ()
314316 defer se .protoMu .Unlock ()
315317
@@ -338,15 +340,15 @@ func (se *statsExporter) protoCreateMetricDescriptor(ctx context.Context, metric
338340 return nil
339341}
340342
341- func (se * statsExporter ) protoTimeSeriesToMonitoringPoints (ts * metricspb.TimeSeries , metricKind googlemetricpb.MetricDescriptor_MetricKind ) (sptl []* monitoringpb.Point , err error ) {
343+ func (se * statsExporter ) protoTimeSeriesToMonitoringPoints (ts * metricspb.TimeSeries , metricKind googlemetricpb.MetricDescriptor_MetricKind ) ([]* monitoringpb.Point , error ) {
344+ sptl := make ([]* monitoringpb.Point , 0 , len (ts .Points ))
342345 for _ , pt := range ts .Points {
343346 // If we have a last value aggregation point i.e. MetricDescriptor_GAUGE
344347 // StartTime should be nil.
345348 startTime := ts .StartTimestamp
346349 if metricKind == googlemetricpb .MetricDescriptor_GAUGE {
347350 startTime = nil
348351 }
349-
350352 spt , err := fromProtoPoint (startTime , pt )
351353 if err != nil {
352354 return nil , err
0 commit comments