Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use start_as_current_span by default #246

Merged
merged 1 commit into from
Oct 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This package doesn't need/use current span reference, feels like start_span is actually available for this kind of scenarios

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point, maybe we do want start_span here.

We may want to put spans into the context by default, even if we close the span before returning control. If start_span started and activated the span, and we had another method named start_span_but_dont_activate, would it still be a good idea to use the latter here?

@Oberon00 what do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current span is actually referenced here, abstracted in the propagators layer:

propagators.inject(tracer, type(headers).__setitem__, headers)

https://github.com/open-telemetry/opentelemetry-python/blob/master/opentelemetry-api/src/opentelemetry/propagators/__init__.py#L47

Which I think is the correct behavior. I'm sure others will also rely on the assumption that the span was started here, regardless of whether the integration directly uses the context or not.

I would argue that most common cases require the tracer to use the new span, and we should only opt out of that behavior in advanced cases.

I'm +1 for start_span_but_dont_activate, or maybe pass that it as a keyword argument. (will_activate=False).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is either a misusage of the inject API or the inject API should provide a way to supply the Span to propagate.

But I definitely agree on using start_as_current_span here. E.g. consider what should happen if we decided to represent HTTP redirects as their own spans: They would need to become child spans.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I view the active span more as an inter-instrumentation communication. If only a single instrumentation library was involved it could devise its own mechanism to manage parent spans easily (although ofc relying on the current span should still be preferred for parent/child relations).

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