Skip to content

Commit efa0819

Browse files
authored
Merge pull request #1031 from brefphp/statsd
Collect which layer was used for invocation
2 parents 9d7a343 + 460a057 commit efa0819

6 files changed

Lines changed: 21 additions & 10 deletions

File tree

runtime/layers/console/bootstrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ if (getenv('BREF_DOWNLOAD_VENDOR')) {
2626
require $appRoot . '/vendor/autoload.php';
2727
}
2828

29-
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
29+
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('console');
3030

3131
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
3232
if (! is_file($handlerFile)) {

runtime/layers/fpm/bootstrap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (getenv('BREF_DOWNLOAD_VENDOR')) {
2525
require $appRoot . '/vendor/autoload.php';
2626
}
2727

28-
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
28+
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('fpm');
2929

3030
$handlerFile = $appRoot . '/' . getenv('_HANDLER');
3131
if (! is_file($handlerFile)) {

runtime/layers/function/bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
require $appRoot . '/vendor/autoload.php';
2525
}
2626

27-
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable();
27+
$lambdaRuntime = LambdaRuntime::fromEnvironmentVariable('function');
2828

2929
$container = Bref::getContainer();
3030

runtime/ping/statsd.config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"debug": false,
3+
"backends": ["aws-cloudwatch-statsd-backend"],
4+
"cloudwatch": {
5+
"region": "EU_WEST_1"
6+
}
7+
}

src/Runtime/LambdaRuntime.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,19 +42,23 @@ final class LambdaRuntime
4242
/** @var Invoker */
4343
private $invoker;
4444

45-
public static function fromEnvironmentVariable(): self
45+
/** @var string */
46+
private $layer;
47+
48+
public static function fromEnvironmentVariable(string $layer): self
4649
{
47-
return new self((string) getenv('AWS_LAMBDA_RUNTIME_API'));
50+
return new self((string) getenv('AWS_LAMBDA_RUNTIME_API'), $layer);
4851
}
4952

50-
public function __construct(string $apiUrl)
53+
public function __construct(string $apiUrl, string $layer)
5154
{
5255
if ($apiUrl === '') {
5356
die('At the moment lambdas can only be executed in an Lambda environment');
5457
}
5558

5659
$this->apiUrl = $apiUrl;
5760
$this->invoker = new Invoker;
61+
$this->layer = $layer;
5862
}
5963

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

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

370374
$sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
371375
// This IP address is the Bref server.

tests/Runtime/LambdaRuntimeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp(): void
3535
{
3636
ob_start();
3737
Server::start();
38-
$this->runtime = new LambdaRuntime('localhost:8126');
38+
$this->runtime = new LambdaRuntime('localhost:8126', 'phpunit');
3939
}
4040

4141
protected function tearDown(): void

0 commit comments

Comments
 (0)