Skip to content

Commit 49a0f96

Browse files
authored
Upgrade to the latest proto version (#637)
This change brings the new additional nesting of proto data (InstrumentationLibrary).
1 parent 98973e7 commit 49a0f96

File tree

18 files changed

+552
-227
lines changed

18 files changed

+552
-227
lines changed

exporter/exporterhelper/tracehelper_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,8 @@ func checkRecordedMetricsForTraceExporterV2(t *testing.T, te exporter.TraceExpor
355355

356356
spans := make([]*data.Span, 2)
357357
rs := data.NewResourceSpans(nil, nil)
358-
rs.SetSpans(spans)
358+
ils := data.NewInstrumentationLibrarySpans(data.NewInstrumentationLibrary(), spans)
359+
rs.SetInstrumentationLibrarySpans([]*data.InstrumentationLibrarySpans{ils})
359360
td := data.NewTraceData([]*data.ResourceSpans{rs})
360361
ctx := observability.ContextWithReceiverName(context.Background(), fakeTraceReceiverName)
361362
const numBatches = 7
@@ -373,7 +374,8 @@ func checkRecordedMetricsForTraceExporterV2(t *testing.T, te exporter.TraceExpor
373374
func generateTraceV2Traffic(t *testing.T, te exporter.TraceExporterV2, numRequests int, wantError error) {
374375
spans := make([]*data.Span, 1)
375376
rs := data.NewResourceSpans(nil, nil)
376-
rs.SetSpans(spans)
377+
ils := data.NewInstrumentationLibrarySpans(data.NewInstrumentationLibrary(), spans)
378+
rs.SetInstrumentationLibrarySpans([]*data.InstrumentationLibrarySpans{ils})
377379
td := data.NewTraceData([]*data.ResourceSpans{rs})
378380
ctx, span := trace.StartSpan(context.Background(), fakeTraceParentSpanName, trace.WithSampler(trace.AlwaysSample()))
379381
defer span.End()

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ require (
2727
github.com/jaegertracing/jaeger v1.17.0
2828
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
2929
github.com/mitchellh/mapstructure v1.1.2
30-
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f
30+
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad
3131
github.com/openzipkin/zipkin-go v0.2.1
3232
github.com/orijtech/prometheus-go-metrics-exporter v0.0.3-0.20190313163149-b321c5297f60
3333
github.com/pavius/impi v0.0.0-20180302134524-c1cbdcb8df2b

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -507,8 +507,8 @@ github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
507507
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
508508
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
509509
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
510-
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f h1:o21WlujGsrjoN3+n99AflASF/K0S6+SOLOj2O9nNplc=
511-
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
510+
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad h1:b0fNt65HvoUg+uDe63CA08SGzKO/gzPvzQQfEFX8Oks=
511+
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
512512
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9/go.mod h1:PLldrQSroqzH70Xl+1DQcGnefIbqsKR7UDaiux3zV+w=
513513
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
514514
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=

internal/data/common.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,36 @@ type Attributes struct {
139139
func NewAttributes(m AttributesMap, droppedCount uint32) Attributes {
140140
return Attributes{m, droppedCount}
141141
}
142+
143+
// InstrumentationLibrary is a message representing the instrumentation library information.
144+
//
145+
// Must use NewResource functions to create new instances.
146+
// Important: zero-initialized instance is not valid for use.
147+
type InstrumentationLibrary struct {
148+
orig *otlpcommon.InstrumentationLibrary
149+
}
150+
151+
// NewInstrumentationLibrary creates a new InstrumentationLibrary.
152+
func NewInstrumentationLibrary() InstrumentationLibrary {
153+
return InstrumentationLibrary{}
154+
}
155+
156+
func newInstrumentationLibrary(orig *otlpcommon.InstrumentationLibrary) InstrumentationLibrary {
157+
return InstrumentationLibrary{orig}
158+
}
159+
160+
func (il InstrumentationLibrary) Name() string {
161+
return il.orig.Name
162+
}
163+
164+
func (il InstrumentationLibrary) SetName(r string) {
165+
il.orig.Name = r
166+
}
167+
168+
func (il InstrumentationLibrary) Version() string {
169+
return il.orig.Version
170+
}
171+
172+
func (il InstrumentationLibrary) SetVersion(r string) {
173+
il.orig.Version = r
174+
}

internal/data/metric.go

Lines changed: 133 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

126128
type 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

217219
func (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) {
337444
func (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

Comments
 (0)