Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Range methods to pdata slices #8938

Closed
Closed
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
25 changes: 25 additions & 0 deletions .chloggen/pdata-slice-range-only.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Use this changelog template to create an entry for release notes.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: pdata

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add "Range" method to slices

# One or more tracking issues or pull requests related to the change
issues: [8938]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:

# Optional: The change log or logs in which this entry should be included.
# e.g. '[user]' or '[user, api]'
# Include 'user' if the change is relevant to end users.
# Include 'api' if there is a change to a library API.
# Default: '[user]'
change_logs: [api]
40 changes: 16 additions & 24 deletions exporter/internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ func (b *dataBuffer) logMetricDataPoints(m pmetric.Metric) {
}

func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) {
for i := 0; i < ps.Len(); i++ {
p := ps.At(i)
ps.Range(func(i int, p pmetric.NumberDataPoint) {
b.logEntry("NumberDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand All @@ -104,12 +103,11 @@ func (b *dataBuffer) logNumberDataPoints(ps pmetric.NumberDataPointSlice) {
}

b.logExemplars("Exemplars", p.Exemplars())
}
})
}

func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice) {
for i := 0; i < ps.Len(); i++ {
p := ps.At(i)
ps.Range(func(i int, p pmetric.HistogramDataPoint) {
b.logEntry("HistogramDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand Down Expand Up @@ -138,12 +136,11 @@ func (b *dataBuffer) logHistogramDataPoints(ps pmetric.HistogramDataPointSlice)
}

b.logExemplars("Exemplars", p.Exemplars())
}
})
}

func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHistogramDataPointSlice) {
for i := 0; i < ps.Len(); i++ {
p := ps.At(i)
ps.Range(func(i int, p pmetric.ExponentialHistogramDataPoint) {
b.logEntry("ExponentialHistogramDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand Down Expand Up @@ -197,12 +194,11 @@ func (b *dataBuffer) logExponentialHistogramDataPoints(ps pmetric.ExponentialHis
}

b.logExemplars("Exemplars", p.Exemplars())
}
})
}

func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice) {
for i := 0; i < ps.Len(); i++ {
p := ps.At(i)
ps.Range(func(i int, p pmetric.SummaryDataPoint) {
b.logEntry("SummaryDataPoints #%d", i)
b.logDataPointAttributes(p.Attributes())

Expand All @@ -211,12 +207,10 @@ func (b *dataBuffer) logDoubleSummaryDataPoints(ps pmetric.SummaryDataPointSlice
b.logEntry("Count: %d", p.Count())
b.logEntry("Sum: %f", p.Sum())

quantiles := p.QuantileValues()
for i := 0; i < quantiles.Len(); i++ {
quantile := quantiles.At(i)
p.QuantileValues().Range(func(i int, quantile pmetric.SummaryDataPointValueAtQuantile) {
b.logEntry("QuantileValue #%d: Quantile %f, Value %f", i, quantile.Quantile(), quantile.Value())
}
}
})
})
}

func (b *dataBuffer) logDataPointAttributes(attributes pcommon.Map) {
Expand All @@ -229,14 +223,14 @@ func (b *dataBuffer) logEvents(description string, se ptrace.SpanEventSlice) {
}

b.logEntry("%s:", description)
for i := 0; i < se.Len(); i++ {
se.Range(func(i int, el ptrace.SpanEvent) {
e := se.At(i)
b.logEntry("SpanEvent #%d", i)
b.logEntry(" -> Name: %s", e.Name())
b.logEntry(" -> Timestamp: %s", e.Timestamp())
b.logEntry(" -> DroppedAttributesCount: %d", e.DroppedAttributesCount())
b.logAttributes(" -> Attributes:", e.Attributes())
}
})
}

func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) {
Expand All @@ -246,15 +240,14 @@ func (b *dataBuffer) logLinks(description string, sl ptrace.SpanLinkSlice) {

b.logEntry("%s:", description)

for i := 0; i < sl.Len(); i++ {
l := sl.At(i)
sl.Range(func(i int, l ptrace.SpanLink) {
b.logEntry("SpanLink #%d", i)
b.logEntry(" -> Trace ID: %s", l.TraceID())
b.logEntry(" -> ID: %s", l.SpanID())
b.logEntry(" -> TraceState: %s", l.TraceState().AsRaw())
b.logEntry(" -> DroppedAttributesCount: %d", l.DroppedAttributesCount())
b.logAttributes(" -> Attributes:", l.Attributes())
}
})
}

func (b *dataBuffer) logExemplars(description string, se pmetric.ExemplarSlice) {
Expand All @@ -264,8 +257,7 @@ func (b *dataBuffer) logExemplars(description string, se pmetric.ExemplarSlice)

b.logEntry("%s:", description)

for i := 0; i < se.Len(); i++ {
e := se.At(i)
se.Range(func(i int, e pmetric.Exemplar) {
b.logEntry("Exemplar #%d", i)
b.logEntry(" -> Trace ID: %s", e.TraceID())
b.logEntry(" -> Span ID: %s", e.SpanID())
Expand All @@ -277,7 +269,7 @@ func (b *dataBuffer) logExemplars(description string, se pmetric.ExemplarSlice)
b.logEntry(" -> Value: %f", e.DoubleValue())
}
b.logAttributes(" -> FilteredAttributes", e.FilteredAttributes())
}
})
}

