Created
July 6, 2020 01:41
-
-
Save drillbits/999a862d7314cc767de745594cab8576 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import flask | |
from google.cloud.logging import Client | |
from google.cloud.logging.resource import Resource | |
def cloud_functions_http_trigger_logcontext(request: flask.Request): | |
project_id = os.environ.get("GCLOUD_PROJECT") | |
region = os.environ.get("FUNCTION_REGION") | |
function_name = os.environ.get("FUNCTION_NAME") | |
execution_id = request.headers.get("Function-Execution-Id") | |
trace_context = request.headers.get("X-Cloud-Trace-Context") | |
if trace_context is None: | |
trace_id = "" | |
else: | |
trace_id = trace_context.split("/")[0] | |
labels = { | |
"execution_id": execution_id, | |
} | |
resource = Resource( | |
type="cloud_function", | |
labels={ | |
"function_name": function_name, | |
"project_id": project_id, | |
"region": region, | |
}, | |
) | |
trace = f"projects/{project_id}/traces/${trace_id}" | |
client = Client() | |
logger = client.logger("cloudfunctions.googleapis.com%2Fcloud-functions") | |
logger.log_struct( | |
{"message": "hai"}, | |
severity="DEBUG", | |
labels=labels, | |
resource=resource, | |
trace=trace, | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment