Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions logging/google/cloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
_APPENGINE_FLEXIBLE_ENV_VM = 'GAE_APPENGINE_HOSTNAME'
"""Environment variable set in App Engine when vm:true is set."""

_APPENGINE_FLEXIBLE_ENV_FLEX = 'GAE_INSTANCE'
"""Environment variable set in App Engine when env:flex is set."""
_APPENGINE_INSTANCE_ID = 'GAE_INSTANCE'
"""Environment variable set in App Engine standard and flexible environment."""

_GKE_CLUSTER_NAME = 'instance/attributes/cluster-name'
"""Attribute in metadata server when in GKE environment."""
Expand Down Expand Up @@ -301,7 +301,7 @@ def get_default_handler(self):
gke_cluster_name = retrieve_metadata_server(_GKE_CLUSTER_NAME)

if (_APPENGINE_FLEXIBLE_ENV_VM in os.environ or
_APPENGINE_FLEXIBLE_ENV_FLEX in os.environ):
_APPENGINE_INSTANCE_ID in os.environ):
return AppEngineHandler(self)
elif gke_cluster_name is not None:
return ContainerEngineHandler()
Expand Down
23 changes: 18 additions & 5 deletions logging/google/cloud/logging/handlers/app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

_DEFAULT_GAE_LOGGER_NAME = 'app'

_GAE_PROJECT_ENV = 'GCLOUD_PROJECT'
_GAE_PROJECT_ENV_FLEX = 'GCLOUD_PROJECT'
_GAE_PROJECT_ENV_STANDARD = 'GOOGLE_CLOUD_PROJECT'
_GAE_SERVICE_ENV = 'GAE_SERVICE'
_GAE_VERSION_ENV = 'GAE_VERSION'

Expand All @@ -54,6 +55,11 @@ def __init__(self, client,
self.name = name
self.client = client
self.transport = transport(client, name)
self.project_id = os.environ.get(
_GAE_PROJECT_ENV_FLEX,
os.environ.get(_GAE_PROJECT_ENV_STANDARD, ''))
Comment thread
myelin marked this conversation as resolved.
self.module_id = os.environ.get(_GAE_SERVICE_ENV, '')
self.version_id = os.environ.get(_GAE_VERSION_ENV, '')
self.resource = self.get_gae_resource()

def get_gae_resource(self):
Expand All @@ -65,9 +71,9 @@ def get_gae_resource(self):
gae_resource = Resource(
type='gae_app',
labels={
'project_id': os.environ.get(_GAE_PROJECT_ENV),
'module_id': os.environ.get(_GAE_SERVICE_ENV),
'version_id': os.environ.get(_GAE_VERSION_ENV),
'project_id': self.project_id,
'module_id': self.module_id,
'version_id': self.version_id,
},
)
return gae_resource
Expand Down Expand Up @@ -100,8 +106,15 @@ def emit(self, record):
:param record: The record to be logged.
"""
message = super(AppEngineHandler, self).format(record)
gae_labels = self.get_gae_labels()
trace_id = ('projects/%s/traces/%s' % (self.project_id,
gae_labels[_TRACE_ID_LABEL])
if _TRACE_ID_LABEL in gae_labels
else None)
self.transport.send(
record,
message,
resource=self.resource,
labels=self.get_gae_labels())
labels=gae_labels,
trace=trace_id,
)
18 changes: 11 additions & 7 deletions logging/tests/unit/handlers/test_app_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)

def test_constructor(self):
from google.cloud.logging.handlers.app_engine import _GAE_PROJECT_ENV
from google.cloud.logging.handlers.app_engine import (
_GAE_PROJECT_ENV_STANDARD)
from google.cloud.logging.handlers.app_engine import _GAE_SERVICE_ENV
from google.cloud.logging.handlers.app_engine import _GAE_VERSION_ENV

client = mock.Mock(project=self.PROJECT, spec=['project'])

with mock.patch('os.environ', new={_GAE_PROJECT_ENV: 'test_project',
_GAE_SERVICE_ENV: 'test_service',
_GAE_VERSION_ENV: 'test_version'}):
with mock.patch('os.environ', new={
_GAE_PROJECT_ENV_STANDARD: 'test_project',
_GAE_SERVICE_ENV: 'test_service',
_GAE_VERSION_ENV: 'test_version',
}):

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

handler = self._make_one(client, transport=_Transport)
self.assertIs(handler.client, client)
self.assertEqual(handler.resource.type, 'gae_app')
Expand All @@ -51,6 +54,7 @@ def test_emit(self):
handler = self._make_one(client, transport=_Transport)
gae_resource = handler.get_gae_resource()
gae_labels = handler.get_gae_labels()
trace = None
logname = 'app'
message = 'hello world'
record = logging.LogRecord(logname, logging, None, None, message,
Expand All @@ -61,7 +65,7 @@ def test_emit(self):
self.assertEqual(handler.transport.name, logname)
self.assertEqual(
handler.transport.send_called_with,
(record, message, gae_resource, gae_labels))
(record, message, gae_resource, gae_labels, trace))

def _get_gae_labels_helper(self, trace_id):
get_trace_patch = mock.patch(
Expand Down Expand Up @@ -98,5 +102,5 @@ def __init__(self, client, name):
self.client = client
self.name = name

def send(self, record, message, resource, labels):
self.send_called_with = (record, message, resource, labels)
def send(self, record, message, resource, labels, trace):
self.send_called_with = (record, message, resource, labels, trace)