Skip to content

Instantly share code, notes, and snippets.

@ashsmith

ashsmith/deployment.sh Secret

Last active Apr 26, 2021
Embed
What would you like to do?
PHP Cloud Functions + Cloud Logging SDK
#!/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
<?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