Skip to content

Commit 0d26aa3

Browse files
committed
set isHostedAwsEnvironment and use for default logging configuration
1 parent 1030122 commit 0d26aa3

File tree

2 files changed

+33
-8
lines changed

2 files changed

+33
-8
lines changed

LambdaEnvironment.scala

+32-7
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,67 @@ final class LambdaEnvironment(
4141
.orElse(systemVariables.get(key))
4242
)
4343

44+
/** The host and port of the runtime API. */
4445
private def getRuntimeApi(): String =
4546
getProperty("AWS_LAMBDA_RUNTIME_API")
4647

48+
/** The name of the function. */
4749
final def getFunctionName(): String =
4850
getProperty("AWS_LAMBDA_FUNCTION_NAME")
4951

52+
/** The version of the function being executed. */
5053
final def getFunctionVersion(): String =
5154
getProperty("AWS_LAMBDA_FUNCTION_VERSION")
5255

56+
/** The amount of memory available to the function in MB. */
5357
final def getMemoryLimitInMB(): Int =
5458
getProperty("AWS_LAMBDA_FUNCTION_MEMORY_SIZE").toInt
5559

60+
/** The name of the Amazon CloudWatch Logs group for the function. */
5661
final def getLogGroupName(): String =
5762
getProperty("AWS_LAMBDA_LOG_GROUP_NAME")
5863

64+
/** The name of the Amazon CloudWatch Logs stream for the function. */
5965
final def getLogStreamName(): String =
6066
getProperty("AWS_LAMBDA_LOG_STREAM_NAME")
6167

68+
/** The path to your Lambda function code. */
69+
final def getLambdaRuntimeDir(): String =
70+
getProperty("LAMBDA_RUNTIME_DIR")
71+
72+
/** The path to runtime libraries. */
73+
final def getLambdaTaskRoot(): String =
74+
getProperty("LAMBDA_TASK_ROOT")
75+
76+
/** Returns true when runs deployed to AWS Lambda */
77+
final val isHostedAwsEnvironment: Boolean =
78+
maybeGetProperty("_HANDLER").isDefined
79+
6280
final val isDebugMode: Boolean =
6381
maybeGetProperty("LAMBDA_RUNTIME_DEBUG_MODE")
6482
.map(parseBooleanFlagDefaultOff)
65-
.getOrElse(false)
83+
.getOrElse(true)
6684

6785
final val isTraceMode: Boolean =
6886
maybeGetProperty("LAMBDA_RUNTIME_TRACE_MODE")
6987
.map(parseBooleanFlagDefaultOff)
70-
.getOrElse(false)
88+
.getOrElse(!isHostedAwsEnvironment)
7189

7290
final val shouldDisplayAnsiColors: Boolean =
7391
maybeGetProperty("ANSI_COLORS_MODE")
74-
.map(parseBooleanFlagDefaultOn)
92+
.map(parseBooleanFlagDefault(!isHostedAwsEnvironment))
7593
.getOrElse(maybeGetProperty("NO_COLOR").forall(p => p.trim() != "1"))
7694

7795
final val shouldLogStructuredJson: Boolean =
7896
!shouldDisplayAnsiColors &&
7997
maybeGetProperty("LAMBDA_RUNTIME_LOG_TYPE")
8098
.map(_.toUpperCase().contains("STRUCTURED"))
81-
.getOrElse(true)
99+
.getOrElse(isHostedAwsEnvironment)
82100

83101
final val shouldLogInJsonArrayFormat: Boolean =
84102
maybeGetProperty("LAMBDA_RUNTIME_LOG_FORMAT")
85103
.map(_.toUpperCase().contains("JSON_ARRAY"))
86-
.getOrElse(true)
104+
.getOrElse(isHostedAwsEnvironment)
87105

88106
final val shouldLogInJsonStringFormat: Boolean =
89107
maybeGetProperty("LAMBDA_RUNTIME_LOG_FORMAT")
@@ -156,18 +174,25 @@ final class LambdaEnvironment(
156174
)
157175

158176
final inline def parseBooleanFlagDefaultOff: String => Boolean = s =>
159-
s.toLowerCase() match {
177+
s.trim().toLowerCase() match {
160178
case "true" => true
161179
case "on" => true
162180
case _ => false
163181
}
164182

165183
final inline def parseBooleanFlagDefaultOn: String => Boolean = s =>
166-
s.toLowerCase() match {
184+
s.trim().toLowerCase() match {
167185
case "false" => false
168186
case "off" => false
169187
case _ => true
170188
}
189+
190+
final inline def parseBooleanFlagDefault(default: Boolean): String => Boolean = s =>
191+
s.trim().toLowerCase() match {
192+
case "false" => false
193+
case "off" => false
194+
case _ => default
195+
}
171196
}
172197

173198
object LambdaEnvironment {

LambdaRuntime.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ trait LambdaRuntime extends EventHandler, EventHandlerTag {
367367
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format_Specification.html
368368
val awsEmbededMetric =
369369
s""""_aws":{"Timestamp":$t1,"CloudWatchMetrics":[{"Namespace":"lambda-${functionName}-metrics","Dimensions":[${
370-
if (tagOpt.isDefined) then "[\"handler\"]" else "[]"
370+
if (tagOpt.isDefined) then "[\"handler\",\"lambdaVersion\"]" else "[\"lambdaVersion\"]"
371371
}],"Metrics":[${LambdaRuntime.durationMetric}]}]}"""
372372

373373
debug(

0 commit comments

Comments
 (0)