<commit_before>import botocore.session
from botocore.exceptions import ParamValidationError
from moto import (  # pylint: disable=import-error
    mock_ec2,
    mock_kinesis,
    mock_kms,
    mock_lambda,
    mock_s3,
    mock_sqs,
)

from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.test.test_base import TestBase


def assert_span_http_status_code(span, code):
    """Assert on the span"s "http.status_code" tag"""
    tag = span.attributes["http.status_code"]
    assert tag == code, "%r != %r" % (tag, code)


class TestBotocoreInstrumentor(TestBase):
    """Botocore integration testsuite"""

    def setUp(self):
        super().setUp()
        BotocoreInstrumentor().instrument()

        self.session = botocore.session.get_session()
        self.session.set_credentials(
            access_key="access-key", secret_key="secret-key"
        )

    def tearDown(self):
        super().tearDown()
        BotocoreInstrumentor().uninstrument()

    @mock_ec2
    def test_traced_client(self):
        ec2 = self.session.create_client("ec2", region_name="us-west-2")

        ec2.describe_instances()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.agent"], "botocore")
        self.assertEqual(span.attributes["aws.region"], "us-west-2")
        self.assertEqual(span.attributes["aws.operation"], "DescribeInstances")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={
                    "endpoint": "ec2",
                    "operation": "describeinstances",
                }
            ),
        )
        self.assertEqual(span.name, "ec2.command")

    @mock_ec2
    def test_traced_client_analytics(self):
        ec2 = self.session.create_client("ec2", region_name="us-west-2")
        ec2.describe_instances()

        spans = self.memory_exporter.get_finished_spans()
        assert spans

    @mock_s3
    def test_s3_client(self):
        s3 = self.session.create_client("s3", region_name="us-west-2")

        s3.list_buckets()
        s3.list_buckets()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 2)
        self.assertEqual(span.attributes["aws.operation"], "ListBuckets")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "s3", "operation": "listbuckets"}
            ),
        )

        # testing for span error
        self.memory_exporter.get_finished_spans()
        with self.assertRaises(ParamValidationError):
            s3.list_objects(bucket="mybucket")
        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[2]
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "s3", "operation": "listobjects"}
            ),
        )

    # Comment test for issue 1088
    # @mock_s3
    # def test_s3_put(self):
    #     params = dict(Key="foo", Bucket="mybucket", Body=b"bar")
    #     s3 = self.session.create_client("s3", region_name="us-west-2")
    #     s3.create_bucket(Bucket="mybucket")
    #     s3.put_object(**params)

    #     spans = self.memory_exporter.get_finished_spans()
    #     assert spans
    #     span = spans[0]
    #     self.assertEqual(len(spans), 2)
    #     self.assertEqual(span.attributes["aws.operation"], "CreateBucket")
    #     assert_span_http_status_code(span, 200)
    #     self.assertEqual(
    #         span.resource,
    #         Resource(
    #             attributes={"endpoint": "s3", "operation": "createbucket"}
    #         ),
    #     )
    #     self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
    #     self.assertEqual(
    #         spans[1].resource,
    #         Resource(attributes={"endpoint": "s3", "operation": "putobject"}),
    #     )
    #     self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
    #     self.assertEqual(
    #         spans[1].attributes["params.Bucket"], str(params["Bucket"])
    #     )
    #     self.assertTrue("params.Body" not in spans[1].attributes.keys())

    @mock_sqs
    def test_sqs_client(self):
        sqs = self.session.create_client("sqs", region_name="us-east-1")

        sqs.list_queues()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListQueues")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "sqs", "operation": "listqueues"}
            ),
        )

    @mock_kinesis
    def test_kinesis_client(self):
        kinesis = self.session.create_client(
            "kinesis", region_name="us-east-1"
        )

        kinesis.list_streams()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListStreams")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "kinesis", "operation": "liststreams"}
            ),
        )

    @mock_kinesis
    def test_unpatch(self):
        kinesis = self.session.create_client(
            "kinesis", region_name="us-east-1"
        )

        BotocoreInstrumentor().uninstrument()

        kinesis.list_streams()
        spans = self.memory_exporter.get_finished_spans()
        assert not spans, spans

    @mock_sqs
    def test_double_patch(self):
        sqs = self.session.create_client("sqs", region_name="us-east-1")

        BotocoreInstrumentor().instrument()
        BotocoreInstrumentor().instrument()

        sqs.list_queues()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        self.assertEqual(len(spans), 1)

    @mock_lambda
    def test_lambda_client(self):
        lamb = self.session.create_client("lambda", region_name="us-east-1")

        lamb.list_functions()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListFunctions")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "lambda", "operation": "listfunctions"}
            ),
        )

    @mock_kms
    def test_kms_client(self):
        kms = self.session.create_client("kms", region_name="us-east-1")

        kms.list_keys(Limit=21)

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListKeys")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(attributes={"endpoint": "kms", "operation": "listkeys"}),
        )

        # checking for protection on sts against security leak
        self.assertTrue("params" not in span.attributes.keys())
