Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor AWS Kinesis exporter to allow for extended types of encoding #3655

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions exporter/awskinesisexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,21 @@ package awskinesisexporter

import (
"context"
"fmt"

awskinesis "github.com/signalfx/opencensus-go-exporter-kinesis"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
jaegertranslator "go.opentelemetry.io/collector/translator/trace/jaeger"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awskinesisexporter/internal/translate"
)

// Exporter implements an OpenTelemetry trace exporter that exports all spans to AWS Kinesis
type Exporter struct {
awskinesis *awskinesis.Exporter
ew translate.ExportWriter
logger *zap.Logger
}

Expand All @@ -55,24 +57,9 @@ func (e Exporter) Shutdown(context.Context) error {

// ConsumeTraces receives a span batch and exports it to AWS Kinesis
func (e Exporter) ConsumeTraces(_ context.Context, td pdata.Traces) error {
pBatches, err := jaegertranslator.InternalTracesToJaegerProto(td)
err := e.ew.WriteTraces(td)
if err != nil {
e.logger.Error("error translating span batch", zap.Error(err))
return consumererror.Permanent(err)
}
// TODO: Use a multi error type
var exportErr error
for _, pBatch := range pBatches {
for _, span := range pBatch.GetSpans() {
if span.Process == nil {
span.Process = pBatch.Process
}
err := e.awskinesis.ExportSpan(span)
if err != nil {
e.logger.Error("error exporting span to awskinesis", zap.Error(err))
exportErr = err
}
}
err = fmt.Errorf("issues writing traces to kinesis: %w", err)
}
return exportErr
return err
}
8 changes: 7 additions & 1 deletion exporter/awskinesisexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/exporter/exporterhelper"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awskinesisexporter/internal/translate"
)

const (
Expand Down Expand Up @@ -93,5 +95,9 @@ func createTracesExporter(
return nil, err
}

return Exporter{k, params.Logger}, nil
return Exporter{
awskinesis: k,
ew: translate.JaegerExporter(k),
logger: params.Logger,
}, nil
}
36 changes: 36 additions & 0 deletions exporter/awskinesisexporter/internal/translate/translate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translate

import (
"errors"

"go.opentelemetry.io/collector/consumer/pdata"
)

var (
// ErrUnsupportedEncodedType is used when the encoder type does not support the type of encoding
ErrUnsupportedEncodedType = errors.New("unsupported type to encode")
)

// ExportWriter wraps the kinesis exporter and transforms the data into
// the desired output format.
type ExportWriter interface {
WriteMetrics(md pdata.Metrics) error

WriteTraces(td pdata.Traces) error

WriteLogs(ld pdata.Logs) error
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translate

import (
awskinesis "github.com/signalfx/opencensus-go-exporter-kinesis"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/consumer/pdata"
jaegertranslator "go.opentelemetry.io/collector/translator/trace/jaeger"
)

type jaeger struct {
kinesis *awskinesis.Exporter
}

// Ensure the jaeger encoder meets the interface at compile time.
var _ ExportWriter = (*jaeger)(nil)

func JaegerExporter(kinesis *awskinesis.Exporter) ExportWriter {
return &jaeger{kinesis: kinesis}
}

func (j *jaeger) WriteTraces(td pdata.Traces) error {
traces, err := jaegertranslator.InternalTracesToJaegerProto(td)
if err != nil {
return err
}

var errs []error
for _, trace := range traces {
for _, span := range trace.GetSpans() {
if span.Process == nil {
span.Process = trace.Process
}
if err := j.kinesis.ExportSpan(span); err != nil {
errs = append(errs, err)
}
}
}

return consumererror.Combine(errs)
}

func (j *jaeger) WriteMetrics(_ pdata.Metrics) error { return ErrUnsupportedEncodedType }
func (j *jaeger) WriteLogs(_ pdata.Logs) error { return ErrUnsupportedEncodedType }
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package translate_test

import (
"testing"

"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/consumer/pdata"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awskinesisexporter/internal/translate"
)

func TestEncodingTraceData(t *testing.T) {
t.Parallel()

assert.NoError(t, translate.JaegerExporter(nil).WriteTraces(pdata.NewTraces()), "Must not error when processing spans")
}

func TestEncodingMetricData(t *testing.T) {
t.Parallel()

assert.Error(t, translate.JaegerExporter(nil).WriteMetrics(pdata.NewMetrics()), "Must error when trying to encode unsupported type")
}

func TestEncodingLogData(t *testing.T) {
t.Parallel()

assert.Error(t, translate.JaegerExporter(nil).WriteLogs(pdata.NewLogs()), "Must error when trying to encode unsupported type")
}