Skip to content

Commit

Permalink
Convert otlptext to implement Marshaler interfaces
Browse files Browse the repository at this point in the history
Signed-off-by: Bogdan Drutu <[email protected]>
  • Loading branch information
bogdandrutu committed Jun 9, 2021
1 parent ba9ccde commit 6c4bfd1
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 62 deletions.
89 changes: 45 additions & 44 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,53 +26,77 @@ import (
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.opentelemetry.io/collector/internal/model"
"go.opentelemetry.io/collector/internal/otlptext"
)

type loggingExporter struct {
logger *zap.Logger
debug bool
logger *zap.Logger
debug bool
logsMarshaler model.LogsMarshaler
metricsMarshaler model.MetricsMarshaler
tracesMarshaler model.TracesMarshaler
}

func (s *loggingExporter) pushTraces(
_ context.Context,
td pdata.Traces,
) error {

func (s *loggingExporter) pushTraces(_ context.Context, td pdata.Traces) error {
s.logger.Info("TracesExporter", zap.Int("#spans", td.SpanCount()))

if !s.debug {
return nil
}

s.logger.Debug(otlptext.Traces(td))

buf, err := s.tracesMarshaler.Marshal(td)
if err != nil {
return err
}
s.logger.Debug(string(buf))
return nil
}

func (s *loggingExporter) pushMetrics(
_ context.Context,
md pdata.Metrics,
) error {
func (s *loggingExporter) pushMetrics(_ context.Context, md pdata.Metrics) error {
s.logger.Info("MetricsExporter", zap.Int("#metrics", md.MetricCount()))

if !s.debug {
return nil
}

s.logger.Debug(otlptext.Metrics(md))
buf, err := s.metricsMarshaler.Marshal(md)
if err != nil {
return err
}
s.logger.Debug(string(buf))
return nil
}

func (s *loggingExporter) pushLogs(_ context.Context, ld pdata.Logs) error {
s.logger.Info("LogsExporter", zap.Int("#logs", ld.LogRecordCount()))

if !s.debug {
return nil
}

buf, err := s.logsMarshaler.Marshal(ld)
if err != nil {
return err
}
s.logger.Debug(string(buf))
return nil
}

func newLoggingExporter(level string, logger *zap.Logger) *loggingExporter {
return &loggingExporter{
debug: strings.ToLower(level) == "debug",
logger: logger,
logsMarshaler: otlptext.NewTextLogsMarshaler(),
metricsMarshaler: otlptext.NewTextMetricsMarshaler(),
tracesMarshaler: otlptext.NewTextTracesMarshaler(),
}
}

// newTracesExporter creates an exporter.TracesExporter that just drops the
// received data and logs debugging messages.
func newTracesExporter(config config.Exporter, level string, logger *zap.Logger) (component.TracesExporter, error) {
s := &loggingExporter{
debug: strings.ToLower(level) == "debug",
logger: logger,
}

s := newLoggingExporter(level, logger)
return exporterhelper.NewTracesExporter(
config,
logger,
Expand All @@ -89,11 +113,7 @@ func newTracesExporter(config config.Exporter, level string, logger *zap.Logger)
// newMetricsExporter creates an exporter.MetricsExporter that just drops the
// received data and logs debugging messages.
func newMetricsExporter(config config.Exporter, level string, logger *zap.Logger) (component.MetricsExporter, error) {
s := &loggingExporter{
debug: strings.ToLower(level) == "debug",
logger: logger,
}

s := newLoggingExporter(level, logger)
return exporterhelper.NewMetricsExporter(
config,
logger,
Expand All @@ -110,11 +130,7 @@ func newMetricsExporter(config config.Exporter, level string, logger *zap.Logger
// newLogsExporter creates an exporter.LogsExporter that just drops the
// received data and logs debugging messages.
func newLogsExporter(config config.Exporter, level string, logger *zap.Logger) (component.LogsExporter, error) {
s := &loggingExporter{
debug: strings.ToLower(level) == "debug",
logger: logger,
}

s := newLoggingExporter(level, logger)
return exporterhelper.NewLogsExporter(
config,
logger,
Expand All @@ -128,21 +144,6 @@ func newLogsExporter(config config.Exporter, level string, logger *zap.Logger) (
)
}

func (s *loggingExporter) pushLogs(
_ context.Context,
ld pdata.Logs,
) error {
s.logger.Info("LogsExporter", zap.Int("#logs", ld.LogRecordCount()))

if !s.debug {
return nil
}

s.logger.Debug(otlptext.Logs(ld))

return nil
}

func loggerSync(logger *zap.Logger) func(context.Context) error {
return func(context.Context) error {
// Currently Sync() return a different error depending on the OS.
Expand Down
38 changes: 38 additions & 0 deletions exporter/loggingexporter/logging_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package loggingexporter

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -62,3 +63,40 @@ func TestLoggingLogsExporterNoErrors(t *testing.T) {

assert.NoError(t, lle.Shutdown(context.Background()))
}

func TestLoggingExporterErrors(t *testing.T) {
le := newLoggingExporter("Debug", zap.NewNop())
require.NotNil(t, le)

errWant := errors.New("my error")
le.tracesMarshaler = &errTracesMarshaler{err: errWant}
le.metricsMarshaler = &errMetricsMarshaler{err: errWant}
le.logsMarshaler = &errLogsMarshaler{err: errWant}
assert.Equal(t, errWant, le.pushTraces(context.Background(), pdata.NewTraces()))
assert.Equal(t, errWant, le.pushMetrics(context.Background(), pdata.NewMetrics()))
assert.Equal(t, errWant, le.pushLogs(context.Background(), pdata.NewLogs()))
}

type errLogsMarshaler struct {
err error
}

func (e errLogsMarshaler) Marshal(pdata.Logs) ([]byte, error) {
return nil, e.err
}

type errMetricsMarshaler struct {
err error
}

func (e errMetricsMarshaler) Marshal(pdata.Metrics) ([]byte, error) {
return nil, e.err
}

type errTracesMarshaler struct {
err error
}

func (e errTracesMarshaler) Marshal(pdata.Traces) ([]byte, error) {
return nil, e.err
}
7 changes: 4 additions & 3 deletions internal/otlptext/databuffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package otlptext

import (
"bytes"
"fmt"
"strconv"
"strings"
Expand All @@ -24,12 +25,12 @@ import (
)

type dataBuffer struct {
str strings.Builder
buf bytes.Buffer
}

func (b *dataBuffer) logEntry(format string, a ...interface{}) {
b.str.WriteString(fmt.Sprintf(format, a...))
b.str.WriteString("\n")
b.buf.WriteString(fmt.Sprintf(format, a...))
b.buf.WriteString("\n")
}

func (b *dataBuffer) logAttr(label string, value string) {
Expand Down
18 changes: 14 additions & 4 deletions internal/otlptext/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@

package otlptext

import "go.opentelemetry.io/collector/consumer/pdata"
import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/model"
)

// Logs data to text
func Logs(ld pdata.Logs) string {
// NewTextLogsMarshaler returns a serializer.LogsMarshaler to encode to OTLP json bytes.
func NewTextLogsMarshaler() model.LogsMarshaler {
return logsMarshaler{}
}

type logsMarshaler struct{}

// Marshal data to text.
func (logsMarshaler) Marshal(ld pdata.Logs) ([]byte, error) {
buf := dataBuffer{}
rls := ld.ResourceLogs()
for i := 0; i < rls.Len(); i++ {
Expand All @@ -39,5 +49,5 @@ func Logs(ld pdata.Logs) string {
}
}

return buf.str.String()
return buf.buf.Bytes(), nil
}
3 changes: 2 additions & 1 deletion internal/otlptext/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestLogs(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
logs := Logs(tt.args.ld)
logs, err := NewTextLogsMarshaler().Marshal(tt.args.ld)
assert.NoError(t, err)
if !tt.empty {
assert.NotEmpty(t, logs)
}
Expand Down
18 changes: 14 additions & 4 deletions internal/otlptext/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@

package otlptext

import "go.opentelemetry.io/collector/consumer/pdata"
import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/model"
)

// Metrics data to text
func Metrics(md pdata.Metrics) string {
// NewTextMetricsMarshaler returns a serializer.MetricsMarshaler to encode to OTLP json bytes.
func NewTextMetricsMarshaler() model.MetricsMarshaler {
return metricsMarshaler{}
}

type metricsMarshaler struct{}

// Marshal data to text.
func (metricsMarshaler) Marshal(md pdata.Metrics) ([]byte, error) {
buf := dataBuffer{}
rms := md.ResourceMetrics()
for i := 0; i < rms.Len(); i++ {
Expand All @@ -39,5 +49,5 @@ func Metrics(md pdata.Metrics) string {
}
}

return buf.str.String()
return buf.buf.Bytes(), nil
}
3 changes: 2 additions & 1 deletion internal/otlptext/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ func TestMetrics(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
metrics := Metrics(tt.args.md)
metrics, err := NewTextMetricsMarshaler().Marshal(tt.args.md)
assert.NoError(t, err)
if !tt.empty {
assert.NotEmpty(t, metrics)
}
Expand Down
18 changes: 14 additions & 4 deletions internal/otlptext/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,20 @@

package otlptext

import "go.opentelemetry.io/collector/consumer/pdata"
import (
"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/internal/model"
)

// Traces data to text
func Traces(td pdata.Traces) string {
// NewTextTracesMarshaler returns a serializer.TracesMarshaler to encode to OTLP json bytes.
func NewTextTracesMarshaler() model.TracesMarshaler {
return tracesMarshaler{}
}

type tracesMarshaler struct{}

// Marshal data to text.
func (tracesMarshaler) Marshal(td pdata.Traces) ([]byte, error) {
buf := dataBuffer{}
rss := td.ResourceSpans()
for i := 0; i < rss.Len(); i++ {
Expand Down Expand Up @@ -52,5 +62,5 @@ func Traces(td pdata.Traces) string {
}
}

return buf.str.String()
return buf.buf.Bytes(), nil
}
3 changes: 2 additions & 1 deletion internal/otlptext/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ func TestTraces(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
traces := Traces(tt.args.td)
traces, err := NewTextTracesMarshaler().Marshal(tt.args.td)
assert.NoError(t, err)
if !tt.empty {
assert.NotEmpty(t, traces)
}
Expand Down

0 comments on commit 6c4bfd1

Please sign in to comment.