Skip to content

Commit 30f1f2e

Browse files
committed
simplify
1 parent bd94bcc commit 30f1f2e

File tree

8 files changed

+161
-191
lines changed

8 files changed

+161
-191
lines changed

sdk/metric/internal/aggregate/exponential_histogram.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,13 @@ func newExpoHistogramDataPoint[N int64 | float64](
5151
noMinMax, noSum bool,
5252
) *expoHistogramDataPoint[N] { // nolint:revive // we need this control flag
5353
dp := &expoHistogramDataPoint[N]{
54-
attrs: attrs,
55-
maxSize: maxSize,
56-
noMinMax: noMinMax,
57-
noSum: noSum,
54+
attrs: attrs,
55+
maxSize: maxSize,
56+
noMinMax: noMinMax,
57+
noSum: noSum,
58+
startTime: now(),
5859
}
5960
dp.scale.Store(maxScale)
60-
if x.PerSeriesStartTimestamps.Enabled() {
61-
dp.startTime = now()
62-
}
6361
return dp
6462
}
6563

sdk/metric/internal/aggregate/exponential_histogram_test.go

Lines changed: 23 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515

1616
"go.opentelemetry.io/otel/attribute"
1717
"go.opentelemetry.io/otel/internal/global"
18+
"go.opentelemetry.io/otel/sdk/internal/x"
1819
"go.opentelemetry.io/otel/sdk/metric/metricdata"
1920
)
2021

@@ -722,6 +723,8 @@ func TestSubNormal(t *testing.T) {
722723
ehdp.record(math.SmallestNonzeroFloat64)
723724
ehdp.record(math.SmallestNonzeroFloat64)
724725

726+
want.startTime = ehdp.startTime
727+
725728
assert.Equal(t, want, ehdp)
726729
}
727730

