Skip to content

Commit 78a0ade

Browse files
committed
[chore] unexport structs in cumulativetodeltaprocessor
This commit unexports internal structs that are not part of the public API or configuration in the cumulativetodeltaprocessor component. Unexported structs: - State -> state (internal tracking state) - DeltaValue -> deltaValue (internal delta calculation) - ValuePoint -> valuePoint (internal value storage) - HistogramPoint -> histogramPoint (internal histogram data) - MetricPoint -> metricPoint (internal metric data) Added constructor functions and getter/setter methods to maintain functionality while keeping the structs unexported. Fixes #40641
1 parent 2c1fb33 commit 78a0ade

File tree

6 files changed

+222
-105
lines changed

6 files changed

+222
-105
lines changed

processor/cumulativetodeltaprocessor/internal/tracking/identity.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,53 @@ func (mi *MetricIdentity) IsFloatVal() bool {
7373
func (mi *MetricIdentity) IsSupportedMetricType() bool {
7474
return mi.MetricType == pmetric.MetricTypeSum || mi.MetricType == pmetric.MetricTypeHistogram
7575
}
76+
77+
// NewMetricIdentity creates a new MetricIdentity
78+
func NewMetricIdentity() MetricIdentity {
79+
return MetricIdentity{}
80+
}
81+
82+
// SetResource sets the Resource field
83+
func (mi *MetricIdentity) SetResource(resource pcommon.Resource) {
84+
mi.Resource = resource
85+
}
86+
87+
// SetInstrumentationLibrary sets the InstrumentationLibrary field
88+
func (mi *MetricIdentity) SetInstrumentationLibrary(scope pcommon.InstrumentationScope) {
89+
mi.InstrumentationLibrary = scope
90+
}
91+
92+
// SetMetricType sets the MetricType field
93+
func (mi *MetricIdentity) SetMetricType(metricType pmetric.MetricType) {
94+
mi.MetricType = metricType
95+
}
96+
97+
// SetMetricName sets the MetricName field
98+
func (mi *MetricIdentity) SetMetricName(name string) {
99+
mi.MetricName = name
100+
}
101+
102+
// SetMetricUnit sets the MetricUnit field
103+
func (mi *MetricIdentity) SetMetricUnit(unit string) {
104+
mi.MetricUnit = unit
105+
}
106+
107+
// SetMetricIsMonotonic sets the MetricIsMonotonic field
108+
func (mi *MetricIdentity) SetMetricIsMonotonic(monotonic bool) {
109+
mi.MetricIsMonotonic = monotonic
110+
}
111+
112+
// SetMetricValueType sets the MetricValueType field
113+
func (mi *MetricIdentity) SetMetricValueType(valueType pmetric.NumberDataPointValueType) {
114+
mi.MetricValueType = valueType
115+
}
116+
117+
// SetStartTimestamp sets the StartTimestamp field
118+
func (mi *MetricIdentity) SetStartTimestamp(timestamp pcommon.Timestamp) {
119+
mi.StartTimestamp = timestamp
120+
}
121+
122+
// SetAttributes sets the Attributes field
123+
func (mi *MetricIdentity) SetAttributes(attributes pcommon.Map) {
124+
mi.Attributes = attributes
125+
}

processor/cumulativetodeltaprocessor/internal/tracking/metric.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,15 @@
33

44
package tracking // import "github.com/open-telemetry/opentelemetry-collector-contrib/processor/cumulativetodeltaprocessor/internal/tracking"
55

6-
type MetricPoint struct {
6+
type metricPoint struct {
77
Identity MetricIdentity
8-
Value ValuePoint
8+
Value valuePoint
9+
}
10+
11+
// NewMetricPoint creates a new metricPoint
12+
func NewMetricPoint(identity MetricIdentity, value valuePoint) metricPoint {
13+
return metricPoint{
14+
Identity: identity,
15+
Value: value,
16+
}
917
}

processor/cumulativetodeltaprocessor/internal/tracking/tracker.go

Lines changed: 50 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,16 @@ var identityBufferPool = sync.Pool{
5959
},
6060
}
6161

62-
type State struct {
62+
type state struct {
6363
sync.Mutex
64-
PrevPoint ValuePoint
64+
prevPoint valuePoint
6565
}
6666

67-
type DeltaValue struct {
67+
type deltaValue struct {
6868
StartTimestamp pcommon.Timestamp
6969
FloatValue float64
7070
IntValue int64
71-
HistogramValue *HistogramPoint
71+
HistogramValue *histogramPoint
7272
}
7373

7474
func NewMetricTracker(ctx context.Context, logger *zap.Logger, maxStaleness time.Duration, initalValue InitialValue) *MetricTracker {
@@ -92,7 +92,7 @@ type MetricTracker struct {
9292
startTime pcommon.Timestamp
9393
}
9494

95-
func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
95+
func (t *MetricTracker) Convert(in metricPoint) (out deltaValue, valid bool) {
9696
metricID := in.Identity
9797
metricPoint := in.Value
9898
if !metricID.IsSupportedMetricType() {
@@ -112,8 +112,8 @@ func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
112112
hashableID := b.String()
113113
identityBufferPool.Put(b)
114114

115-
s, ok := t.states.LoadOrStore(hashableID, &State{
116-
PrevPoint: metricPoint,
115+
s, ok := t.states.LoadOrStore(hashableID, &state{
116+
prevPoint: metricPoint,
117117
})
118118
if !ok {
119119
switch metricID.MetricType {
@@ -141,16 +141,16 @@ func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
141141

142142
valid = true
143143

144-
state := s.(*State)
144+
state := s.(*state)
145145
state.Lock()
146146
defer state.Unlock()
147147

148-
out.StartTimestamp = state.PrevPoint.ObservedTimestamp
148+
out.StartTimestamp = state.prevPoint.ObservedTimestamp
149149

150150
switch metricID.MetricType {
151151
case pmetric.MetricTypeHistogram:
152152
value := metricPoint.HistogramValue
153-
prevValue := state.PrevPoint.HistogramValue
153+
prevValue := state.prevPoint.HistogramValue
154154
if math.IsNaN(value.Sum) {
155155
value.Sum = prevValue.Sum
156156
}
@@ -174,7 +174,7 @@ func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
174174
case pmetric.MetricTypeSum:
175175
if metricID.IsFloatVal() {
176176
value := metricPoint.FloatValue
177-
prevValue := state.PrevPoint.FloatValue
177+
prevValue := state.prevPoint.FloatValue
178178
delta := value - prevValue
179179

180180
// Detect reset (non-monotonic sums are not converted)
@@ -185,7 +185,7 @@ func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
185185
out.FloatValue = delta
186186
} else {
187187
value := metricPoint.IntValue
188-
prevValue := state.PrevPoint.IntValue
188+
prevValue := state.prevPoint.IntValue
189189
delta := value - prevValue
190190

191191
// Detect reset (non-monotonic sums are not converted)
@@ -198,13 +198,13 @@ func (t *MetricTracker) Convert(in MetricPoint) (out DeltaValue, valid bool) {
198198
case pmetric.MetricTypeEmpty, pmetric.MetricTypeGauge, pmetric.MetricTypeExponentialHistogram, pmetric.MetricTypeSummary:
199199
}
200200

201-
state.PrevPoint = metricPoint
201+
state.prevPoint = metricPoint
202202
return
203203
}
204204

205205
func (t *MetricTracker) removeStale(staleBefore pcommon.Timestamp) {
206206
t.states.Range(func(key, value any) bool {
207-
s := value.(*State)
207+
s := value.(*state)
208208

209209
// There is a known race condition here.
210210
// Because the state may be in the process of updating at the
@@ -220,7 +220,7 @@ func (t *MetricTracker) removeStale(staleBefore pcommon.Timestamp) {
220220
// not be persisted. The next update will load an entirely
221221
// new state.
222222
s.Lock()
223-
lastObserved := s.PrevPoint.ObservedTimestamp
223+
lastObserved := s.prevPoint.ObservedTimestamp
224224
s.Unlock()
225225
if lastObserved < staleBefore {
226226
t.logger.Debug("removing stale state key", zap.String("key", key.(string)))
@@ -243,3 +243,38 @@ func (t *MetricTracker) sweeper(ctx context.Context, remove func(pcommon.Timesta
243243
}
244244
}
245245
}
246+
247+
// GetStartTimestamp returns the StartTimestamp field of deltaValue
248+
func (dv *deltaValue) GetStartTimestamp() pcommon.Timestamp {
249+
return dv.StartTimestamp
250+
}
251+
252+
// GetFloatValue returns the FloatValue field of deltaValue
253+
func (dv *deltaValue) GetFloatValue() float64 {
254+
return dv.FloatValue
255+
}
256+
257+
// GetIntValue returns the IntValue field of deltaValue
258+
func (dv *deltaValue) GetIntValue() int64 {
259+
return dv.IntValue
260+
}
261+
262+
// GetHistogramValue returns the HistogramValue field of deltaValue
263+
func (dv *deltaValue) GetHistogramValue() *histogramPoint {
264+
return dv.HistogramValue
265+
}
266+
267+
// GetCount returns the Count field of histogramPoint
268+
func (hp *histogramPoint) GetCount() uint64 {
269+
return hp.Count
270+
}
271+
272+
// GetSum returns the Sum field of histogramPoint
273+
func (hp *histogramPoint) GetSum() float64 {
274+
return hp.Sum
275+
}
276+
277+
// GetBuckets returns the Buckets field of histogramPoint
278+
func (hp *histogramPoint) GetBuckets() []uint64 {
279+
return hp.Buckets
280+
}

0 commit comments

Comments
 (0)