Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect which layer was used for invocation #1031

Merged
merged 1 commit into from Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion runtime/layers/console/bootstrap
Expand Up @@ -26,7 +26,7 @@ if (getenv('BREF_DOWNLOAD_VENDOR')) {
require $appRoot . '/vendor/autoload.php';
}

$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('console');

$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handlerFile)) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/layers/fpm/bootstrap
Expand Up @@ -25,7 +25,7 @@ if (getenv('BREF_DOWNLOAD_VENDOR')) {
require $appRoot . '/vendor/autoload.php';
}

$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('fpm');

$handlerFile = $appRoot . '/' . getenv('_HANDLER');
if (! is_file($handlerFile)) {
Expand Down
2 changes: 1 addition & 1 deletion runtime/layers/function/bootstrap.php
Expand Up @@ -24,7 +24,7 @@
require $appRoot . '/vendor/autoload.php';
}

$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('function');

$container = Bref::getContainer();

Expand Down
7 changes: 7 additions & 0 deletions runtime/ping/statsd.config.json
@@ -0,0 +1,7 @@
{
"debug": false,
"backends": ["aws-cloudwatch-statsd-backend"],
"cloudwatch": {
"region": "EU_WEST_1"
}
}
16 changes: 10 additions & 6 deletions src/Runtime/LambdaRuntime.php
Expand Up @@ -42,19 +42,23 @@ final class LambdaRuntime
/** @var Invoker */
private $invoker;

public static function fromEnvironmentVariable(): self
/** @var string */
private $layer;

public static function fromEnvironmentVariable(string $layer): self
{
return new self((string) getenv('AWS_LAMBDA_RUNTIME_API'));
return new self((string) getenv('AWS_LAMBDA_RUNTIME_API'), $layer);
}

public function __construct(string $apiUrl)
public function __construct(string $apiUrl, string $layer)
{
if ($apiUrl === '') {
die('At the moment lambdas can only be executed in an Lambda environment');
}

$this->apiUrl = $apiUrl;
$this->invoker = new Invoker;
$this->layer = $layer;
}

public function __destruct()
Expand Down Expand Up @@ -313,7 +317,7 @@ private function postJson(string $url, $data): void
* WHAT?
* The data sent in the ping is anonymous.
* It does not contain any identifiable data about anything (the project, users, etc.).
* The only data it contains is: "A Bref invocation happened".
* The only data it contains is: "A Bref invocation happened using a specific layer".
* You can verify that by checking the content of the message in the function.
*
* HOW?
Expand Down Expand Up @@ -354,7 +358,7 @@ private function ping(): void

/**
* Here is the content sent to the Bref analytics server.
* It signals an invocation happened.
* It signals an invocation happened on which layer.
* Nothing else is sent.
*
* `Invocations_100` is used to signal that this is 1 ping equals 100 invocations.
Expand All @@ -365,7 +369,7 @@ private function ping(): void
*
* See https://github.com/statsd/statsd/blob/master/docs/metric_types.md for more information.
*/
$message = 'Invocations_100:1|c';
$message = "Invocations_100:1|c\nLayer_{$this->layer}_100:1|c";

$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
// This IP address is the Bref server.
Expand Down
2 changes: 1 addition & 1 deletion tests/Runtime/LambdaRuntimeTest.php
Expand Up @@ -35,7 +35,7 @@ protected function setUp(): void
{
ob_start();
Server::start();
$this->runtime = new LambdaRuntime('localhost:8126');
$this->runtime = new LambdaRuntime('localhost:8126', 'phpunit');
}

protected function tearDown(): void
Expand Down