Skip to content

Commit

Permalink
Make start_as_current_span default (#246)
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t authored Oct 28, 2019
1 parent dba806f commit f803b30
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 29 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ tracer = trace.tracer()
tracer.add_span_processor(
SimpleExportSpanProcessor(ConsoleSpanExporter())
)
with tracer.start_span('foo'):
with tracer.start_span('bar'):
with tracer.start_span('baz'):
with tracer.start_as_current_span('foo'):
with tracer.start_as_current_span('bar'):
with tracer.start_as_current_span('baz'):
print(Context)
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def configure_opentelemetry(flask_app: flask.Flask):
def hello():
# emit a trace that measures how long the
# sleep takes
with trace.tracer().start_span("example-request"):
with trace.tracer().start_as_current_span("example-request"):
requests.get("http://www.example.com")
return "hello"

Expand Down
2 changes: 1 addition & 1 deletion examples/trace/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

@app.route("/")
def hello():
with trace.tracer().start_span("parent"):
with trace.tracer().start_as_current_span("parent"):
requests.get("https://www.wikipedia.org/wiki/Rabbit")
return "hello"

Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-azure-monitor/examples/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

@app.route("/")
def hello():
with trace.tracer().start_span("parent"):
with trace.tracer().start_as_current_span("parent"):
requests.get("https://www.wikipedia.org/wiki/Rabbit")
return "hello"

Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-azure-monitor/examples/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@
SimpleExportSpanProcessor(AzureMonitorSpanExporter())
)

with tracer.start_span("hello") as span:
with tracer.start_as_current_span("hello") as span:
print("Hello, World!")
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def instrumented_request(self, method, url, *args, **kwargs):
path = "<URL parses to None>"
path = parsed_url.path

with tracer.start_span(path, kind=SpanKind.CLIENT) as span:
with tracer.start_as_current_span(path, kind=SpanKind.CLIENT) as span:
span.set_attribute("component", "http")
span.set_attribute("http.method", method.upper())
span.set_attribute("http.url", url)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def setspanattr(key, value):
self.span.set_attribute = setspanattr
self.start_span_patcher = mock.patch.object(
self.tracer,
"start_span",
"start_as_current_span",
autospec=True,
spec_set=True,
return_value=self.span_context_manager,
)
self.start_span = self.start_span_patcher.start()
self.start_as_current_span = self.start_span_patcher.start()

mocked_response = requests.models.Response()
mocked_response.status_code = 200
Expand All @@ -70,7 +70,7 @@ def test_basic(self):
url = "https://www.example.org/foo/bar?x=y#top"
requests.get(url=url)
self.assertEqual(1, len(self.send.call_args_list))
self.tracer.start_span.assert_called_with(
self.tracer.start_as_current_span.assert_called_with(
"/foo/bar", kind=trace.SpanKind.CLIENT
)
self.span_context_manager.__enter__.assert_called_with()
Expand All @@ -97,10 +97,10 @@ def test_invalid_url(self):
with self.assertRaises(exception_type):
requests.post(url=url)
self.assertTrue(
self.tracer.start_span.call_args[0][0].startswith(
self.tracer.start_as_current_span.call_args[0][0].startswith(
"<Unparsable URL"
),
msg=self.tracer.start_span.call_args,
msg=self.tracer.start_as_current_span.call_args,
)
self.span_context_manager.__enter__.assert_called_with()
exitspan = self.span_context_manager.__exit__
Expand Down
2 changes: 1 addition & 1 deletion ext/opentelemetry-ext-jaeger/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ gRPC is still not supported by this implementation.
# add to the tracer
tracer.add_span_processor(span_processor)
with tracer.start_span('foo'):
with tracer.start_as_current_span('foo'):
print('Hello world!')
# shutdown the span processor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,16 @@
tracer.add_span_processor(span_processor)

# create some spans for testing
with tracer.start_span("foo") as foo:
with tracer.start_as_current_span("foo") as foo:
time.sleep(0.1)
foo.set_attribute("my_atribbute", True)
foo.add_event("event in foo", {"name": "foo1"})
with tracer.start_span("bar") as bar:
with tracer.start_as_current_span("bar") as bar:
time.sleep(0.2)
bar.set_attribute("speed", 100.0)
bar.add_link(foo.get_context())

with tracer.start_span("baz") as baz:
with tracer.start_as_current_span("baz") as baz:
time.sleep(0.3)
baz.set_attribute("name", "mauricio")

Expand Down
4 changes: 4 additions & 0 deletions opentelemetry-api/tests/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ def test_start_span(self):
with self.tracer.start_span("") as span:
self.assertIsInstance(span, trace.Span)

def test_start_as_current_span(self):
with self.tracer.start_as_current_span("") as span:
self.assertIsInstance(span, trace.Span)

def test_create_span(self):
span = self.tracer.create_span("")
self.assertIsInstance(span, trace.Span)
Expand Down
21 changes: 21 additions & 0 deletions opentelemetry-sdk/tests/trace/export/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ def test_simple_span_processor(self):
span_processor = export.SimpleExportSpanProcessor(my_exporter)
tracer.add_span_processor(span_processor)

with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("xxx"):
pass

self.assertListEqual(["xxx", "bar", "foo"], spans_names_list)

def test_simple_span_processor_no_context(self):
"""Check that we process spans that are never made active.
SpanProcessors should act on a span's start and end events whether or
not it is ever the active span.
"""
tracer = trace.Tracer()

spans_names_list = []

my_exporter = MySpanExporter(destination=spans_names_list)
span_processor = export.SimpleExportSpanProcessor(my_exporter)
tracer.add_span_processor(span_processor)

with tracer.start_span("foo"):
with tracer.start_span("bar"):
with tracer.start_span("xxx"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def test_get_finished_spans(self):
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
tracer.add_span_processor(span_processor)

with tracer.start_span("foo"):
with tracer.start_span("bar"):
with tracer.start_span("xxx"):
with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("xxx"):
pass

span_list = memory_exporter.get_finished_spans()
Expand All @@ -47,9 +47,9 @@ def test_clear(self):
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
tracer.add_span_processor(span_processor)

with tracer.start_span("foo"):
with tracer.start_span("bar"):
with tracer.start_span("xxx"):
with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("xxx"):
pass

memory_exporter.clear()
Expand All @@ -63,9 +63,9 @@ def test_shutdown(self):
span_processor = export.SimpleExportSpanProcessor(memory_exporter)
tracer.add_span_processor(span_processor)

with tracer.start_span("foo"):
with tracer.start_span("bar"):
with tracer.start_span("xxx"):
with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("xxx"):
pass

span_list = memory_exporter.get_finished_spans()
Expand All @@ -74,9 +74,9 @@ def test_shutdown(self):
memory_exporter.shutdown()

# after shutdown no new spans are accepted
with tracer.start_span("foo"):
with tracer.start_span("bar"):
with tracer.start_span("xxx"):
with tracer.start_as_current_span("foo"):
with tracer.start_as_current_span("bar"):
with tracer.start_as_current_span("xxx"):
pass

span_list = memory_exporter.get_finished_spans()
Expand Down

0 comments on commit f803b30

Please sign in to comment.