Go-xrayprofile - Selective profiling of AWS Lambda functions

go-xrayprofile

AWS X-Ray is handy for understanding the overall performance of your systems. Sometimes you want much greater function- or line-level detail on what your Lambda function is busy doing - but not all the time, as profiling can incur a performance impact.

go-xrayprofile is a middleware that strikes a balance. It can selectively profile (using fgprof) the execution of a configurable percentage of Lambda function invocations. These profiles are written to S3 for later download and analysis using go tool pprof.

Example usage

Integrate go-xrayprofile into your project by wrapping your existing Lambda handler function like so:

package main

import (
	"context"
	"encoding/json"
	"github.com/aidansteele/go-xrayprofile"
	"github.com/aws/aws-lambda-go/lambda"
	"time"
)

func main() {
	handler := lambda.NewHandler(handle)
	// nil options is also fine
	handler = xrayprofile.Wrap(handler, &xrayprofile.Options{})
	lambda.Start(handler)
}

func handle(ctx context.Context, input json.RawMessage) error {
	time.Sleep(2 * time.Second)
	return nil
}

With the above options (the default), one in 65,536 invocations will be profiled. To profile 1% of all invocations you can set options.Threshold to 655, for 10% it would be 6553 and so on.

How it works

The decision whether to profile or not is based on the last four hexadecimal digits of the X-Ray root trace ID. E.g. if options.Threshold is set to 500 and the trace header is X-Amzn-Trace-Id: Root=1-5759e988-bd862e3fe1be46a99427[01F0] (brackets inserted for clarity) then the invocation will be profiled because 0x01F0 <= 500.

We base this decision on the X-Ray trace header (rather than generating our own random number) because then a single user-initiated trace can result in profiles for every Lambda function associated with that trace stored together on S3. This is likely more useful than unrelated traces for understanding end-to-end performance.

Help wanted

It would be great if people took this idea and implemented it for other languages, especially those that have native support on AWS Lambda.

Similar Resources

Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure.

Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure.

tutor-pet API Simple CRUD API written in Go, built using AWS SAM tool and using the AWS' infrastructure. Macro architecture: Code architecture: Pre-Re

Aug 17, 2022

Aws-parameter-bulk - Export AWS SSM Parameter Store values in bulk to .env files

aws-parameter-bulk Utility to read parameters from AWS Systems Manager (SSM) Par

Oct 18, 2022

Aws-console-plugin - The current HashiCorp Vault AWS Secret Engine currently supports the creation of short lived API keys using the IAM User

aws-console-plugin Background The current HashiCorp Vault AWS Secret Engine curr

Feb 7, 2022

Aws-cognito-demo-go - Source code for AWS Cognito in Go

AWS Cognito Demo in Go Source code for YouTube series, AWS Cognito in Go - https

Dec 10, 2022

Una prueba técnica: Servicio Golang REST API local, sobre Docker, gRPC, AWS Serverless y sobre Kubernetes en AWS EC2

Una prueba técnica: Servicio Golang REST API local, sobre Docker, gRPC, AWS Serverless y sobre Kubernetes en AWS EC2

May 7, 2022

Aws-cdk-go-examples - Example projects using the AWS CDK by Golang

aws-cdk-go-examples Example projects using the AWS CDK by Golang Useful commands

Nov 24, 2022

Lambda Extension for iamlive

iamlive Lambda Extension The iamlive Lambda Extension helps generate a least-privilege IAM policy by monitoring the AWS calls made within the Lambda e

Dec 17, 2022

Lambda service function

openfaas-lambda These are sets of example that i've used when migrating aws lambda to openfaas platform. A part of documenting on how it works, I'm op

Oct 26, 2022

A Lambda function built with SAM (Serverless Application Module)

AWS SAM Lambda Function © Israel Pereira Tavares da Silva The AWS Serverless Application Model (SAM) is an open-source framework for building serverle

Dec 19, 2021
Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises
Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises

AWS, Turn off my Account, please Lambda stack to turn off and destroy all resources from your personal AWS Account to avoid billing surprises Resource

Oct 25, 2022
This repository shows how can we use `AWS Lambda` to build serverless applications in golang.

Serverless Api in Go with AWS Lambda Here we are going to use AWS Lambda to build serverless applications in golang. Prerequisites You’ll need an AWS

Nov 3, 2021
Prueba de concepto: Boletia, una aplicación para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gateway, Lambda, DynamoDB, DynamoDB Streams
Prueba de concepto: Boletia, una aplicación para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gateway, Lambda, DynamoDB, DynamoDB Streams

Prueba de concepto: Boletia, una aplicación para venta de boletos, basada en microservicios event-driven. Desarrollada sobre AWS Serverless: Api Gatew

May 7, 2022
Golang AWS SAM Lambda example

Golang AWS SAM Lambda example This example project shows how to use AWS SAM with

Nov 18, 2022
Mrrobot - A simple greetings bot for Slack that uses events api and hosted on AWS Lambda

Mr. Robot a greeter bot for your slack community build_docker

Aug 21, 2022
Simple no frills AWS S3 Golang Library using REST with V4 Signing (without AWS Go SDK)

simples3 : Simple no frills AWS S3 Library using REST with V4 Signing Overview SimpleS3 is a golang library for uploading and deleting objects on S3 b

Nov 4, 2022
Integrate AWS EKS Anywhere cluster with AWS Services
 Integrate AWS EKS Anywhere cluster with AWS Services

This article provides step-by-step instruction on integrating AWS EKS Anywhere with AWS Services so the applications running on customer data center can securely connect with these services.

Mar 6, 2022
Apis para la administracion de notifiaciones, utilizando servicios como AWS SNS y AWS SQS

notificacion_api Servicio para envío de notificaciónes por difusión en AWS SNS Especificaciones Técnicas Tecnologías Implementadas y Versiones Golang

Jan 7, 2022
AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.
AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

AWS credential_process utility to assume AWS IAM Roles with Yubikey Touch and Authenticator App TOPT MFA to provide temporary session credentials; With encrypted caching and support for automatic credential refresh.

Dec 20, 2022
A package for access aws service using AWS SDK for Golang

goaws ?? A package for access aws service using AWS SDK for Golang Advantage with goaws package Example for get user list IAM with AWS SDK for Golang

Nov 25, 2021