func valueToString(v pcommon.Value) string {
Expand Down
20 changes: 6 additions & 14 deletions exporter/internal/otlptext/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ type textLogsMarshaler struct{}
// MarshalLogs plog.Logs to OTLP text.
func (textLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
buf := dataBuffer{}
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
ld.ResourceLogs().Range(func(i int, rl plog.ResourceLogs) {
buf.logEntry("ResourceLog #%d", i)
rl := rls.At(i)
buf.logEntry("Resource SchemaURL: %s", rl.SchemaUrl())
buf.logAttributes("Resource attributes", rl.Resource().Attributes())
ills := rl.ScopeLogs()
for j := 0; j < ills.Len(); j++ {
rl.ScopeLogs().Range(func(j int, ils plog.ScopeLogs) {
buf.logEntry("ScopeLogs #%d", j)
ils := ills.At(j)
buf.logEntry("ScopeLogs SchemaURL: %s", ils.SchemaUrl())
buf.logInstrumentationScope(ils.Scope())

logs := ils.LogRecords()
for k := 0; k < logs.Len(); k++ {
ils.LogRecords().Range(func(k int, lr plog.LogRecord) {
buf.logEntry("LogRecord #%d", k)
lr := logs.At(k)
buf.logEntry("ObservedTimestamp: %s", lr.ObservedTimestamp())
buf.logEntry("Timestamp: %s", lr.Timestamp())
buf.logEntry("SeverityText: %s", lr.SeverityText())
Expand All @@ -43,9 +36,8 @@ func (textLogsMarshaler) MarshalLogs(ld plog.Logs) ([]byte, error) {
buf.logEntry("Trace ID: %s", lr.TraceID())
buf.logEntry("Span ID: %s", lr.SpanID())
buf.logEntry("Flags: %d", lr.Flags())
}
}
}

})
})
})
return buf.buf.Bytes(), nil
}
19 changes: 6 additions & 13 deletions exporter/internal/otlptext/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,20 @@ type textMetricsMarshaler struct{}
// MarshalMetrics pmetric.Metrics to OTLP text.
func (textMetricsMarshaler) MarshalMetrics(md pmetric.Metrics) ([]byte, error) {
buf := dataBuffer{}
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
md.ResourceMetrics().Range(func(i int, rm pmetric.ResourceMetrics) {
buf.logEntry("ResourceMetrics #%d", i)
rm := rms.At(i)
buf.logEntry("Resource SchemaURL: %s", rm.SchemaUrl())
buf.logAttributes("Resource attributes", rm.Resource().Attributes())
ilms := rm.ScopeMetrics()
for j := 0; j < ilms.Len(); j++ {
rm.ScopeMetrics().Range(func(j int, ilm pmetric.ScopeMetrics) {
buf.logEntry("ScopeMetrics #%d", j)
ilm := ilms.At(j)
buf.logEntry("ScopeMetrics SchemaURL: %s", ilm.SchemaUrl())
buf.logInstrumentationScope(ilm.Scope())
metrics := ilm.Metrics()
for k := 0; k < metrics.Len(); k++ {
ilm.Metrics().Range(func(k int, metric pmetric.Metric) {
buf.logEntry("Metric #%d", k)
metric := metrics.At(k)
buf.logMetricDescriptor(metric)
buf.logMetricDataPoints(metric)
}
}
}

})
})
})
return buf.buf.Bytes(), nil
}
20 changes: 6 additions & 14 deletions exporter/internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,16 @@ type textTracesMarshaler struct{}
// MarshalTraces ptrace.Traces to OTLP text.
func (textTracesMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
buf := dataBuffer{}
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
td.ResourceSpans().Range(func(i int, rs ptrace.ResourceSpans) {
buf.logEntry("ResourceSpans #%d", i)
rs := rss.At(i)
buf.logEntry("Resource SchemaURL: %s", rs.SchemaUrl())
buf.logAttributes("Resource attributes", rs.Resource().Attributes())
ilss := rs.ScopeSpans()
for j := 0; j < ilss.Len(); j++ {
rs.ScopeSpans().Range(func(j int, ils ptrace.ScopeSpans) {
buf.logEntry("ScopeSpans #%d", j)
ils := ilss.At(j)
buf.logEntry("ScopeSpans SchemaURL: %s", ils.SchemaUrl())
buf.logInstrumentationScope(ils.Scope())

spans := ils.Spans()
for k := 0; k < spans.Len(); k++ {
ils.Spans().Range(func(k int, span ptrace.Span) {
buf.logEntry("Span #%d", k)
span := spans.At(k)
buf.logAttr("Trace ID", span.TraceID())
buf.logAttr("Parent ID", span.ParentSpanID())
buf.logAttr("ID", span.SpanID())
Expand All @@ -48,9 +41,8 @@ func (textTracesMarshaler) MarshalTraces(td ptrace.Traces) ([]byte, error) {
buf.logAttributes("Attributes", span.Attributes())
buf.logEvents("Events", span.Events())
buf.logLinks("Links", span.Links())
}
}
}

})
})
})
return buf.buf.Bytes(), nil
}
29 changes: 27 additions & 2 deletions pdata/internal/cmd/pdatagen/internal/base_slices.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,16 @@ func (es {{ .structName }}) Sort(less func(a, b {{ .elementName }}) bool) {
es.state.AssertMutable()
sort.SliceStable(*es.orig, func(i, j int) bool { return less(es.At(i), es.At(j)) })
}
{{- end }}`
{{- end }}


// Range iterates over {{ .structName }} and executes f against each element.
// The function also passes the iteration index.
func (es {{ .structName }}) Range(f func(int, {{ .elementName }})) {
for i := 0; i < es.Len(); i++ {
f(i, es.At(i))
}
}`

