open-telemetry / opentelemetry-python-contrib

OpenTelemetry instrumentation for Python modules
https://opentelemetry.io
Apache License 2.0
701 stars 584 forks source link

Falcon exceptions should not always be masked as 500 and marked as error #1532

Open andre-meneses-fivestars opened 1 year ago

andre-meneses-fivestars commented 1 year ago

Is your feature request related to a problem? In a Falcon app, if we raise an Exception of a type that is a subclass of a falcon.HTTPError or falcon.HTTPStatus it ends up being always marked as error and the trace status code set as 500 (Except if it is a 404).

Describe the solution you'd like All exceptions that are subclasses of falcon.HTTPError or falcon.HTTPStatus should not always be masked with 500 status code. Additionally, status codes in the 4xx range are not server errors and should not be marked as errors, which, from what I can read in the code are not, but since everything is being set as 500, they end up being marked as errors.

Describe alternatives you've considered My suggestion would be something like checking the exception to see if it is a subclass of a falcon.HTTPError or falcon.HTTPStatus and try to extract its status code from there. Only if it wasn't possible we would follow into the current path.

#instrumentation/falcon/__init__.py around like 499 could be something like:

if exc_type and not req_succeeded:
  if "HTTPNotFound" in exc_type.__name__:
    status = "404"
    reason = "NotFound"
  else:
    if isinstance(exc, falcon.HTTPError) or isinstance(exc, falcon.HTTPStatus):
      try:
        status = int(exc.title.split(" ")[0])
      except ValueError:
        status = "500"
    reason = f"{exc_type.__name__}: {exc}"
rahuldimri commented 1 year ago

@andre-meneses-fivestars if you are not working on this. I would love to give a try on this.

andre-meneses-fivestars commented 1 year ago

@andre-meneses-fivestars if you are not working on this. I would love to give a try on this.

@rahuldimri I'm not looking at this. I managed to workaround in the project I'm working on...

rahuldimri commented 1 year ago

@andre-meneses-fivestars Can you assign this to me please.

FilipNikolovski commented 2 months ago

@rahuldimri @srikanthccv is there any progress on this one? This is also a big issue for us, I could pick it up if you don't mind.