Skip to content

Commit

Permalink
Use sync.Pools for ResourceLogs in otelloghttp transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
hiroyaonoe committed Apr 18, 2024
1 parent fe3de70 commit e4f5cfa
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 9 deletions.
3 changes: 2 additions & 1 deletion exporters/otlp/otlplog/otlploghttp/exporter.go
Expand Up @@ -46,10 +46,11 @@ func (e *Exporter) Export(ctx context.Context, records []log.Record) error {
if e.stopped.Load() {
return nil
}
otlp := transformResourceLogs(records)
otlp, free := transformResourceLogs(records)
if otlp == nil {
return nil
}
defer free()
return e.client.Load().UploadLogs(ctx, otlp)
}

Expand Down
4 changes: 2 additions & 2 deletions exporters/otlp/otlplog/otlploghttp/exporter_test.go
Expand Up @@ -44,9 +44,9 @@ func TestExporterExport(t *testing.T) {

orig := transformResourceLogs
var got []log.Record
transformResourceLogs = func(r []log.Record) []*logpb.ResourceLogs {
transformResourceLogs = func(r []log.Record) ([]*logpb.ResourceLogs, func()) {
got = r
return make([]*logpb.ResourceLogs, 1)
return make([]*logpb.ResourceLogs, 1), func() {}
}
t.Cleanup(func() { transformResourceLogs = orig })

Expand Down
19 changes: 14 additions & 5 deletions exporters/otlp/otlplog/otlploghttp/internal/transform/log.go
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/slim/otlp/common/v1"
Expand All @@ -18,18 +19,26 @@ import (
"go.opentelemetry.io/otel/sdk/log"
)

var resourceLogsPool = sync.Pool{
New: func() any {
return []*lpb.ResourceLogs{}
},
}

// ResourceLogs returns an slice of OTLP ResourceLogs generated from records.
func ResourceLogs(records []log.Record) []*lpb.ResourceLogs {
func ResourceLogs(records []log.Record) (out []*lpb.ResourceLogs, free func()) {
if len(records) == 0 {
return nil
return nil, func() {}
}

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

func resourceLogsMap(records []log.Record) map[attribute.Distinct]*lpb.ResourceLogs {
Expand Down
Expand Up @@ -163,7 +163,16 @@ var (

func TestResourceLogs(t *testing.T) {
want := []*lpb.ResourceLogs{pbResourceLogs}
assert.Equal(t, want, ResourceLogs(records))
out, free := ResourceLogs(records)
assert.Equal(t, want, out)
free()
want = []*lpb.ResourceLogs{{
ScopeLogs: []*lpb.ScopeLogs{{
LogRecords: pbLogRecords[2:],
}},
}}
out, free = ResourceLogs(records[2:])
assert.Equal(t, want, out)
}

func TestSeverityNumber(t *testing.T) {
Expand Down

0 comments on commit e4f5cfa

Please sign in to comment.