I've stuck with problem with google cloud logging and google cloud trace using google cloud kubernetes
I've the application which consumes gcloud pubsub topic and I want to unify logs in trace of every pubsub message handle func call
My Gcloud Logging handler code
class GCLHandler(CloudLoggingHandler):
def emit(self, record):
message = super(GCLHandler, self).format(record)
resource = Resource(
type='k8s_container',
labels={
'cluster_name': os.environ['CLUSTER_NAME'],
'container_name': os.environ['POD_APP_NAME'],
'location': os.environ['CLUSTER_LOCATION'],
'namespace_name': os.environ['POD_NAMESPACE'],
'pod_name': os.environ['POD_NAME'],
'project_id': _settings.PROJECT_NAME
}
)
labels: Dict[str, Any] = {
'k8s-pod/app': os.environ['POD_APP_NAME'],
'k8s-pod/app_kubernetes_io/managed-by': os.environ['POD_MANAGED_BY'],
'k8s-pod/pod-template-hash': os.environ['POD_TEMPLATE_HASH']
}
trace = getattr(record, 'traceId', None)
if trace is not None:
trace = f'projects/{_settings.PROJECT_NAME}/traces/{trace}'
self.transport.send(
record,
message,
resource=resource,
labels=labels,
trace=trace,
span_id=getattr(record, 'spanId', None)
)
I use opensensus integration with gcloud trace and logging, so I can get traceId and spanId and pass it into gcloud logging transport, it work fine and LogEntry in logs viewer contains proper traceId and spanId
My code of using gcloud trace looks like
config_integration.trace_integrations(['logging'])
logger = logging.getLogger(__name__)
exporter = stackdriver_exporter.StackdriverExporter(
project_id=settings.PROJECT_NAME
)
async def handle_message(message: Message) -> None:
tracer = Tracer(exporter=exporter, sampler=AlwaysOnSampler())
with tracer.span(name=f'Message#{message.message_id}'):
logger.debug(f'debug')
logger.info(f'info')
logger.warning(f'warning')
So, I can these logs in Logs Viewer, but they aren't gropped in one trace, but if I use gcloud trace viewer and search by traceId, I will find this trace with connected logs. Q: There is any way to display trace in logs viewer as it displayed in any appengine service as appengine.googleapis.com/Frequest_log?