Skip to content
This repository was archived by the owner on Aug 13, 2026. It is now read-only.
This repository was archived by the owner on Aug 13, 2026. It is now read-only.

Logging: don't import grpc if grpc disabled #12

Closed

Description

@kapilt

I noticed the Stackdriver module supports usage with grpc installed, which is great. However it currently still tries to import _gapic even if grpc usage is explicitly disabled via env var. This causes some additional latency for cli & serverless usage.

$ cat x.py
import time
import sys


def main():
    mcount = len(sys.modules)
    t = time.time()
    from google.cloud.logging import Client
    print("modules:%d %0.2f sec" % (
        len(sys.modules) - mcount, time.time() - t))


if __name__ == '__main__':
    main()

so this tries two tests, one with the default google.cloud.logging.client imports, and one where the try/except around the _gapic imports is replaced with the except clause ie modification of these lines
https://github.com/googleapis/google-cloud-python/blob/master/logging/google/cloud/logging/client.py#L21

default import

(custodian) bash-3.2$ python x.py 
modules:576 0.51 sec
(custodian) bash-3.2$ python x.py 
modules:576 0.50 sec
(custodian) bash-3.2$ python x.py 
modules:576 0.44 sec
(custodian) bash-3.2$ python x.py 
modules:576 0.47 sec

Disable import of _gapi in

(custodian) bash-3.2$ python x.py 
modules:510 0.38 sec
(custodian) bash-3.2$ python x.py 
modules:510 0.37 sec
(custodian) bash-3.2$ python x.py 
modules:510 0.37 sec
(custodian) bash-3.2$ python x.py 
modules:510 0.38 sec
(custodian) bash-3.2$ python x.py 
modules:510 0.37 sec

so baseline improvement for cli / serverless execution of ~100ms. for serverless environments this effect is more pronounced I've noticed.

Ideally the DISABLE_GRPC environment could be checked before attempting to import the _gapic modules.

Activity

  1. crwilcox commented on Dec 26, 2018

    @crwilcox
    Contributor

    @kapilt this could be done. Today this is mostly done to guard against cases where gRPC isn't available. Would you be interested in submitting a PR for this change?

  2. changed the title [-]google.cloud.logging - don't import grpc if grpc disabled[/-] [+]Logging: don't import grpc if grpc disabled[/+] on Jan 3, 2019
  3. added this to the v3.0.0 milestone on Feb 4, 2021
  4. self-assigned this
    on Feb 5, 2021
  5. added
    priority: p3Desirable enhancement or fix. May not be included in next release.
    on Feb 5, 2021
  6. daniel-sanche commented on Oct 15, 2021

    @daniel-sanche
    Contributor

    This will be fixed in thbe v3.0.0 release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

api: loggingIssues related to the googleapis/python-logging API.performancepriority: p3Desirable enhancement or fix. May not be included in next release.type: feature request‘Nice-to-have’ improvement, new feature or different behavior or design.

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions