-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add otelaws middleware to support trace propagation in the AWS SDK v2…
… module (#2856) * Add middleware for trace propagation * Add tests and make propagator configurable * Fix func doc * Update changelog * Fix linter errors * Fix changelog again Co-authored-by: Anthony Mirabella <a9@aneurysm9.com>
- Loading branch information
1 parent
151c999
commit c011266
Showing
5 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/aws_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright The 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 otelaws | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/aws/smithy-go/middleware" | ||
smithyhttp "github.com/aws/smithy-go/transport/http" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"go.opentelemetry.io/otel/propagation" | ||
) | ||
|
||
type mockPropagator struct { | ||
injectKey string | ||
injectValue string | ||
} | ||
|
||
func (p mockPropagator) Inject(ctx context.Context, carrier propagation.TextMapCarrier) { | ||
carrier.Set(p.injectKey, p.injectValue) | ||
} | ||
func (p mockPropagator) Extract(ctx context.Context, carrier propagation.TextMapCarrier) context.Context { | ||
return context.TODO() | ||
} | ||
func (p mockPropagator) Fields() []string { | ||
return []string{} | ||
} | ||
|
||
func Test_otelMiddlewares_finalizeMiddleware(t *testing.T) { | ||
stack := middleware.Stack{ | ||
Finalize: middleware.NewFinalizeStep(), | ||
} | ||
|
||
propagator := mockPropagator{ | ||
injectKey: "mock-key", | ||
injectValue: "mock-value", | ||
} | ||
|
||
m := otelMiddlewares{ | ||
propagator: propagator, | ||
} | ||
|
||
err := m.finalizeMiddleware(&stack) | ||
require.NoError(t, err) | ||
|
||
input := &smithyhttp.Request{ | ||
Request: &http.Request{ | ||
Header: http.Header{}, | ||
}, | ||
} | ||
|
||
next := middleware.HandlerFunc(func(ctx context.Context, input interface{}) (output interface{}, metadata middleware.Metadata, err error) { | ||
return nil, middleware.Metadata{}, nil | ||
}) | ||
|
||
_, _, _ = stack.Finalize.HandleMiddleware(context.Background(), input, next) | ||
|
||
// Assert header has been updated with injected values | ||
key := http.CanonicalHeaderKey(propagator.injectKey) | ||
value := propagator.injectValue | ||
|
||
assert.Contains(t, input.Header, key) | ||
assert.Contains(t, input.Header[key], value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
instrumentation/github.com/aws/aws-sdk-go-v2/otelaws/config_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// Copyright The 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 otelaws | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"go.opentelemetry.io/otel" | ||
) | ||
|
||
func TestWithTextMapPropagator(t *testing.T) { | ||
cfg := config{} | ||
propagator := otel.GetTextMapPropagator() | ||
|
||
option := WithTextMapPropagator(propagator) | ||
option.apply(&cfg) | ||
|
||
assert.Equal(t, cfg.TextMapPropagator, propagator) | ||
} |