-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
odeke-em
wants to merge
1
commit into
open-telemetry:main
from
orijtech:receiver-prometheus-helpers-for-pdata-metrics
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
143 changes: 143 additions & 0 deletions
143
internal/coreinternal/metricstestutil/metricsutil_pdata.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.