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

Pool otlploghttp transform maps #5378

Merged
merged 8 commits into from
May 22, 2024
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
45 changes: 33 additions & 12 deletions exporters/otlp/otlplog/otlploghttp/internal/transform/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package transform // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/transform"

import (
"sync"
"time"

cpb "go.opentelemetry.io/proto/otlp/common/v1"
Expand All @@ -24,19 +25,30 @@ func ResourceLogs(records []log.Record) []*lpb.ResourceLogs {
return nil
}

resMap := resourceLogsMap(records)
resMap := resourceLogsMapPool.Get().(map[attribute.Distinct]*lpb.ResourceLogs)
defer func() {
clear(resMap)
resourceLogsMapPool.Put(resMap)
}()
resourceLogsMap(&resMap, records)

out := make([]*lpb.ResourceLogs, 0, len(resMap))
for _, rl := range resMap {
out = append(out, rl)
}
return out
}

func resourceLogsMap(records []log.Record) map[attribute.Distinct]*lpb.ResourceLogs {
out := make(map[attribute.Distinct]*lpb.ResourceLogs)
var resourceLogsMapPool = sync.Pool{
New: func() any {
return make(map[attribute.Distinct]*lpb.ResourceLogs)
},
}

func resourceLogsMap(dst *map[attribute.Distinct]*lpb.ResourceLogs, records []log.Record) {
for _, r := range records {
res := r.Resource()
rl, ok := out[res.Equivalent()]
rl, ok := (*dst)[res.Equivalent()]
if !ok {
rl = new(lpb.ResourceLogs)
if res.Len() > 0 {
Expand All @@ -45,28 +57,38 @@ func resourceLogsMap(records []log.Record) map[attribute.Distinct]*lpb.ResourceL
}
}
rl.SchemaUrl = res.SchemaURL()
out[res.Equivalent()] = rl
(*dst)[res.Equivalent()] = rl
}
rl.ScopeLogs = ScopeLogs(records)
}
return out
}

// ScopeLogs returns a slice of OTLP ScopeLogs generated from recoreds.
func ScopeLogs(records []log.Record) []*lpb.ScopeLogs {
scopeMap := scopeLogsMap(records)
scopeMap := scopeLogsMapPool.Get().(map[instrumentation.Scope]*lpb.ScopeLogs)
defer func() {
clear(scopeMap)
scopeLogsMapPool.Put(scopeMap)
}()
scopeLogsMap(&scopeMap, records)

out := make([]*lpb.ScopeLogs, 0, len(scopeMap))
for _, sl := range scopeMap {
out = append(out, sl)
}
return out
}

func scopeLogsMap(records []log.Record) map[instrumentation.Scope]*lpb.ScopeLogs {
out := make(map[instrumentation.Scope]*lpb.ScopeLogs)
var scopeLogsMapPool = sync.Pool{
New: func() any {
return make(map[instrumentation.Scope]*lpb.ScopeLogs)
},
}

func scopeLogsMap(dst *map[instrumentation.Scope]*lpb.ScopeLogs, records []log.Record) {
for _, r := range records {
scope := r.InstrumentationScope()
sl, ok := out[scope]
sl, ok := (*dst)[scope]
if !ok {
sl = new(lpb.ScopeLogs)
var emptyScope instrumentation.Scope
Expand All @@ -77,11 +99,10 @@ func scopeLogsMap(records []log.Record) map[instrumentation.Scope]*lpb.ScopeLogs
}
sl.SchemaUrl = scope.SchemaURL
}
out[scope] = sl
(*dst)[scope] = sl
}
sl.LogRecords = append(sl.LogRecords, LogRecord(r))
}
return out
}

// LogRecord returns an OTLP LogRecord generated from record.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,14 @@ func TestSeverityNumber(t *testing.T) {
assert.Equal(t, want, SeverityNumber(api.Severity(i)))
}
}

func BenchmarkResourceLogs(b *testing.B) {
b.ReportAllocs()
b.RunParallel(func(pb *testing.PB) {
var out []*lpb.ResourceLogs
for pb.Next() {
out = ResourceLogs(records)
}
_ = out
})
}