Skip to content

Commit dabbaf2

Browse files
committed
Add logGroupName and logStreamName, rename memorySize -> memoryLimitInMB
Implements changes suggested in #3
1 parent a4be603 commit dabbaf2

File tree

4 files changed

+29
-5
lines changed

4 files changed

+29
-5
lines changed

ChangeLog.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ AWS Lambda change log
33

44
## ?.?.? / ????-??-??
55

6+
* Fixed issue #3: Context object compatibility. Context now also declares
7+
the properties *logGroupName* and *logStreamName* like in NodeJS, and
8+
uses *memoryLimitInMB* instead of *memorySize*.
9+
(@thekid)
610
* Merged PR #4: Pass version along to docker images. Use `xp lambda runtime`
711
to use the current PHP version, `xp lambda runtime:8.0` to use the newest
812
PHP 8.0 release, and `xp lambda runtime:8.0.10` for version pinning.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ public class com.amazon.aws.lambda.Context implements lang.Value {
154154
public string $functionName
155155
public string $functionVersion
156156
public string $memorySize
157+
public string $logGroupName
158+
public string $logStreamName
157159
public string $region
158160
public int $payloadLength
159161

src/main/php/com/amazon/aws/lambda/Context.class.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
*/
1414
class Context implements Value {
1515
public $awsRequestId, $invokedFunctionArn, $traceId, $clientContext, $cognitoIdentity, $deadline;
16-
public $functionName, $functionVersion, $memorySize, $region;
16+
public $functionName, $functionVersion, $memoryLimitInMB, $logGroupName, $logStreamName, $region;
1717
public $payloadLength;
1818

1919
/**
@@ -31,8 +31,10 @@ public function __construct(array $headers, array $environment= []) {
3131
$this->cognitoIdentity= $headers['Lambda-Runtime-Cognito-Identity'][0] ?? null;
3232
$this->functionName= $environment['AWS_LAMBDA_FUNCTION_NAME'] ?? 'test';
3333
$this->functionVersion= $environment['AWS_LAMBDA_FUNCTION_VERSION'] ?? '$LATEST';
34-
$this->memorySize= $environment['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] ?? '1536';
34+
$this->memoryLimitInMB= $environment['AWS_LAMBDA_FUNCTION_MEMORY_SIZE'] ?? '1536';
3535
$this->region= $environment['AWS_REGION'] ?? 'us-east-1';
36+
$this->logGroupName= $environment['AWS_LAMBDA_LOG_GROUP_NAME'] ?? null;
37+
$this->logStreamName= $environment['AWS_LAMBDA_LOG_STREAM_NAME'] ?? null;
3638
$this->payloadLength= cast($headers['Content-Length'][0] ?? null, '?int');
3739
}
3840

@@ -53,7 +55,9 @@ public function toString() {
5355
" [deadline ] %s\n".
5456
" [functionName ] %s\n".
5557
" [functionVersion ] %s\n".
56-
" [memoryLimit ] %s\n".
58+
" [memoryLimitInMB ] %s\n".
59+
" [logGroupName ] %s\n".
60+
" [logStreamName ] %s\n".
5761
" [region ] %s\n".
5862
" [clientContext ] %s\n".
5963
" [identity ] %s\n".
@@ -66,7 +70,9 @@ public function toString() {
6670
$this->deadline ?? '(null)',
6771
$this->functionName,
6872
$this->functionVersion,
69-
$this->memoryLimit,
73+
$this->memoryLimitInMB,
74+
$this->logGroupName ?? '(null)',
75+
$this->logStreamName ?? '(null)',
7076
$this->region,
7177
$this->clientContext ?? '(null)',
7278
$this->identity ?? '(null)'

src/test/php/com/amazon/aws/lambda/unittest/ContextTest.class.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ class ContextTest {
1616
'AWS_LAMBDA_FUNCTION_NAME' => 'test',
1717
'AWS_LAMBDA_FUNCTION_VERSION' => '$LATEST',
1818
'AWS_LAMBDA_FUNCTION_MEMORY_SIZE' => 1536,
19+
'AWS_LAMBDA_LOG_GROUP_NAME' => '/aws/lambda/test',
20+
'AWS_LAMBDA_LOG_STREAM_NAME' => '2021/08/27/[$LATEST]17c13f63daf4a2a178be63f5531f77cc',
1921
'AWS_REGION' => 'us-east-1',
2022
];
2123

@@ -32,7 +34,9 @@ private function headers() {
3234
private function environment() {
3335
yield ['AWS_LAMBDA_FUNCTION_NAME', 'functionName'];
3436
yield ['AWS_LAMBDA_FUNCTION_VERSION', 'functionVersion'];
35-
yield ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'memorySize'];
37+
yield ['AWS_LAMBDA_FUNCTION_MEMORY_SIZE', 'memoryLimitInMB'];
38+
yield ['AWS_LAMBDA_LOG_GROUP_NAME', 'logGroupName'];
39+
yield ['AWS_LAMBDA_LOG_STREAM_NAME', 'logStreamName'];
3640
yield ['AWS_REGION', 'region'];
3741
}
3842

@@ -75,4 +79,12 @@ public function remainingTime_accessor() {
7579
round((new Context($this->headers))->remainingTime(1629390002.279), 1)
7680
);
7781
}
82+
83+
#[Test]
84+
public function string_representation_is_not_empty() {
85+
Assert::notEquals(
86+
'',
87+
(new Context($this->headers, $this->environment))->toString()
88+
);
89+
}
7890
}

0 commit comments

Comments
 (0)