Skip to content

Commit

Permalink
fix issue where errors exporting from the collector are not logged
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Nov 1, 2023
1 parent 2f06d89 commit 63ebe18
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
4 changes: 4 additions & 0 deletions exporter/collector/integrationtest/inmemoryocexporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,13 @@ func NewTraceTestExporter(
cfg.TraceConfig.ClientConfig.UseInsecure = true
cfg.ProjectID = "fakeprojectid"

logger, err := zap.NewDevelopment()
require.NoError(t, err)

exporter, err := collector.NewGoogleCloudTracesExporter(
ctx,
cfg,
logger,
"latest",
collector.DefaultTimeout,
)
Expand Down
3 changes: 3 additions & 0 deletions exporter/collector/integrationtest/traces_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector"
"github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/collector/integrationtest/testcases"
"github.com/stretchr/testify/require"
"go.uber.org/zap"
)

func createTracesExporter(
Expand All @@ -35,9 +36,11 @@ func createTracesExporter(
) *collector.TraceExporter {
cfg := test.CreateTraceConfig()
cfg.ProjectID = os.Getenv("PROJECT_ID")
logger, _ := zap.NewProduction()
exporter, err := collector.NewGoogleCloudTracesExporter(
ctx,
cfg,
logger,
"latest",
collector.DefaultTimeout,
)
Expand Down
11 changes: 11 additions & 0 deletions exporter/collector/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,17 @@ func (l *LogsExporter) Shutdown(ctx context.Context) error {
}

func (l *LogsExporter) PushLogs(ctx context.Context, ld plog.Logs) error {
err := l.pushLogs(ctx, ld)
if err != nil {
l.obs.log.Error(
"Exporting logs failed",
zap.Error(err),
)
}
return err
}

func (l *LogsExporter) pushLogs(ctx context.Context, ld plog.Logs) error {
projectEntries, err := l.mapper.createEntries(ld)
if err != nil {
return err
Expand Down
9 changes: 8 additions & 1 deletion exporter/collector/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,14 @@ func (me *MetricsExporter) PushMetrics(ctx context.Context, m pmetric.Metrics) e
}
}
}
return errors.Join(errs...)
err := errors.Join(errs...)
if err != nil {
me.obs.log.Error(
"Exporting metrics failed",
zap.Error(err),
)
}
return err
}

// exportToTimeSeries is the default exporting call to GCM.
Expand Down
15 changes: 12 additions & 3 deletions exporter/collector/traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,22 @@ import (
"go.opentelemetry.io/collector/pdata/ptrace"
"go.opentelemetry.io/otel/attribute"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
"go.uber.org/zap"

texporter "github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/trace"
)

// TraceExporter is a wrapper struct of OT cloud trace exporter.
type TraceExporter struct {
texporter *texporter.Exporter
logger *zap.Logger
}

func (te *TraceExporter) Shutdown(ctx context.Context) error {
return te.texporter.Shutdown(ctx)
}

func NewGoogleCloudTracesExporter(ctx context.Context, cfg Config, version string, timeout time.Duration) (*TraceExporter, error) {
func NewGoogleCloudTracesExporter(ctx context.Context, cfg Config, log *zap.Logger, version string, timeout time.Duration) (*TraceExporter, error) {
// TODO: https://github.com/GoogleCloudPlatform/opentelemetry-operations-go/pull/537#discussion_r1038290097
//nolint:errcheck
view.Register(ocgrpc.DefaultClientViews...)
Expand Down Expand Up @@ -70,7 +72,7 @@ func NewGoogleCloudTracesExporter(ctx context.Context, cfg Config, version strin
return nil, fmt.Errorf("error creating GoogleCloud Trace exporter: %w", err)
}

return &TraceExporter{texporter: exp}, nil
return &TraceExporter{texporter: exp, logger: log}, nil
}

func mappingFuncFromAKM(akm []AttributeMapping) func(attribute.Key) attribute.Key {
Expand Down Expand Up @@ -98,5 +100,12 @@ func (te *TraceExporter) PushTraces(ctx context.Context, td ptrace.Traces) error
spans = append(spans, sd...)
}

return te.texporter.ExportSpans(ctx, spans)
err := te.texporter.ExportSpans(ctx, spans)
if err != nil {
te.logger.Error(
"Exporting spans failed",
zap.Error(err),
)
}
return err
}
3 changes: 2 additions & 1 deletion exporter/collector/traces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/zap"
"google.golang.org/grpc"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/timestamppb"
Expand Down Expand Up @@ -100,7 +101,7 @@ func TestGoogleCloudTraceExport(t *testing.T) {

//nolint:errcheck
go srv.Serve(lis)
sde, err := NewGoogleCloudTracesExporter(ctx, test.cfg, "latest", DefaultTimeout)
sde, err := NewGoogleCloudTracesExporter(ctx, test.cfg, zap.NewNop(), "latest", DefaultTimeout)
if test.expectedErr != "" {
assert.EqualError(t, err, test.expectedErr)
return
Expand Down

0 comments on commit 63ebe18

Please sign in to comment.