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

Logging: cannot log on GCE #10

Closed
Lance0404 opened this issue Apr 17, 2019 · 7 comments · Fixed by #200
Closed

Logging: cannot log on GCE #10

Lance0404 opened this issue Apr 17, 2019 · 7 comments · Fixed by #200
Assignees
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Milestone

Comments

@Lance0404
Copy link

I have a web api deployed with docker image on GCP GCE. However failed to output any log to stackdriver logging under the tag which my VM stands for.

Most importantly, the same api deployed in my dev env (IDC) can sent log to GCP stackdriver logging successfully, which can be seen in stackdriver logging under the tag 'global'. Of course, with GOOGLE_APPLICATION_CREDENTIALS included.

Environment details

  1. Python 3.6.7
  2. ubuntu 18.04
  3. Cloud API access scopes: Allow full access to all Cloud APIs
  4. google related module
google-api-core==1.9.0
google-auth==1.6.3
google-cloud-core==0.29.1
google-cloud-logging==1.10.0
googleapis-common-protos==1.5.9
  1. the service account creating this VM has the role Logs Writer added.

Steps to reproduce

  1. deploy api with docker-compose within that GCE

Code example

  • this is how i prepare my cloud handler
client = google.cloud.logging.Client()
cloud_handler = CloudLoggingHandler(client, name='cloud')
logging.getLogger('cc').addHandler(cloud_handler)

Really appreciate any support. Let me know if further info are needed.

@tseaver tseaver changed the title cannot output GCE (python api) log to stackdriver logging Logging: cannot log on GCE Apr 17, 2019
@timfanda35
Copy link

You can try to set the log level.

I write a sample code to reproduce the problem:

import logging
import google
from google.cloud.logging.handlers.handlers import CloudLoggingHandler

client = google.cloud.logging.Client()
cloud_handler = CloudLoggingHandler(client, name='cloud')
logger = logging.getLogger('cc')
# logger.setLevel(logging.INFO)
logger.addHandler(cloud_handler)

logger.info("Hello World!")

Uncomment the # logger.setLevel(logging.INFO), and execute sample code.
You should see the logs in Stackdriver Logging global/cloud after seconds.

Reference:
https://googleapis.github.io/google-cloud-python/latest/logging/usage.html#cloud-logging-handler

@Lance0404
Copy link
Author

Lance0404 commented Apr 23, 2019

Hi @timfanda35 ,

Thank you so much for the fast reply and really sorry for my delayed response. I have modified my code in your way as:

client = google.cloud.logging.Client()
cloud_handler = CloudLoggingHandler(client, name='cloud')
# logging.getLogger('cc').addHandler(cloud_handler)
logger = logging.getLogger('cc')
logger.addHandler(cloud_handler)
logger.info('Hello World!')

However, yet to succeed.

The code locates in a file mylogging.py, which will be imported at the start of my flask app.
An easy way to test if my app is able to log onto stackdriver logging.

python manage.py shell

output message as below:

2019-04-23 02:01:33,090 - google.cloud.logging.handlers.transports.background_thread:background_thread:_thread_main:133 - DEBUG - 25 - - - Background thread started.
2019-04-23 02:01:33,090 - cc:mylogging:<module>:140 - INFO - 25 - - - Hello World!
2019-04-23 02:01:33,090 - cc:mylogging:<module>:140 - INFO - 25 - - - Hello World!
2019-04-23 02:01:33,120 - google.auth.transport.requests:requests:__call__:117 - DEBUG - 25 - - - Making request: POST https://oauth2.googleapis.com/token
2019-04-23 02:01:33,129 - urllib3.connectionpool:connectionpool:_new_conn:813 - DEBUG - 25 - - - Starting new HTTPS connection (1): oauth2.googleapis.com:443
2019-04-23 02:01:33,210 - urllib3.connectionpool:connectionpool:_make_request:393 - DEBUG - 25 - - - https://oauth2.googleapis.com:443 "POST /token HTTP/1.1" 200 None
2019-04-23 02:01:33,218 - flask.app:factory:create_app:16 - INFO - 25 - - - flask app is up by Lance!
2019-04-23 02:01:33,219 - flask.app:factory:create_app:30 - DEBUG - 25 - - - app.config['CLEAN_TABLE'] False
2019-04-23 02:01:33,301 - google.cloud.logging.handlers.transports.background_thread:background_thread:_safely_commit_batch:123 - DEBUG - 25 - - - Submitted 1 logs
2019-04-23 02:01:33,603 - flask.app:manage:<module>:24 - INFO - 25 - - - enter flask shell mode
>>>

From above, it seems like the code is functional or log shouldn't be shown as the format i designed. And it looks like some log was sent to gcp with status code as 200. But there is still no log shown in the stackdriver logging under the tag "GCE VM instance 'my instance name'".

How come it can be done in machine outside GCE, like my local machine, but not on GCE.

Thank you for the help.

@Lance0404
Copy link
Author

Hi @timfanda35 ,

Sorry I didn't notice that you told me to check stackdriver logging under the tag 'global/cloud'. The log sure did show up under this tag. Thank you so much.

I was always looking under the tag "GCE VM instance 'my instance name'", which was linked directly from the GCE GUI.

Is there any way to direct the log to stackdriver logging "GCE VM instance 'my instance name'"? Which is more straightforward.

Thank you for the help.

@mnguyen25
Copy link

Hi @Lance0404, is there any update on this?

@Lance0404
Copy link
Author

Hi @mnguyen25 , sorry that I don't have breakthrough for this, cause I changed to using AWS from this May due to my job changed. Thanks for asking. Would also be glad to know if someone figure it out.

@mnguyen25
Copy link

mnguyen25 commented Aug 12, 2019

@Lance0404, lately today, I figured it out, still don't know how to distinguish between cloud env and local env however

from google.cloud.logging.resource import Resource
import requests

res = Resource(
	type="gce_instance",
	labels={
		"instance_id": requests.get("http://metadata/computeMetadata/v1/instance/id", headers={"Metadata-Flavor": "Google"}).text
        },
)

client = google.cloud.logging.Client()

handler = client.get_default_handler(resource=res)

This will fail locally cause the request would hang there and fail eventually.

@plamut plamut transferred this issue from googleapis/google-cloud-python Feb 5, 2020
@product-auto-label product-auto-label bot added the api: logging Issues related to the googleapis/python-logging API. label Feb 5, 2020
@plamut plamut added the type: question Request for information or clarification. Not an issue. label Feb 5, 2020
@daniel-sanche daniel-sanche added this to the v2.4.0 milestone Feb 4, 2021
@daniel-sanche daniel-sanche added the priority: p2 Moderately-important priority. Fix may not be included in next release. label Feb 5, 2021
@daniel-sanche daniel-sanche self-assigned this Feb 5, 2021
@yoshi-automation yoshi-automation added the 🚨 This issue needs some love. label Feb 5, 2021
@daniel-sanche daniel-sanche added priority: p3 Desirable enhancement or fix. May not be included in next release. and removed priority: p2 Moderately-important priority. Fix may not be included in next release. labels Feb 8, 2021
@yoshi-automation yoshi-automation removed the 🚨 This issue needs some love. label Feb 8, 2021
@daniel-sanche daniel-sanche linked a pull request Mar 2, 2021 that will close this issue
@daniel-sanche
Copy link
Contributor

This should be fixed after #200. You will see it in the next release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: logging Issues related to the googleapis/python-logging API. priority: p3 Desirable enhancement or fix. May not be included in next release. type: question Request for information or clarification. Not an issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants