Skip to content

Commit 8188cef

Browse files
chore: lambda otel (#16)
* chore: otel for lambdas * chore: set otel sampling rates * fix: fmt * chore: mv aws_lambda_layers_account_id * fix: pass in aws_lambda_layers_account_id var * fix: use arn string for otel layer * fix: layers not needed
1 parent 130347d commit 8188cef

9 files changed

Lines changed: 62 additions & 13 deletions

File tree

infra/live/global_vars.hcl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ locals {
1010
"application-autoscaling:*",
1111
"cloudwatch:*",
1212
"sqs:*",
13-
"cloudfront:*"
13+
"cloudfront:*",
14+
"xray:*"
1415
]
1516
}
1617

infra/live/prod/environment_vars.hcl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ locals {
66
inputs = {
77
log_retention_days = local.log_retention_days
88
deploy_branches = local.deploy_branches
9+
otel_sample_rate = 0.1 # 10% of traces sampled
910
}

infra/modules/aws/_shared/lambda/data.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
data "aws_iam_policy_document" "lambda_xray" {
2+
statement {
3+
effect = "Allow"
4+
actions = [
5+
"xray:PutTraceSegments",
6+
"xray:PutTelemetryRecords",
7+
]
8+
resources = ["*"]
9+
}
10+
}
11+
112
data "aws_s3_bucket" "code_bucket" {
213
bucket = var.code_bucket
314
}

infra/modules/aws/_shared/lambda/main.tf

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,21 @@ resource "aws_s3_object" "bootstrap_lambda_zip" {
3030
content_type = "application/zip"
3131
}
3232

33-
resource "aws_lambda_function" "lambda" {
34-
function_name = local.lambda_name
35-
role = aws_iam_role.iam_for_lambda.arn
36-
handler = local.lambda_handler
37-
runtime = local.lambda_runtime
33+
resource "aws_iam_policy" "lambda_xray" {
34+
name = "${local.lambda_name}-xray"
35+
policy = data.aws_iam_policy_document.lambda_xray.json
36+
}
37+
38+
resource "aws_iam_role_policy_attachment" "lambda_xray" {
39+
role = aws_iam_role.iam_for_lambda.name
40+
policy_arn = aws_iam_policy.lambda_xray.arn
41+
}
3842

43+
resource "aws_lambda_function" "lambda" {
44+
function_name = local.lambda_name
45+
role = aws_iam_role.iam_for_lambda.arn
46+
handler = local.lambda_handler
47+
runtime = local.lambda_runtime
3948
reserved_concurrent_executions = local.pc_reserved_count
4049

4150
s3_bucket = data.aws_s3_bucket.code_bucket.bucket
@@ -44,8 +53,15 @@ resource "aws_lambda_function" "lambda" {
4453
# publish ONE immutable version so we can create an alias
4554
publish = true
4655

56+
tracing_config {
57+
mode = "Active"
58+
}
59+
4760
environment {
48-
variables = var.environment_variables
61+
variables = merge(var.environment_variables, {
62+
OTEL_TRACES_SAMPLER = "parentbased_traceidratio"
63+
OTEL_TRACES_SAMPLER_ARG = tostring(var.otel_sample_rate)
64+
})
4965
}
5066

5167
# tags for identifying the code deploy app and its deployment config. Used in CI/CD pipelines.

infra/modules/aws/_shared/lambda/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
### start of static vars set in root.hcl ###
2+
variable "otel_sample_rate" {
3+
type = number
4+
description = "OpenTelemetry trace sampling rate — 0.0 = no traces, 1.0 = 100% of traces sampled"
5+
default = 1.0 # 100%
6+
}
7+
28
variable "project_name" {
39
type = string
410
description = "Project name used in naming resources"

infra/modules/aws/api/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module "lambda_api" {
22
source = "../_shared/lambda"
33

4-
project_name = var.project_name
5-
environment = var.environment
6-
code_bucket = var.code_bucket
4+
project_name = var.project_name
5+
environment = var.environment
6+
code_bucket = var.code_bucket
7+
otel_sample_rate = var.otel_sample_rate
78

89
lambda_name = local.lambda_name
910

infra/modules/aws/api/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ variable "code_bucket" {
1313
type = string
1414
description = "Bucket where deployable code artifacts are uploaded"
1515
}
16+
17+
variable "otel_sample_rate" {
18+
type = number
19+
description = "OpenTelemetry trace sampling rate — 0.0 = no traces, 1.0 = 100% of traces sampled"
20+
default = 1.0 # 100%
21+
}
1622
### end of static vars set in root.hcl ###
1723

1824
variable "deployment_config" {

infra/modules/aws/lambda_worker/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
module "lambda_worker" {
22
source = "../_shared/lambda"
33

4-
project_name = var.project_name
5-
environment = var.environment
6-
code_bucket = var.code_bucket
4+
project_name = var.project_name
5+
environment = var.environment
6+
code_bucket = var.code_bucket
7+
otel_sample_rate = var.otel_sample_rate
78

89
lambda_name = local.lambda_name
910

infra/modules/aws/lambda_worker/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ variable "code_bucket" {
1313
type = string
1414
description = "Bucket where deployable code artifacts are uploaded"
1515
}
16+
17+
variable "otel_sample_rate" {
18+
type = number
19+
description = "OpenTelemetry trace sampling rate — 0.0 = no traces, 1.0 = 100% of traces sampled"
20+
default = 1.0 # 100%
21+
}
1622
### end of static vars set in root.hcl ###
1723

1824
variable "sqs_queue_name" {

0 commit comments

Comments
 (0)