const sliceTestTemplate = `func Test{{ .structName }}(t *testing.T) {
es := New{{ .structName }}()
Expand Down Expand Up @@ -281,7 +290,23 @@ func Test{{ .structName }}_Sort(t *testing.T) {
assert.True(t, uintptr(unsafe.Pointer(es.At(i-1).orig)) > uintptr(unsafe.Pointer(es.At(i).orig)))
}
}
{{- end }}`
{{- end }}

func Test{{ .structName }}_Range(t *testing.T) {
// Test _Range on empty slice
emptySlice := New{{ .structName }}()
emptySlice.Range(func(i int, el {{ .elementName }}) {
t.Fail()
})

// Test _Range
slice := generateTest{{ .structName }}()
total := 0
slice.Range(func(i int, el {{ .elementName }}) {
total+=i
})
assert.Equal(t, 0+1+2+3+4+5+6, total)
}`

const sliceGenerateTest = `func generateTest{{ .structName }}() {{ .structName }} {
es := New{{ .structName }}()
Expand Down
8 changes: 8 additions & 0 deletions pdata/plog/generated_logrecordslice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pdata/plog/generated_logrecordslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pdata/plog/generated_resourcelogsslice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions pdata/plog/generated_resourcelogsslice_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pdata/plog/generated_scopelogsslice.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading