Skip to content

Commit

Permalink
[processor/transform] use default unit in extract_count_metric() (#31636
Browse files Browse the repository at this point in the history
)

**Description:** 

The `transformprocessor` offers a function `extract_count_metric()`.
Currently this function copies over the `unit` from the original metric
to a new `count` metric. However, this unit is not applicable as the
value is a count of how many values were recorded in a Explicit
Bucket/Exponential Histogram or Summary.

Therefore this PR changes that function so that it adds the default unit
(`1`) to the extracted count metric instead.

**Link to tracking Issue:** #31575 

**Testing:** Unit tests (updated)

**Documentation:** Added a changelog entry as the change is user-facing
(the unit of the emitted telemetry is changed)

---------

Co-authored-by: Tyler Helmuth <12352919+TylerHelmuth@users.noreply.github.com>
  • Loading branch information
pichlermarc and TylerHelmuth committed Mar 12, 2024
1 parent c566a37 commit c999301
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
29 changes: 29 additions & 0 deletions .chloggen/fix_extract-count-default-unit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: "bug_fix"

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: transformprocessor

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Change metric unit for metrics extracted with `extract_count_metric()` to be the default unit (`1`)

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [31575]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext: |
The original metric `unit` does not apply to extracted `count` metrics the same way it does to `sum`, `min` or `max`.
Metrics extracted using `extract_count_metric()` now use the more appropriate default unit (`1`) instead.
# If your change doesn't affect end users or the exported elements of any package,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [user]
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ func extractCountMetric(monotonic bool) (ottl.ExprFunc[ottlmetric.TransformConte
countMetric := pmetric.NewMetric()
countMetric.SetDescription(metric.Description())
countMetric.SetName(metric.Name() + "_count")
countMetric.SetUnit(metric.Unit())
// Use the default unit as the original metric unit does not apply to the 'count' field
countMetric.SetUnit("1")
countMetric.SetEmptySum().SetAggregationTemporality(aggTemp)
countMetric.Sum().SetIsMonotonic(monotonic)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Test_extractCountMetric(t *testing.T) {
histogramMetric := getTestHistogramMetric()
histogramMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetUnit("1")
countMetric.SetEmptySum()
countMetric.Sum().SetAggregationTemporality(histogramMetric.Histogram().AggregationTemporality())
countMetric.Sum().SetIsMonotonic(false)
Expand All @@ -44,6 +45,7 @@ func Test_extractCountMetric(t *testing.T) {
histogramMetric := getTestHistogramMetric()
histogramMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetUnit("1")
countMetric.SetEmptySum()
countMetric.Sum().SetAggregationTemporality(histogramMetric.Histogram().AggregationTemporality())
countMetric.Sum().SetIsMonotonic(true)
Expand All @@ -64,6 +66,7 @@ func Test_extractCountMetric(t *testing.T) {
expHistogramMetric := getTestExponentialHistogramMetric()
expHistogramMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetUnit("1")
countMetric.SetEmptySum()
countMetric.Sum().SetAggregationTemporality(expHistogramMetric.ExponentialHistogram().AggregationTemporality())
countMetric.Sum().SetIsMonotonic(false)
Expand All @@ -85,6 +88,7 @@ func Test_extractCountMetric(t *testing.T) {
expHistogramMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetEmptySum()
countMetric.SetUnit("1")
countMetric.Sum().SetAggregationTemporality(expHistogramMetric.ExponentialHistogram().AggregationTemporality())
countMetric.Sum().SetIsMonotonic(true)

Expand All @@ -105,6 +109,7 @@ func Test_extractCountMetric(t *testing.T) {
summaryMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetEmptySum()
countMetric.SetUnit("1")
countMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
countMetric.Sum().SetIsMonotonic(false)

Expand All @@ -125,6 +130,7 @@ func Test_extractCountMetric(t *testing.T) {
summaryMetric.CopyTo(metrics.AppendEmpty())
countMetric := metrics.AppendEmpty()
countMetric.SetEmptySum()
countMetric.SetUnit("1")
countMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
countMetric.Sum().SetIsMonotonic(true)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func Test_ProcessMetrics_MetricContext(t *testing.T) {
countMetric.SetName(histogramMetric.Name() + "_count")
countMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityDelta)
countMetric.Sum().SetIsMonotonic(true)
countMetric.SetUnit(histogramMetric.Unit())
countMetric.SetUnit("1")

histogramDp0 := histogramMetric.Histogram().DataPoints().At(0)
countDp0 := countMetric.Sum().DataPoints().AppendEmpty()
Expand Down

0 comments on commit c999301

Please sign in to comment.