Skip to content

Commit 241cb29

Browse files
committed
feat: improve lambda execution detection
Signed-off-by: Kavindu Dodanduwa <[email protected]>
1 parent e053df5 commit 241cb29

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

receiver/awslambdareceiver/receiver.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"errors"
1111
"fmt"
1212
"os"
13+
"strings"
1314

1415
"github.com/aws/aws-lambda-go/events"
1516
"github.com/aws/aws-lambda-go/lambda"
@@ -95,8 +96,8 @@ func newMetricsReceiver(cfg *Config, set receiver.Settings, next consumer.Metric
9596
// when lambda is triggered
9697
func (a *awsLambdaReceiver) Start(ctx context.Context, host component.Host) error {
9798
// Verify we're running in a Lambda environment
98-
if os.Getenv("_LAMBDA_SERVER_PORT") == "" || os.Getenv("AWS_LAMBDA_RUNTIME_API") == "" {
99-
return errors.New("receiver must be used in an AWS Lambda environment: required environment variables _LAMBDA_SERVER_PORT and AWS_LAMBDA_RUNTIME_API are not set")
99+
if os.Getenv("AWS_EXECUTION_ENV") == "" || !strings.HasPrefix(os.Getenv("AWS_EXECUTION_ENV"), "AWS_Lambda_") {
100+
return errors.New("awslambdareceiver must be used in an AWS Lambda environment: missing environment variable AWS_EXECUTION_ENV")
100101
}
101102

102103
handler, err := a.newHandler(ctx, host, a.s3Provider)

receiver/awslambdareceiver/receiver_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ import (
2525

2626
func TestCreateLogs(t *testing.T) {
2727
// Set Lambda environment variables required by Start()
28-
t.Setenv("_LAMBDA_SERVER_PORT", "9001")
29-
t.Setenv("AWS_LAMBDA_RUNTIME_API", "localhost:9001")
28+
t.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_python3.12")
3029

3130
// Create receiver using factory with S3 encoding config.
3231
// Note: The S3Encoding value must match the component ID used when registering the extension.
@@ -95,8 +94,7 @@ func TestCreateLogs(t *testing.T) {
9594

9695
func TestCreateMetrics(t *testing.T) {
9796
// Set Lambda environment variables required by Start()
98-
t.Setenv("_LAMBDA_SERVER_PORT", "9001")
99-
t.Setenv("AWS_LAMBDA_RUNTIME_API", "localhost:9001")
97+
t.Setenv("AWS_EXECUTION_ENV", "AWS_Lambda_python3.12")
10098

10199
// Create receiver using factory with a dummy encoding extension.
102100
factory := NewFactory()
@@ -173,8 +171,7 @@ func TestCreateMetricsRequiresS3Encoding(t *testing.T) {
173171

174172
func TestStartRequiresLambdaEnvironment(t *testing.T) {
175173
// Ensure Lambda environment variables are not set
176-
t.Setenv("_LAMBDA_SERVER_PORT", "")
177-
t.Setenv("AWS_LAMBDA_RUNTIME_API", "")
174+
t.Setenv("AWS_EXECUTION_ENV", "")
178175

179176
factory := NewFactory()
180177
cfg := factory.CreateDefaultConfig().(*Config)

0 commit comments

Comments
 (0)