Skip to content

Commit

Permalink
Merge pull request #1894 from tseaver/logging-gax_via_envvar
Browse files Browse the repository at this point in the history
Enable logging-over-gRPC
  • Loading branch information
tseaver authored Jun 27, 2016
2 parents dbb05e5 + a4022b6 commit 95f2a20
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 12 deletions.
47 changes: 41 additions & 6 deletions gcloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,31 @@

"""Client for interacting with the Google Cloud Logging API."""

import os

try:
from google.logging.v2.config_service_v2_api import (
ConfigServiceV2Api as GeneratedSinksAPI)
from google.logging.v2.logging_service_v2_api import (
LoggingServiceV2Api as GeneratedLoggingAPI)
from google.logging.v2.metrics_service_v2_api import (
MetricsServiceV2Api as GeneratedMetricsAPI)
from gcloud.logging._gax import _LoggingAPI as GAXLoggingAPI
from gcloud.logging._gax import _MetricsAPI as GAXMetricsAPI
from gcloud.logging._gax import _SinksAPI as GAXSinksAPI
except ImportError: # pragma: NO COVER
_HAVE_GAX = False
GeneratedLoggingAPI = GAXLoggingAPI = None
GeneratedMetricsAPI = GAXMetricsAPI = None
GeneratedSinksAPI = GAXSinksAPI = None
else:
_HAVE_GAX = True

from gcloud.client import JSONClient
from gcloud.logging.connection import Connection
from gcloud.logging.connection import _LoggingAPI
from gcloud.logging.connection import _MetricsAPI
from gcloud.logging.connection import _SinksAPI
from gcloud.logging.connection import _LoggingAPI as JSONLoggingAPI
from gcloud.logging.connection import _MetricsAPI as JSONMetricsAPI
from gcloud.logging.connection import _SinksAPI as JSONSinksAPI
from gcloud.logging.entries import ProtobufEntry
from gcloud.logging.entries import StructEntry
from gcloud.logging.entries import TextEntry
Expand All @@ -27,6 +47,9 @@
from gcloud.logging.sink import Sink


_USE_GAX = _HAVE_GAX and (os.environ.get('GCLOUD_ENABLE_GAX') is not None)


class Client(JSONClient):
"""Client to bundle configuration needed for API requests.
Expand Down Expand Up @@ -60,7 +83,11 @@ def logging_api(self):
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.logs
"""
if self._logging_api is None:
self._logging_api = _LoggingAPI(self.connection)
if _USE_GAX:
generated = GeneratedLoggingAPI()
self._logging_api = GAXLoggingAPI(generated)
else:
self._logging_api = JSONLoggingAPI(self.connection)
return self._logging_api

@property
Expand All @@ -71,7 +98,11 @@ def sinks_api(self):
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.sinks
"""
if self._sinks_api is None:
self._sinks_api = _SinksAPI(self.connection)
if _USE_GAX:
generated = GeneratedSinksAPI()
self._sinks_api = GAXSinksAPI(generated)
else:
self._sinks_api = JSONSinksAPI(self.connection)
return self._sinks_api

@property
Expand All @@ -82,7 +113,11 @@ def metrics_api(self):
https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/projects.metrics
"""
if self._metrics_api is None:
self._metrics_api = _MetricsAPI(self.connection)
if _USE_GAX:
generated = GeneratedMetricsAPI()
self._metrics_api = GAXMetricsAPI(generated)
else:
self._metrics_api = JSONMetricsAPI(self.connection)
return self._metrics_api

def logger(self, name):
Expand Down
120 changes: 114 additions & 6 deletions gcloud/logging/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,39 +38,147 @@ def test_ctor(self):
client = self._makeOne(project=self.PROJECT, credentials=creds)
self.assertEqual(client.project, self.PROJECT)

def test_logging_api(self):
def test_logging_api_wo_gax(self):
from gcloud.logging.connection import _LoggingAPI
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey
client = self._makeOne(self.PROJECT, credentials=_Credentials())
conn = client.connection = object()
api = client.logging_api

with _Monkey(MUT, _USE_GAX=False):
api = client.logging_api

self.assertTrue(isinstance(api, _LoggingAPI))
self.assertTrue(api._connection is conn)
# API instance is cached
again = client.logging_api
self.assertTrue(again is api)

def test_sinks_api(self):
def test_logging_api_w_gax(self):
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey

wrapped = object()
_called_with = []

def _generated_api(*args, **kw):
_called_with.append((args, kw))
return wrapped

class _GaxLoggingAPI(object):

def __init__(self, _wrapped):
self._wrapped = _wrapped

creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)

with _Monkey(MUT,
_USE_GAX=True,
GeneratedLoggingAPI=_generated_api,
GAXLoggingAPI=_GaxLoggingAPI):
api = client.logging_api

self.assertIsInstance(api, _GaxLoggingAPI)
self.assertTrue(api._wrapped is wrapped)
# API instance is cached
again = client.logging_api
self.assertTrue(again is api)

def test_sinks_api_wo_gax(self):
from gcloud.logging.connection import _SinksAPI
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey
client = self._makeOne(self.PROJECT, credentials=_Credentials())
conn = client.connection = object()
api = client.sinks_api

with _Monkey(MUT, _USE_GAX=False):
api = client.sinks_api

self.assertTrue(isinstance(api, _SinksAPI))
self.assertTrue(api._connection is conn)
# API instance is cached
again = client.sinks_api
self.assertTrue(again is api)

def test_metrics_api(self):
def test_sinks_api_w_gax(self):
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey

wrapped = object()
_called_with = []

def _generated_api(*args, **kw):
_called_with.append((args, kw))
return wrapped

class _GaxSinksAPI(object):

def __init__(self, _wrapped):
self._wrapped = _wrapped

creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)

with _Monkey(MUT,
_USE_GAX=True,
GeneratedSinksAPI=_generated_api,
GAXSinksAPI=_GaxSinksAPI):
api = client.sinks_api

self.assertIsInstance(api, _GaxSinksAPI)
self.assertTrue(api._wrapped is wrapped)
# API instance is cached
again = client.sinks_api
self.assertTrue(again is api)

def test_metrics_api_wo_gax(self):
from gcloud.logging.connection import _MetricsAPI
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey
client = self._makeOne(self.PROJECT, credentials=_Credentials())
conn = client.connection = object()
api = client.metrics_api

with _Monkey(MUT, _USE_GAX=False):
api = client.metrics_api

self.assertTrue(isinstance(api, _MetricsAPI))
self.assertTrue(api._connection is conn)
# API instance is cached
again = client.metrics_api
self.assertTrue(again is api)

def test_metrics_api_w_gax(self):
from gcloud.logging import client as MUT
from gcloud._testing import _Monkey

wrapped = object()
_called_with = []

def _generated_api(*args, **kw):
_called_with.append((args, kw))
return wrapped

class _GaxMetricsAPI(object):

def __init__(self, _wrapped):
self._wrapped = _wrapped

creds = _Credentials()
client = self._makeOne(project=self.PROJECT, credentials=creds)

with _Monkey(MUT,
_USE_GAX=True,
GeneratedMetricsAPI=_generated_api,
GAXMetricsAPI=_GaxMetricsAPI):
api = client.metrics_api

self.assertIsInstance(api, _GaxMetricsAPI)
self.assertTrue(api._wrapped is wrapped)
# API instance is cached
again = client.metrics_api
self.assertTrue(again is api)

def test_logger(self):
from gcloud.logging.logger import Logger
creds = _Credentials()
Expand Down

0 comments on commit 95f2a20

Please sign in to comment.