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