Skip to content

Instantly share code, notes, and snippets.

@napicella
Created January 3, 2021 13:28
Show Gist options
  • Save napicella/da9cf37288e75c81ee6c126ec4a3be83 to your computer and use it in GitHub Desktop.
Save napicella/da9cf37288e75c81ee6c126ec4a3be83 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"time"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/prozz/aws-embedded-metrics-golang/emf"
)
func handler(ctx context.Context, event MyEvent) (string, error) {
_, _ = getAwsSession()
// do something with the AWS session
return "done", nil
}
func main() {
lambda.Start(handler)
}
type MyEvent struct {
URL string `json:"url"`
}
func getAwsSession() (*session.Session, error) {
s, e := session.NewSession()
if e != nil {
return nil, e
}
s.Handlers.Complete.PushFrontNamed(request.NamedHandler{
Name: "MetricHandler",
Fn: func(r *request.Request) {
m := emf.New().Namespace("CustomNamespace")
defer m.Log()
m.DimensionSet(
emf.NewDimension("Endpoint", r.ClientInfo.ServiceName),
emf.NewDimension("Operation", r.Operation.Name)).
MetricFloatAs("Duration", float64(time.Since(r.AttemptTime).Milliseconds()), emf.Milliseconds)
},
})
return s, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment