Skip to content

Commit

Permalink
Add otlploghttp transform pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Apr 10, 2024
1 parent 054a63f commit d30f9a5
Show file tree
Hide file tree
Showing 4 changed files with 385 additions and 26 deletions.
4 changes: 2 additions & 2 deletions exporters/otlp/otlplog/otlploghttp/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
)

type client struct {
uploadLogs func(context.Context, *logpb.ResourceLogs) error
uploadLogs func(context.Context, []*logpb.ResourceLogs) error
}

func (c *client) UploadLogs(ctx context.Context, rl *logpb.ResourceLogs) error {
func (c *client) UploadLogs(ctx context.Context, rl []*logpb.ResourceLogs) error {
if c.uploadLogs != nil {
return c.uploadLogs(ctx, rl)
}
Expand Down
8 changes: 3 additions & 5 deletions exporters/otlp/otlplog/otlploghttp/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package otlploghttp // import "go.opentelemetry.io/otel/exporters/otlp/otlplog/o

import (
"context"
"errors"
"sync/atomic"

"go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp/internal/transform"
Expand Down Expand Up @@ -47,12 +46,11 @@ func (e *Exporter) Export(ctx context.Context, records []log.Record) error {
if e.stopped.Load() {
return nil
}
otlp, err := transformResourceLogs(records)
otlp := transformResourceLogs(records)
if otlp != nil {
// Best effort upload of transformable logs.
err = errors.Join(err, e.client.Load().UploadLogs(ctx, otlp))
return e.client.Load().UploadLogs(ctx, otlp)
}
return err
return nil

Check warning on line 53 in exporters/otlp/otlplog/otlploghttp/exporter.go

View check run for this annotation

Codecov / codecov/patch

exporters/otlp/otlplog/otlploghttp/exporter.go#L53

Added line #L53 was not covered by tests
}

// Shutdown shuts down the Exporter. Calls to Export or ForceFlush will perform
Expand Down
21 changes: 5 additions & 16 deletions exporters/otlp/otlplog/otlploghttp/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,45 +19,34 @@ import (
)

func TestExporterExportErrors(t *testing.T) {
var (
errUpload = errors.New("upload")
errTForm = errors.New("transform")
)

errUpload := errors.New("upload")
c := &client{
uploadLogs: func(context.Context, *logpb.ResourceLogs) error {
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
return errUpload
},
}

orig := transformResourceLogs
transformResourceLogs = func(r []log.Record) (*logpb.ResourceLogs, error) {
return new(logpb.ResourceLogs), errTForm
}
t.Cleanup(func() { transformResourceLogs = orig })

e, err := newExporter(c, config{})
require.NoError(t, err, "New")

err = e.Export(context.Background(), make([]log.Record, 1))
assert.ErrorIs(t, err, errUpload)
assert.ErrorIs(t, err, errTForm)
}

func TestExporterExport(t *testing.T) {
var uploads int
c := &client{
uploadLogs: func(context.Context, *logpb.ResourceLogs) error {
uploadLogs: func(context.Context, []*logpb.ResourceLogs) error {
uploads++
return nil
},
}

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

Expand Down
Loading

0 comments on commit d30f9a5

Please sign in to comment.