Skip to content

Commit 939900b

Browse files
author
Alex Boten
committed
Merge remote-tracking branch 'origin/master' into codeboten/issue_303
2 parents 39df92d + 105fe2f commit 939900b

File tree

30 files changed

+680
-234
lines changed

30 files changed

+680
-234
lines changed

.flake8

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ignore =
33
E501 # line too long, defer to black
44
F401 # unused import, defer to pylint
55
W503 # allow line breaks after binary ops, not after
6+
E203 # allow whitespace before ':' (https://github.com/psf/black#slices)
67
exclude =
78
.bzr
89
.git

.github/CODEOWNERS

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
# This file controls who is tagged for review for any given pull request.
33

44
# For anything not explicitly taken by someone else:
5-
* @a-feld @c24t @carlosalberto @lzchen @Oberon00 @reyang @toumorokoshi
5+
* @open-telemetry/python-approvers

.isort.cfg

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ line_length=79
1414
multi_line_output=3
1515
skip=target
1616
skip_glob=ext/opentelemetry-ext-jaeger/src/opentelemetry/ext/jaeger/gen/*
17+
known_first_party=opentelemetry

examples/basic_tracer/tests/test_tracer.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,17 @@
1313
# limitations under the License.
1414
import os
1515
import subprocess
16+
import sys
1617
import unittest
1718

1819

1920
class TestBasicTracerExample(unittest.TestCase):
2021
def test_basic_tracer(self):
2122
dirpath = os.path.dirname(os.path.realpath(__file__))
2223
test_script = "{}/../tracer.py".format(dirpath)
23-
output = subprocess.check_output(test_script).decode()
24+
output = subprocess.check_output(
25+
(sys.executable, test_script)
26+
).decode()
2427

2528
self.assertIn('name="foo"', output)
2629
self.assertIn('name="bar"', output)

examples/basic_tracer/tracer.py

-2
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,3 @@
4848
with tracer.start_as_current_span("bar"):
4949
with tracer.start_as_current_span("baz"):
5050
print(Context)
51-
52-
span_processor.shutdown()

examples/http/server.py

-1
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,3 @@ def hello():
6565

6666
if __name__ == "__main__":
6767
app.run(debug=True)
68-
span_processor.shutdown()

examples/http/tests/test_http.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
# limitations under the License.
1414
import os
1515
import subprocess
16+
import sys
1617
import unittest
1718
from time import sleep
1819

@@ -22,13 +23,15 @@ class TestHttpExample(unittest.TestCase):
2223
def setup_class(cls):
2324
dirpath = os.path.dirname(os.path.realpath(__file__))
2425
server_script = "{}/../server.py".format(dirpath)
25-
cls.server = subprocess.Popen([server_script])
26+
cls.server = subprocess.Popen([sys.executable, server_script])
2627
sleep(1)
2728

2829
def test_http(self):
2930
dirpath = os.path.dirname(os.path.realpath(__file__))
3031
test_script = "{}/../tracer_client.py".format(dirpath)
31-
output = subprocess.check_output(test_script).decode()
32+
output = subprocess.check_output(
33+
(sys.executable, test_script)
34+
).decode()
3235
self.assertIn('name="/"', output)
3336

3437
@classmethod

examples/http/tracer_client.py

-1
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,3 @@
5151
# Spans and propagating context as appropriate.
5252
http_requests.enable(tracer)
5353
response = requests.get(url="http://127.0.0.1:5000/")
54-
span_processor.shutdown()

examples/opentelemetry-example-app/src/opentelemetry_example_app/metrics_example.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,17 @@
3030
("environment",),
3131
)
3232

33-
label_values = ("staging",)
33+
label_set = meter.get_label_set({"environment": "staging"})
3434

3535
# Direct metric usage
36-
counter.add(label_values, 25)
36+
counter.add(label_set, 25)
3737

3838
# Handle usage
39-
counter_handle = counter.get_handle(label_values)
39+
counter_handle = counter.get_handle(label_set)
4040
counter_handle.add(100)
4141

4242
# Record batch usage
43-
meter.record_batch(label_values, [(counter, 50)])
43+
meter.record_batch(label_set, [(counter, 50)])
4444
print(counter_handle.data)
4545

4646
# TODO: exporters

ext/opentelemetry-ext-flask/src/opentelemetry/ext/flask/__init__.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -62,20 +62,21 @@ def _before_flask_request():
6262

6363
tracer = trace.tracer()
6464

65+
attributes = otel_wsgi.collect_request_attributes(environ)
66+
if flask_request.url_rule:
67+
# For 404 that result from no route found, etc, we don't have a url_rule.
68+
attributes["http.route"] = flask_request.url_rule.rule
6569
span = tracer.start_span(
6670
span_name,
6771
parent_span,
6872
kind=trace.SpanKind.SERVER,
73+
attributes=attributes,
6974
start_time=environ.get(_ENVIRON_STARTTIME_KEY),
7075
)
7176
activation = tracer.use_span(span, end_on_exit=True)
7277
activation.__enter__()
7378
environ[_ENVIRON_ACTIVATION_KEY] = activation
7479
environ[_ENVIRON_SPAN_KEY] = span
75-
otel_wsgi.add_request_attributes(span, environ)
76-
if flask_request.url_rule:
77-
# For 404 that result from no route found, etc, we don't have a url_rule.
78-
span.set_attribute("http.route", flask_request.url_rule.rule)
7980

8081

8182
def _teardown_flask_request(exc):

ext/opentelemetry-ext-flask/tests/test_flask_integration.py

+49-26
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import opentelemetry.ext.flask as otel_flask
2222
from opentelemetry import trace as trace_api
2323
from opentelemetry.ext.testutil.wsgitestutil import WsgiTestBase
24+
from opentelemetry.sdk.trace import MAX_NUM_ATTRIBUTES
25+
from opentelemetry.sdk.util import BoundedDict
2426

2527

2628
class TestFlaskIntegration(WsgiTestBase):
@@ -40,15 +42,22 @@ def hello_endpoint(helloid):
4042
self.client = Client(self.app, BaseResponse)
4143

4244
def test_simple(self):
43-
expected_attrs = {
44-
"component": "http",
45-
"http.method": "GET",
46-
"http.host": "localhost",
47-
"http.url": "http://localhost/hello/123",
48-
"http.route": "/hello/<int:helloid>",
49-
"http.status_code": 200,
50-
"http.status_text": "OK",
51-
}
45+
expected_attrs = BoundedDict.from_map(
46+
MAX_NUM_ATTRIBUTES,
47+
{
48+
"component": "http",
49+
"http.method": "GET",
50+
"http.server_name": "localhost",
51+
"http.scheme": "http",
52+
"host.port": 80,
53+
"http.host": "localhost",
54+
"http.target": "/hello/123",
55+
"http.flavor": "1.1",
56+
"http.route": "/hello/<int:helloid>",
57+
"http.status_text": "OK",
58+
"http.status_code": 200,
59+
},
60+
)
5261
resp = self.client.get("/hello/123")
5362
self.assertEqual(200, resp.status_code)
5463
self.assertEqual([b"Hello: 123"], list(resp.response))
@@ -59,14 +68,21 @@ def test_simple(self):
5968
self.assertEqual(span_list[0].attributes, expected_attrs)
6069

6170
def test_404(self):
62-
expected_attrs = {
63-
"component": "http",
64-
"http.method": "POST",
65-
"http.host": "localhost",
66-
"http.url": "http://localhost/bye",
67-
"http.status_code": 404,
68-
"http.status_text": "NOT FOUND",
69-
}
71+
expected_attrs = BoundedDict.from_map(
72+
MAX_NUM_ATTRIBUTES,
73+
{
74+
"component": "http",
75+
"http.method": "POST",
76+
"http.server_name": "localhost",
77+
"http.scheme": "http",
78+
"host.port": 80,
79+
"http.host": "localhost",
80+
"http.target": "/bye",
81+
"http.flavor": "1.1",
82+
"http.status_text": "NOT FOUND",
83+
"http.status_code": 404,
84+
},
85+
)
7086
resp = self.client.post("/bye")
7187
self.assertEqual(404, resp.status_code)
7288
resp.close()
@@ -77,15 +93,22 @@ def test_404(self):
7793
self.assertEqual(span_list[0].attributes, expected_attrs)
7894

7995
def test_internal_error(self):
80-
expected_attrs = {
81-
"component": "http",
82-
"http.method": "GET",
83-
"http.host": "localhost",
84-
"http.url": "http://localhost/hello/500",
85-
"http.route": "/hello/<int:helloid>",
86-
"http.status_code": 500,
87-
"http.status_text": "INTERNAL SERVER ERROR",
88-
}
96+
expected_attrs = BoundedDict.from_map(
97+
MAX_NUM_ATTRIBUTES,
98+
{
99+
"component": "http",
100+
"http.method": "GET",
101+
"http.server_name": "localhost",
102+
"http.scheme": "http",
103+
"host.port": 80,
104+
"http.host": "localhost",
105+
"http.target": "/hello/500",
106+
"http.flavor": "1.1",
107+
"http.route": "/hello/<int:helloid>",
108+
"http.status_text": "INTERNAL SERVER ERROR",
109+
"http.status_code": 500,
110+
},
111+
)
89112
resp = self.client.get("/hello/500")
90113
self.assertEqual(500, resp.status_code)
91114
resp.close()

ext/opentelemetry-ext-jaeger/README.rst

-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,6 @@ gRPC is still not supported by this implementation.
6161
with tracer.start_as_current_span('foo'):
6262
print('Hello world!')
6363
64-
# shutdown the span processor
65-
# TODO: this has to be improved so user doesn't need to call it manually
66-
span_processor.shutdown()
67-
6864
The `examples <./examples>`_ folder contains more elaborated examples.
6965

7066
References

ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py

-4
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,3 @@
4646
time.sleep(0.2)
4747

4848
time.sleep(0.1)
49-
50-
# shutdown the span processor
51-
# TODO: this has to be improved so user doesn't need to call it manually
52-
span_processor.shutdown()

ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def test_explicit_start_time(self):
132132
now = time.time()
133133
with self.shim.start_active_span("TestSpan", start_time=now) as scope:
134134
result = util.time_seconds_from_ns(scope.span.unwrap().start_time)
135-
# Tolerate inaccuracies of less than a microsecond.
136-
# TODO: Put a link to an explanation in the docs.
135+
# Tolerate inaccuracies of less than a microsecond. See Note:
136+
# https://open-telemetry.github.io/opentelemetry-python/opentelemetry.ext.opentracing_shim.html
137137
# TODO: This seems to work consistently, but we should find out the
138138
# biggest possible loss of precision.
139139
self.assertAlmostEqual(result, now, places=6)
@@ -146,8 +146,8 @@ def test_explicit_end_time(self):
146146
span.finish(now)
147147

148148
end_time = util.time_seconds_from_ns(span.unwrap().end_time)
149-
# Tolerate inaccuracies of less than a microsecond.
150-
# TODO: Put a link to an explanation in the docs.
149+
# Tolerate inaccuracies of less than a microsecond. See Note:
150+
# https://open-telemetry.github.io/opentelemetry-python/opentelemetry.ext.opentracing_shim.html
151151
# TODO: This seems to work consistently, but we should find out the
152152
# biggest possible loss of precision.
153153
self.assertAlmostEqual(end_time, now, places=6)
@@ -412,8 +412,8 @@ def test_log_kv(self):
412412
span.unwrap().events[1].timestamp
413413
)
414414
self.assertEqual(span.unwrap().events[1].attributes["foo"], "bar")
415-
# Tolerate inaccuracies of less than a microsecond.
416-
# TODO: Put a link to an explanation in the docs.
415+
# Tolerate inaccuracies of less than a microsecond. See Note:
416+
# https://open-telemetry.github.io/opentelemetry-python/opentelemetry.ext.opentracing_shim.html
417417
# TODO: This seems to work consistently, but we should find out the
418418
# biggest possible loss of precision.
419419
self.assertAlmostEqual(result, now, places=6)

0 commit comments

Comments
 (0)