Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions exporter/exporterhelper/tracehelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ func checkRecordedMetricsForTraceExporterV2(t *testing.T, te exporter.TraceExpor

spans := make([]*data.Span, 2)
rs := data.NewResourceSpans(nil, nil)
rs.SetSpans(spans)
ils := data.NewInstrumentationLibrarySpans(data.NewInstrumentationLibrary(), spans)
rs.SetInstrumentationLibrarySpans([]*data.InstrumentationLibrarySpans{ils})
td := data.NewTraceData([]*data.ResourceSpans{rs})
ctx := observability.ContextWithReceiverName(context.Background(), fakeTraceReceiverName)
const numBatches = 7
Expand All @@ -373,7 +374,8 @@ func checkRecordedMetricsForTraceExporterV2(t *testing.T, te exporter.TraceExpor
func generateTraceV2Traffic(t *testing.T, te exporter.TraceExporterV2, numRequests int, wantError error) {
spans := make([]*data.Span, 1)
rs := data.NewResourceSpans(nil, nil)
rs.SetSpans(spans)
ils := data.NewInstrumentationLibrarySpans(data.NewInstrumentationLibrary(), spans)
rs.SetInstrumentationLibrarySpans([]*data.InstrumentationLibrarySpans{ils})
td := data.NewTraceData([]*data.ResourceSpans{rs})
ctx, span := trace.StartSpan(context.Background(), fakeTraceParentSpanName, trace.WithSampler(trace.AlwaysSample()))
defer span.End()
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ require (
github.com/jaegertracing/jaeger v1.17.0
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024
github.com/mitchellh/mapstructure v1.1.2
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad
github.com/openzipkin/zipkin-go v0.2.1
github.com/orijtech/prometheus-go-metrics-exporter v0.0.3-0.20190313163149-b321c5297f60
github.com/pavius/impi v0.0.0-20180302134524-c1cbdcb8df2b
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,8 @@ github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/onsi/gomega v1.7.0 h1:XPnZz8VVBHjVsy1vzJmRwIcSwiUO+JFfrv/xGiigmME=
github.com/onsi/gomega v1.7.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f h1:o21WlujGsrjoN3+n99AflASF/K0S6+SOLOj2O9nNplc=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200308012146-674ae1c8703f/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad h1:b0fNt65HvoUg+uDe63CA08SGzKO/gzPvzQQfEFX8Oks=
github.com/open-telemetry/opentelemetry-proto v0.0.0-20200315170400-caed74b167ad/go.mod h1:PMR5GI0F7BSpio+rBGFxNm6SLzg3FypDTcFuQZnO+F8=
github.com/opentracing-contrib/go-stdlib v0.0.0-20190519235532-cf7a6c988dc9/go.mod h1:PLldrQSroqzH70Xl+1DQcGnefIbqsKR7UDaiux3zV+w=
github.com/opentracing/opentracing-go v1.1.0 h1:pWlfV3Bxv7k65HYwkikxat0+s3pV4bsqf19k25Ur8rU=
github.com/opentracing/opentracing-go v1.1.0/go.mod h1:UkNAQd3GIcIGf0SeVgPpRdFStlNbqXla1AfSYxPUl2o=
Expand Down
33 changes: 33 additions & 0 deletions internal/data/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,36 @@ type Attributes struct {
func NewAttributes(m AttributesMap, droppedCount uint32) Attributes {
return Attributes{m, droppedCount}
}

// InstrumentationLibrary is a message representing the instrumentation library information.
//
// Must use NewResource functions to create new instances.
// Important: zero-initialized instance is not valid for use.
type InstrumentationLibrary struct {
orig *otlpcommon.InstrumentationLibrary
}

// NewInstrumentationLibrary creates a new InstrumentationLibrary.
func NewInstrumentationLibrary() InstrumentationLibrary {
return InstrumentationLibrary{}
}

func newInstrumentationLibrary(orig *otlpcommon.InstrumentationLibrary) InstrumentationLibrary {
return InstrumentationLibrary{orig}
}

func (il InstrumentationLibrary) Name() string {
return il.orig.Name
}

func (il InstrumentationLibrary) SetName(r string) {
il.orig.Name = r
}

func (il InstrumentationLibrary) Version() string {
return il.orig.Version
}

func (il InstrumentationLibrary) SetVersion(r string) {
il.orig.Version = r
}
159 changes: 133 additions & 26 deletions internal/data/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ func (md MetricData) MetricCount() int {
metricCount := 0
// TODO: Do not access internal members, add a metricCount to ResourceMetrics.
for _, rm := range md.pimpl.resourceMetrics {
metricCount += len(rm.pimpl.metrics)
for _, ilm := range rm.pimpl.instrumentationLibraryMetrics {
metricCount += len(ilm.pimpl.metrics)
}
}
return metricCount
}
Expand Down Expand Up @@ -124,8 +126,8 @@ type ResourceMetrics struct {
}

type internalResourceMetrics struct {
resource *Resource
metrics []Metric
resource *Resource
instrumentationLibraryMetrics []InstrumentationLibraryMetrics
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any chance we can shorten the type and field names?

// True when the slice was replace.
sliceChanged bool
// True if the pimpl was initialized.
Expand Down Expand Up @@ -174,14 +176,14 @@ func (rm ResourceMetrics) SetResource(r *Resource) {
rm.pimpl.resource = r
}

func (rm ResourceMetrics) Metrics() []Metric {
func (rm ResourceMetrics) InstrumentationLibraryMetrics() []InstrumentationLibraryMetrics {
rm.initInternallIfNeeded()
return rm.pimpl.metrics
return rm.pimpl.instrumentationLibraryMetrics
}

func (rm ResourceMetrics) SetMetrics(s []Metric) {
func (rm ResourceMetrics) SetInstrumentationLibraryMetrics(s []InstrumentationLibraryMetrics) {
rm.initInternallIfNeeded()
rm.pimpl.metrics = s
rm.pimpl.instrumentationLibraryMetrics = s
// We don't update the orig slice because this may be called multiple times.
rm.pimpl.sliceChanged = true
}
Expand All @@ -201,27 +203,132 @@ func (rm ResourceMetrics) flushInternal() {
if rm.pimpl.sliceChanged {
// Reconstruct the slice because we don't know what elements were removed/added.
// User may have changed internal fields in any Metric, flush all of them.
rm.orig.Metrics = make([]*otlpmetrics.Metric, len(rm.pimpl.metrics))
for i := range rm.pimpl.metrics {
rm.orig.Metrics[i] = rm.pimpl.metrics[i].getOrig()
rm.pimpl.metrics[i].flushInternal()
rm.orig.InstrumentationLibraryMetrics = make([]*otlpmetrics.InstrumentationLibraryMetrics, len(rm.pimpl.instrumentationLibraryMetrics))
for i := range rm.pimpl.instrumentationLibraryMetrics {
rm.orig.InstrumentationLibraryMetrics[i] = rm.pimpl.instrumentationLibraryMetrics[i].getOrig()
rm.pimpl.instrumentationLibraryMetrics[i].flushInternal()
}
} else {
// User may have changed internal fields in any Metric, flush all of them.
for i := range rm.pimpl.metrics {
rm.pimpl.metrics[i].flushInternal()
for i := range rm.pimpl.instrumentationLibraryMetrics {
rm.pimpl.instrumentationLibraryMetrics[i].flushInternal()
}
}
}

func (rm ResourceMetrics) initInternallIfNeeded() {
if !rm.pimpl.initialized {
rm.pimpl.resource = newResource(rm.orig.Resource)
rm.pimpl.metrics = newMetricSliceFromOrig(rm.orig.Metrics)
rm.pimpl.instrumentationLibraryMetrics = newInstrumentationLibraryMetricsSliceFromOrig(rm.orig.InstrumentationLibraryMetrics)
rm.pimpl.initialized = true
}
}

// InstrumentationLibraryMetrics is a collection of metrics from a Resource.
//
// Must use NewResourceMetrics functions to create new instances.
// Important: zero-initialized instance is not valid for use.
type InstrumentationLibraryMetrics struct {
// Wrap OTLP InstrumentationLibraryMetric.
orig *otlpmetrics.InstrumentationLibraryMetrics

// Override a few fields. These fields are the source of truth. Their counterparts
// stored in corresponding fields of "orig" are ignored.
pimpl *internalInstrumentationLibraryMetrics
}

type internalInstrumentationLibraryMetrics struct {
instrumentationLibrary InstrumentationLibrary
metrics []Metric
// True when the slice was replace.
sliceChanged bool
// True if the pimpl was initialized.
initialized bool
}

// NewInstrumentationLibraryMetricsSlice creates a slice of InstrumentationLibraryMetrics that are correctly initialized.
func NewInstrumentationLibraryMetricsSlice(len int) []InstrumentationLibraryMetrics {
// Slice for underlying orig.
origs := make([]otlpmetrics.InstrumentationLibraryMetrics, len)
// Slice for underlying pimpl.
pimpls := make([]internalInstrumentationLibraryMetrics, len)
// Slice for wrappers.
wrappers := make([]InstrumentationLibraryMetrics, len)
for i := range origs {
wrappers[i].orig = &origs[i]
wrappers[i].pimpl = &pimpls[i]
}
return wrappers
}

func newInstrumentationLibraryMetricsSliceFromOrig(origs []*otlpmetrics.InstrumentationLibraryMetrics) []InstrumentationLibraryMetrics {
// Slice for underlying pimpl.
pimpls := make([]internalInstrumentationLibraryMetrics, len(origs))
// Slice for wrappers.
wrappers := make([]InstrumentationLibraryMetrics, len(origs))
for i := range origs {
wrappers[i].orig = origs[i]
wrappers[i].pimpl = &pimpls[i]
}
return wrappers
}

func (ilm InstrumentationLibraryMetrics) InstrumentationLibrary() InstrumentationLibrary {
ilm.initInternallIfNeeded()
return ilm.pimpl.instrumentationLibrary
}

func (ilm InstrumentationLibraryMetrics) SetResource(il InstrumentationLibrary) {
ilm.initInternallIfNeeded()
ilm.pimpl.instrumentationLibrary = il
}

func (ilm InstrumentationLibraryMetrics) Metrics() []Metric {
ilm.initInternallIfNeeded()
return ilm.pimpl.metrics
}

func (ilm InstrumentationLibraryMetrics) SetMetrics(ms []Metric) {
ilm.initInternallIfNeeded()
ilm.pimpl.metrics = ms
// We don't update the orig slice because this may be called multiple times.
ilm.pimpl.sliceChanged = true
}

func (ilm InstrumentationLibraryMetrics) initInternallIfNeeded() {
if !ilm.pimpl.initialized {
ilm.pimpl.instrumentationLibrary = newInstrumentationLibrary(ilm.orig.InstrumentationLibrary)
ilm.pimpl.metrics = newMetricSliceFromOrig(ilm.orig.Metrics)
ilm.pimpl.initialized = true
}
}

func (ilm InstrumentationLibraryMetrics) getOrig() *otlpmetrics.InstrumentationLibraryMetrics {
return ilm.orig
}

func (ilm InstrumentationLibraryMetrics) flushInternal() {
if !ilm.pimpl.initialized {
// Guaranteed no changes via internal fields.
return
}

if ilm.pimpl.sliceChanged {
// Reconstruct the slice because we don't know what elements were removed/added.
// User may have changed internal fields in any Metric, flush all of them.
ilm.orig.Metrics = make([]*otlpmetrics.Metric, len(ilm.pimpl.metrics))
for i := range ilm.pimpl.metrics {
ilm.orig.Metrics[i] = ilm.pimpl.metrics[i].getOrig()
ilm.pimpl.metrics[i].flushInternal()
}
} else {
// User may have changed internal fields in any Metric, flush all of them.
for i := range ilm.pimpl.metrics {
ilm.pimpl.metrics[i].flushInternal()
}
}
}

// Metric defines a metric which has a descriptor and one or more timeseries points.
//
// Must use NewMetric* functions to create new instances.
Expand Down Expand Up @@ -337,10 +444,10 @@ func (m Metric) SetSummaryDataPoints(v []SummaryDataPoint) {
func (m Metric) initInternallIfNeeded() {
if !m.pimpl.initialized {
m.pimpl.metricDescriptor = newMetricDescriptorFromOrig(m.orig.MetricDescriptor)
m.pimpl.int64DataPoints = newInt64DataPointSliceFromOrig(m.orig.Int64Datapoints)
m.pimpl.doubleDataPoints = newDoubleDataPointSliceFormOrgig(m.orig.DoubleDatapoints)
m.pimpl.histogramDataPoints = newHistogramDataPointSliceFromOrig(m.orig.HistogramDatapoints)
m.pimpl.summaryDataPoints = newSummaryDataPointSliceFromOrig(m.orig.SummaryDatapoints)
m.pimpl.int64DataPoints = newInt64DataPointSliceFromOrig(m.orig.Int64DataPoints)
m.pimpl.doubleDataPoints = newDoubleDataPointSliceFormOrgig(m.orig.DoubleDataPoints)
m.pimpl.histogramDataPoints = newHistogramDataPointSliceFromOrig(m.orig.HistogramDataPoints)
m.pimpl.summaryDataPoints = newSummaryDataPointSliceFromOrig(m.orig.SummaryDataPoints)
m.pimpl.initialized = true
}
}
Expand Down Expand Up @@ -384,33 +491,33 @@ func (m Metric) flushInternal() {
}

if len(m.pimpl.int64DataPoints) != 0 {
m.orig.Int64Datapoints = make([]*otlpmetrics.Int64DataPoint, len(m.pimpl.int64DataPoints))
m.orig.Int64DataPoints = make([]*otlpmetrics.Int64DataPoint, len(m.pimpl.int64DataPoints))
for i := range m.pimpl.int64DataPoints {
m.orig.Int64Datapoints[i] = m.pimpl.int64DataPoints[i].getOrig()
m.orig.Int64DataPoints[i] = m.pimpl.int64DataPoints[i].getOrig()
m.pimpl.int64DataPoints[i].flushInternal()
}
}

if len(m.pimpl.doubleDataPoints) != 0 {
m.orig.DoubleDatapoints = make([]*otlpmetrics.DoubleDataPoint, len(m.pimpl.doubleDataPoints))
m.orig.DoubleDataPoints = make([]*otlpmetrics.DoubleDataPoint, len(m.pimpl.doubleDataPoints))
for i := range m.pimpl.doubleDataPoints {
m.orig.DoubleDatapoints[i] = m.pimpl.doubleDataPoints[i].getOrig()
m.orig.DoubleDataPoints[i] = m.pimpl.doubleDataPoints[i].getOrig()
m.pimpl.doubleDataPoints[i].flushInternal()
}
}

if len(m.pimpl.histogramDataPoints) != 0 {
m.orig.HistogramDatapoints = make([]*otlpmetrics.HistogramDataPoint, len(m.pimpl.histogramDataPoints))
m.orig.HistogramDataPoints = make([]*otlpmetrics.HistogramDataPoint, len(m.pimpl.histogramDataPoints))
for i := range m.pimpl.histogramDataPoints {
m.orig.HistogramDatapoints[i] = m.pimpl.histogramDataPoints[i].getOrig()
m.orig.HistogramDataPoints[i] = m.pimpl.histogramDataPoints[i].getOrig()
m.pimpl.histogramDataPoints[i].flushInternal()
}
}

if len(m.pimpl.summaryDataPoints) != 0 {
m.orig.SummaryDatapoints = make([]*otlpmetrics.SummaryDataPoint, len(m.pimpl.summaryDataPoints))
m.orig.SummaryDataPoints = make([]*otlpmetrics.SummaryDataPoint, len(m.pimpl.summaryDataPoints))
for i := range m.pimpl.summaryDataPoints {
m.orig.SummaryDatapoints[i] = m.pimpl.summaryDataPoints[i].getOrig()
m.orig.SummaryDataPoints[i] = m.pimpl.summaryDataPoints[i].getOrig()
m.pimpl.summaryDataPoints[i].flushInternal()
}

Expand Down
Loading