mirror of
https://github.com/golang/go
synced 2024-11-06 06:36:20 -07:00
eff45d17df
This makes keys a pointer, which reduces the allocations during logging, and has a significant improvement in memory and cpu cost when there is no exporter. Packing a string into an interface requires a 16 byte allocation, packing a pointer does not. name old time/op new time/op delta /LogNoExporter-8 4.39µs ± 2% 3.83µs ± 2% -12.74% (p=0.000 n=18+20) /Log-8 23.5µs ± 2% 22.6µs ± 1% -4.20% (p=0.000 n=19+18) name old alloc/op new alloc/op delta /LogNoExporter-8 1.70kB ± 0% 1.38kB ± 0% -18.78% (p=0.000 n=20+20) /Log-8 4.91kB ± 0% 4.91kB ± 0% ~ (p=1.000 n=20+20) name old allocs/op new allocs/op delta /LogNoExporter-8 83.0 ± 0% 63.0 ± 0% -24.10% (p=0.000 n=20+20) /Log-8 163 ± 0% 163 ± 0% ~ (all equal) Change-Id: Iec127f1bff8d5c8f4bd0a6d9f6d8fc4b8bc740b2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/222599 Run-TryBot: Ian Cottrell <iancottrell@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Robert Findley <rfindley@google.com>
215 lines
5.4 KiB
Go
215 lines
5.4 KiB
Go
// 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 ocagent
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
|
"golang.org/x/tools/internal/telemetry/export/ocagent/wire"
|
|
"golang.org/x/tools/internal/telemetry/metric"
|
|
)
|
|
|
|
// dataToMetricDescriptor return a *wire.MetricDescriptor based on data.
|
|
func dataToMetricDescriptor(data event.MetricData) *wire.MetricDescriptor {
|
|
if data == nil {
|
|
return nil
|
|
}
|
|
descriptor := &wire.MetricDescriptor{
|
|
Name: data.Handle(),
|
|
Description: getDescription(data),
|
|
// TODO: Unit?
|
|
Type: dataToMetricDescriptorType(data),
|
|
LabelKeys: getLabelKeys(data),
|
|
}
|
|
|
|
return descriptor
|
|
}
|
|
|
|
// getDescription returns the description of data.
|
|
func getDescription(data event.MetricData) string {
|
|
switch d := data.(type) {
|
|
case *metric.Int64Data:
|
|
return d.Info.Description
|
|
|
|
case *metric.Float64Data:
|
|
return d.Info.Description
|
|
|
|
case *metric.HistogramInt64Data:
|
|
return d.Info.Description
|
|
|
|
case *metric.HistogramFloat64Data:
|
|
return d.Info.Description
|
|
}
|
|
|
|
return ""
|
|
}
|
|
|
|
// getLabelKeys returns a slice of *wire.LabelKeys based on the keys
|
|
// in data.
|
|
func getLabelKeys(data event.MetricData) []*wire.LabelKey {
|
|
switch d := data.(type) {
|
|
case *metric.Int64Data:
|
|
return infoKeysToLabelKeys(d.Info.Keys)
|
|
|
|
case *metric.Float64Data:
|
|
return infoKeysToLabelKeys(d.Info.Keys)
|
|
|
|
case *metric.HistogramInt64Data:
|
|
return infoKeysToLabelKeys(d.Info.Keys)
|
|
|
|
case *metric.HistogramFloat64Data:
|
|
return infoKeysToLabelKeys(d.Info.Keys)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// dataToMetricDescriptorType returns a wire.MetricDescriptor_Type based on the
|
|
// underlying type of data.
|
|
func dataToMetricDescriptorType(data event.MetricData) wire.MetricDescriptor_Type {
|
|
switch d := data.(type) {
|
|
case *metric.Int64Data:
|
|
if d.IsGauge {
|
|
return wire.MetricDescriptor_GAUGE_INT64
|
|
}
|
|
return wire.MetricDescriptor_CUMULATIVE_INT64
|
|
|
|
case *metric.Float64Data:
|
|
if d.IsGauge {
|
|
return wire.MetricDescriptor_GAUGE_DOUBLE
|
|
}
|
|
return wire.MetricDescriptor_CUMULATIVE_DOUBLE
|
|
|
|
case *metric.HistogramInt64Data:
|
|
return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION
|
|
|
|
case *metric.HistogramFloat64Data:
|
|
return wire.MetricDescriptor_CUMULATIVE_DISTRIBUTION
|
|
}
|
|
|
|
return wire.MetricDescriptor_UNSPECIFIED
|
|
}
|
|
|
|
// dataToTimeseries returns a slice of *wire.TimeSeries based on the
|
|
// points in data.
|
|
func dataToTimeseries(data event.MetricData, start time.Time) []*wire.TimeSeries {
|
|
if data == nil {
|
|
return nil
|
|
}
|
|
|
|
numRows := numRows(data)
|
|
startTimestamp := convertTimestamp(start)
|
|
timeseries := make([]*wire.TimeSeries, 0, numRows)
|
|
|
|
for i := 0; i < numRows; i++ {
|
|
timeseries = append(timeseries, &wire.TimeSeries{
|
|
StartTimestamp: &startTimestamp,
|
|
// TODO: labels?
|
|
Points: dataToPoints(data, i),
|
|
})
|
|
}
|
|
|
|
return timeseries
|
|
}
|
|
|
|
// numRows returns the number of rows in data.
|
|
func numRows(data event.MetricData) int {
|
|
switch d := data.(type) {
|
|
case *metric.Int64Data:
|
|
return len(d.Rows)
|
|
case *metric.Float64Data:
|
|
return len(d.Rows)
|
|
case *metric.HistogramInt64Data:
|
|
return len(d.Rows)
|
|
case *metric.HistogramFloat64Data:
|
|
return len(d.Rows)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
// dataToPoints returns an array of *wire.Points based on the point(s)
|
|
// in data at index i.
|
|
func dataToPoints(data event.MetricData, i int) []*wire.Point {
|
|
switch d := data.(type) {
|
|
case *metric.Int64Data:
|
|
timestamp := convertTimestamp(*d.EndTime)
|
|
return []*wire.Point{
|
|
{
|
|
Value: wire.PointInt64Value{
|
|
Int64Value: d.Rows[i],
|
|
},
|
|
Timestamp: ×tamp,
|
|
},
|
|
}
|
|
case *metric.Float64Data:
|
|
timestamp := convertTimestamp(*d.EndTime)
|
|
return []*wire.Point{
|
|
{
|
|
Value: wire.PointDoubleValue{
|
|
DoubleValue: d.Rows[i],
|
|
},
|
|
Timestamp: ×tamp,
|
|
},
|
|
}
|
|
case *metric.HistogramInt64Data:
|
|
row := d.Rows[i]
|
|
bucketBounds := make([]float64, len(d.Info.Buckets))
|
|
for i, val := range d.Info.Buckets {
|
|
bucketBounds[i] = float64(val)
|
|
}
|
|
return distributionToPoints(row.Values, row.Count, float64(row.Sum), bucketBounds, *d.EndTime)
|
|
case *metric.HistogramFloat64Data:
|
|
row := d.Rows[i]
|
|
return distributionToPoints(row.Values, row.Count, row.Sum, d.Info.Buckets, *d.EndTime)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// distributionToPoints returns an array of *wire.Points containing a
|
|
// wire.PointDistributionValue representing a distribution with the
|
|
// supplied counts, count, and sum.
|
|
func distributionToPoints(counts []int64, count int64, sum float64, bucketBounds []float64, end time.Time) []*wire.Point {
|
|
buckets := make([]*wire.Bucket, len(counts))
|
|
for i := 0; i < len(counts); i++ {
|
|
buckets[i] = &wire.Bucket{
|
|
Count: counts[i],
|
|
}
|
|
}
|
|
timestamp := convertTimestamp(end)
|
|
return []*wire.Point{
|
|
{
|
|
Value: wire.PointDistributionValue{
|
|
DistributionValue: &wire.DistributionValue{
|
|
Count: count,
|
|
Sum: sum,
|
|
// TODO: SumOfSquaredDeviation?
|
|
Buckets: buckets,
|
|
BucketOptions: &wire.BucketOptionsExplicit{
|
|
Bounds: bucketBounds,
|
|
},
|
|
},
|
|
},
|
|
Timestamp: ×tamp,
|
|
},
|
|
}
|
|
}
|
|
|
|
// infoKeysToLabelKeys returns an array of *wire.LabelKeys containing the
|
|
// string values of the elements of labelKeys.
|
|
func infoKeysToLabelKeys(infoKeys []*event.Key) []*wire.LabelKey {
|
|
labelKeys := make([]*wire.LabelKey, 0, len(infoKeys))
|
|
for _, key := range infoKeys {
|
|
labelKeys = append(labelKeys, &wire.LabelKey{
|
|
Key: fmt.Sprintf("%v", key.Name),
|
|
})
|
|
}
|
|
|
|
return labelKeys
|
|
}
|