@@ -737,25 +740,29 @@ func TestExponentialHistogramAggregation(t *testing.T) {
737740

738741
t.Run("Int64/Cumulative", func(t *testing.T) {
739742
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
740-
testCumulativeExpoHist[int64](false)(t)
743+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
744+
testCumulativeExpoHist[int64]()(t)
741745
})
742746
c.Reset()
743747

744748
t.Run("Int64/Cumulative/PerSeriesStartTimeEnabled", func(t *testing.T) {
745749
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
746-
testCumulativeExpoHist[int64](true)(t)
750+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
751+
testCumulativeExpoHist[int64]()(t)
747752
})
748753
c.Reset()
749754

750755
t.Run("Float64/Cumulative", func(t *testing.T) {
751756
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
752-
testCumulativeExpoHist[float64](false)(t)
757+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
758+
testCumulativeExpoHist[float64]()(t)
753759
})
754760
c.Reset()
755761

756762
t.Run("Float64/Cumulative/PerSeriesStartTimeEnabled", func(t *testing.T) {
757763
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
758-
testCumulativeExpoHist[float64](true)(t)
764+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
765+
testCumulativeExpoHist[float64]()(t)
759766
})
760767
c.Reset()
761768
}
@@ -796,7 +803,7 @@ func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) {
796803
{
797804
Attributes: fltrAlice,
798805
StartTime: y2kPlus(1),
799-
Time: y2kPlus(2),
806+
Time: y2kPlus(3),
800807
Count: 7,
801808
Min: metricdata.NewExtrema[N](-1),
802809
Max: metricdata.NewExtrema[N](16),
@@ -850,8 +857,8 @@ func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) {
850857
DataPoints: []metricdata.ExponentialHistogramDataPoint[N]{
851858
{
852859
Attributes: fltrAlice,
853-
StartTime: y2kPlus(3),
854-
Time: y2kPlus(4),
860+
StartTime: y2kPlus(4),
861+
Time: y2kPlus(7),
855862
Count: 7,
856863
Min: metricdata.NewExtrema[N](-1),
857864
Max: metricdata.NewExtrema[N](16),
@@ -868,8 +875,8 @@ func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) {
868875
},
869876
{
870877
Attributes: overflowSet,
871-
StartTime: y2kPlus(3),
872-
Time: y2kPlus(4),
878+
StartTime: y2kPlus(4),
879+
Time: y2kPlus(7),
873880
Count: 6,
874881
Min: metricdata.NewExtrema[N](1),
875882
Max: metricdata.NewExtrema[N](16),
@@ -887,8 +894,7 @@ func testDeltaExpoHist[N int64 | float64]() func(t *testing.T) {
887894
})
888895
}
889896

890-
//nolint:revive // perSeriesStartTimeEnabled used to test a feature gate.
891-
func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) func(t *testing.T) {
897+
func testCumulativeExpoHist[N int64 | float64]() func(t *testing.T) {
892898
in, out := Builder[N]{
893899
Temporality: metricdata.CumulativeTemporality,
894900
Filter: attrFltr,
@@ -898,19 +904,9 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
898904
aliceStartTime := y2kPlus(0)
899905
overflowStartTime := y2kPlus(0)
900906

901-
timeStep1 := y2kPlus(2)
902-
timeStep2 := y2kPlus(3)
903-
timeStep3 := y2kPlus(4)
904-
timeStep4 := y2kPlus(5)
905-
906-
if perSeriesStartTimeEnabled {
907+
if x.PerSeriesStartTimestamps.Enabled() {
907908
aliceStartTime = y2kPlus(2)
908909
overflowStartTime = y2kPlus(6)
909-
910-
timeStep1 = y2kPlus(3)
911-
timeStep2 = y2kPlus(4)
912-
timeStep3 = y2kPlus(5)
913-
timeStep4 = y2kPlus(7)
914910
}
915911

916912
ctx := context.Background()
@@ -943,7 +939,7 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
943939
{
944940
Attributes: fltrAlice,
945941
StartTime: aliceStartTime,
946-
Time: timeStep1,
942+
Time: y2kPlus(3),
947943
Count: 7,
948944
Min: metricdata.NewExtrema[N](-1),
949945
Max: metricdata.NewExtrema[N](16),
@@ -976,7 +972,7 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
976972
{
977973
Attributes: fltrAlice,
978974
StartTime: aliceStartTime,
979-
Time: timeStep2,
975+
Time: y2kPlus(4),
980976
Count: 10,
981977
Min: metricdata.NewExtrema[N](-1),
982978
Max: metricdata.NewExtrema[N](16),
@@ -1005,7 +1001,7 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
10051001
{
10061002
Attributes: fltrAlice,
10071003
StartTime: aliceStartTime,
1008-
Time: timeStep3,
1004+
Time: y2kPlus(5),
10091005
Count: 10,
10101006
Min: metricdata.NewExtrema[N](-1),
10111007
Max: metricdata.NewExtrema[N](16),
@@ -1042,7 +1038,7 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
10421038
{
10431039
Attributes: fltrAlice,
10441040
StartTime: aliceStartTime,
1045-
Time: timeStep4,
1041+
Time: y2kPlus(7),
10461042
Count: 10,
10471043
Min: metricdata.NewExtrema[N](-1),
10481044
Max: metricdata.NewExtrema[N](16),
@@ -1060,7 +1056,7 @@ func testCumulativeExpoHist[N int64 | float64](perSeriesStartTimeEnabled bool) f
10601056
{
10611057
Attributes: overflowSet,
10621058
StartTime: overflowStartTime,
1063-
Time: timeStep4,
1059+
Time: y2kPlus(7),
10641060
Count: 6,
10651061
Min: metricdata.NewExtrema[N](1),
10661062
Max: metricdata.NewExtrema[N](16),

sdk/metric/internal/aggregate/histogram.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,10 +282,6 @@ func (s *cumulativeHistogram[N]) measure(
282282
droppedAttr []attribute.KeyValue,
283283
) {
284284
h := s.values.LoadOrStoreAttr(fltrAttr, func(attr attribute.Set) any {
285-
var startTime time.Time
286-
if x.PerSeriesStartTimestamps.Enabled() {
287-
startTime = now()
288-
}
289285
hPt := &hotColdHistogramPoint[N]{
290286
res: s.newRes(attr),
291287
attrs: attr,
@@ -304,7 +300,7 @@ func (s *cumulativeHistogram[N]) measure(
304300
counts: make([]atomic.Uint64, len(s.bounds)+1),
305301
},
306302
},
307-
startTime: startTime,
303+
startTime: now(),
308304
}
309305
return hPt
310306
}).(*hotColdHistogramPoint[N])

sdk/metric/internal/aggregate/histogram_test.go

Lines changed: 29 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"github.com/stretchr/testify/require"
1414

1515
"go.opentelemetry.io/otel/attribute"
16+
"go.opentelemetry.io/otel/sdk/internal/x"
1617
"go.opentelemetry.io/otel/sdk/metric/metricdata"
1718
"go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"
1819
)
@@ -37,44 +38,51 @@ func TestHistogram(t *testing.T) {
3738

3839
t.Run("Int64/Cumulative/Sum", func(t *testing.T) {
3940
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
40-
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64], perSeriesStartTimeEnabled: false})(t)
41+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
42+
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64]})(t)
4143
})
4244
c.Reset()
4345

4446
t.Run("Int64/Cumulative/Sum/PerSeriesStartTimeEnabled", func(t *testing.T) {
4547
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
46-
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64], perSeriesStartTimeEnabled: true})(t)
48+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
49+
testCumulativeHist[int64](conf[int64]{hPt: hPointSummed[int64]})(t)
4750
})
4851
c.Reset()
4952

5053
t.Run("Int64/Cumulative/NoSum", func(t *testing.T) {
5154
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
52-
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64], perSeriesStartTimeEnabled: false})(t)
55+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
56+
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64]})(t)
5357
})
5458
c.Reset()
5559

