Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4067489

Browse files
authored
Merge branch 'master' into drop-3.4-support
2 parents c8b36a4 + d376df1 commit 4067489

File tree

4 files changed

+56
-36
lines changed

4 files changed

+56
-36
lines changed

instrumentation/opentelemetry-instrumentation-botocore/tests/test_botocore_instrumentation.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,35 +104,36 @@ def test_s3_client(self):
104104
)
105105

106106
# Comment test for issue 1088
107-
# @mock_s3
108-
# def test_s3_put(self):
109-
# params = dict(Key="foo", Bucket="mybucket", Body=b"bar")
110-
# s3 = self.session.create_client("s3", region_name="us-west-2")
111-
# s3.create_bucket(Bucket="mybucket")
112-
# s3.put_object(**params)
113-
114-
# spans = self.memory_exporter.get_finished_spans()
115-
# assert spans
116-
# span = spans[0]
117-
# self.assertEqual(len(spans), 2)
118-
# self.assertEqual(span.attributes["aws.operation"], "CreateBucket")
119-
# assert_span_http_status_code(span, 200)
120-
# self.assertEqual(
121-
# span.resource,
122-
# Resource(
123-
# attributes={"endpoint": "s3", "operation": "createbucket"}
124-
# ),
125-
# )
126-
# self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
127-
# self.assertEqual(
128-
# spans[1].resource,
129-
# Resource(attributes={"endpoint": "s3", "operation": "putobject"}),
130-
# )
131-
# self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
132-
# self.assertEqual(
133-
# spans[1].attributes["params.Bucket"], str(params["Bucket"])
134-
# )
135-
# self.assertTrue("params.Body" not in spans[1].attributes.keys())
107+
@mock_s3
108+
def test_s3_put(self):
109+
params = dict(Key="foo", Bucket="mybucket", Body=b"bar")
110+
s3 = self.session.create_client("s3", region_name="us-west-2")
111+
location = {"LocationConstraint": "us-west-2"}
112+
s3.create_bucket(Bucket="mybucket", CreateBucketConfiguration=location)
113+
s3.put_object(**params)
114+
115+
spans = self.memory_exporter.get_finished_spans()
116+
assert spans
117+
span = spans[0]
118+
self.assertEqual(len(spans), 2)
119+
self.assertEqual(span.attributes["aws.operation"], "CreateBucket")
120+
assert_span_http_status_code(span, 200)
121+
self.assertEqual(
122+
span.resource,
123+
Resource(
124+
attributes={"endpoint": "s3", "operation": "createbucket"}
125+
),
126+
)
127+
self.assertEqual(spans[1].attributes["aws.operation"], "PutObject")
128+
self.assertEqual(
129+
spans[1].resource,
130+
Resource(attributes={"endpoint": "s3", "operation": "putobject"}),
131+
)
132+
self.assertEqual(spans[1].attributes["params.Key"], str(params["Key"]))
133+
self.assertEqual(
134+
spans[1].attributes["params.Bucket"], str(params["Bucket"])
135+
)
136+
self.assertTrue("params.Body" not in spans[1].attributes.keys())
136137

137138
@mock_sqs
138139
def test_sqs_client(self):

instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@
5656
# pylint: disable=unused-argument
5757
def _instrument(tracer_provider=None, span_callback=None):
5858
"""Enables tracing of all requests calls that go through
59-
:code:`requests.session.Session.request` (this includes
60-
:code:`requests.get`, etc.)."""
59+
:code:`requests.session.Session.request` (this includes
60+
:code:`requests.get`, etc.)."""
6161

6262
# Since
6363
# https://github.com/psf/requests/commit/d72d1162142d1bf8b1b5711c664fbbd674f349d1
@@ -121,9 +121,10 @@ def _instrumented_requests_call(
121121
with get_tracer(
122122
__name__, __version__, tracer_provider
123123
).start_as_current_span(span_name, kind=SpanKind.CLIENT) as span:
124-
span.set_attribute("component", "http")
125-
span.set_attribute("http.method", method.upper())
126-
span.set_attribute("http.url", url)
124+
if span.is_recording():
125+
span.set_attribute("component", "http")
126+
span.set_attribute("http.method", method.upper())
127+
span.set_attribute("http.url", url)
127128

128129
headers = get_or_create_headers()
129130
propagators.inject(type(headers).__setitem__, headers)
@@ -139,13 +140,13 @@ def _instrumented_requests_call(
139140
finally:
140141
context.detach(token)
141142

142-
if exception is not None:
143+
if exception is not None and span.is_recording():
143144
span.set_status(
144145
Status(_exception_to_canonical_code(exception))
145146
)
146147
span.record_exception(exception)
147148

148-
if result is not None:
149+
if result is not None and span.is_recording():
149150
span.set_attribute("http.status_code", result.status_code)
150151
span.set_attribute("http.status_text", result.reason)
151152
span.set_status(

instrumentation/opentelemetry-instrumentation-requests/tests/test_requests_integration.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,23 @@ def test_suppress_instrumentation(self):
147147

148148
self.assert_span(num_spans=0)
149149

150+
def test_not_recording(self):
151+
with mock.patch("opentelemetry.trace.INVALID_SPAN") as mock_span:
152+
RequestsInstrumentor().uninstrument()
153+
# original_tracer_provider returns a default tracer provider, which
154+
# in turn will return an INVALID_SPAN, which is always not recording
155+
RequestsInstrumentor().instrument(
156+
tracer_provider=self.original_tracer_provider
157+
)
158+
mock_span.is_recording.return_value = False
159+
result = self.perform_request(self.URL)
160+
self.assertEqual(result.text, "Hello!")
161+
self.assert_span(None, 0)
162+
self.assertFalse(mock_span.is_recording())
163+
self.assertTrue(mock_span.is_recording.called)
164+
self.assertFalse(mock_span.set_attribute.called)
165+
self.assertFalse(mock_span.set_status.called)
166+
150167
def test_distributed_context(self):
151168
previous_propagator = propagators.get_global_textmap()
152169
try:

tests/util/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ package_dir=
3838
packages=find_namespace:
3939
install_requires =
4040
opentelemetry-api
41+
opentelemetry-sdk
4142

4243
[options.extras_require]
4344
test = flask~=1.0

0 commit comments

Comments
 (0)