Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

contrib/iinternal/coreinternal: add helpers for Pdata Metrics #5231

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 143 additions & 0 deletions internal/coreinternal/metricstestutil/metricsutil_pdata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package metricstestutil

import "go.opentelemetry.io/collector/model/pdata"

type KV struct {
Key, Value string
}

func DistPointPdata(ts pdata.Timestamp, bounds []float64, counts []uint64) *pdata.HistogramDataPoint {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not return pointer to pdata.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pdata usually benefits more if instead of constructing new Metric, you append it to a MetricSlice. Less allocations.

hdp := pdata.NewHistogramDataPoint()
hdp.SetExplicitBounds(bounds)
hdp.SetBucketCounts(counts)
hdp.SetTimestamp(ts)
var sum float64
var count uint64
for i, bcount := range counts {
count += bcount
if i > 0 {
sum += float64(bcount) * bounds[i-1]
}
}
hdp.SetCount(count)
hdp.SetSum(sum)

return &hdp
}

func GaugeDistMetricPdata(name string, kvp []*KV, startTs pdata.Timestamp, points ...*pdata.HistogramDataPoint) *pdata.Metric {
hMetric := CumulativeDistMetricPdata(name, kvp, startTs, points...)
hMetric.Histogram().SetAggregationTemporality(pdata.AggregationTemporalityDelta)
return hMetric
}

func CumulativeDistMetricPdata(name string, kvp []*KV, startTs pdata.Timestamp, points ...*pdata.HistogramDataPoint) *pdata.Metric {
metric := pdata.NewMetric()
metric.SetName(name)
metric.SetDataType(pdata.MetricDataTypeHistogram)
histogram := metric.Histogram()
histogram.SetAggregationTemporality(pdata.AggregationTemporalityCumulative)

destPointL := histogram.DataPoints()
// By default the AggregationTemporality is Cumulative until it'll be changed by the caller.
for _, point := range points {
destPoint := destPointL.AppendEmpty()
point.CopyTo(destPoint)
point.SetStartTimestamp(startTs)
attrs := destPoint.Attributes()
for _, kv := range kvp {
attrs.InsertString(kv.Key, kv.Value)
}
}
return &metric
}

func DoublePointPdata(ts pdata.Timestamp, value float64) *pdata.NumberDataPoint {
ndp := pdata.NewNumberDataPoint()
ndp.SetTimestamp(ts)
ndp.SetDoubleVal(value)

return &ndp
}

func GaugeMetricPdata(name string, kvp []*KV, startTs pdata.Timestamp, points ...*pdata.NumberDataPoint) *pdata.Metric {
metric := pdata.NewMetric()
metric.SetName(name)
metric.SetDataType(pdata.MetricDataTypeGauge)

destPointL := metric.Gauge().DataPoints()
for _, point := range points {
destPoint := destPointL.AppendEmpty()
point.CopyTo(destPoint)
point.SetStartTimestamp(startTs)
attrs := destPoint.Attributes()
for _, kv := range kvp {
attrs.InsertString(kv.Key, kv.Value)
}
}
return &metric
}

func SummaryPointPdata(ts pdata.Timestamp, count uint64, sum float64, quantiles, values []float64) *pdata.SummaryDataPoint {
sdp := pdata.NewSummaryDataPoint()
sdp.SetTimestamp(ts)
sdp.SetCount(count)
sdp.SetSum(sum)
qvL := sdp.QuantileValues()
for i := 0; i < len(quantiles); i++ {
qvi := qvL.AppendEmpty()
qvi.SetQuantile(quantiles[i])
qvi.SetValue(values[i])
}
return &sdp
}

func SummaryMetricPdata(name string, kvp []*KV, startTs pdata.Timestamp, points ...*pdata.SummaryDataPoint) *pdata.Metric {
metric := pdata.NewMetric()
metric.SetName(name)
metric.SetDataType(pdata.MetricDataTypeSummary)

destPointL := metric.Summary().DataPoints()
for _, point := range points {
destPoint := destPointL.AppendEmpty()
point.CopyTo(destPoint)
point.SetStartTimestamp(startTs)
attrs := destPoint.Attributes()
for _, kv := range kvp {
attrs.InsertString(kv.Key, kv.Value)
}
}
return &metric
}

func SumMetricPdata(name string, kvp []*KV, startTs pdata.Timestamp, points ...*pdata.NumberDataPoint) *pdata.Metric {
metric := pdata.NewMetric()
metric.SetName(name)
metric.SetDataType(pdata.MetricDataTypeSum)

destPointL := metric.Sum().DataPoints()
for _, point := range points {
destPoint := destPointL.AppendEmpty()
point.CopyTo(destPoint)
point.SetStartTimestamp(startTs)
attrs := destPoint.Attributes()
for _, kv := range kvp {
attrs.InsertString(kv.Key, kv.Value)
}
}
return &metric
}