1

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?

Nikita Davydov
  • 837
  • 7
  • 18
  • As I can see, you want to be able to display trace in logs viewer as it displayed in any GAE service as `appengine.googleapis.com/Frequest_log`, but you found no way to configure it, am I right? – Serhii Rohoza Nov 03 '20 at 17:18
  • @SerhiiRohoza Yes, you're right. At the moment, I recognized that I should create parent log with different from childlogs logName. I will try to do that and if it works I will create answer – Nikita Davydov Nov 04 '20 at 11:24
  • Okay. I'm looking forward to hear from you. If no luck, I have a workaround but it's time consuming. – Serhii Rohoza Nov 04 '20 at 12:57
  • @SerhiiRohoza No, it doesn't work, I created simple TextEntry with logName pubsub_message and after that I sent some logs with logName stdout, and they're not groupped. Is it true that I can do groupped logs only with httpRequest log? – Nikita Davydov Nov 05 '20 at 12:59
  • It looks like you should file a [feature request](https://cloud.google.com/support/docs/issue-trackers#feature_requests) at [Google Public Issue Tracker](https://cloud.google.com/support/docs/issue-trackers) in order to to be able to display trace in logs viewer as it displayed in any GAE service as `appengine.googleapis.com/Frequest_log`. – Serhii Rohoza Nov 05 '20 at 13:12
  • @SerhiiRohoza I'm very sad about that because in my opinion it's obvious feature to implement and use everywhere – Nikita Davydov Nov 06 '20 at 16:56
  • Please add the link to the Public Issue Tracker if you decided to file a request. – Serhii Rohoza Nov 09 '20 at 07:35
  • 1
    Have no time for it, sorry :( I created fake http_request payload and groupped my logs – Nikita Davydov Nov 09 '20 at 19:35
  • Thanks. I posted a community wiki answer to make your workaround more noticeable for other community members. If you post your own answer I'll remove it. – Serhii Rohoza Nov 10 '20 at 07:45

1 Answers1

0

As it was confirmed by @Nikita Davydov in the comment section there's a workaround: you can create a fake http_request payload to group logs.

If it doesn't work for you, you can file a feature request at Google Public Issue Tracker in order to change current behavior.

Serhii Rohoza
  • 4,287
  • 2
  • 16
  • 29