Source File
metrics.go
Belonging Package
contrib.go.opencensus.io/exporter/stackdriver
package stackdriver
import (
distributionpb
labelpb
googlemetricpb
monitoredrespb
monitoringpb
)
const (
exemplarAttachmentTypeString = "type.googleapis.com/google.protobuf.StringValue"
exemplarAttachmentTypeSpanCtx = "type.googleapis.com/google.monitoring.v3.SpanContext"
)
func ( *statsExporter) ( context.Context, []*metricdata.Metric) error {
if len() == 0 {
return nil
}
for , := range {
}
return nil
}
func ( *statsExporter) ( []*metricdata.Metric) {
:= .uploadMetrics()
if != nil {
.o.handleError()
}
}
func ( *statsExporter) ( []*metricdata.Metric) error {
, := newContextWithTimeout(.o.Context, .o.Timeout)
defer ()
var []error
, := trace.StartSpan(
,
"contrib.go.opencensus.io/exporter/stackdriver.uploadMetrics",
trace.WithSampler(trace.NeverSample()),
)
defer .End()
if := .createMetricDescriptorFromMetric(, ); != nil {
.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: .Error()})
= append(, )
continue
}
}
var []*monitoringpb.TimeSeries
for , := range {
, := .metricToMpbTs(, )
if != nil {
.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: .Error()})
= append(, )
continue
}
if != nil {
= append(, ...)
}
}
for , := 0, 0; < len(); = {
= + maxTimeSeriesPerUpload
if > len() {
= len()
}
:= [:]
:= .combineTimeSeriesToCreateTimeSeriesRequest()
for , := range {
if := createTimeSeries(, .c, ); != nil {
.SetStatus(trace.Status{Code: trace.StatusCodeUnknown, Message: .Error()})
= append(, )
}
}
}
:= len()
if == 0 {
return nil
} else if == 1 {
return [0]
}
:= make([]string, 0, )
for , := range {
= append(, .Error())
}
return fmt.Errorf("[%s]", strings.Join(, "; "))
}
func ( *statsExporter) ( context.Context, *metricdata.Metric) ([]*monitoringpb.TimeSeries, error) {
if == nil {
return nil, errNilMetricOrMetricDescriptor
}
:= .metricRscToMpbRsc(.Resource)
:= .Descriptor.Name
:= .metricTypeFromProto()
:= .Descriptor.LabelKeys
, := metricDescriptorTypeToMetricKind()
return nil, nil
}
:= make([]*monitoringpb.TimeSeries, 0, len(.TimeSeries))
for , := range .TimeSeries {
, := .metricTsToMpbPoint(, )
continue
}
, := metricLabelsToTsLabels(.defaultLabels, , .LabelValues)
continue
}
var *monitoredrespb.MonitoredResource
var monitoredresource.Interface
if .o.ResourceByDescriptor != nil {
= convertMonitoredResourceToPB()
if .Type == "" {
.Type = "global"
.Labels = nil
}
} else {
=
}
= append(, &monitoringpb.TimeSeries{
Metric: &googlemetricpb.Metric{
Type: ,
Labels: ,
},
Resource: ,
Points: ,
})
}
return , nil
}
if .o.SkipCMD {
return nil
}
.metricMu.Lock()
defer .metricMu.Unlock()
:= .Descriptor.Name
if , := .metricDescriptors[]; {
return nil
}
if builtinMetric(.metricTypeFromProto()) {
.metricDescriptors[] = true
return nil
}
, := .metricToMpbMetricDescriptor()
if != nil {
return
}
if = .createMetricDescriptor(, ); != nil {
return
}
.metricDescriptors[] = true
return nil
}
func ( *statsExporter) ( *metricdata.Metric) (*googlemetricpb.MetricDescriptor, error) {
if == nil {
return nil, errNilMetricOrMetricDescriptor
}
:= .metricTypeFromProto(.Descriptor.Name)
:= .displayName(.Descriptor.Name)
, := metricDescriptorTypeToMetricKind()
:= &googlemetricpb.MetricDescriptor{
Name: fmt.Sprintf("projects/%s/metricDescriptors/%s", .o.ProjectID, ),
DisplayName: ,
Description: .Descriptor.Description,
Unit: string(.Descriptor.Unit),
Type: ,
MetricKind: ,
ValueType: ,
Labels: metricLableKeysToLabels(.defaultLabels, .Descriptor.LabelKeys),
}
return , nil
}
func ( map[string]labelValue, []metricdata.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(.Key),
Description: .Description,
ValueType: labelpb.LabelDescriptor_STRING, // We only use string tags
})
}
return
}
func ( *metricdata.Metric) (googlemetricpb.MetricDescriptor_MetricKind, googlemetricpb.MetricDescriptor_ValueType) {
if == nil {
return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
}
switch .Descriptor.Type {
case metricdata.TypeCumulativeInt64:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_INT64
case metricdata.TypeCumulativeFloat64:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_DOUBLE
case metricdata.TypeCumulativeDistribution:
return googlemetricpb.MetricDescriptor_CUMULATIVE, googlemetricpb.MetricDescriptor_DISTRIBUTION
case metricdata.TypeGaugeFloat64:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_DOUBLE
case metricdata.TypeGaugeInt64:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_INT64
case metricdata.TypeGaugeDistribution:
return googlemetricpb.MetricDescriptor_GAUGE, googlemetricpb.MetricDescriptor_DISTRIBUTION
return googlemetricpb.MetricDescriptor_METRIC_KIND_UNSPECIFIED, googlemetricpb.MetricDescriptor_VALUE_TYPE_UNSPECIFIED
}
}
func ( *statsExporter) ( *resource.Resource) *monitoredrespb.MonitoredResource {
if == nil {
:= .o.Resource
if == nil {
= &monitoredrespb.MonitoredResource{
Type: "global",
}
}
return
}
:= .Type
if == "" {
= "global"
}
:= &monitoredrespb.MonitoredResource{
Type: ,
}
if .Labels != nil {
.Labels = make(map[string]string, len(.Labels))
.Labels[] =
}
}
return
}
func ( *statsExporter) ( *metricdata.TimeSeries, googlemetricpb.MetricDescriptor_MetricKind) ( []*monitoringpb.Point, error) {
for , := range .Points {
:= timestampProto(.StartTime)
if == googlemetricpb.MetricDescriptor_GAUGE {
= nil
}
, := metricPointToMpbPoint(, &, .o.ProjectID)
if != nil {
return nil,
}
= append(, )
}
return , nil
}
func ( *timestamp.Timestamp, *metricdata.Point, string) (*monitoringpb.Point, error) {
if == nil {
return nil, nil
}
, := metricPointToMpbValue(, )
if != nil {
return nil,
}
:= &monitoringpb.Point{
Value: ,
Interval: &monitoringpb.TimeInterval{
StartTime: ,
EndTime: timestampProto(.Time),
},
}
return , nil
}
func ( *metricdata.Point, string) (*monitoringpb.TypedValue, error) {
if == nil {
return nil, nil
}
var error
var *monitoringpb.TypedValue
switch v := .Value.(type) {
default:
= fmt.Errorf("protoToMetricPoint: unknown Data type: %T", .Value)
case int64:
= &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_Int64Value{
Int64Value: ,
},
}
case float64:
= &monitoringpb.TypedValue{
Value: &monitoringpb.TypedValue_DoubleValue{
DoubleValue: ,
},
}
case *metricdata.Distribution:
:=
var *monitoringpb.TypedValue_DistributionValue
var float64
if .Count > 0 {
= float64(.Sum) / float64(.Count)
}
= &monitoringpb.TypedValue_DistributionValue{
DistributionValue: &distributionpb.Distribution{
Count: .Count,
Mean: ,
SumOfSquaredDeviation: .SumOfSquaredDeviation,
},
}
:= false
if := .BucketOptions; != nil {
= shouldInsertZeroBound(.Bounds...)
.DistributionValue.BucketOptions = &distributionpb.Distribution_BucketOptions{
Options: &distributionpb.Distribution_BucketOptions_ExplicitBuckets{
Bounds: addZeroBoundOnCondition(, .Bounds...),
},
},
}
}
, := metricBucketToBucketCountsAndExemplars(.Buckets, )
.DistributionValue.BucketCounts = addZeroBucketCountOnCondition(, ...)
.DistributionValue.Exemplars =
= &monitoringpb.TypedValue{Value: }
}
return ,
}
func ( []metricdata.Bucket, string) ([]int64, []*distributionpb.Distribution_Exemplar) {
:= make([]int64, len())
var []*distributionpb.Distribution_Exemplar
for , := range {
[] = .Count
if .Exemplar != nil {
= append(, metricExemplarToPbExemplar(.Exemplar, ))
}
}
return ,
}
func ( *metricdata.Exemplar, string) *distributionpb.Distribution_Exemplar {
return &distributionpb.Distribution_Exemplar{
Value: .Value,
Timestamp: timestampProto(.Timestamp),
Attachments: attachmentsToPbAttachments(.Attachments, ),
}
}
func ( metricdata.Attachments, string) []*any.Any {
var []*any.Any
for , := range {
if , := .(trace.SpanContext); {
= append(, toPbSpanCtxAttachment(, ))
= append(, toPbStringAttachment())
}
}
return
}
func ( interface{}) *any.Any {
:= fmt.Sprintf("%v", )
return &any.Any{
TypeUrl: exemplarAttachmentTypeString,
Value: []byte(),
}
}
func ( trace.SpanContext, string) *any.Any {
:= monitoringpb.SpanContext{
SpanName: fmt.Sprintf("projects/%s/traces/%s/spans/%s", , .TraceID.String(), .SpanID.String()),
}
, := proto.Marshal(&)
return &any.Any{
TypeUrl: exemplarAttachmentTypeSpanCtx,
Value: ,
}
![]() |
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. |