Skip to content

Commit 1c9df03

Browse files
committed
Support deltatemporality
1 parent 260a599 commit 1c9df03

File tree

1 file changed

+36
-10
lines changed
  • connector/spanmetricsconnector/internal/metrics

1 file changed

+36
-10
lines changed

connector/spanmetricsconnector/internal/metrics/metrics.go

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ type explicitHistogram struct {
4747
bounds []float64
4848

4949
maxExemplarCount *int
50-
startTimestamp pcommon.Timestamp
50+
51+
startTimestamp pcommon.Timestamp
52+
lastSeenTimestamp pcommon.Timestamp
5153
}
5254

5355
type exponentialHistogram struct {
@@ -57,7 +59,9 @@ type exponentialHistogram struct {
5759
histogram *structure.Histogram[float64]
5860

5961
maxExemplarCount *int
60-
startTimestamp pcommon.Timestamp
62+
63+
startTimestamp pcommon.Timestamp
64+
lastSeenTimestamp pcommon.Timestamp
6165
}
6266

6367
type generateStartTimestamp = func(Key) pcommon.Timestamp
@@ -91,6 +95,7 @@ func (m *explicitHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Map,
9195
m.metrics[key] = h
9296
}
9397

98+
h.lastSeenTimestamp = startTimestamp
9499
return h
95100
}
96101

@@ -104,8 +109,14 @@ func (m *explicitHistogramMetrics) BuildMetrics(
104109
dps.EnsureCapacity(len(m.metrics))
105110
for _, h := range m.metrics {
106111
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)
107119
dp.SetTimestamp(timestamp)
108-
dp.SetStartTimestamp(h.startTimestamp)
109120
dp.ExplicitBounds().FromRaw(h.bounds)
110121
dp.BucketCounts().FromRaw(h.bucketCounts)
111122
dp.SetCount(h.count)
@@ -115,7 +126,6 @@ func (m *explicitHistogramMetrics) BuildMetrics(
115126
}
116127
h.exemplars.CopyTo(dp.Exemplars())
117128
h.attributes.CopyTo(dp.Attributes())
118-
dp.Attributes().PutInt("startTimestamp", int64(h.startTimestamp))
119129
}
120130
}
121131

@@ -143,6 +153,7 @@ func (m *exponentialHistogramMetrics) GetOrCreate(key Key, attributes pcommon.Ma
143153
m.metrics[key] = h
144154
}
145155

156+
h.lastSeenTimestamp = startTimeStamp
146157
return h
147158
}
148159

@@ -156,15 +167,20 @@ func (m *exponentialHistogramMetrics) BuildMetrics(
156167
dps.EnsureCapacity(len(m.metrics))
157168
for _, e := range m.metrics {
158169
dp := dps.AppendEmpty()
159-
dp.SetStartTimestamp(e.startTimestamp)
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)
160177
dp.SetTimestamp(timestamp)
161178
expoHistToExponentialDataPoint(e.histogram, dp)
162179
for i := 0; i < e.exemplars.Len(); i++ {
163180
e.exemplars.At(i).SetTimestamp(timestamp)
164181
}
165182
e.exemplars.CopyTo(dp.Exemplars())
166183
e.attributes.CopyTo(dp.Attributes())
167-
dp.Attributes().PutInt("startTimestamp", int64(e.startTimestamp))
168184
}
169185
}
170186

@@ -239,11 +255,14 @@ func (h *exponentialHistogram) AddExemplar(traceID pcommon.TraceID, spanID pcomm
239255
}
240256

241257
type Sum struct {
242-
attributes pcommon.Map
243-
count uint64
258+
attributes pcommon.Map
259+
count uint64
260+
244261
exemplars pmetric.ExemplarSlice
245262
maxExemplarCount *int
246-
startTimestamp pcommon.Timestamp
263+
264+
startTimestamp pcommon.Timestamp
265+
lastSeenTimestamp pcommon.Timestamp
247266
}
248267

249268
func (s *Sum) Add(value uint64) {
@@ -273,6 +292,7 @@ func (m *SumMetrics) GetOrCreate(key Key, attributes pcommon.Map, startTimestamp
273292
}
274293
m.metrics[key] = s
275294
}
295+
s.lastSeenTimestamp = startTimestamp
276296
return s
277297
}
278298

@@ -298,7 +318,13 @@ func (m *SumMetrics) BuildMetrics(
298318
dps.EnsureCapacity(len(m.metrics))
299319
for _, s := range m.metrics {
300320
dp := dps.AppendEmpty()
301-
dp.SetStartTimestamp(s.startTimestamp)
321+
var startTimeStamp pcommon.Timestamp
322+
if temporality == pmetric.AggregationTemporalityDelta {
323+
startTimeStamp = s.lastSeenTimestamp
324+
} else {
325+
startTimeStamp = s.startTimestamp
326+
}
327+
dp.SetStartTimestamp(startTimeStamp)
302328
dp.SetTimestamp(timestamp)
303329
dp.SetIntValue(int64(s.count))
304330
for i := 0; i < s.exemplars.Len(); i++ {

0 commit comments

Comments
 (0)