github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/internal/event/export/ocagent/wire/metrics.go (about) 1 // Copyright 2019 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package wire 6 7 import ( 8 "encoding/json" 9 "fmt" 10 ) 11 12 type ExportMetricsServiceRequest struct { 13 Node *Node `json:"node,omitempty"` 14 Metrics []*Metric `json:"metrics,omitempty"` 15 Resource *Resource `json:"resource,omitempty"` 16 } 17 18 type Metric struct { 19 MetricDescriptor *MetricDescriptor `json:"metric_descriptor,omitempty"` 20 Timeseries []*TimeSeries `json:"timeseries,omitempty"` 21 Resource *Resource `json:"resource,omitempty"` 22 } 23 24 type MetricDescriptor struct { 25 Name string `json:"name,omitempty"` 26 Description string `json:"description,omitempty"` 27 Unit string `json:"unit,omitempty"` 28 Type MetricDescriptor_Type `json:"type,omitempty"` 29 LabelKeys []*LabelKey `json:"label_keys,omitempty"` 30 } 31 32 type MetricDescriptor_Type int32 33 34 const ( 35 MetricDescriptor_UNSPECIFIED MetricDescriptor_Type = 0 36 MetricDescriptor_GAUGE_INT64 MetricDescriptor_Type = 1 37 MetricDescriptor_GAUGE_DOUBLE MetricDescriptor_Type = 2 38 MetricDescriptor_GAUGE_DISTRIBUTION MetricDescriptor_Type = 3 39 MetricDescriptor_CUMULATIVE_INT64 MetricDescriptor_Type = 4 40 MetricDescriptor_CUMULATIVE_DOUBLE MetricDescriptor_Type = 5 41 MetricDescriptor_CUMULATIVE_DISTRIBUTION MetricDescriptor_Type = 6 42 MetricDescriptor_SUMMARY MetricDescriptor_Type = 7 43 ) 44 45 type LabelKey struct { 46 Key string `json:"key,omitempty"` 47 Description string `json:"description,omitempty"` 48 } 49 50 type TimeSeries struct { 51 StartTimestamp *Timestamp `json:"start_timestamp,omitempty"` 52 LabelValues []*LabelValue `json:"label_values,omitempty"` 53 Points []*Point `json:"points,omitempty"` 54 } 55 56 type LabelValue struct { 57 Value string `json:"value,omitempty"` 58 HasValue bool `json:"has_value,omitempty"` 59 } 60 61 type Point struct { 62 Timestamp *Timestamp `json:"timestamp,omitempty"` 63 Value PointValue `json:"value,omitempty"` 64 } 65 66 type PointInt64Value struct { 67 Int64Value int64 `json:"int64Value,omitempty"` 68 } 69 70 // MarshalJSON creates JSON formatted the same way as jsonpb so that the 71 // OpenCensus service can correctly determine the underlying value type. 72 // This custom MarshalJSON exists because, 73 // by default *Point is JSON marshalled as: 74 // {"value": {"int64Value": 1}} 75 // but it should be marshalled as: 76 // {"int64Value": 1} 77 func (p *Point) MarshalJSON() ([]byte, error) { 78 if p == nil { 79 return []byte("null"), nil 80 } 81 82 switch d := p.Value.(type) { 83 case PointInt64Value: 84 return json.Marshal(&struct { 85 Timestamp *Timestamp `json:"timestamp,omitempty"` 86 Value int64 `json:"int64Value,omitempty"` 87 }{ 88 Timestamp: p.Timestamp, 89 Value: d.Int64Value, 90 }) 91 case PointDoubleValue: 92 return json.Marshal(&struct { 93 Timestamp *Timestamp `json:"timestamp,omitempty"` 94 Value float64 `json:"doubleValue,omitempty"` 95 }{ 96 Timestamp: p.Timestamp, 97 Value: d.DoubleValue, 98 }) 99 case PointDistributionValue: 100 return json.Marshal(&struct { 101 Timestamp *Timestamp `json:"timestamp,omitempty"` 102 Value *DistributionValue `json:"distributionValue,omitempty"` 103 }{ 104 Timestamp: p.Timestamp, 105 Value: d.DistributionValue, 106 }) 107 default: 108 return nil, fmt.Errorf("unknown point type %T", p.Value) 109 } 110 } 111 112 type PointDoubleValue struct { 113 DoubleValue float64 `json:"doubleValue,omitempty"` 114 } 115 116 type PointDistributionValue struct { 117 DistributionValue *DistributionValue `json:"distributionValue,omitempty"` 118 } 119 120 type PointSummaryValue struct { 121 SummaryValue *SummaryValue `json:"summaryValue,omitempty"` 122 } 123 124 type PointValue interface { 125 labelPointValue() 126 } 127 128 func (PointInt64Value) labelPointValue() {} 129 func (PointDoubleValue) labelPointValue() {} 130 func (PointDistributionValue) labelPointValue() {} 131 func (PointSummaryValue) labelPointValue() {} 132 133 type DistributionValue struct { 134 Count int64 `json:"count,omitempty"` 135 Sum float64 `json:"sum,omitempty"` 136 SumOfSquaredDeviation float64 `json:"sum_of_squared_deviation,omitempty"` 137 BucketOptions BucketOptions `json:"bucket_options,omitempty"` 138 Buckets []*Bucket `json:"buckets,omitempty"` 139 } 140 141 type BucketOptionsExplicit struct { 142 Bounds []float64 `json:"bounds,omitempty"` 143 } 144 145 type BucketOptions interface { 146 labelBucketOptions() 147 } 148 149 func (*BucketOptionsExplicit) labelBucketOptions() {} 150 151 var _ BucketOptions = (*BucketOptionsExplicit)(nil) 152 var _ json.Marshaler = (*BucketOptionsExplicit)(nil) 153 154 // Declared for the purpose of custom JSON marshaling without cycles. 155 type bucketOptionsExplicitAlias BucketOptionsExplicit 156 157 // MarshalJSON creates JSON formatted the same way as jsonpb so that the 158 // OpenCensus service can correctly determine the underlying value type. 159 // This custom MarshalJSON exists because, 160 // by default BucketOptionsExplicit is JSON marshalled as: 161 // {"bounds":[1,2,3]} 162 // but it should be marshalled as: 163 // {"explicit":{"bounds":[1,2,3]}} 164 func (be *BucketOptionsExplicit) MarshalJSON() ([]byte, error) { 165 return json.Marshal(&struct { 166 Explicit *bucketOptionsExplicitAlias `json:"explicit,omitempty"` 167 }{ 168 Explicit: (*bucketOptionsExplicitAlias)(be), 169 }) 170 } 171 172 type Bucket struct { 173 Count int64 `json:"count,omitempty"` 174 Exemplar *Exemplar `json:"exemplar,omitempty"` 175 } 176 177 type Exemplar struct { 178 Value float64 `json:"value,omitempty"` 179 Timestamp *Timestamp `json:"timestamp,omitempty"` 180 Attachments map[string]string `json:"attachments,omitempty"` 181 } 182 183 type SummaryValue struct { 184 Count *Int64Value `json:"count,omitempty"` 185 Sum *DoubleValue `json:"sum,omitempty"` 186 Snapshot *Snapshot `json:"snapshot,omitempty"` 187 } 188 189 type Snapshot struct { 190 Count *Int64Value `json:"count,omitempty"` 191 Sum *DoubleValue `json:"sum,omitempty"` 192 PercentileValues []*SnapshotValueAtPercentile `json:"percentile_values,omitempty"` 193 } 194 195 type SnapshotValueAtPercentile struct { 196 Percentile float64 `json:"percentile,omitempty"` 197 Value float64 `json:"value,omitempty"` 198 }