@@ -77,7 +77,9 @@ func (md MetricData) MetricCount() int {
7777 metricCount := 0
7878 // TODO: Do not access internal members, add a metricCount to ResourceMetrics.
7979 for _ , rm := range md .pimpl .resourceMetrics {
80- metricCount += len (rm .pimpl .metrics )
80+ for _ , ilm := range rm .pimpl .instrumentationLibraryMetrics {
81+ metricCount += len (ilm .pimpl .metrics )
82+ }
8183 }
8284 return metricCount
8385}
@@ -124,8 +126,8 @@ type ResourceMetrics struct {
124126}
125127
126128type internalResourceMetrics struct {
127- resource * Resource
128- metrics [] Metric
129+ resource * Resource
130+ instrumentationLibraryMetrics [] InstrumentationLibraryMetrics
129131 // True when the slice was replace.
130132 sliceChanged bool
131133 // True if the pimpl was initialized.
@@ -174,14 +176,14 @@ func (rm ResourceMetrics) SetResource(r *Resource) {
174176 rm .pimpl .resource = r
175177}
176178
177- func (rm ResourceMetrics ) Metrics () []Metric {
179+ func (rm ResourceMetrics ) InstrumentationLibraryMetrics () []InstrumentationLibraryMetrics {
178180 rm .initInternallIfNeeded ()
179- return rm .pimpl .metrics
181+ return rm .pimpl .instrumentationLibraryMetrics
180182}
181183
182- func (rm ResourceMetrics ) SetMetrics (s []Metric ) {
184+ func (rm ResourceMetrics ) SetInstrumentationLibraryMetrics (s []InstrumentationLibraryMetrics ) {
183185 rm .initInternallIfNeeded ()
184- rm .pimpl .metrics = s
186+ rm .pimpl .instrumentationLibraryMetrics = s
185187 // We don't update the orig slice because this may be called multiple times.
186188 rm .pimpl .sliceChanged = true
187189}
@@ -201,27 +203,132 @@ func (rm ResourceMetrics) flushInternal() {
201203 if rm .pimpl .sliceChanged {
202204 // Reconstruct the slice because we don't know what elements were removed/added.
203205 // User may have changed internal fields in any Metric, flush all of them.
204- rm .orig .Metrics = make ([]* otlpmetrics.Metric , len (rm .pimpl .metrics ))
205- for i := range rm .pimpl .metrics {
206- rm .orig .Metrics [i ] = rm .pimpl .metrics [i ].getOrig ()
207- rm .pimpl .metrics [i ].flushInternal ()
206+ rm .orig .InstrumentationLibraryMetrics = make ([]* otlpmetrics.InstrumentationLibraryMetrics , len (rm .pimpl .instrumentationLibraryMetrics ))
207+ for i := range rm .pimpl .instrumentationLibraryMetrics {
208+ rm .orig .InstrumentationLibraryMetrics [i ] = rm .pimpl .instrumentationLibraryMetrics [i ].getOrig ()
209+ rm .pimpl .instrumentationLibraryMetrics [i ].flushInternal ()
208210 }
209211 } else {
210212 // User may have changed internal fields in any Metric, flush all of them.
211- for i := range rm .pimpl .metrics {
212- rm .pimpl .metrics [i ].flushInternal ()
213+ for i := range rm .pimpl .instrumentationLibraryMetrics {
214+ rm .pimpl .instrumentationLibraryMetrics [i ].flushInternal ()
213215 }
214216 }
215217}
216218
217219func (rm ResourceMetrics ) initInternallIfNeeded () {
218220 if ! rm .pimpl .initialized {
219221 rm .pimpl .resource = newResource (rm .orig .Resource )
220- rm .pimpl .metrics = newMetricSliceFromOrig (rm .orig .Metrics )
222+ rm .pimpl .instrumentationLibraryMetrics = newInstrumentationLibraryMetricsSliceFromOrig (rm .orig .InstrumentationLibraryMetrics )
221223 rm .pimpl .initialized = true
222224 }
223225}
224226
227+ // InstrumentationLibraryMetrics is a collection of metrics from a Resource.
228+ //
229+ // Must use NewResourceMetrics functions to create new instances.
230+ // Important: zero-initialized instance is not valid for use.
231+ type InstrumentationLibraryMetrics struct {
232+ // Wrap OTLP InstrumentationLibraryMetric.
233+ orig * otlpmetrics.InstrumentationLibraryMetrics
234+
235+ // Override a few fields. These fields are the source of truth. Their counterparts
236+ // stored in corresponding fields of "orig" are ignored.
237+ pimpl * internalInstrumentationLibraryMetrics
238+ }
239+
240+ type internalInstrumentationLibraryMetrics struct {
241+ instrumentationLibrary InstrumentationLibrary
242+ metrics []Metric
243+ // True when the slice was replace.
244+ sliceChanged bool
245+ // True if the pimpl was initialized.
246+ initialized bool
247+ }
248+
249+ // NewInstrumentationLibraryMetricsSlice creates a slice of InstrumentationLibraryMetrics that are correctly initialized.
250+ func NewInstrumentationLibraryMetricsSlice (len int ) []InstrumentationLibraryMetrics {
251+ // Slice for underlying orig.
252+ origs := make ([]otlpmetrics.InstrumentationLibraryMetrics , len )
253+ // Slice for underlying pimpl.
254+ pimpls := make ([]internalInstrumentationLibraryMetrics , len )
255+ // Slice for wrappers.
256+ wrappers := make ([]InstrumentationLibraryMetrics , len )
257+ for i := range origs {
258+ wrappers [i ].orig = & origs [i ]
259+ wrappers [i ].pimpl = & pimpls [i ]
260+ }
261+ return wrappers
262+ }
263+
264+ func newInstrumentationLibraryMetricsSliceFromOrig (origs []* otlpmetrics.InstrumentationLibraryMetrics ) []InstrumentationLibraryMetrics {
265+ // Slice for underlying pimpl.
266+ pimpls := make ([]internalInstrumentationLibraryMetrics , len (origs ))
267+ // Slice for wrappers.
268+ wrappers := make ([]InstrumentationLibraryMetrics , len (origs ))
269+ for i := range origs {
270+ wrappers [i ].orig = origs [i ]
271+ wrappers [i ].pimpl = & pimpls [i ]
272+ }
273+ return wrappers
274+ }
275+
276+ func (ilm InstrumentationLibraryMetrics ) InstrumentationLibrary () InstrumentationLibrary {
277+ ilm .initInternallIfNeeded ()
278+ return ilm .pimpl .instrumentationLibrary
279+ }
280+
281+ func (ilm InstrumentationLibraryMetrics ) SetResource (il InstrumentationLibrary ) {
282+ ilm .initInternallIfNeeded ()
283+ ilm .pimpl .instrumentationLibrary = il
284+ }
285+
286+ func (ilm InstrumentationLibraryMetrics ) Metrics () []Metric {
287+ ilm .initInternallIfNeeded ()
288+ return ilm .pimpl .metrics
289+ }
290+
291+ func (ilm InstrumentationLibraryMetrics ) SetMetrics (ms []Metric ) {
292+ ilm .initInternallIfNeeded ()
293+ ilm .pimpl .metrics = ms
294+ // We don't update the orig slice because this may be called multiple times.
295+ ilm .pimpl .sliceChanged = true
296+ }
297+
298+ func (ilm InstrumentationLibraryMetrics ) initInternallIfNeeded () {
299+ if ! ilm .pimpl .initialized {
300+ ilm .pimpl .instrumentationLibrary = newInstrumentationLibrary (ilm .orig .InstrumentationLibrary )
301+ ilm .pimpl .metrics = newMetricSliceFromOrig (ilm .orig .Metrics )
302+ ilm .pimpl .initialized = true
303+ }
304+ }
305+
306+ func (ilm InstrumentationLibraryMetrics ) getOrig () * otlpmetrics.InstrumentationLibraryMetrics {
307+ return ilm .orig
308+ }
309+
310+ func (ilm InstrumentationLibraryMetrics ) flushInternal () {
311+ if ! ilm .pimpl .initialized {
312+ // Guaranteed no changes via internal fields.
313+ return
314+ }
315+
316+ if ilm .pimpl .sliceChanged {
317+ // Reconstruct the slice because we don't know what elements were removed/added.
318+ // User may have changed internal fields in any Metric, flush all of them.
319+ ilm .orig .Metrics = make ([]* otlpmetrics.Metric , len (ilm .pimpl .metrics ))
320+ for i := range ilm .pimpl .metrics {
321+ ilm .orig .Metrics [i ] = ilm .pimpl .metrics [i ].getOrig ()
322+ ilm .pimpl .metrics [i ].flushInternal ()
323+ }
324+ } else {
325+ // User may have changed internal fields in any Metric, flush all of them.
326+ for i := range ilm .pimpl .metrics {
327+ ilm .pimpl .metrics [i ].flushInternal ()
328+ }
329+ }
330+ }
331+
225332// Metric defines a metric which has a descriptor and one or more timeseries points.
226333//
227334// Must use NewMetric* functions to create new instances.
@@ -337,10 +444,10 @@ func (m Metric) SetSummaryDataPoints(v []SummaryDataPoint) {
337444func (m Metric ) initInternallIfNeeded () {
338445 if ! m .pimpl .initialized {
339446 m .pimpl .metricDescriptor = newMetricDescriptorFromOrig (m .orig .MetricDescriptor )
340- m .pimpl .int64DataPoints = newInt64DataPointSliceFromOrig (m .orig .Int64Datapoints )
341- m .pimpl .doubleDataPoints = newDoubleDataPointSliceFormOrgig (m .orig .DoubleDatapoints )
342- m .pimpl .histogramDataPoints = newHistogramDataPointSliceFromOrig (m .orig .HistogramDatapoints )
343- m .pimpl .summaryDataPoints = newSummaryDataPointSliceFromOrig (m .orig .SummaryDatapoints )
447+ m .pimpl .int64DataPoints = newInt64DataPointSliceFromOrig (m .orig .Int64DataPoints )
448+ m .pimpl .doubleDataPoints = newDoubleDataPointSliceFormOrgig (m .orig .DoubleDataPoints )
449+ m .pimpl .histogramDataPoints = newHistogramDataPointSliceFromOrig (m .orig .HistogramDataPoints )
450+ m .pimpl .summaryDataPoints = newSummaryDataPointSliceFromOrig (m .orig .SummaryDataPoints )
344451 m .pimpl .initialized = true
345452 }
346453}
@@ -384,33 +491,33 @@ func (m Metric) flushInternal() {
384491 }
385492
386493 if len (m .pimpl .int64DataPoints ) != 0 {
387- m .orig .Int64Datapoints = make ([]* otlpmetrics.Int64DataPoint , len (m .pimpl .int64DataPoints ))
494+ m .orig .Int64DataPoints = make ([]* otlpmetrics.Int64DataPoint , len (m .pimpl .int64DataPoints ))
388495 for i := range m .pimpl .int64DataPoints {
389- m .orig .Int64Datapoints [i ] = m .pimpl .int64DataPoints [i ].getOrig ()
496+ m .orig .Int64DataPoints [i ] = m .pimpl .int64DataPoints [i ].getOrig ()
390497 m .pimpl .int64DataPoints [i ].flushInternal ()
391498 }
392499 }
393500
394501 if len (m .pimpl .doubleDataPoints ) != 0 {
395- m .orig .DoubleDatapoints = make ([]* otlpmetrics.DoubleDataPoint , len (m .pimpl .doubleDataPoints ))
502+ m .orig .DoubleDataPoints = make ([]* otlpmetrics.DoubleDataPoint , len (m .pimpl .doubleDataPoints ))
396503 for i := range m .pimpl .doubleDataPoints {
397- m .orig .DoubleDatapoints [i ] = m .pimpl .doubleDataPoints [i ].getOrig ()
504+ m .orig .DoubleDataPoints [i ] = m .pimpl .doubleDataPoints [i ].getOrig ()
398505 m .pimpl .doubleDataPoints [i ].flushInternal ()
399506 }
400507 }
401508
402509 if len (m .pimpl .histogramDataPoints ) != 0 {
403- m .orig .HistogramDatapoints = make ([]* otlpmetrics.HistogramDataPoint , len (m .pimpl .histogramDataPoints ))
510+ m .orig .HistogramDataPoints = make ([]* otlpmetrics.HistogramDataPoint , len (m .pimpl .histogramDataPoints ))
404511 for i := range m .pimpl .histogramDataPoints {
405- m .orig .HistogramDatapoints [i ] = m .pimpl .histogramDataPoints [i ].getOrig ()
512+ m .orig .HistogramDataPoints [i ] = m .pimpl .histogramDataPoints [i ].getOrig ()
406513 m .pimpl .histogramDataPoints [i ].flushInternal ()
407514 }
408515 }
409516
410517 if len (m .pimpl .summaryDataPoints ) != 0 {
411- m .orig .SummaryDatapoints = make ([]* otlpmetrics.SummaryDataPoint , len (m .pimpl .summaryDataPoints ))
518+ m .orig .SummaryDataPoints = make ([]* otlpmetrics.SummaryDataPoint , len (m .pimpl .summaryDataPoints ))
412519 for i := range m .pimpl .summaryDataPoints {
413- m .orig .SummaryDatapoints [i ] = m .pimpl .summaryDataPoints [i ].getOrig ()
520+ m .orig .SummaryDataPoints [i ] = m .pimpl .summaryDataPoints [i ].getOrig ()
414521 m .pimpl .summaryDataPoints [i ].flushInternal ()
415522 }
416523
0 commit comments