- Notifications
You must be signed in to change notification settings - Fork 798
botocore: Add support for AWS Secrets Manager semantic convention attribute #3765
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits Select commit Hold shift + click to select a range
08b3c55
botocore: Add support for AWS Secrets Manager semantic convention att…
lukeina2z eb87ba8
add ChangeLog.
lukeina2z aedb099
Merge branch 'main' into secrets-foo
tammy-baylis-swi 20b7a86
Update instrumentation/opentelemetry-instrumentation-botocore/src/ope…
lukeina2z 5cc2893
Merge branch 'main' into secrets-foo
emdneto aa7db40
Update instrumentation/opentelemetry-instrumentation-botocore/tests/t…
xrmx 02037a9
Merge branch 'main' into secrets-foo
xrmx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
45 changes: 45 additions & 0 deletions 45 ...entation-botocore/src/opentelemetry/instrumentation/botocore/extensions/secretsmanager.py
This file contains hidden or 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,45 @@ | ||
# 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. | ||
from opentelemetry.instrumentation.botocore.extensions.types import ( | ||
_AttributeMapT, | ||
_AwsSdkExtension, | ||
_BotocoreInstrumentorContext, | ||
_BotoResultT, | ||
) | ||
from opentelemetry.semconv._incubating.attributes.aws_attributes import ( | ||
AWS_SECRETSMANAGER_SECRET_ARN, | ||
) | ||
from opentelemetry.trace.span import Span | ||
| ||
| ||
class _SecretsManagerExtension(_AwsSdkExtension): | ||
def extract_attributes(self, attributes: _AttributeMapT): | ||
""" | ||
SecretId is extracted if a secret ARN, the function extracts the attribute | ||
only if the SecretId parameter is provided as an arn which starts with | ||
`arn:aws:secretsmanager:` | ||
""" | ||
secret_id = self._call_context.params.get("SecretId") | ||
if secret_id and secret_id.startswith("arn:aws:secretsmanager:"): | ||
attributes[AWS_SECRETSMANAGER_SECRET_ARN] = secret_id | ||
| ||
def on_success( | ||
self, | ||
span: Span, | ||
result: _BotoResultT, | ||
instrumentor_context: _BotocoreInstrumentorContext, | ||
): | ||
secret_arn = result.get("ARN") | ||
if secret_arn: | ||
span.set_attribute(AWS_SECRETSMANAGER_SECRET_ARN, secret_arn) |
86 changes: 86 additions & 0 deletions 86 instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_secretsmanager.py
This file contains hidden or 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,86 @@ | ||
# 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. | ||
| ||
import botocore.session | ||
from moto import mock_aws | ||
| ||
from opentelemetry.instrumentation.botocore import BotocoreInstrumentor | ||
from opentelemetry.semconv._incubating.attributes.aws_attributes import ( | ||
AWS_SECRETSMANAGER_SECRET_ARN, | ||
) | ||
from opentelemetry.test.test_base import TestBase | ||
| ||
| ||
class TestSecretsManagerExtension(TestBase): | ||
def setUp(self): | ||
super().setUp() | ||
BotocoreInstrumentor().instrument() | ||
session = botocore.session.get_session() | ||
session.set_credentials( | ||
access_key="access-key", secret_key="secret-key" | ||
) | ||
self.region = "us-west-2" | ||
self.client = session.create_client( | ||
"secretsmanager", region_name=self.region | ||
) | ||
| ||
def tearDown(self): | ||
super().tearDown() | ||
BotocoreInstrumentor().uninstrument() | ||
| ||
def create_secret_and_get_arn(self, name: str = "test-secret") -> str: | ||
""" | ||
Create a secret in mocked Secrets Manager and return its ARN. | ||
""" | ||
# Clear spans before creating secret for helper method | ||
self.memory_exporter.clear() | ||
response = self.client.create_secret( | ||
Name=name, SecretString="test-secret-value" | ||
) | ||
return response["ARN"] | ||
| ||
@mock_aws | ||
def test_tag_resource_with_arn(self): | ||
secret_arn = self.create_secret_and_get_arn() | ||
| ||
self.client.tag_resource( | ||
SecretId=secret_arn, Tags=[{"Key": "Environment", "Value": "Test"}] | ||
) | ||
| ||
spans = self.memory_exporter.get_finished_spans() | ||
assert spans | ||
self.assertEqual(len(spans), 2) | ||
span = spans[1] # tag_resource span | ||
self.assertEqual( | ||
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN], | ||
secret_arn, | ||
) | ||
| ||
@mock_aws | ||
def test_create_secret(self): | ||
secret_name = "test-secret" | ||
response = self.client.create_secret( | ||
Name=secret_name, SecretString="test-secret-value" | ||
) | ||
secret_arn = response["ARN"] | ||
| ||
spans = self.memory_exporter.get_finished_spans() | ||
assert spans | ||
self.assertEqual(len(spans), 1) | ||
span = spans[0] # create_secret span | ||
# Should capture ARN from response | ||
self.assertEqual( | ||
span.attributes[AWS_SECRETSMANAGER_SECRET_ARN], | ||
secret_arn, | ||
) |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.