|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package metrics |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "go.opentelemetry.io/collector/pdata/pcommon" |
| 12 | + "go.opentelemetry.io/collector/pdata/pmetric" |
| 13 | + |
| 14 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl" |
| 15 | + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric" |
| 16 | +) |
| 17 | + |
| 18 | +func TestScale(t *testing.T) { |
| 19 | + type testCase struct { |
| 20 | + name string |
| 21 | + args ScaleArguments |
| 22 | + valueFunc func() pmetric.Metric |
| 23 | + wantFunc func() pmetric.Metric |
| 24 | + wantErr bool |
| 25 | + } |
| 26 | + tests := []testCase{ |
| 27 | + { |
| 28 | + name: "scale gauge float metric", |
| 29 | + valueFunc: func() pmetric.Metric { |
| 30 | + metric := pmetric.NewMetric() |
| 31 | + metric.SetName("test-metric") |
| 32 | + metric.SetEmptyGauge() |
| 33 | + metric.Gauge().DataPoints().AppendEmpty().SetDoubleValue(10.0) |
| 34 | + |
| 35 | + return metric |
| 36 | + }, |
| 37 | + args: ScaleArguments{ |
| 38 | + Multiplier: 10.0, |
| 39 | + Unit: ottl.NewTestingOptional[ottl.StringGetter[ottlmetric.TransformContext]](ottl.StandardStringGetter[ottlmetric.TransformContext]{ |
| 40 | + Getter: func(_ context.Context, _ ottlmetric.TransformContext) (any, error) { |
| 41 | + return "kWh", nil |
| 42 | + }, |
| 43 | + }), |
| 44 | + }, |
| 45 | + wantFunc: func() pmetric.Metric { |
| 46 | + metric := pmetric.NewMetric() |
| 47 | + metric.SetName("test-metric") |
| 48 | + metric.SetEmptyGauge() |
| 49 | + metric.SetUnit("kWh") |
| 50 | + metric.Gauge().DataPoints().AppendEmpty().SetDoubleValue(100.0) |
| 51 | + |
| 52 | + return metric |
| 53 | + }, |
| 54 | + wantErr: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "scale gauge int metric", |
| 58 | + valueFunc: func() pmetric.Metric { |
| 59 | + metric := pmetric.NewMetric() |
| 60 | + metric.SetName("test-metric") |
| 61 | + metric.SetEmptyGauge() |
| 62 | + metric.Gauge().DataPoints().AppendEmpty().SetIntValue(10) |
| 63 | + |
| 64 | + return metric |
| 65 | + }, |
| 66 | + args: ScaleArguments{ |
| 67 | + Multiplier: 10.0, |
| 68 | + }, |
| 69 | + wantFunc: func() pmetric.Metric { |
| 70 | + metric := pmetric.NewMetric() |
| 71 | + metric.SetName("test-metric") |
| 72 | + metric.SetEmptyGauge() |
| 73 | + metric.Gauge().DataPoints().AppendEmpty().SetIntValue(100.0) |
| 74 | + |
| 75 | + return metric |
| 76 | + }, |
| 77 | + wantErr: false, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "scale sum metric", |
| 81 | + valueFunc: func() pmetric.Metric { |
| 82 | + metric := pmetric.NewMetric() |
| 83 | + metric.SetName("test-metric") |
| 84 | + metric.SetEmptySum() |
| 85 | + metric.Sum().DataPoints().AppendEmpty().SetDoubleValue(10.0) |
| 86 | + |
| 87 | + return metric |
| 88 | + }, |
| 89 | + args: ScaleArguments{ |
| 90 | + Multiplier: 10.0, |
| 91 | + }, |
| 92 | + wantFunc: func() pmetric.Metric { |
| 93 | + metric := pmetric.NewMetric() |
| 94 | + metric.SetName("test-metric") |
| 95 | + metric.SetEmptySum() |
| 96 | + metric.Sum().DataPoints().AppendEmpty().SetDoubleValue(100.0) |
| 97 | + |
| 98 | + return metric |
| 99 | + }, |
| 100 | + wantErr: false, |
| 101 | + }, |
| 102 | + { |
| 103 | + name: "scale histogram metric", |
| 104 | + valueFunc: func() pmetric.Metric { |
| 105 | + metric := getTestScalingHistogramMetric(1, 4, 1, 3, []float64{1, 10}, []uint64{1, 2}, []float64{1.0}, 1, 1) |
| 106 | + return metric |
| 107 | + }, |
| 108 | + args: ScaleArguments{ |
| 109 | + Multiplier: 10.0, |
| 110 | + }, |
| 111 | + wantFunc: func() pmetric.Metric { |
| 112 | + metric := getTestScalingHistogramMetric(1, 40, 10, 30, []float64{10, 100}, []uint64{1, 2}, []float64{10.0}, 1, 1) |
| 113 | + return metric |
| 114 | + }, |
| 115 | + wantErr: false, |
| 116 | + }, |
| 117 | + { |
| 118 | + name: "scale summary metric", |
| 119 | + valueFunc: func() pmetric.Metric { |
| 120 | + metric := pmetric.NewMetric() |
| 121 | + dp := metric.SetEmptySummary().DataPoints().AppendEmpty() |
| 122 | + dp.SetSum(10.0) |
| 123 | + qv := dp.QuantileValues().AppendEmpty() |
| 124 | + qv.SetValue(10.0) |
| 125 | + |
| 126 | + return metric |
| 127 | + }, |
| 128 | + args: ScaleArguments{ |
| 129 | + Multiplier: 10.0, |
| 130 | + }, |
| 131 | + wantFunc: func() pmetric.Metric { |
| 132 | + metric := pmetric.NewMetric() |
| 133 | + dp := metric.SetEmptySummary().DataPoints().AppendEmpty() |
| 134 | + dp.SetSum(100.0) |
| 135 | + qv := dp.QuantileValues().AppendEmpty() |
| 136 | + qv.SetValue(100.0) |
| 137 | + |
| 138 | + return metric |
| 139 | + }, |
| 140 | + wantErr: false, |
| 141 | + }, |
| 142 | + { |
| 143 | + name: "unsupported: exponential histogram metric", |
| 144 | + valueFunc: func() pmetric.Metric { |
| 145 | + metric := pmetric.NewMetric() |
| 146 | + metric.SetEmptyExponentialHistogram() |
| 147 | + return metric |
| 148 | + }, |
| 149 | + args: ScaleArguments{ |
| 150 | + Multiplier: 10.0, |
| 151 | + }, |
| 152 | + wantFunc: func() pmetric.Metric { |
| 153 | + // value should not be modified |
| 154 | + metric := pmetric.NewMetric() |
| 155 | + metric.SetEmptyExponentialHistogram() |
| 156 | + return metric |
| 157 | + }, |
| 158 | + wantErr: true, |
| 159 | + }, |
| 160 | + } |
| 161 | + for _, tt := range tests { |
| 162 | + t.Run(tt.name, func(t *testing.T) { |
| 163 | + target := ottlmetric.NewTransformContext( |
| 164 | + tt.valueFunc(), |
| 165 | + pmetric.NewMetricSlice(), |
| 166 | + pcommon.NewInstrumentationScope(), |
| 167 | + pcommon.NewResource(), |
| 168 | + pmetric.NewScopeMetrics(), |
| 169 | + pmetric.NewResourceMetrics(), |
| 170 | + ) |
| 171 | + |
| 172 | + expressionFunc, _ := Scale(tt.args) |
| 173 | + _, err := expressionFunc(context.Background(), target) |
| 174 | + |
| 175 | + if tt.wantErr { |
| 176 | + assert.Error(t, err) |
| 177 | + } else { |
| 178 | + assert.NoError(t, err) |
| 179 | + } |
| 180 | + assert.EqualValues(t, tt.wantFunc(), target.GetMetric()) |
| 181 | + }) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func getTestScalingHistogramMetric(count uint64, sum, min, max float64, bounds []float64, bucketCounts []uint64, exemplars []float64, start, timestamp pcommon.Timestamp) pmetric.Metric { |
| 186 | + metric := pmetric.NewMetric() |
| 187 | + metric.SetName("test-metric") |
| 188 | + metric.SetEmptyHistogram() |
| 189 | + histogramDatapoint := metric.Histogram().DataPoints().AppendEmpty() |
| 190 | + histogramDatapoint.SetCount(count) |
| 191 | + histogramDatapoint.SetSum(sum) |
| 192 | + histogramDatapoint.SetMin(min) |
| 193 | + histogramDatapoint.SetMax(max) |
| 194 | + histogramDatapoint.ExplicitBounds().FromRaw(bounds) |
| 195 | + histogramDatapoint.BucketCounts().FromRaw(bucketCounts) |
| 196 | + for i := 0; i < len(exemplars); i++ { |
| 197 | + exemplar := histogramDatapoint.Exemplars().AppendEmpty() |
| 198 | + exemplar.SetTimestamp(1) |
| 199 | + exemplar.SetDoubleValue(exemplars[i]) |
| 200 | + } |
| 201 | + histogramDatapoint.SetStartTimestamp(start) |
| 202 | + histogramDatapoint.SetTimestamp(timestamp) |
| 203 | + return metric |
| 204 | +} |
0 commit comments