go-agent icon indicating copy to clipboard operation
go-agent copied to clipboard

How to use AWS Lambda Web Adapter in golang app

Open Fujita-Naoyuki opened this issue 9 months ago • 0 comments

Summary

newrelic cannot be used with AWS Lambda with the following configuration

Desired Behaviour

newrelic is available and information can be referenced on APM

Possible Solution

  • Best solution: A library will be created to work with AWS Lambda Web Adapter like nrlambda
  • Another solution: Documents related to settings for operation using the newrelic.NewApplication method will be published.

Additional context

Sample: doesn't work

  • main.go
package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/newrelic/go-agent/v3/integrations/logcontext-v2/logWriter"

	"github.com/newrelic/go-agent/v3/newrelic"
)

var app *newrelic.Application
var writer logWriter.LogWriter
var logger *log.Logger

func usersHandler(rw http.ResponseWriter, req *http.Request) {
	txn := app.StartTransaction("usersHandler")
	defer txn.End()
	fmt.Fprintf(rw, "Hello, World")
	logger.Printf("receive hello world request")
}

func main() {
	app, _ = newrelic.NewApplication(
		newrelic.ConfigAppName("newrelic-sample"),
		newrelic.ConfigLicense("mykey"),
		newrelic.ConfigAppLogForwardingEnabled(true),
	)
	writer = logWriter.New(os.Stdout, app)
	logger = log.New(&writer, "", log.Default().Flags())
	logger.Printf("cold start")

	http.HandleFunc(newrelic.WrapHandleFunc(app, "/", usersHandler))

	http.ListenAndServe(":8000", nil)
}
  • Dockerfile
# build
FROM golang:1.21 as build
WORKDIR /helloworld
COPY go.mod go.sum ./
COPY main.go .
RUN GOOS=linux CGO_ENABLED=0  go build -o main main.go

# Copy artifacts to a clean image
FROM alpine:3.9
COPY --from=public.ecr.aws/awsguru/aws-lambda-adapter:0.8.3 /lambda-adapter /opt/extensions/lambda-adapter
COPY --from=build /helloworld/main /main
ENV PORT=8000
EXPOSE 8000
ENTRYPOINT [ "/main" ]

Fujita-Naoyuki avatar May 04 '24 16:05 Fujita-Naoyuki