简体   繁体   English

TypeError 从云端将结构化日志写入谷歌云日志 function

[英]TypeError writing a structured log to google cloud logging from a cloud function

I am trying to write a log from a cloud function into cloud logging, but I am having trouble doing this.我正在尝试将云 function 中的日志写入云日志记录,但我在执行此操作时遇到了麻烦。 My code is the following, where the message is a log like this: "20210319-11:39:58-CET - INFO - this is an info message" and the atttributes are simply strings.我的代码如下,其中消息是这样的日志:“20210319-11:39:58-CET - INFO - 这是一条信息消息”并且属性只是字符串。

import base64
import json
import os
import pytz
from datetime import datetime
from google.cloud import logging
from google.cloud.logging_v2.resource import Resource

RESOURCE_TYPE = "cloudiot_devices"
PROJECT = os.environ.get("GCP_PROJECT")
DEVICE_REGISTRY = REGISTRY_ID
LOCATION = LOCATION
DATEFORMAT = "%Y%m%d-%H:%M:%S"

def iot_logger(event, context):
    """Triggered from a message on a Cloud Pub/Sub topic.
    Args:
         event (dict): Event payload.
         context (google.cloud.functions.Context): Metadata for the event.
    """
    log = base64.b64decode(event['data']).decode('utf-8')
    attributes = event['attributes']

    print(log)
    print(json.dumps(attributes))

    print("preparing timestamp")
    # Extract elements from log
    date_str,severity,message = log.split(" - ")

    # This is the main content for the log
    log_content = {"message":message}

    # turn log timestamp str to an aware datetime object for our timestamp
    tzname = date_str.split("-")[-1]
    # remove time zone to avoid tripping up the datetime module
    date_str = date_str[:-4]
    date_unaware = datetime.strptime(date_str,DATEFORMAT)
    date_aware = date_unaware.astimezone(pytz.timezone(tzname))

    print(f"done preparing timestamp: {date_aware}")

    # extract attributes
    logger_name = attributes["logger_name"]
    device_num_id = attributes["device_num_id"]

    print("creating resource")
    # create resource 
    resource_labels = {
        'project_id':PROJECT,
        'device_num_id':device_num_id,
        'device_registry_id':DEVICE_REGISTRY,
        'location':LOCATION
    }
    log_resource = Resource(type=RESOURCE_TYPE,labels=resource_labels)

    print("initiating client")
    # initiate logging client
    log_client = logging.Client()

    logger = log_client.logger(logger_name)

    print("creating structured log")
    logger.log_struct(log_content,severity=severity,resource=log_resource,timestamp=date_aware)

However, I keep facing issues with the log_struct command and I haven't been able to figure out what is the problem.但是,我一直面临 log_struct 命令的问题,我无法弄清楚问题所在。 Initially I was passing a string to the timestamp and it wouldn't work, so now I am passing a date object.最初我将一个字符串传递给时间戳但它不起作用,所以现在我传递一个日期 object。 But now the error is different: EDIT: There's more than one:但现在错误不同了:编辑:不止一个:

Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 589, in _ConvertFieldValuePair
    self.ConvertMessage(value, sub_message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 485, in ConvertMessage
    self._ConvertFieldValuePair(value, message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 603, in _ConvertFieldValuePair
    raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
google.protobuf.json_format.ParseError: Failed to parse labels field: expected string or bytes-like object. 
Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 589, in _ConvertFieldValuePair
    self.ConvertMessage(value, sub_message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 485, in ConvertMessage
    self._ConvertFieldValuePair(value, message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 603, in _ConvertFieldValuePair
    raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
google.protobuf.json_format.ParseError: Failed to parse labels field: expected string or bytes-like object. 
"Traceback (most recent call last):
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 149, in view_func
    function(data, context)
  File "/workspace/main.py", line 64, in iot_logger
    logger.log_struct(log_content,severity=severity,resource=log_resource,timestamp=date_aware)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/logging_v2/logger.py", line 180, in log_struct
    self._do_log(client, StructEntry, info, **kw)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/logging_v2/logger.py", line 133, in _do_log
    client.logging_api.write_entries([api_repr])
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/logging_v2/_gapic.py", line 140, in write_entries
    log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/logging_v2/_gapic.py", line 140, in <listcomp>
    log_entry_pbs = [_log_entry_mapping_to_pb(entry) for entry in entries]
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/cloud/logging_v2/_gapic.py", line 507, in _log_entry_mapping_to_pb
    ParseDict(mapping, entry_pb)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 454, in ParseDict
    parser.ConvertMessage(js_dict, message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 485, in ConvertMessage
    self._ConvertFieldValuePair(value, message)
  File "/layers/google.python.pip/pip/lib/python3.8/site-packages/google/protobuf/json_format.py", line 597, in _ConvertFieldValuePair
    raise ParseError('Failed to parse {0} field: {1}.'.format(name, e))
google.protobuf.json_format.ParseError: Failed to parse resource field: Failed to parse labels field: expected string or bytes-like object.."

It says it needs a string or bytes-like object, but I am not sure where.它说它需要一个类似 object 的字符串或字节,但我不确定在哪里。 I imagine it's not related to the date, as it was giving me an error when it was a string.我想它与日期无关,因为当它是一个字符串时它给了我一个错误。 Also the resource I believe should work, I am looking at a very similar example on this stackoverflow question, but it doesn't work for me:另外我认为应该可以使用的资源,我正在查看有关此 stackoverflow 问题的一个非常相似的示例,但它对我不起作用:

Google Cloud Functions Python Logging issue Google Cloud Functions Python 日志记录问题

What am I doing wrong?我究竟做错了什么?

I solved it with the help of @jabbson.我在@jabbson 的帮助下解决了这个问题。 I ran into a few issues, but mostly I was giving it a couple of wrong things without realizing it.我遇到了一些问题,但主要是我在没有意识到的情况下给了它一些错误的东西。

I was using the environment variables as pointed out here: https://cloud.google.com/functions/docs/env-var#nodejs_8_python_37_and_go_111 but for some reason I was getting null for project id.我正在使用此处指出的环境变量: https://cloud.google.com/functions/docs/env-var#nodejs_8_python_37_and_go_111但由于某种原因,我得到了 null 的项目 ID。 Also I had a typo in my resource_type.我的资源类型也有错字。

Lastly, I had to play around with the timestamp until it actually worked like I wanted it to.最后,我不得不玩弄时间戳,直到它真正像我想要的那样工作。 The LogEntry documentation says it's a string, but in this case I had to pass it a datetime object and converting local time to UTC so I could actually find it in cloud-logging. LogEntry 文档说它是一个字符串,但在这种情况下,我必须向它传递一个日期时间 object 并将本地时间转换为 UTC,这样我才能在云日志中找到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM