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

Set span status on wsgi errors #864

Merged
merged 5 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions ext/opentelemetry-ext-wsgi/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- Set span status on wsgi errors
([#864](https://github.com/open-telemetry/opentelemetry-python/pull/864))

## 0.4a0

Released 2020-02-21
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
This library provides a WSGI middleware that can be used on any WSGI framework
(such as Django / Flask) to track requests timing through OpenTelemetry.
Expand Down Expand Up @@ -211,8 +210,8 @@ def __call__(self, environ, start_response):
return _end_span_after_iterating(
iterable, span, self.tracer, token
)
except: # noqa
# TODO Set span status (cf. https://github.com/open-telemetry/opentelemetry-python/issues/292)
except Exception as ex:
span.set_status(Status(StatusCanonicalCode.INTERNAL, str(ex)))
span.end()
context.detach(token)
raise
Expand Down
15 changes: 15 additions & 0 deletions ext/opentelemetry-ext-wsgi/tests/test_wsgi_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import opentelemetry.ext.wsgi as otel_wsgi
from opentelemetry import trace as trace_api
from opentelemetry.test.wsgitestutil import WsgiTestBase
from opentelemetry.trace.status import StatusCanonicalCode


class Response:
Expand Down Expand Up @@ -73,6 +74,11 @@ def error_wsgi(environ, start_response):
return [b"*"]


def error_wsgi_unhandled(environ, start_response):
assert isinstance(environ, dict)
raise ValueError


class TestWsgiApplication(WsgiTestBase):
def validate_response(
self, response, error=None, span_name="HTTP GET", http_method="GET"
Expand Down Expand Up @@ -148,6 +154,15 @@ def test_wsgi_exc_info(self):
response = app(self.environ, self.start_response)
self.validate_response(response, error=ValueError)

def test_wsgi_internal_error(self):
app = otel_wsgi.OpenTelemetryMiddleware(error_wsgi_unhandled)
self.assertRaises(ValueError, app, self.environ, self.start_response)
span_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
self.assertEqual(
span_list[0].status.canonical_code, StatusCanonicalCode.INTERNAL,
)

def test_override_span_name(self):
"""Test that span_names can be overwritten by our callback function."""
span_name = "Dymaxion"
Expand Down