Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save drillbits/999a862d7314cc767de745594cab8576 to your computer and use it in GitHub Desktop.
Save drillbits/999a862d7314cc767de745594cab8576 to your computer and use it in GitHub Desktop.
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