Skip to content

Commit a1998f0

Browse files
FrapschenLucianoGiannotti
authored andcommitted
[connector/spanmetricsconnector] Initialise new calls_total metrics at 0 (open-telemetry#39052)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description fix: open-telemetry#38537 Signed-off-by: Murphy Chen <[email protected]>
1 parent ee4087d commit a1998f0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: spanmetricsconnector
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Initialise new calls_total metrics at 0
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [38537]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

connector/spanmetricsconnector/connector_test.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,13 @@ func verifyConsumeMetricsInput(tb testing.TB, input pmetric.Metrics, expectedTem
188188
require.Equal(tb, numDataPoints, callsDps.Len())
189189
for dpi := 0; dpi < numDataPoints; dpi++ {
190190
dp := callsDps.At(dpi)
191+
expectIntValue := numCumulativeConsumptions
192+
// this calls init value is 0 for the first Consumption.
193+
if numCumulativeConsumptions == 1 {
194+
expectIntValue = 0
195+
}
191196
assert.Equal(tb,
192-
int64(numCumulativeConsumptions),
197+
int64(expectIntValue),
193198
dp.IntValue(),
194199
"There should only be one metric per Service/name/kind combination",
195200
)

connector/spanmetricsconnector/internal/metrics/metrics.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,11 @@ type Sum struct {
241241
count uint64
242242
exemplars pmetric.ExemplarSlice
243243
maxExemplarCount *int
244+
// isFirst is used to track if this datapoint is new to the Sum. This
245+
// is used to ensure that new Sum metrics being with 0, and then are incremented
246+
// to the desired value. This avoids Prometheus throwing away the first
247+
// value in the series, due to the transition from null -> x.
248+
isFirst bool
244249
}
245250

246251
func (s *Sum) Add(value uint64) {
@@ -266,6 +271,7 @@ func (m *SumMetrics) GetOrCreate(key Key, attributes pcommon.Map) *Sum {
266271
attributes: attributes,
267272
exemplars: pmetric.NewExemplarSlice(),
268273
maxExemplarCount: m.maxExemplarCount,
274+
isFirst: true,
269275
}
270276
m.metrics[key] = s
271277
}
@@ -297,7 +303,12 @@ func (m *SumMetrics) BuildMetrics(
297303
dp := dps.AppendEmpty()
298304
dp.SetStartTimestamp(startTimestamp(k))
299305
dp.SetTimestamp(timestamp)
300-
dp.SetIntValue(int64(s.count))
306+
if s.isFirst {
307+
dp.SetIntValue(0)
308+
s.isFirst = false
309+
} else {
310+
dp.SetIntValue(int64(s.count))
311+
}
301312
for i := 0; i < s.exemplars.Len(); i++ {
302313
s.exemplars.At(i).SetTimestamp(timestamp)
303314
}

0 commit comments

Comments
 (0)