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 #3434

Closed
wants to merge 5 commits into from
Closed
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
36 changes: 36 additions & 0 deletions exporter/awskinesisexporter/encoding/encoder.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 encoding

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")
)

// Encoder allows for the internal types to be converted to an consumable
// exported type which is written to the kinesis stream
type Encoder interface {
EncodeMetrics(md pdata.Metrics) error

EncodeTraces(td pdata.Traces) error

EncodeLogs(ld pdata.Logs) error
}
55 changes: 55 additions & 0 deletions exporter/awskinesisexporter/encoding/encoder_jaeger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// 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 encoding

import (
awskinesis "github.com/signalfx/opencensus-go-exporter-kinesis"
"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 _ Encoder = (*jaeger)(nil)

func Jaeger(kinesis *awskinesis.Exporter) Encoder {
return &jaeger{kinesis: kinesis}
}

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

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 {
return err
}
}
}

return nil
}

func (j *jaeger) EncodeMetrics(_ pdata.Metrics) error { return ErrUnsupportedEncodedType }
func (j *jaeger) EncodeLogs(_ pdata.Logs) error { return ErrUnsupportedEncodedType }
42 changes: 42 additions & 0 deletions exporter/awskinesisexporter/encoding/encoder_jaeger_test.go
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 encoding_test

import (
"testing"

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

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

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

assert.NoError(t, encoding.Jaeger(nil).EncodeTraces(pdata.NewTraces()), "Must not error when processing spans")
}

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

assert.Error(t, encoding.Jaeger(nil).EncodeMetrics(pdata.NewMetrics()), "Must error when trying to encode unsupported type")
}

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

assert.Error(t, encoding.Jaeger(nil).EncodeLogs(pdata.NewLogs()), "Must error when trying to encode unsupported type")
}
26 changes: 6 additions & 20 deletions exporter/awskinesisexporter/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ import (
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/encoding"
)

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

Expand All @@ -55,24 +56,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.encoder.EncodeTraces(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
}
}
e.logger.Error("Unable to write jaeger traces to kinesis", zap.Error(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/encoding"
)

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

return Exporter{k, params.Logger}, nil
return Exporter{
awskinesis: k,
encoder: encoding.Jaeger(k),
logger: params.Logger,
}, nil
}