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

Converted resource to hold Resource attribute than a string #866

Merged
merged 9 commits into from
Jun 30, 2020
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@

from opentelemetry.ext.boto.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.sdk.trace import Resource
from opentelemetry.trace import SpanKind, get_tracer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -123,9 +124,14 @@ def _common_request( # pylint: disable=too-many-locals
) as span:
if args:
http_method = args[0]
span.resource = "%s.%s" % (endpoint_name, http_method.lower())
span.resource = Resource(
labels={
"endpoint": endpoint_name,
"http_method": http_method.lower(),
}
)
else:
span.resource = endpoint_name
span.resource = Resource(labels={"endpoint": endpoint_name})

add_span_arg_tags(
span, endpoint_name, args, args_name, traced_args,
Expand Down
49 changes: 40 additions & 9 deletions ext/opentelemetry-ext-boto/tests/test_boto_instrumentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)

from opentelemetry.ext.boto import BotoInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.test.test_base import TestBase


Expand Down Expand Up @@ -69,7 +70,12 @@ def test_ec2_client(self):
span = spans[1]
self.assertEqual(span.attributes["aws.operation"], "RunInstances")
assert_span_http_status_code(span, 200)
self.assertEqual(span.resource, "ec2.runinstances")
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "ec2", "http_method": "runinstances"}
),
)
self.assertEqual(span.attributes["http.method"], "POST")
self.assertEqual(span.attributes["aws.region"], "us-west-2")
self.assertEqual(span.name, "ec2.command")
Expand Down Expand Up @@ -123,7 +129,10 @@ def test_s3_client(self):
self.assertEqual(len(spans), 3)
span = spans[2]
assert_span_http_status_code(span, 200)
self.assertEqual(span.resource, "s3.head")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
)
self.assertEqual(span.attributes["http.method"], "HEAD")
self.assertEqual(span.attributes["aws.operation"], "head_bucket")
self.assertEqual(span.name, "s3.command")
Expand All @@ -135,7 +144,10 @@ def test_s3_client(self):
spans = self.memory_exporter.get_finished_spans()
assert spans
span = spans[2]
self.assertEqual(span.resource, "s3.head")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
)

@mock_s3_deprecated
def test_s3_put(self):
Expand All @@ -152,15 +164,24 @@ def test_s3_put(self):
self.assertEqual(len(spans), 3)
self.assertEqual(spans[0].attributes["aws.operation"], "create_bucket")
assert_span_http_status_code(spans[0], 200)
self.assertEqual(spans[0].resource, "s3.put")
self.assertEqual(
spans[0].resource,
Resource(labels={"endpoint": "s3", "http_method": "put"}),
)
# get bucket
self.assertEqual(spans[1].attributes["aws.operation"], "head_bucket")
self.assertEqual(spans[1].resource, "s3.head")
self.assertEqual(
spans[1].resource,
Resource(labels={"endpoint": "s3", "http_method": "head"}),
)
# put object
self.assertEqual(
spans[2].attributes["aws.operation"], "_send_file_internal"
)
self.assertEqual(spans[2].resource, "s3.put")
self.assertEqual(
spans[2].resource,
Resource(labels={"endpoint": "s3", "http_method": "put"}),
)

