-
-
Save ashsmith/69541dc0e406d767e3fa3de978d66677 to your computer and use it in GitHub Desktop.
PHP Cloud Functions + Cloud Logging SDK
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
#!/bin/bash | |
PROJECT=$(gcloud config get-value project) | |
FUNCTION_NAME="${1:-ash-test}" | |
REGION="${2:-us-central1}" | |
ENTRYPOINT="${3:-helloHttp}" | |
gcloud functions deploy $FUNCTION_NAME \ | |
--runtime php74 \ | |
--trigger-http \ | |
--entry-point $ENTRYPOINT \ | |
--set-env-vars=REGION=$REGION,FUNCTION=$FUNCTION_NAME,PROJECT_ID=$PROJECT |
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
<?php | |
use Psr\Http\Message\ServerRequestInterface; | |
use Google\Cloud\Logging\LoggingClient; | |
function helloHttp(ServerRequestInterface $request): string { | |
$queryString = $request->getQueryParams(); | |
$name = $queryString['name'] ?? 'world'; | |
$logging = new LoggingClient(); | |
$logger = $logging->psrLogger('app', [ | |
'resource' => [ | |
'type' => 'cloud_function', | |
'labels' => [ | |
'function_name' => $_ENV['FUNCTION'], | |
'region' => $_ENV['REGION'], | |
], | |
], | |
'labels' => [ | |
'execution_id' => $_ENV['HTTP_FUNCTION_EXECUTION_ID'], | |
] | |
]); | |
// Fetch the Trace ID | |
$traceContext = $_ENV['HTTP_X_CLOUD_TRACE_CONTEXT'] ?? ''; | |
$tradeID = explode("/", $traceContext)[0]; | |
// Configure tracing context for logging | |
$loggerOptions = [ | |
"stackdriverOptions" => [ | |
"trace" => "projects/{$_ENV['PROJECT_ID']}/traces/{$tradeID}", | |
], | |
]; | |
$logger->error("hello world", $loggerOptions); | |
return sprintf('Hello, %s!', $name); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment