Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GAE REQUEST_LOG_ID env variable is wrong

Using the code below I'm sending an email on error. I'm trying to include a link to the Cloud Console logs in the email but the request ID seems to be wrong about 30% of the time.

If I find the request with the wrong ID it's always almost a perfect match except the last three characters are 0 (in the Stackdriver console) instead of 101 (returned from the env variable), always the same substitution - is this a bug with cloud console or am I trying to use these IDs wrong?

The code (stripped down version):

class ErrorAlertMiddleware(object):
    def process_response(self, request, response):
        if response.status_code == 500:
            logger.info(os.environ.get('REQUEST_LOG_ID'))

            msg = 'Link to logs: https://console.cloud.google.com/logs/viewer?' + '&'.join((
                'project=%s' % MY_APP_ID,
                'expandAll=true',
                'filters=request_id:%s' % os.environ.get('REQUEST_LOG_ID'),
                'resource=gae_app',
            ))

            # this is a utility func that simply sends email
            sendemail(ERROR_RECIPIENT, msg)
        return response

Note I've also logged the REQUEST_LOG_ID to ensure it's not being encoded or something and the log output matches what shows in the link

like image 565
Michoel Avatar asked Jan 12 '17 03:01

Michoel


1 Answers

Instead of os.environ.get('REQUEST_LOG_ID'), use request.environ.get('REQUEST_LOG_ID').

It may be possible that os.environ['REQUEST_LOG_ID'] changes between the start of the current request and the time you access it, but request.environ['REQUEST_LOG_ID'] should not change once the request is initialized. The docs state that if one request ID is greater than another, than it occurred later than the other. This implies that the requestID in the Stackdriver console was generated before the one in your email link. This makes me think that somewhere along the line, os.environ['REQUEST_LOG_ID'] is being updated from '....000' to '....101' before you access it, while the copy in request.environ['REQUEST_LOG_ID'] should remain unchanged.

For more info on the request.environ, take a look at the source code in google.appengine.runtime.request_environment.py. I haven't really found documentation on that, but that code led me to believe that the os.environ is not as safe to access as the request.environ.

like image 99
Brendan Goggin Avatar answered Oct 06 '22 01:10

Brendan Goggin