Skip to content

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

@kapilt

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.

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