Skip to content

Commit

Permalink
resolving some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Paramadon committed Dec 12, 2024
1 parent cbb2dee commit 4bd7a1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
17 changes: 8 additions & 9 deletions exporter/awsemfexporter/emf_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ import (
"context"
"errors"
"fmt"
"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"strings"
"sync"

"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/google/uuid"
"go.opentelemetry.io/collector/component"
Expand All @@ -22,6 +21,7 @@ import (
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/awsemfexporter/internal/appsignals"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/awsutil"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/aws/cwlogs"
)

Expand Down Expand Up @@ -58,12 +58,17 @@ func newEmfExporter(config *Config, set exporter.Settings) (*emfExporter, error)

config.logger = set.Logger

collectorIdentifier, err := uuid.NewRandom()
if err != nil {
return nil, err
}

// Initialize emfExporter without AWS session and structured logs
emfExporter := &emfExporter{
config: config,
metricTranslator: newMetricTranslator(*config),
retryCnt: config.AWSSessionSettings.MaxRetries,
collectorID: uuid.New().String(),
collectorID: collectorIdentifier.String(),
pusherMap: map[cwlogs.StreamKey]cwlogs.Pusher{},
processResourceLabels: func(map[string]string) {},
}
Expand Down Expand Up @@ -210,12 +215,6 @@ func (emf *emfExporter) start(_ context.Context, host component.Host) error {
// Assign to the struct
emf.svcStructuredLog = svcStructuredLog

if emf.config.IsAppSignalsEnabled() {
userAgent := appsignals.NewUserAgent()
svcStructuredLog.Handlers().Build.PushBackNamed(userAgent.Handler())
emf.processResourceLabels = userAgent.Process
}

// Optionally configure middleware
if emf.config.MiddlewareID != nil {
awsmiddleware.TryConfigure(emf.config.logger, host, *emf.config.MiddlewareID, awsmiddleware.SDKv1(svcStructuredLog.Handlers()))
Expand Down
22 changes: 19 additions & 3 deletions exporter/awsemfexporter/emf_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@ import (

"github.com/amazon-contributing/opentelemetry-collector-contrib/extension/awsmiddleware"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs/cloudwatchlogsiface"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/consumer/consumererror"
"go.opentelemetry.io/collector/exporter/exportertest"
"go.opentelemetry.io/collector/pdata/pmetric"
Expand Down Expand Up @@ -286,9 +289,19 @@ func TestConsumeMetricsWithOnlyLogStreamPlaceholder(t *testing.T) {
assert.NotNil(t, pusherMap)
}

type mockCWLogsClient struct {
cloudwatchlogsiface.CloudWatchLogsAPI
}

func (m *mockCWLogsClient) PutLogEvents(*cloudwatchlogs.PutLogEventsInput) (*cloudwatchlogs.PutLogEventsOutput, error) {
// Return a successful response
return &cloudwatchlogs.PutLogEventsOutput{}, nil
}

func TestConsumeMetricsWithWrongPlaceholder(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

factory := NewFactory()
expCfg := factory.CreateDefaultConfig().(*Config)
expCfg.Region = "us-west-2"
Expand All @@ -308,8 +321,9 @@ func TestConsumeMetricsWithWrongPlaceholder(t *testing.T) {
assert.NotNil(t, exp)

exp.config.logger = logger

// Create a mock host
mockHost := &mockHost{}
mockHost := componenttest.NewNopHost()

// Call start
err = exp.start(ctx, mockHost)
Expand All @@ -318,19 +332,21 @@ func TestConsumeMetricsWithWrongPlaceholder(t *testing.T) {
md := generateTestMetrics(testMetric{
metricNames: []string{"metric_1", "metric_2"},
metricValues: [][]float64{{100}, {4}},
resourceAttributeMap: map[string]any{
resourceAttributeMap: map[string]interface{}{
"aws.ecs.cluster.name": "test-cluster-name",
"aws.ecs.task.id": "test-task-id",
},
})
require.NoError(t, exp.pushMetricsData(ctx, md))
require.Error(t, exp.pushMetricsData(ctx, md)) // this returns Permanent error: UnrecognizedClientException: The security token included in the request is invalid.
require.NoError(t, exp.shutdown(ctx))

pusherMap, ok := exp.pusherMap[cwlogs.StreamKey{
LogGroupName: expCfg.LogGroupName,
LogStreamName: expCfg.LogStreamName,
}]
assert.True(t, ok)
assert.NotNil(t, pusherMap)

}

func TestPushMetricsDataWithErr(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion exporter/awsemfexporter/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package awsemfexporter // import "github.com/open-telemetry/opentelemetry-collec

import (
"context"

"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/exporter"
"go.opentelemetry.io/collector/exporter/exporterhelper"
Expand Down Expand Up @@ -59,7 +60,6 @@ func createMetricsExporter(ctx context.Context, params exporter.Settings, config
return nil, err
}


exporter, err := exporterhelper.NewMetricsExporter(
ctx,
params,
Expand Down

0 comments on commit 4bd7a1e

Please sign in to comment.