<commit_msg>instrumentation/botocore: fix failing test (#1100)<commit_after>import botocore.session
from botocore.exceptions import ParamValidationError
from moto import (  # pylint: disable=import-error
    mock_ec2,
    mock_kinesis,
    mock_kms,
    mock_lambda,
    mock_s3,
    mock_sqs,
)

from opentelemetry.instrumentation.botocore import BotocoreInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.test.test_base import TestBase


def assert_span_http_status_code(span, code):
    """Assert on the span"s "http.status_code" tag"""
    tag = span.attributes["http.status_code"]
    assert tag == code, "%r != %r" % (tag, code)


class TestBotocoreInstrumentor(TestBase):
    """Botocore integration testsuite"""

    def setUp(self):
        super().setUp()
        BotocoreInstrumentor().instrument()

        self.session = botocore.session.get_session()
        self.session.set_credentials(
            access_key="access-key", secret_key="secret-key"
        )

    def tearDown(self):
        super().tearDown()
        BotocoreInstrumentor().uninstrument()

    @mock_ec2
    def test_traced_client(self):
        ec2 = self.session.create_client("ec2", region_name="us-west-2")

        ec2.describe_instances()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.agent"], "botocore")
        self.assertEqual(span.attributes["aws.region"], "us-west-2")
        self.assertEqual(span.attributes["aws.operation"], "DescribeInstances")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={
                    "endpoint": "ec2",
                    "operation": "describeinstances",
                }
            ),
        )
        self.assertEqual(span.name, "ec2.command")

    @mock_ec2
    def test_traced_client_analytics(self):
        ec2 = self.session.create_client("ec2", region_name="us-west-2")
        ec2.describe_instances()

        spans = self.memory_exporter.get_finished_spans()
        assert spans

    @mock_s3
    def test_s3_client(self):
        s3 = self.session.create_client("s3", region_name="us-west-2")

        s3.list_buckets()
        s3.list_buckets()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 2)
        self.assertEqual(span.attributes["aws.operation"], "ListBuckets")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "s3", "operation": "listbuckets"}
            ),
        )

        # testing for span error
        self.memory_exporter.get_finished_spans()
        with self.assertRaises(ParamValidationError):
            s3.list_objects(bucket="mybucket")
        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[2]
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "s3", "operation": "listobjects"}
            ),
        )

    # Comment test for issue 1088
    @mock_s3
    def test_s3_put(self):
        params = dict(Key="foo", Bucket="mybucket", Body=b"bar")
        s3 = self.session.create_client("s3", region_name="us-west-2")
        location = {"LocationConstraint": "us-west-2"}
        s3.create_bucket(Bucket="mybucket", CreateBucketConfiguration=location)
        s3.put_object(**params)

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 2)
        self.assertEqual(span.attributes["aws.operation"], "CreateBucket")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "s3", "operation": "createbucket"}
            ),
        )
        self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
        self.assertEqual(
            spans[1].resource,
            Resource(attributes={"endpoint": "s3", "operation": "putobject"}),
        )
        self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
        self.assertEqual(
            spans[1].attributes["params.Bucket"], str(params["Bucket"])
        )
        self.assertTrue("params.Body" not in spans[1].attributes.keys())

    @mock_sqs
    def test_sqs_client(self):
        sqs = self.session.create_client("sqs", region_name="us-east-1")

        sqs.list_queues()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListQueues")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "sqs", "operation": "listqueues"}
            ),
        )

    @mock_kinesis
    def test_kinesis_client(self):
        kinesis = self.session.create_client(
            "kinesis", region_name="us-east-1"
        )

        kinesis.list_streams()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListStreams")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "kinesis", "operation": "liststreams"}
            ),
        )

    @mock_kinesis
    def test_unpatch(self):
        kinesis = self.session.create_client(
            "kinesis", region_name="us-east-1"
        )

        BotocoreInstrumentor().uninstrument()

        kinesis.list_streams()
        spans = self.memory_exporter.get_finished_spans()
        assert not spans, spans

    @mock_sqs
    def test_double_patch(self):
        sqs = self.session.create_client("sqs", region_name="us-east-1")

        BotocoreInstrumentor().instrument()
        BotocoreInstrumentor().instrument()

        sqs.list_queues()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        self.assertEqual(len(spans), 1)

    @mock_lambda
    def test_lambda_client(self):
        lamb = self.session.create_client("lambda", region_name="us-east-1")

        lamb.list_functions()

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListFunctions")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(
                attributes={"endpoint": "lambda", "operation": "listfunctions"}
            ),
        )

    @mock_kms
    def test_kms_client(self):
        kms = self.session.create_client("kms", region_name="us-east-1")

        kms.list_keys(Limit=21)

        spans = self.memory_exporter.get_finished_spans()
        assert spans
        span = spans[0]
        self.assertEqual(len(spans), 1)
        self.assertEqual(span.attributes["aws.region"], "us-east-1")
        self.assertEqual(span.attributes["aws.operation"], "ListKeys")
        assert_span_http_status_code(span, 200)
        self.assertEqual(
            span.resource,
            Resource(attributes={"endpoint": "kms", "operation": "listkeys"}),
        )

        # checking for protection on sts against security leak
        self.assertTrue("params" not in span.attributes.keys())

{'fraction_non_alphanumeric': 0.12345138280565636, 'fraction_numerical': 0.011825804029533225, 'mean_word_length': 2.4379436437943642, 'pattern_counts': {'":': 37, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4268996', 'n_tokens_mistral': 4986, 'n_tokens_neox': 4767, 'n_words': 736}