5660
t.Run("Int64/Cumulative/NoSum/PerSeriesStartTimeEnabled", func(t *testing.T) {
5761
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
58-
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64], perSeriesStartTimeEnabled: true})(t)
62+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
63+
testCumulativeHist[int64](conf[int64]{noSum: true, hPt: hPoint[int64]})(t)
5964
})
6065
c.Reset()
6166

6267
t.Run("Float64/Cumulative/Sum", func(t *testing.T) {
6368
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
64-
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64], perSeriesStartTimeEnabled: false})(t)
69+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
70+
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64]})(t)
6571
})
6672
c.Reset()
6773

6874
t.Run("Float64/Cumulative/Sum/PerSeriesStartTimeEnabled", func(t *testing.T) {
6975
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
70-
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64], perSeriesStartTimeEnabled: true})(t)
76+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
77+
testCumulativeHist[float64](conf[float64]{hPt: hPointSummed[float64]})(t)
7178
})
7279
c.Reset()
7380

7481
t.Run("Float64/Cumulative/NoSum", func(t *testing.T) {
7582
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "false")
83+
assert.False(t, x.PerSeriesStartTimestamps.Enabled())
7684
testCumulativeHist[float64](
77-
conf[float64]{noSum: true, hPt: hPoint[float64], perSeriesStartTimeEnabled: false},
85+
conf[float64]{noSum: true, hPt: hPoint[float64]},
7886
)(
7987
t,
8088
)
@@ -83,8 +91,9 @@ func TestHistogram(t *testing.T) {
8391

8492
t.Run("Float64/Cumulative/NoSum/PerSeriesStartTimeEnabled", func(t *testing.T) {
8593
t.Setenv("OTEL_GO_X_PER_SERIES_START_TIMESTAMPS", "true")
94+
assert.True(t, x.PerSeriesStartTimestamps.Enabled())
8695
testCumulativeHist[float64](
87-
conf[float64]{noSum: true, hPt: hPoint[float64], perSeriesStartTimeEnabled: true},
96+
conf[float64]{noSum: true, hPt: hPoint[float64]},
8897
)(
8998
t,
9099
)
@@ -93,9 +102,8 @@ func TestHistogram(t *testing.T) {
93102
}
94103

95104
type conf[N int64 | float64] struct {
96-
noSum bool
97-
hPt func(attribute.Set, N, uint64, time.Time, time.Time) metricdata.HistogramDataPoint[N]
98-
perSeriesStartTimeEnabled bool
105+
noSum bool
106+
hPt func(attribute.Set, N, uint64, time.Time, time.Time) metricdata.HistogramDataPoint[N]
99107
}
100108

101109
func testDeltaHist[N int64 | float64](c conf[N]) func(t *testing.T) {
@@ -196,20 +204,10 @@ func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) {
196204
bobStartTime := y2kPlus(0)
197205
overflowStartTime := y2kPlus(0)
198206

199-
timeStep1 := y2kPlus(2)
200-
timeStep2 := y2kPlus(3)
201-
timeStep3 := y2kPlus(4)
202-
timeStep4 := y2kPlus(5)
203-
204-
if c.perSeriesStartTimeEnabled {
207+
if x.PerSeriesStartTimestamps.Enabled() {
205208
aliceStartTime = y2kPlus(2)
206209
bobStartTime = y2kPlus(3)
207210
overflowStartTime = y2kPlus(7)
208-
209-
timeStep1 = y2kPlus(4)
210-
timeStep2 = y2kPlus(5)
211-
timeStep3 = y2kPlus(6)
212-
timeStep4 = y2kPlus(8)
213211
}
214212

215213
ctx := context.Background()
@@ -237,8 +235,8 @@ func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) {
237235
agg: metricdata.Histogram[N]{
238236
Temporality: metricdata.CumulativeTemporality,
239237
DataPoints: []metricdata.HistogramDataPoint[N]{
240-
c.hPt(fltrAlice, 2, 3, aliceStartTime, timeStep1),
241-
c.hPt(fltrBob, 10, 2, bobStartTime, timeStep1),
238+
c.hPt(fltrAlice, 2, 3, aliceStartTime, y2kPlus(4)),
239+
c.hPt(fltrBob, 10, 2, bobStartTime, y2kPlus(4)),
242240
},
243241
},
244242
},
@@ -253,8 +251,8 @@ func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) {
253251
agg: metricdata.Histogram[N]{
254252
Temporality: metricdata.CumulativeTemporality,
255253
DataPoints: []metricdata.HistogramDataPoint[N]{
256-
c.hPt(fltrAlice, 2, 4, aliceStartTime, timeStep2),
257-
c.hPt(fltrBob, 10, 3, bobStartTime, timeStep2),
254+
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(5)),
255+
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(5)),
258256
},
259257
},
260258
},
@@ -266,8 +264,8 @@ func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) {
266264
agg: metricdata.Histogram[N]{
267265
Temporality: metricdata.CumulativeTemporality,
268266
DataPoints: []metricdata.HistogramDataPoint[N]{
269-
c.hPt(fltrAlice, 2, 4, aliceStartTime, timeStep3),
270-
c.hPt(fltrBob, 10, 3, bobStartTime, timeStep3),
267+
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(6)),
268+
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(6)),
271269
},
272270
},
273271
},
@@ -283,9 +281,9 @@ func testCumulativeHist[N int64 | float64](c conf[N]) func(t *testing.T) {
283281
agg: metricdata.Histogram[N]{
284282
Temporality: metricdata.CumulativeTemporality,
285283
DataPoints: []metricdata.HistogramDataPoint[N]{
286-
c.hPt(fltrAlice, 2, 4, aliceStartTime, timeStep4),
287-
c.hPt(fltrBob, 10, 3, bobStartTime, timeStep4),
288-
c.hPt(overflowSet, 1, 2, overflowStartTime, timeStep4),
284+
c.hPt(fltrAlice, 2, 4, aliceStartTime, y2kPlus(8)),
285+
c.hPt(fltrBob, 10, 3, bobStartTime, y2kPlus(8)),
286+
c.hPt(overflowSet, 1, 2, overflowStartTime, y2kPlus(8)),
289287
},
290288
},
291289
},

sdk/metric/internal/aggregate/lastvalue.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,10 @@ func (s *lastValueMap[N]) measure(
3333
droppedAttr []attribute.KeyValue,
3434
) {
3535
lv := s.values.LoadOrStoreAttr(fltrAttr, func(attr attribute.Set) any {
36-
var startTime time.Time
37-
if x.PerSeriesStartTimestamps.Enabled() {
38-
startTime = now()
39-
}
4036
return &lastValuePoint[N]{
4137
res: s.newRes(attr),
4238
attrs: attr,
43-
startTime: startTime,
39+
startTime: now(),
4440
}
4541
}).(*lastValuePoint[N])
4642

0 commit comments

Comments
 (0)