Skip to content

Commit fc56cd1

Browse files
committed
Fix startTimestamp for delta metrics
Apparently, for delta temporality, all the metrics collected are reset to empty state after consuming one round of data. Hence storing the lastseentimestamp within the metric doesn't work. This PR moves lastseentimestamp out of metric into a map (as it was stored previously) of
1 parent 1c9df03 commit fc56cd1

File tree

2 files changed

+30
-38
lines changed

2 files changed

+30
-38
lines changed

connector/spanmetricsconnector/connector.go

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,16 @@ func (p *connectorImp) buildMetrics() pmetric.Metrics {
279279
* - For delta metrics: (T1, T2), (T2, T3), (T3, T4) ...
280280
*/
281281
deltaMetricKeys := make(map[metrics.Key]bool)
282+
timeStampGenerator := func(mk metrics.Key, startTime pcommon.Timestamp) pcommon.Timestamp {
283+
if p.config.GetAggregationTemporality() == pmetric.AggregationTemporalityDelta {
284+
if lastTimestamp, ok := p.lastDeltaTimestamps.Get(mk); ok {
285+
startTime = lastTimestamp
286+
}
287+
// Collect lastDeltaTimestamps keys that need to be updated. Metrics can share the same key, so defer the update.
288+
deltaMetricKeys[mk] = true
289+
}
290+
return startTime
291+
}
282292

283293
metricsNamespace := p.config.Namespace
284294
if legacyMetricNamesFeatureGate.IsEnabled() && metricsNamespace == DefaultNamespace {
@@ -288,21 +298,21 @@ func (p *connectorImp) buildMetrics() pmetric.Metrics {
288298
sums := rawMetrics.sums
289299
metric := sm.Metrics().AppendEmpty()
290300
metric.SetName(buildMetricName(metricsNamespace, metricNameCalls))
291-
sums.BuildMetrics(metric, timestamp, p.config.GetAggregationTemporality())
301+
sums.BuildMetrics(metric, timestamp, timeStampGenerator, p.config.GetAggregationTemporality())
292302

293303
if !p.config.Histogram.Disable {
294304
histograms := rawMetrics.histograms
295305
metric = sm.Metrics().AppendEmpty()
296306
metric.SetName(buildMetricName(metricsNamespace, metricNameDuration))
297307
metric.SetUnit(p.config.Histogram.Unit.String())
298-
histograms.BuildMetrics(metric, timestamp, p.config.GetAggregationTemporality())
308+
histograms.BuildMetrics(metric, timestamp, timeStampGenerator, p.config.GetAggregationTemporality())
299309
}
300310

301311
events := rawMetrics.events
302312
if p.events.Enabled {
303313
metric = sm.Metrics().AppendEmpty()
304314
metric.SetName(buildMetricName(metricsNamespace, metricNameEvents))
305-
events.BuildMetrics(metric, timestamp, p.config.GetAggregationTemporality())
315+
events.BuildMetrics(metric, timestamp, timeStampGenerator, p.config.GetAggregationTemporality())
306316
}
307317

308318
for mk := range deltaMetricKeys {

connector/spanmetricsconnector/internal/metrics/metrics.go

Lines changed: 17 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ type Key string
1515

1616
type HistogramMetrics interface {
1717
GetOrCreate(key Key, attributes pcommon.Map, startTimestamp pcommon.Timestamp) Histogram
18-
BuildMetrics(pmetric.Metric, pcommon.Timestamp, pmetric.AggregationTemporality)
18+
BuildMetrics(pmetric.Metric, pcommon.Timestamp, func(Key, pcommon.Timestamp) pcommon.Timestamp, pmetric.AggregationTemporality)
1919
ClearExemplars()
2020
}
2121

@@ -48,8 +48,7 @@ type explicitHistogram struct {
4848

4949
maxExemplarCount *int
5050

51-
startTimestamp pcommon.Timestamp
52-
lastSeenTimestamp pcommon.Timestamp
51+
startTimestamp pcommon.Timestamp
5352
}
5453

5554
type exponentialHistogram struct {
@@ -60,8 +59,7 @@ type exponentialHistogram struct {
6059

6160
maxExemplarCount *int
6261

63-
startTimestamp pcommon.Timestamp
64-
lastSeenTimestamp pcommon.Timestamp
62+
startTimestamp pcommon.Timestamp
6563
}
6664

6765
type generateStartTimestamp = func(Key) pcommon.Timestamp
@@ -91,31 +89,26 @@ func (m *explicitHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Map,
9189
bounds: m.bounds,
9290
bucketCounts: make([]uint64, len(m.bounds)+1),
9391
maxExemplarCount: m.maxExemplarCount,
92+
startTimestamp: startTimestamp,
9493
}
9594
m.metrics[key] = h
9695
}
97-
98-
h.lastSeenTimestamp = startTimestamp
9996
return h
10097
}
10198

10299
func (m *explicitHistogramMetrics) BuildMetrics(
103100
metric pmetric.Metric,
104101
timestamp pcommon.Timestamp,
102+
startTimeStampGenerator func(Key, pcommon.Timestamp) pcommon.Timestamp,
105103
temporality pmetric.AggregationTemporality,
106104
) {
107105
metric.SetEmptyHistogram().SetAggregationTemporality(temporality)
108106
dps := metric.Histogram().DataPoints()
109107
dps.EnsureCapacity(len(m.metrics))
110-
for _, h := range m.metrics {
108+
for k, h := range m.metrics {
111109
dp := dps.AppendEmpty()
112-
var startTimeStamp pcommon.Timestamp
113-
if temporality == pmetric.AggregationTemporalityDelta {
114-
startTimeStamp = h.lastSeenTimestamp
115-
} else {
116-
startTimeStamp = h.startTimestamp
117-
}
118-
dp.SetStartTimestamp(startTimeStamp)
110+
startTimestamp := startTimeStampGenerator(k, h.startTimestamp)
111+
dp.SetStartTimestamp(startTimestamp)
119112
dp.SetTimestamp(timestamp)
120113
dp.ExplicitBounds().FromRaw(h.bounds)
121114
dp.BucketCounts().FromRaw(h.bucketCounts)
@@ -149,31 +142,26 @@ func (m *exponentialHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Ma
149142
attributes: attributes,
150143
exemplars: pmetric.NewExemplarSlice(),
151144
maxExemplarCount: m.maxExemplarCount,
145+
startTimestamp: startTimeStamp,
152146
}
153147
m.metrics[key] = h
154148
}
155-
156-
h.lastSeenTimestamp = startTimeStamp
157149
return h
158150
}
159151

160152
func (m *exponentialHistogramMetrics) BuildMetrics(
161153
metric pmetric.Metric,
162154
timestamp pcommon.Timestamp,
155+
startTimeStampGenerator func(Key, pcommon.Timestamp) pcommon.Timestamp,
163156
temporality pmetric.AggregationTemporality,
164157
) {
165158
metric.SetEmptyExponentialHistogram().SetAggregationTemporality(temporality)
166159
dps := metric.ExponentialHistogram().DataPoints()
167160
dps.EnsureCapacity(len(m.metrics))
168-
for _, e := range m.metrics {
161+
for k, e := range m.metrics {
169162
dp := dps.AppendEmpty()
170-
var startTimeStamp pcommon.Timestamp
171-
if temporality == pmetric.AggregationTemporalityDelta {
172-
startTimeStamp = e.lastSeenTimestamp
173-
} else {
174-
startTimeStamp = e.startTimestamp
175-
}
176-
dp.SetStartTimestamp(startTimeStamp)
163+
startTimestamp := startTimeStampGenerator(k, e.startTimestamp)
164+
dp.SetStartTimestamp(startTimestamp)
177165
dp.SetTimestamp(timestamp)
178166
expoHistToExponentialDataPoint(e.histogram, dp)
179167
for i := 0; i < e.exemplars.Len(); i++ {
@@ -261,8 +249,7 @@ type Sum struct {
261249
exemplars pmetric.ExemplarSlice
262250
maxExemplarCount *int
263251

264-
startTimestamp pcommon.Timestamp
265-
lastSeenTimestamp pcommon.Timestamp
252+
startTimestamp pcommon.Timestamp
266253
}
267254

268255
func (s *Sum) Add(value uint64) {
@@ -292,7 +279,6 @@ func (m *SumMetrics) GetOrCreate(key Key, attributes pcommon.Map, startTimestamp
292279
}
293280
m.metrics[key] = s
294281
}
295-
s.lastSeenTimestamp = startTimestamp
296282
return s
297283
}
298284

@@ -309,21 +295,17 @@ func (s *Sum) AddExemplar(traceID pcommon.TraceID, spanID pcommon.SpanID, value
309295
func (m *SumMetrics) BuildMetrics(
310296
metric pmetric.Metric,
311297
timestamp pcommon.Timestamp,
298+
startTimeStampGenerator func(Key, pcommon.Timestamp) pcommon.Timestamp,
312299
temporality pmetric.AggregationTemporality,
313300
) {
314301
metric.SetEmptySum().SetIsMonotonic(true)
315302
metric.Sum().SetAggregationTemporality(temporality)
316303

317304
dps := metric.Sum().DataPoints()
318305
dps.EnsureCapacity(len(m.metrics))
319-
for _, s := range m.metrics {
306+
for k, s := range m.metrics {
320307
dp := dps.AppendEmpty()
321-
var startTimeStamp pcommon.Timestamp
322-
if temporality == pmetric.AggregationTemporalityDelta {
323-
startTimeStamp = s.lastSeenTimestamp
324-
} else {
325-
startTimeStamp = s.startTimestamp
326-
}
308+
startTimeStamp := startTimeStampGenerator(k, s.startTimestamp)
327309
dp.SetStartTimestamp(startTimeStamp)
328310
dp.SetTimestamp(timestamp)
329311
dp.SetIntValue(int64(s.count))

0 commit comments

Comments
 (0)