|
| 1 | +/* |
| 2 | + * Copyright 2023 Amazon.com, Inc. or its affiliates. |
| 3 | + * Licensed under the Apache License, Version 2.0 (the |
| 4 | + * "License"); you may not use this file except in compliance |
| 5 | + * with the License. You may obtain a copy of the License at |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * Unless required by applicable law or agreed to in writing, software |
| 8 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | + * See the License for the specific language governing permissions and |
| 11 | + * limitations under the License. |
| 12 | + * |
| 13 | + */ |
| 14 | +package helloworld |
| 15 | + |
| 16 | +import com.amazonaws.services.lambda.runtime.Context |
| 17 | +import com.amazonaws.services.lambda.runtime.RequestHandler |
| 18 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent |
| 19 | +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent |
| 20 | +import com.amazonaws.xray.entities.Subsegment |
| 21 | +import org.apache.logging.log4j.LogManager |
| 22 | +import software.amazon.cloudwatchlogs.emf.logger.MetricsLogger |
| 23 | +import software.amazon.cloudwatchlogs.emf.model.DimensionSet |
| 24 | +import software.amazon.cloudwatchlogs.emf.model.Unit |
| 25 | +import software.amazon.lambda.powertools.logging.Logging |
| 26 | +import software.amazon.lambda.powertools.logging.LoggingUtils |
| 27 | +import software.amazon.lambda.powertools.metrics.Metrics |
| 28 | +import software.amazon.lambda.powertools.metrics.MetricsUtils |
| 29 | +import software.amazon.lambda.powertools.tracing.CaptureMode |
| 30 | +import software.amazon.lambda.powertools.tracing.Tracing |
| 31 | +import software.amazon.lambda.powertools.tracing.TracingUtils |
| 32 | +import java.io.BufferedReader |
| 33 | +import java.io.IOException |
| 34 | +import java.io.InputStreamReader |
| 35 | +import java.net.URL |
| 36 | +import java.util.stream.Collectors |
| 37 | + |
| 38 | +/** |
| 39 | + * Handler for requests to Lambda function. |
| 40 | + */ |
| 41 | + |
| 42 | +class App : RequestHandler<APIGatewayProxyRequestEvent?, APIGatewayProxyResponseEvent> { |
| 43 | + @Logging(logEvent = true, samplingRate = 0.7) |
| 44 | + @Tracing(captureMode = CaptureMode.RESPONSE_AND_ERROR) |
| 45 | + @Metrics(namespace = "ServerlessAirline", service = "payment", captureColdStart = true) |
| 46 | + |
| 47 | + override fun handleRequest(input: APIGatewayProxyRequestEvent?, context: Context?): APIGatewayProxyResponseEvent { |
| 48 | + val headers = mapOf("Content-Type" to "application/json", "X-Custom-Header" to "application/json") |
| 49 | + MetricsUtils.metricsLogger().putMetric("CustomMetric1", 1.0, Unit.COUNT) |
| 50 | + MetricsUtils.withSingleMetric("CustomMetrics2", 1.0, Unit.COUNT, "Another") { metric: MetricsLogger -> |
| 51 | + metric.setDimensions(DimensionSet.of("AnotherService", "CustomService")) |
| 52 | + metric.setDimensions(DimensionSet.of("AnotherService1", "CustomService1")) |
| 53 | + } |
| 54 | + LoggingUtils.appendKey("test", "willBeLogged") |
| 55 | + val response = APIGatewayProxyResponseEvent().withHeaders(headers) |
| 56 | + return try { |
| 57 | + val pageContents = getPageContents("https://checkip.amazonaws.com") |
| 58 | + log.info(pageContents) |
| 59 | + TracingUtils.putAnnotation("Test", "New") |
| 60 | + val output = """ |
| 61 | + { |
| 62 | + "message": "hello world", |
| 63 | + "location": "$pageContents" |
| 64 | + } |
| 65 | + """.trimIndent() |
| 66 | + TracingUtils.withSubsegment("loggingResponse") { _: Subsegment? -> |
| 67 | + val sampled = "log something out" |
| 68 | + log.info(sampled) |
| 69 | + log.info(output) |
| 70 | + } |
| 71 | + log.info("After output") |
| 72 | + response.withStatusCode(200).withBody(output) |
| 73 | + } catch (e: RuntimeException) { |
| 74 | + response.withBody("{}").withStatusCode(500) |
| 75 | + } catch (e: IOException) { |
| 76 | + response.withBody("{}").withStatusCode(500) |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + @Tracing |
| 81 | + private fun log() { |
| 82 | + log.info("inside threaded logging for function") |
| 83 | + } |
| 84 | + |
| 85 | + @Tracing(namespace = "getPageContents", captureMode = CaptureMode.DISABLED) |
| 86 | + @Throws(IOException::class) |
| 87 | + private fun getPageContents(address: String): String { |
| 88 | + val url = URL(address) |
| 89 | + TracingUtils.putMetadata("getPageContents", address) |
| 90 | + return InputStreamReader(url.openStream()).use { reader -> |
| 91 | + reader.readText().trim() |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + private val log = LogManager.getLogger(App::class) |
| 96 | +} |
0 commit comments