GoPLS Viewer

Home|gopls/internal/event/export/ocagent/metrics.go
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
5package ocagent
6
7import (
8    "time"
9
10    "golang.org/x/tools/internal/event/export/metric"
11    "golang.org/x/tools/internal/event/export/ocagent/wire"
12    "golang.org/x/tools/internal/event/label"
13)
14
15// dataToMetricDescriptor return a *wire.MetricDescriptor based on data.
16func dataToMetricDescriptor(data metric.Data) *wire.MetricDescriptor {
17    if data == nil {
18        return nil
19    }
20    descriptor := &wire.MetricDescriptor{
21        Name:        data.Handle(),
22        DescriptiongetDescription(data),
23        // TODO: Unit?
24        Type:      dataToMetricDescriptorType(data),
25        LabelKeysgetLabelKeys(data),
26    }
27
28    return descriptor
29}
30
31// getDescription returns the description of data.
32func getDescription(data metric.Datastring {
33    switch d := data.(type) {
34    case *metric.Int64Data:
35        return d.Info.Description
36
37    case *metric.Float64Data:
38        return d.Info.Description
39
40    case *metric.HistogramInt64Data:
41        return d.Info.Description
42
43    case *metric.HistogramFloat64Data:
44        return d.Info.Description
45    }
46
47    return ""
48}
49
50// getLabelKeys returns a slice of *wire.LabelKeys based on the keys
51// in data.
52func getLabelKeys(data metric.Data) []*wire.LabelKey {
53    switch d := data.(type) {
54    case *metric.Int64Data:
55        return infoKeysToLabelKeys(d.Info.Keys)
56
57    case *metric.Float64Data:
58        return infoKeysToLabelKeys(d.Info.Keys)
59
60    case *metric.HistogramInt64Data:
61        return infoKeysToLabelKeys(d.Info.Keys)
62
63    case *metric.HistogramFloat64Data:
64        return infoKeysToLabelKeys(d.Info.Keys)
65    }
66
67    return nil
68}
69
70// dataToMetricDescriptorType returns a wire.MetricDescriptor_Type based on the
71// underlying type of data.
72func dataToMetricDescriptorType(data metric.Datawire.MetricDescriptor_Type {
73    switch d := data.(type) {
74    case *metric.Int64Data:
75        if d.IsGauge {
76            return wire.MetricDescriptor_GAUGE_INT64
77        }
78        return wire.MetricDescriptor_CUMULATIVE_INT64
79
80    case *metric.Float64Data:
81        if d.IsGauge {
82            return wire.MetricDescriptor_GAUGE_DOUBLE
83        }
84        return wire.MetricDescriptor_CUMULATIVE_DOUBLE
85
86    case *metric.HistogramInt64Data:
87        return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION
88
89    case *metric.HistogramFloat64Data:
90        return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION
91    }
92
93    return wire.MetricDescriptor_UNSPECIFIED
94}
95
96// dataToTimeseries returns a slice of *wire.TimeSeries based on the
97// points in data.
98func dataToTimeseries(data metric.Datastart time.Time) []*wire.TimeSeries {
99    if data == nil {
100        return nil
101    }
102
103    numRows := numRows(data)
104    startTimestamp := convertTimestamp(start)
105    timeseries := make([]*wire.TimeSeries0numRows)
106
107    for i := 0i < numRowsi++ {
108        timeseries = append(timeseries, &wire.TimeSeries{
109            StartTimestamp: &startTimestamp,
110            // TODO: labels?
111            PointsdataToPoints(datai),
112        })
113    }
114
115    return timeseries
116}
117
118// numRows returns the number of rows in data.
119func numRows(data metric.Dataint {
120    switch d := data.(type) {
121    case *metric.Int64Data:
122        return len(d.Rows)
123    case *metric.Float64Data:
124        return len(d.Rows)
125    case *metric.HistogramInt64Data:
126        return len(d.Rows)
127    case *metric.HistogramFloat64Data:
128        return len(d.Rows)
129    }
130
131    return 0
132}
133
134// dataToPoints returns an array of *wire.Points based on the point(s)
135// in data at index i.
136func dataToPoints(data metric.Datai int) []*wire.Point {
137    switch d := data.(type) {
138    case *metric.Int64Data:
139        timestamp := convertTimestamp(d.EndTime)
140        return []*wire.Point{
141            {
142                Valuewire.PointInt64Value{
143                    Int64Valued.Rows[i],
144                },
145                Timestamp: &timestamp,
146            },
147        }
148    case *metric.Float64Data:
149        timestamp := convertTimestamp(d.EndTime)
150        return []*wire.Point{
151            {
152                Valuewire.PointDoubleValue{
153                    DoubleValued.Rows[i],
154                },
155                Timestamp: &timestamp,
156            },
157        }
158    case *metric.HistogramInt64Data:
159        row := d.Rows[i]
160        bucketBounds := make([]float64len(d.Info.Buckets))
161        for ival := range d.Info.Buckets {
162            bucketBounds[i] = float64(val)
163        }
164        return distributionToPoints(row.Valuesrow.Countfloat64(row.Sum), bucketBoundsd.EndTime)
165    case *metric.HistogramFloat64Data:
166        row := d.Rows[i]
167        return distributionToPoints(row.Valuesrow.Countrow.Sumd.Info.Bucketsd.EndTime)
168    }
169
170    return nil
171}
172
173// distributionToPoints returns an array of *wire.Points containing a
174// wire.PointDistributionValue representing a distribution with the
175// supplied counts, count, and sum.
176func distributionToPoints(counts []int64count int64sum float64bucketBounds []float64end time.Time) []*wire.Point {
177    buckets := make([]*wire.Bucketlen(counts))
178    for i := 0i < len(counts); i++ {
179        buckets[i] = &wire.Bucket{
180            Countcounts[i],
181        }
182    }
183    timestamp := convertTimestamp(end)
184    return []*wire.Point{
185        {
186            Valuewire.PointDistributionValue{
187                DistributionValue: &wire.DistributionValue{
188                    Countcount,
189                    Sum:   sum,
190                    // TODO: SumOfSquaredDeviation?
191                    Bucketsbuckets,
192                    BucketOptions: &wire.BucketOptionsExplicit{
193                        BoundsbucketBounds,
194                    },
195                },
196            },
197            Timestamp: &timestamp,
198        },
199    }
200}
201
202// infoKeysToLabelKeys returns an array of *wire.LabelKeys containing the
203// string values of the elements of labelKeys.
204func infoKeysToLabelKeys(infoKeys []label.Key) []*wire.LabelKey {
205    labelKeys := make([]*wire.LabelKey0len(infoKeys))
206    for _key := range infoKeys {
207        labelKeys = append(labelKeys, &wire.LabelKey{
208            Keykey.Name(),
209        })
210    }
211
212    return labelKeys
213}
214
MembersX
metric
dataToMetricDescriptor.descriptor
dataToMetricDescriptorType
dataToTimeseries.start
dataToTimeseries.numRows
dataToTimeseries.i
dataToPoints.BlockStmt.RangeStmt_3898.val
time
infoKeysToLabelKeys.infoKeys
distributionToPoints.sum
numRows
distributionToPoints.end
label
dataToMetricDescriptorType.data
dataToPoints.data
distributionToPoints.counts
distributionToPoints.buckets
getLabelKeys
dataToTimeseries
dataToTimeseries.data
dataToTimeseries.startTimestamp
numRows.data
dataToPoints.BlockStmt.timestamp
infoKeysToLabelKeys
infoKeysToLabelKeys.labelKeys
getDescription.data
getDescription
distributionToPoints.i
wire
dataToPoints
dataToPoints.i
dataToPoints.BlockStmt.RangeStmt_3898.i
distributionToPoints.bucketBounds
getLabelKeys.data
dataToPoints.BlockStmt.bucketBounds
distributionToPoints.timestamp
dataToTimeseries.timeseries
dataToMetricDescriptor.data
distributionToPoints
distributionToPoints.count
infoKeysToLabelKeys.RangeStmt_5292.key
dataToMetricDescriptor
Members
X