Example #1
0
// Adds the specified metrics or updates them if they already exist.
func (sink *gcmSink) register(metrics []core.Metric) error {
	sink.Lock()
	defer sink.Unlock()
	if sink.registered {
		return nil
	}

	for _, metric := range metrics {
		metricName := fullMetricName(metric.MetricDescriptor.Name)
		if _, err := sink.gcmService.MetricDescriptors.Delete(sink.project, metricName).Do(); err != nil {
			glog.Infof("[GCM] Deleting metric %v failed: %v", metricName, err)
		}
		labels := make([]*gcm.MetricDescriptorLabelDescriptor, 0)

		// Node autoscaling metrics have special labels.
		if core.IsNodeAutoscalingMetric(metric.MetricDescriptor.Name) {
			// All and autoscaling. Do not populate for other filters.
			if sink.metricFilter != metricsAll &&
				sink.metricFilter != metricsOnlyAutoscaling {
				continue
			}

			for _, l := range core.GcmNodeAutoscalingLabels() {
				labels = append(labels, &gcm.MetricDescriptorLabelDescriptor{
					Key:         fullLabelName(l.Key),
					Description: l.Description,
				})
			}
		} else {
			// Only all.
			if sink.metricFilter != metricsAll {
				continue
			}

			for _, l := range core.GcmLabels() {
				labels = append(labels, &gcm.MetricDescriptorLabelDescriptor{
					Key:         fullLabelName(l.Key),
					Description: l.Description,
				})
			}
		}

		t := &gcm.MetricDescriptorTypeDescriptor{
			MetricType: metric.MetricDescriptor.Type.String(),
			ValueType:  metric.MetricDescriptor.ValueType.String(),
		}
		desc := &gcm.MetricDescriptor{
			Name:           metricName,
			Project:        sink.project,
			Description:    metric.MetricDescriptor.Description,
			Labels:         labels,
			TypeDescriptor: t,
		}
		if _, err := sink.gcmService.MetricDescriptors.Create(sink.project, desc).Do(); err != nil {
			return err
		}
	}
	sink.registered = true
	return nil
}
Example #2
0
func (sink *gcmSink) getTimeseriesPoint(timestamp time.Time, labels map[string]string, metric string, val core.MetricValue, createTime time.Time) *gcm.TimeseriesPoint {
	point := &gcm.Point{
		Start: timestamp.Format(time.RFC3339),
		End:   timestamp.Format(time.RFC3339),
	}
	switch val.ValueType {
	case core.ValueInt64:
		point.Int64Value = &val.IntValue
	case core.ValueFloat:
		v := float64(val.FloatValue)
		point.DoubleValue = &v
	default:
		glog.Errorf("Type not supported %v in %v", val.ValueType, metric)
		return nil
	}
	// For cumulative metric use the provided start time.
	if val.MetricType == core.MetricCumulative {
		point.Start = createTime.Format(time.RFC3339)
	}

	finalLabels := make(map[string]string)
	if core.IsNodeAutoscalingMetric(metric) {
		// All and autoscaling. Do not populate for other filters.
		if sink.metricFilter != metricsAll &&
			sink.metricFilter != metricsOnlyAutoscaling {
			return nil
		}

		finalLabels[fullLabelName(core.LabelHostname.Key)] = labels[core.LabelHostname.Key]
		finalLabels[fullLabelName(core.LabelGCEResourceID.Key)] = labels[core.LabelHostID.Key]
		finalLabels[fullLabelName(core.LabelGCEResourceType.Key)] = "instance"
	} else {
		// Only all.
		if sink.metricFilter != metricsAll {
			return nil
		}
		supportedLables := core.GcmLabels()
		for key, value := range labels {
			if _, ok := supportedLables[key]; ok {
				finalLabels[fullLabelName(key)] = value
			}
		}
	}
	desc := &gcm.TimeseriesDescriptor{
		Project: sink.project,
		Labels:  finalLabels,
		Metric:  fullMetricName(metric),
	}

	return &gcm.TimeseriesPoint{Point: point, TimeseriesDesc: desc}
}