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 d8ae2d2
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 45 deletions.
59 changes: 32 additions & 27 deletions exporter/loggingexporter/logging_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,59 @@ import (
"go.opentelemetry.io/collector/internal/otlptext"
)

var (
logsMarshaler = otlptext.NewTextLogsMarshaler()
metricsMarshaler = otlptext.NewTextMetricsMarshaler()
tracesMarshaler = otlptext.NewTextTracesMarshaler()
)

type loggingExporter struct {
logger *zap.Logger
debug bool
}

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 := 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 := 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 := logsMarshaler.Marshal(ld)
if err != nil {
return err
}
s.logger.Debug(string(buf))
return nil
}

Expand Down Expand Up @@ -128,21 +148,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
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 d8ae2d2

Please sign in to comment.