Source File
metrics_proto.go
Belonging Package
contrib.go.opencensus.io/exporter/stackdriver
package stackdriver
import (
commonpb
metricspb
resourcepb
timestamppb
distributionpb
labelpb
googlemetricpb
monitoredrespb
monitoringpb
)
var errNilMetricOrMetricDescriptor = errors.New("non-nil metric or metric descriptor")
var percentileLabelKey = &metricspb.LabelKey{
Key: "percentile",
Description: "the value at a given percentile of a distribution",
}
var globalResource = &resource.Resource{Type: "global"}
var domains = []string{"googleapis.com", "kubernetes.io", "istio.io", "knative.dev"}
func ( *statsExporter) ( context.Context, *commonpb.Node, *resourcepb.Resource, []*metricspb.Metric) (int, error) {
if len() == 0 {
return 0, errNilMetricOrMetricDescriptor
}
:= make(map[*resourcepb.Resource]*monitoredrespb.MonitoredResource)
:= newMetricsBatcher(, .o.ProjectID, .o.NumberOfWorkers, .c, .o.Timeout)
for , := range {
continue
}
:= .getResource(, , )
if .GetMetricDescriptor().GetType() == metricspb.MetricDescriptor_SUMMARY {
:= .convertSummaryMetrics()
for , := range {
if := .createMetricDescriptorFromMetricProto(, ); != nil {
.recordDroppedTimeseries(len(.GetTimeseries()), )
continue
}
.protoMetricToTimeSeries(, , , )
}
} else {
if := .createMetricDescriptorFromMetricProto(, ); != nil {
.recordDroppedTimeseries(len(.GetTimeseries()), )
continue
}
.protoMetricToTimeSeries(, , , )
}
}
return .droppedTimeSeries, .close()
}
func ( *statsExporter) ( *metricspb.Metric) []*metricspb.Metric {
var []*metricspb.Metric
for , := range .Timeseries {
var []*metricspb.TimeSeries
var []*metricspb.TimeSeries
var []*metricspb.TimeSeries
:= .GetLabelValues()
:= .StartTimestamp
for , := range .GetPoints() {
:= .GetTimestamp()
:= .GetSummaryValue()
if .Sum != nil {
:= &metricspb.TimeSeries{
LabelValues: ,
StartTimestamp: ,
Points: []*metricspb.Point{
{
Value: &metricspb.Point_DoubleValue{
DoubleValue: .Sum.Value,
},
Timestamp: ,
},
},
}
= append(, )
}
if .Count != nil {
:= &metricspb.TimeSeries{
LabelValues: ,
StartTimestamp: ,
Points: []*metricspb.Point{
{
Value: &metricspb.Point_Int64Value{
Int64Value: .Count.Value,
},
Timestamp: ,
},
},
}
= append(, )
}
:= .GetSnapshot()
for , := range .GetPercentileValues() {
:= [0:]
= append(, &metricspb.LabelValue{
HasValue: true,
Value: fmt.Sprintf("%f", .Percentile),
})
:= &metricspb.TimeSeries{
LabelValues: ,
StartTimestamp: nil,
Points: []*metricspb.Point{
{
Value: &metricspb.Point_DoubleValue{
DoubleValue: .Value,
},
Timestamp: ,
},
},
}
= append(, )
}
}
if len() > 0 {
:= &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: fmt.Sprintf("%s_summary_sum", .GetMetricDescriptor().GetName()),
Description: .GetMetricDescriptor().GetDescription(),
Type: metricspb.MetricDescriptor_CUMULATIVE_DOUBLE,
Unit: .GetMetricDescriptor().GetUnit(),
LabelKeys: .GetMetricDescriptor().GetLabelKeys(),
},
Timeseries: ,
Resource: .Resource,
}
= append(, )
}
if len() > 0 {
:= &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: fmt.Sprintf("%s_summary_count", .GetMetricDescriptor().GetName()),
Description: .GetMetricDescriptor().GetDescription(),
Type: metricspb.MetricDescriptor_CUMULATIVE_INT64,
Unit: "1",
LabelKeys: .GetMetricDescriptor().GetLabelKeys(),
},
Timeseries: ,
Resource: .Resource,
}
= append(, )
}
if len() > 0 {
:= .GetMetricDescriptor().GetLabelKeys()[0:]
= append(, percentileLabelKey)
:= &metricspb.Metric{
MetricDescriptor: &metricspb.MetricDescriptor{
Name: fmt.Sprintf("%s_summary_percentile", .GetMetricDescriptor().GetName()),
Description: .GetMetricDescriptor().GetDescription(),
Type: metricspb.MetricDescriptor_GAUGE_DOUBLE,
Unit: .GetMetricDescriptor().GetUnit(),
LabelKeys: ,
},
Timeseries: ,
Resource: .Resource,
}
= append(, )
}
}
return
}
func ( *statsExporter) ( *resourcepb.Resource, *metricspb.Metric, map[*resourcepb.Resource]*monitoredrespb.MonitoredResource) *monitoredrespb.MonitoredResource {
var =
if .Resource != nil {
= .Resource
}
, := []
if ! {
= .o.MapResource(resourcepbToResource())
[] =
}
return
}
func ( *resourcepb.Resource) *resource.Resource {
if == nil {
return globalResource
}
:= &resource.Resource{
Type: .Type,
Labels: make(map[string]string, len(.Labels)),
}
for , := range .Labels {
.Labels[] =
}
return
}
func ( *statsExporter) ( context.Context, *monitoredrespb.MonitoredResource, *metricspb.Metric, *metricsBatcher) {
if == nil || .MetricDescriptor == nil {
.recordDroppedTimeseries(len(.GetTimeseries()), errNilMetricOrMetricDescriptor)
}
:= .metricTypeFromProto(.GetMetricDescriptor().GetName())
:= .GetMetricDescriptor().GetLabelKeys()
, := protoMetricDescriptorTypeToMetricKind()
:= make([]string, 0, len())
for , := range {
= append(, sanitize(.GetKey()))
}
for , := range .Timeseries {
continue
}
, := .protoTimeSeriesToMonitoringPoints(, )
if != nil {
.recordDroppedTimeseries(1, )
continue
}
, := labelsPerTimeSeries(.defaultLabels, , .GetLabelValues())
if != nil {
.recordDroppedTimeseries(1, )
continue
}
.addTimeSeries(&monitoringpb.TimeSeries{
Metric: &googlemetricpb.Metric{
Type: ,
Labels: ,
},
MetricKind: ,
ValueType: ,
Resource: ,
Points: ,
})
}
}
func ( map[string]labelValue, []string, []*metricspb.LabelValue) (map[string]string, error) {
if len() != len() {
return nil, fmt.Errorf("length mismatch: len(labelKeys)=%d len(labelValues)=%d", len(), len())
}
for , := range {
[] = .val
}
for , := range {
:= []
if !.GetHasValue() {
continue
}
[] = .GetValue()
}
return , nil
}
if .o.SkipCMD {
return nil
}
, := newContextWithTimeout(, .o.Timeout)
defer ()
.protoMu.Lock()
defer .protoMu.Unlock()
:= .GetMetricDescriptor().GetName()
if , := .protoMetricDescriptors[]; {
return nil
}
if builtinMetric(.metricTypeFromProto()) {
.protoMetricDescriptors[] = true
return nil
}
, := .protoToMonitoringMetricDescriptor(, .defaultLabels)
if != nil {
return
}
if = .createMetricDescriptor(, ); != nil {
return
}
.protoMetricDescriptors[] = true
return nil
}
func ( *statsExporter) ( *metricspb.TimeSeries, googlemetricpb.MetricDescriptor_MetricKind) ([]*monitoringpb.Point, error) {
:= make([]*monitoringpb.Point, 0, len(.Points))
:= .StartTimestamp
if == googlemetricpb.MetricDescriptor_GAUGE {
= nil
}
, := fromProtoPoint(, )
if != nil {
return nil,
}
= append(, )
}
return , nil
}
func ( *statsExporter) ( *metricspb.Metric, map[string]labelValue) (*googlemetricpb.MetricDescriptor, error) {
if == nil || .MetricDescriptor == nil {
return nil, errNilMetricOrMetricDescriptor
}
:= .GetMetricDescriptor()
:= .GetName()
:= .GetUnit()
:= .GetDescription()
:= .metricTypeFromProto()
:= .displayName()
, := protoMetricDescriptorTypeToMetricKind()
:= &googlemetricpb.MetricDescriptor{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", .o.ProjectID, ),
DisplayName: ,
Description: ,
Unit: ,
Type: ,
MetricKind: ,
ValueType: ,
Labels: labelDescriptorsFromProto(, .GetMetricDescriptor().GetLabelKeys()),
}
return , nil
}
func ( map[string]labelValue, []*metricspb.LabelKey) []*labelpb.LabelDescriptor {
:= make([]*labelpb.LabelDescriptor, 0, len()+len())
for , := range {
= append(, &labelpb.LabelDescriptor{
Key: sanitize(),
Description: .desc,
ValueType: labelpb.LabelDescriptor_STRING,
})
}
for , := range {
= append(, &labelpb.LabelDescriptor{
Key: sanitize(.GetKey()),
Description: .GetDescription(),
ValueType: labelpb.LabelDescriptor_STRING, // We only use string tags
})
}
return
}
func ( *statsExporter) ( string) string {
:= .o.MetricPrefix
if .o.GetMetricPrefix != nil {
= .o.GetMetricPrefix()
}
if != "" {
= path.Join(, )
}
= path.Join(defaultDomain, )
}
return
}
func ( string) bool {
for , := range domains {
if strings.Contains(, ) {
return true
}
}
return false
}
func ( *timestamppb.Timestamp, *metricspb.Point) (*monitoringpb.Point, error) {
if == nil {
return nil, nil
}
, := protoToMetricPoint(.Value)
if != nil {
return nil,
}
return &monitoringpb.Point{
Value: ,
Interval: &monitoringpb.TimeInterval{
StartTime: ,
EndTime: .Timestamp,
},
}, nil
}
func ( interface{}) (*monitoringpb.TypedValue, error) {
if == nil {
return nil, nil
}
switch v := .(type) {
return nil, fmt.Errorf("protoToMetricPoint: unknown Data type: %T", )
case *metricspb.Point_Int64Value:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: .Int64Value,
},
}, nil
case *metricspb.Point_DoubleValue:
return &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: .DoubleValue,
},
}, nil
case *metricspb.Point_DistributionValue:
:= .DistributionValue
var *monitoringpb.TypedValue_DistributionValue
if != nil {
var float64
if .Count > 0 {
= float64(.Sum) / float64(.Count)
}
= &monitoringpb.TypedValue_DistributionValue{
DistributionValue: &distributionpb.Distribution{
Count: .Count,
Mean: ,
SumOfSquaredDeviation: .SumOfSquaredDeviation,
},
}
:= false
if := .BucketOptions; != nil && .Type != nil {
, := .Type.(*metricspb.DistributionValue_BucketOptions_Explicit_)
if && != nil && .Explicit != nil {
= shouldInsertZeroBound(.Explicit.Bounds...)
.DistributionValue.BucketOptions = &distributionpb.Distribution_BucketOptions{
Options: &distributionpb.Distribution_BucketOptions_ExplicitBuckets{
Bounds: addZeroBoundOnCondition(, .Explicit.Bounds...),
},
},
}
}
}
.DistributionValue.BucketCounts = addZeroBucketCountOnCondition(, bucketCounts(.Buckets)...)
}
return &monitoringpb.TypedValue{Value: }, nil
}
}
func ( []*metricspb.DistributionValue_Bucket) []int64 {
:= make([]int64, len())
for , := range {
if != nil {
[] = .Count
}
}
return
}
func ( *metricspb.Metric) (googlemetricpb.MetricDescriptor_MetricKind, googlemetricpb.MetricDescriptor_ValueType) {
:= .GetMetricDescriptor()
if == nil {
return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
}
switch .Type {
case metricspb.MetricDescriptor_CUMULATIVE_INT64:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_INT64
case metricspb.MetricDescriptor_CUMULATIVE_DOUBLE:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_DOUBLE
case metricspb.MetricDescriptor_CUMULATIVE_DISTRIBUTION:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_DISTRIBUTION
case metricspb.MetricDescriptor_GAUGE_DOUBLE:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_DOUBLE
case metricspb.MetricDescriptor_GAUGE_INT64:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_INT64
case metricspb.MetricDescriptor_GAUGE_DISTRIBUTION:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_DISTRIBUTION
default:
return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |