Skip to content

Commit

Permalink
Merge pull request #1031 from brefphp/statsd
Browse files Browse the repository at this point in the history
Collect which layer was used for invocation
  • Loading branch information
mnapoli authored Sep 20, 2021
2 parents 9d7a343 + 460a057 commit efa0819
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion runtime/layers/console/bootstrap
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit efa0819

Please sign in to comment.