Skip to content

Commit f803b30

Browse files
authored
Make start_as_current_span default (#246)
1 parent dba806f commit f803b30

File tree

12 files changed

+54
-29
lines changed

12 files changed

+54
-29
lines changed

README.md

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 4 additions & 0 deletions
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)

0 commit comments

Comments
 (0)