instalambda

package module
v1.21.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 18, 2024 License: MIT Imports: 14 Imported by: 0

README

Instana instrumentation for AWS Lambda

This module contains instrumentation code for AWS Lambda functions written in Go that use github.com/aws/aws-lambda-go as a runtime.

PkgGoDev

Installation

To add github.com/instana/go-sensor/instrumentation/instalambda to your go.mod file, from your project directory run:

$ go get github.com/instana/go-sensor/instrumentation/instalambda

Usage

For detailed usage example see the documentation or example_test.go.

Instrumenting a lambda.Handler

To instrument a lambda.Handler wrap it with instalambda.WrapHandler() before passing it to labmda.StartHandler():

type Handler struct {
	// ...
}

func (Handler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
	// ...
}

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	h := &Handler{
		// ...
	}

	// Instrument your handler before passing it to lambda.StartHandler()
	lambda.StartHandler(instalambda.WrapHandler(h, sensor))
}
Instrumenting a handler function

To instrument a handler function passed to lambda.Start() or lambda.StartWithContext() first create an instrumented handler from it using instalambda.NewHandler() and then pass it to lambda.StartHandler():

func handle() {
	return "Hello, ƛ!", nil
}

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	// Create a new instrumented lambda.Handler from your handle function
	h := instalambda.NewHandler(func() (string, error) {

	}, sensor)

	// Pass the instrumented handler to lambda.StartHandler()
	lambda.StartHandler(h)
}
Trace context propagation

Whenever a handler function accepts context.Context as a first argument (and (lambda.Handler).Invoke() always does), instalambda instrumentation injects the entry span for this Lambda invokation into it. This span can be retireved with instana.SpanFromContext() and used as a parent to create any intermediate or exit spans within the handler function:

func MyHandler(ctx context.Context) error {
	// Pass the handler context to a subcall to trace its execution
	subCall(ctx)

	// ...

	// Propagate the trace context within an HTTP request to another service monitored with Instana
	// using an instrumented http.Client
	req, err := http.NewRequest("GET", url, nil)
    client := &http.Client{
	    Transport: instana.RoundTripper(sensor, nil),
	}

	client.Do(req.WithContext(ctx))

	// ...
}

func subCall(ctx context.Context) {
	if parent, ok := instana.SpanFromContext(ctx); ok {
		// start a new span, using the Lambda entry span as a parent
		sp = parent.Tracer().StartSpan(/* ... */)
		defer sp.Finish()
	}

	// ...
}

Documentation

Overview

Package instalambda provides Instana tracing instrumentation for AWS Lambda functions

Example

This example demonstrates how to instrument a handler function with Instana

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/lambda"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalambda"
)

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	// Create a new instrumented lambda.Handler from your handle function
	h := instalambda.NewHandler(func(ctx context.Context) (string, error) {
		// If your handler function takes context.Context as a first argument,
		// instrumentation will inject the parent span into it, so you can continue
		// the trace beyond your Lambda handler, e.g. when making external HTTP calls,
		// database queries, etc.
		if parent, ok := instana.SpanFromContext(ctx); ok {
			sp := parent.Tracer().StartSpan("internal")
			defer sp.Finish()
		}

		return "Hello, ƛ!", nil
	}, sensor)

	// Pass the instrumented handler to lambda.StartHandler()
	lambda.StartHandler(h)
}
Output:

Example (ApiGateway)

This example demonstrates how to instrument a handler function invoked with an API Gateway event

package main

import (
	"net/http"

	"github.com/aws/aws-lambda-go/events"
	"github.com/aws/aws-lambda-go/lambda"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalambda"
)

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	// Create a new instrumented lambda.Handler from your handle function
	h := instalambda.NewHandler(func(event *events.APIGatewayV2HTTPRequest) (events.APIGatewayV2HTTPResponse, error) {
		return events.APIGatewayV2HTTPResponse{
			StatusCode: http.StatusOK,
			Body:       "Hello, ƛ!",
		}, nil
	}, sensor)

	// Pass the instrumented handler to lambda.StartHandler()
	lambda.StartHandler(h)
}
Output:

Index

Examples

Constants

View Source
const Version = "1.21.0"

Version is the instrumentation module semantic version

Variables

This section is empty.

Functions

func NewHandler

func NewHandler(handlerFunc interface{}, sensor instana.TracerLogger) *wrappedHandler

NewHandler creates a new instrumented handler that can be used with `lambda.StartHandler()` from a handler function

Example

To instrument a handler function, create a new lambda.Handler using instalambda.NewHandler() and pass it to lambda.StartHandler()

package main

import (
	"github.com/aws/aws-lambda-go/lambda"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalambda"
)

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	// Create a new instrumented lambda.Handler from your handle function
	h := instalambda.NewHandler(func() (string, error) {
		return "Hello, ƛ!", nil
	}, sensor)

	// Pass the instrumented handler to lambda.StartHandler()
	lambda.StartHandler(h)
}
Output:

func WrapHandler

func WrapHandler(h lambda.Handler, sensor instana.TracerLogger) *wrappedHandler

WrapHandler instruments a lambda.Handler to trace the invokations with Instana

Example

To instrument a lambda.Handler, instrument it using instalambda.WrapHandler before passing to lambda.StartHandler()

package main

import (
	"context"

	"github.com/aws/aws-lambda-go/lambda"

	instana "github.com/instana/go-sensor"
	"github.com/instana/go-sensor/instrumentation/instalambda"
)

func main() {
	// Initialize a new sensor
	sensor := instana.NewSensor("my-go-lambda")

	h := Handler{
		// ...
	}

	// Instrument your handler before passing it to lambda.StartHandler()
	lambda.StartHandler(instalambda.WrapHandler(h, sensor))
}

// Handler is an example AWS Lambda handler
type Handler struct{}

// Invoke handles AWS Lambda events
func (Handler) Invoke(ctx context.Context, payload []byte) ([]byte, error) {
	return []byte("Hello, ƛ!"), nil
}
Output:

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL