Skip to content

Commit 30568de

Browse files
committed
Make start_as_current_span default
1 parent 26d56c0 commit 30568de

File tree

12 files changed

+54
-29
lines changed

12 files changed

+54
-29
lines changed

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ tracer = trace.tracer()
6161
tracer.add_span_processor(
6262
SimpleExportSpanProcessor(ConsoleSpanExporter())
6363
)
64-
with tracer.start_span('foo'):
65-
with tracer.start_span('bar'):
66-
with tracer.start_span('baz'):
64+
with tracer.start_as_current_span('foo'):
65+
with tracer.start_as_current_span('bar'):
66+
with tracer.start_as_current_span('baz'):
6767
print(Context)
6868
```
6969

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def configure_opentelemetry(flask_app: flask.Flask):
6767
def hello():
6868
# emit a trace that measures how long the
6969
# sleep takes
70-
with trace.tracer().start_span("example-request"):
70+
with trace.tracer().start_as_current_span("example-request"):
7171
requests.get("http://www.example.com")
7272
return "hello"
7373

examples/trace/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545

4646
@app.route("/")
4747
def hello():
48-
with trace.tracer().start_span("parent"):
48+
with trace.tracer().start_as_current_span("parent"):
4949
requests.get("https://www.wikipedia.org/wiki/Rabbit")
5050
return "hello"
5151

ext/opentelemetry-ext-azure-monitor/examples/server.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
@app.route("/")
3636
def hello():
37-
with trace.tracer().start_span("parent"):
37+
with trace.tracer().start_as_current_span("parent"):
3838
requests.get("https://www.wikipedia.org/wiki/Rabbit")
3939
return "hello"
4040

ext/opentelemetry-ext-azure-monitor/examples/trace.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@
2323
SimpleExportSpanProcessor(AzureMonitorSpanExporter())
2424
)
2525

26-
with tracer.start_span("hello") as span:
26+
with tracer.start_as_current_span("hello") as span:
2727
print("Hello, World!")

ext/opentelemetry-ext-http-requests/src/opentelemetry/ext/http_requests/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def instrumented_request(self, method, url, *args, **kwargs):
6565
path = "<URL parses to None>"
6666
path = parsed_url.path
6767

68-
with tracer.start_span(path, kind=SpanKind.CLIENT) as span:
68+
with tracer.start_as_current_span(path, kind=SpanKind.CLIENT) as span:
6969
span.set_attribute("component", "http")
7070
span.set_attribute("http.method", method.upper())
7171
span.set_attribute("http.url", url)

ext/opentelemetry-ext-http-requests/tests/test_requests_integration.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ def setspanattr(key, value):
4040
self.span.set_attribute = setspanattr
4141
self.start_span_patcher = mock.patch.object(
4242
self.tracer,
43-
"start_span",
43+
"start_as_current_span",
4444
autospec=True,
4545
spec_set=True,
4646
return_value=self.span_context_manager,
4747
)
48-
self.start_span = self.start_span_patcher.start()
48+
self.start_as_current_span = self.start_span_patcher.start()
4949

5050
mocked_response = requests.models.Response()
5151
mocked_response.status_code = 200
@@ -70,7 +70,7 @@ def test_basic(self):
7070
url = "https://www.example.org/foo/bar?x=y#top"
7171
requests.get(url=url)
7272
self.assertEqual(1, len(self.send.call_args_list))
73-
self.tracer.start_span.assert_called_with(
73+
self.tracer.start_as_current_span.assert_called_with(
7474
"/foo/bar", kind=trace.SpanKind.CLIENT
7575
)
7676
self.span_context_manager.__enter__.assert_called_with()
@@ -97,10 +97,10 @@ def test_invalid_url(self):
9797
with self.assertRaises(exception_type):
9898
requests.post(url=url)
9999
self.assertTrue(
100-
self.tracer.start_span.call_args[0][0].startswith(
100+
self.tracer.start_as_current_span.call_args[0][0].startswith(
101101
"<Unparsable URL"
102102
),
103-
msg=self.tracer.start_span.call_args,
103+
msg=self.tracer.start_as_current_span.call_args,
104104
)
105105
self.span_context_manager.__enter__.assert_called_with()
106106
exitspan = self.span_context_manager.__exit__

ext/opentelemetry-ext-jaeger/README.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ gRPC is still not supported by this implementation.
5151
# add to the tracer
5252
tracer.add_span_processor(span_processor)
5353
54-
with tracer.start_span('foo'):
54+
with tracer.start_as_current_span('foo'):
5555
print('Hello world!')
5656
5757
# shutdown the span processor

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,16 @@
2929
tracer.add_span_processor(span_processor)
3030

3131
# create some spans for testing
32-
with tracer.start_span("foo") as foo:
32+
with tracer.start_as_current_span("foo") as foo:
3333
time.sleep(0.1)
3434
foo.set_attribute("my_atribbute", True)
3535
foo.add_event("event in foo", {"name": "foo1"})
36-
with tracer.start_span("bar") as bar:
36+
with tracer.start_as_current_span("bar") as bar:
3737
time.sleep(0.2)
3838
bar.set_attribute("speed", 100.0)
3939
bar.add_link(foo.get_context())
4040

41-
with tracer.start_span("baz") as baz:
41+
with tracer.start_as_current_span("baz") as baz:
4242
time.sleep(0.3)
4343
baz.set_attribute("name", "mauricio")
4444

opentelemetry-api/tests/trace/test_tracer.py

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ def test_start_span(self):
2929
with self.tracer.start_span("") as span:
3030
self.assertIsInstance(span, trace.Span)
3131

32+
def test_start_as_current_span(self):
33+
with self.tracer.start_as_current_span("") as span:
34+
self.assertIsInstance(span, trace.Span)
35+
3236
def test_create_span(self):
3337
span = self.tracer.create_span("")
3438
self.assertIsInstance(span, trace.Span)

opentelemetry-sdk/tests/trace/export/test_export.py

+21
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,27 @@ def test_simple_span_processor(self):
4848
span_processor = export.SimpleExportSpanProcessor(my_exporter)
4949
tracer.add_span_processor(span_processor)
5050

51+
with tracer.start_as_current_span("foo"):
52+
with tracer.start_as_current_span("bar"):
53+
with tracer.start_as_current_span("xxx"):
54+
pass
55+
56+
self.assertListEqual(["xxx", "bar", "foo"], spans_names_list)
57+
58+
def test_simple_span_processor_no_context(self):
59+
"""Check that we process spans that are never made active.
60+
61+
SpanProcessors should act on a span's start and end events whether or
62+
not it is ever the active span.
63+
"""
64+
tracer = trace.Tracer()
65+
66+
spans_names_list = []
67+
68+
my_exporter = MySpanExporter(destination=spans_names_list)
69+
span_processor = export.SimpleExportSpanProcessor(my_exporter)
70+
tracer.add_span_processor(span_processor)
71+
5172
with tracer.start_span("foo"):
5273
with tracer.start_span("bar"):
5374
with tracer.start_span("xxx"):

opentelemetry-sdk/tests/trace/export/test_in_memory_span_exporter.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ def test_get_finished_spans(self):
3131
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
3232
tracer.add_span_processor(span_processor)
3333

34-
with tracer.start_span("foo"):
35-
with tracer.start_span("bar"):
36-
with tracer.start_span("xxx"):
34+
with tracer.start_as_current_span("foo"):
35+
with tracer.start_as_current_span("bar"):
36+
with tracer.start_as_current_span("xxx"):
3737
pass
3838

3939
span_list = memory_exporter.get_finished_spans()
@@ -47,9 +47,9 @@ def test_clear(self):
4747
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
4848
tracer.add_span_processor(span_processor)
4949

50-
with tracer.start_span("foo"):
51-
with tracer.start_span("bar"):
52-
with tracer.start_span("xxx"):
50+
with tracer.start_as_current_span("foo"):
51+
with tracer.start_as_current_span("bar"):
52+
with tracer.start_as_current_span("xxx"):
5353
pass
5454

5555
memory_exporter.clear()
@@ -63,9 +63,9 @@ def test_shutdown(self):
6363
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
6464
tracer.add_span_processor(span_processor)
6565

66-
with tracer.start_span("foo"):
67-
with tracer.start_span("bar"):
68-
with tracer.start_span("xxx"):
66+
with tracer.start_as_current_span("foo"):
67+
with tracer.start_as_current_span("bar"):
68+
with tracer.start_as_current_span("xxx"):
6969
pass
7070

7171
span_list = memory_exporter.get_finished_spans()
@@ -74,9 +74,9 @@ def test_shutdown(self):
7474
memory_exporter.shutdown()
7575

7676
# after shutdown no new spans are accepted
77-
with tracer.start_span("foo"):
78-
with tracer.start_span("bar"):
79-
with tracer.start_span("xxx"):
77+
with tracer.start_as_current_span("foo"):
78+
with tracer.start_as_current_span("bar"):
79+
with tracer.start_as_current_span("xxx"):
8080
pass
8181

8282
span_list = memory_exporter.get_finished_spans()

0 commit comments

Comments
 (0)