GithubHelp home page GithubHelp logo

go-xrayprofile's Introduction

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.

go-xrayprofile's People

Contributors

aidansteele avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.