@mock_lambda_deprecated
def test_unpatch(self):
Expand Down Expand Up @@ -200,7 +221,10 @@ def test_lambda_client(self):
self.assertEqual(len(spans), 2)
span = spans[0]
assert_span_http_status_code(span, 200)
self.assertEqual(span.resource, "lambda.get")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "lambda", "http_method": "get"}),
)
self.assertEqual(span.attributes["http.method"], "GET")
self.assertEqual(span.attributes["aws.region"], "us-east-2")
self.assertEqual(span.attributes["aws.operation"], "list_functions")
Expand All @@ -214,7 +238,12 @@ def test_sts_client(self):
spans = self.memory_exporter.get_finished_spans()
assert spans
span = spans[0]
self.assertEqual(span.resource, "sts.getfederationtoken")
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "sts", "http_method": "getfederationtoken"}
),
)
self.assertEqual(span.attributes["aws.region"], "us-west-2")
self.assertEqual(
span.attributes["aws.operation"], "GetFederationToken"
Expand All @@ -238,5 +267,7 @@ def test_elasticache_client(self):
spans = self.memory_exporter.get_finished_spans()
assert spans
span = spans[0]
self.assertEqual(span.resource, "elasticache")
self.assertEqual(
span.resource, Resource(labels={"endpoint": "elasticcache"})
)
self.assertEqual(span.attributes["aws.region"], "us-west-2")
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

from opentelemetry.ext.botocore.version import __version__
from opentelemetry.instrumentation.instrumentor import BaseInstrumentor
from opentelemetry.sdk.trace import Resource
from opentelemetry.trace import SpanKind, get_tracer

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -98,10 +99,15 @@ def _patched_api_call(self, original_func, instance, args, kwargs):
operation = None
if args:
operation = args[0]
span.resource = "%s.%s" % (endpoint_name, operation.lower())
span.resource = Resource(
labels={
"endpoint": endpoint_name,
"operation": operation.lower(),
}
)

else:
span.resource = endpoint_name
span.resource = Resource(labels={"endpoint": endpoint_name})

add_span_arg_tags(
span,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
)

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


Expand Down Expand Up @@ -49,7 +50,12 @@ def test_traced_client(self):
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, "ec2.describeinstances")
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "ec2", "operation": "describeinstances"}
),
)
self.assertEqual(span.name, "ec2.command")

@mock_ec2
Expand All @@ -73,7 +79,10 @@ def test_s3_client(self):
self.assertEqual(len(spans), 2)
self.assertEqual(span.attributes["aws.operation"], "ListBuckets")
assert_span_http_status_code(span, 200)
self.assertEqual(span.resource, "s3.listbuckets")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "listbuckets"}),
)

# testing for span error
self.memory_exporter.get_finished_spans()
Expand All @@ -82,7 +91,10 @@ def test_s3_client(self):
spans = self.memory_exporter.get_finished_spans()
assert spans
span = spans[2]
self.assertEqual(span.resource, "s3.listobjects")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "listobjects"}),
)

@mock_s3
def test_s3_put(self):
Expand All @@ -97,9 +109,15 @@ def test_s3_put(self):
self.assertEqual(len(spans), 2)
self.assertEqual(span.attributes["aws.operation"], "CreateBucket")
assert_span_http_status_code(span, 200)
self.assertEqual(span.resource, "s3.createbucket")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "s3", "operation": "createbucket"}),
)
self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
self.assertEqual(spans[1].resource, "s3.putobject")
self.assertEqual(
spans[1].resource,
Resource(labels={"endpoint": "s3", "operation": "putobject"}),
)
self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
self.assertEqual(
spans[1].attributes["params.Bucket"], str(params["Bucket"])
Expand All @@ -119,7 +137,10 @@ def test_sqs_client(self):
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, "sqs.listqueues")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "sqs", "operation": "listqueues"}),
)

@mock_kinesis
def test_kinesis_client(self):
Expand All @@ -136,7 +157,12 @@ def test_kinesis_client(self):
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, "kinesis.liststreams")
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "kinesis", "operation": "liststreams"}
),
)

@mock_kinesis
def test_unpatch(self):
Expand Down Expand Up @@ -176,7 +202,12 @@ def test_lambda_client(self):
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, "lambda.listfunctions")
self.assertEqual(
span.resource,
Resource(
labels={"endpoint": "lambda", "operation": "listfunctions"}
),
)

@mock_kms
def test_kms_client(self):
Expand All @@ -191,7 +222,10 @@ def test_kms_client(self):
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, "kms.listkeys")
self.assertEqual(
span.resource,
Resource(labels={"endpoint": "kms", "operation": "listkeys"}),
)

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