Git Product home page Git Product logo

py-common-services-lib's Introduction

AWS Lambda logging

This projects provides a function to log AWS Lambda executions to cloudwatch.

Requirements

  • python

Usage

from common_services import create_log_message

def handle_request(event,context):
    log = create_log_message.CreateAuditMessage(context=context)
    log.createauditmessage(business_key="test",business_value="test",log_level="INFO",trace_id="12334",information="this is a info message",message="received")

Structuring AWS Lambda Logs

  1. Use the CreateAuditMessage_class.py to structure your logs as a json

  2. Below are the logs structured when used the CreateAuditMessage_Class.

    Received:

    {
      "timestamp": "2020-03-05T22:40:51",
      "logEvent": "Received",
      "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
      "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
      "logLevel": "INFO",
      "componentName": "ship-gr-idoc-to-fusion-connectivity",
      "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
      "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKey": {
          "InboundDelivery": "533197700-533197700",
          "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
          "PlantCode": "1062"
       },
      "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
    }

    Delivered:

    {
          "timestamp": "2020-03-05T22:40:51",
          "logEvent": "Delivered",
          "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
          "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
          "logLevel": "INFO",
          "componentName": "ship-gr-idoc-to-fusion-connectivity",
          "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
          "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKey": {
              "InboundDelivery": "533197700-533197700",
              "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
              "PlantCode": "1062"
           },
          "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
      }

    Discarded:

    {
          "timestamp": "2020-03-05T22:40:51",
          "logEvent": "Discarded",
          "message" : "Invalid Carton number",
          "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
          "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
          "logLevel": "INFO",
          "componentName": "ship-gr-idoc-to-fusion-connectivity",
          "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
          "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
          "businessKey": {
              "InboundDelivery": "533197700-533197700",
              "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
              "PlantCode": "1062"
           },
          "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
      }

    Failure:

    {
      "timestamp": "2020-03-05T22:40:53",
      "message": "An exception of type Exception occurred. Arguments:('Tansient Error while posting to fusion. Message will be retried OSB Service Callout action received SOAP Fault response',)",
      "logEvent": "Failure",
      "logGroup": "/aws/lambda/ship-gr-idoc-to-fusion-connectivity",
      "logStream": "2020/03/05/[$LATEST]33d7348572e14ec48efa0ea7f9745d9f",
      "logLevel": "ERROR",
      "componentName": "ship-gr-idoc-to-fusion-connectivity",
      "traceId": "a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKeyStructure": "InboundDelivery|RefId|PlantCode|TrackingId",
      "businessKeyValue": "533197700-533197700|Vamsi-64ee-49f3-abc8-d2c49edc1139|1062|a158f731-c424-44af-84fb-aca07fb9a0f7",
      "businessKey": {
          "InboundDelivery": "533197700-533197700",
          "RefId": "Vamsi-64ee-49f3-abc8-d2c49edc1139",
          "PlantCode": "1062",
          "TrackingId": "a158f731-c424-44af-84fb-aca07fb9a0f7"
      },
      "awsRequestId": "22793623-886c-530e-b173-60f80a49aac1"
    }
  3. It is recommended to write a Received log when the lambda is invoked and Delivered audit when the lambda execution is completed. This helps us to track the transaction status in the lambda. The business keys are unique attributes that can identify an event. Chose the business keys as per your business needs. When need be, we can create reports and stats using these business keys

  4. When the logs are structured as outlined above, we can create a Splunk dashboard using the below method

py-common-services-lib's People

Contributors

vamsi245 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.