firebase / firebase/firebase-functions-python

logging library doesn't support Exceptions

Ouverte
#172 3 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
feature-request
Langage dominant
Python
Étoiles
167
Forks
34
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

The `logger` in `firebase_functions` doesn't seem to provide reasonable support for exceptions.

In [the example](https://firebase.google.com/docs/functions/writing-and-viewing-logs?gen=2nd#python_2), firebase suggests extracting a portion of the error and including it:

```
try
...
except:
e = sys.exc_info()[0]
# Attach an error object as the second argument
logger.error("Unable to read quote from Firestore, sending default instead", error=e)
```

However this fails with an error:

```
../../../../Library/Caches/pypoetry/virtualenvs/oasis-zYbk3Ww0-py3.11/lib/python3.11/site-packages/firebase_functions/logger.py:130: in error
write(_entry_from_args(LogSeverity.ERROR, *args, **kwargs))
../../../../Library/Caches/pypoetry/virtualenvs/oasis-zYbk3Ww0-py3.11/lib/python3.11/site-packages/firebase_functions/logger.py:95: in write
print(_json.dumps(_remove_circular(entry)), file=write_file)
../../../../.pyenv/versions/3.11.7/lib/python3.11/json/__init__.py:231: in dumps
return _default_encoder.encode(obj)
../../../../.pyenv/versions/3.11.7/lib/python3.11/json/encoder.py:200: in encode
chunks = self.iterencode(o, _one_shot=True)
../../../../.pyenv/versions/3.11.7/lib/python3.11/json/encoder.py:258: in iterencode
return _iterencode(o, 0)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = , o =

def default(self, o):
"""Implement this method in a subclass such that it returns
a serializable object for ``o``, or calls the base implementation
(to raise a ``TypeError``).

For example, to support arbitrary iterators, you could
implement default like this::

def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
# Let the base class default method raise the TypeError
return JSONEncoder.default(self, o)

"""
> raise TypeError(f'Object of type {o.__class__.__name__} '
f'is not JSON serializable')
E TypeError: Object of type type is not JSON serializable

../../../../.pyenv/versions/3.11.7/lib/python3.11/json/encoder.py:180: TypeError
```

Moreover it's not really an ideal API to log a subset of an exception. Ideally the entire stack trace would be included.

If you instead simply `print` the error, you can see a reasonable stack trace in the logs for the function.

I'd expect the example to not throw. I'd also expect the logging API to explicitly support logging Exceptions with stack traces

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez dans firebase_functions/logger.py, en particulier avec error(), write() et _entry_from_args(), puis comparez l’exemple documenté de logging Firebase avec l’échec de sérialisation JSON présenté ici. La tâche est terminée lorsque le logging d’une Exception ne provoque plus d’erreur et que l’API de logging inclut la stack trace complète de l’Exception dans les logs de la fonction.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
python
Domaine
observability-sre
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
Calme
Clarté
Plutôt claire
Accessibilité débutants
52/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.