extensions

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 11 Imported by: 1

README

lambda-extensions

lambda-extensions is a library for AWS Lambda Extensions API.

Usage

Register an external extension
package main

import (
    "context"
    "log"

    extensions "github.com/fujiwara/lambda-extensions"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()
    client, _ := extensions.NewClient()
    client.CallbackInvoke = func (ctx context.Context, event *extensions.InvokeEvent) error {
        log.Printf("invoke event: %v", event)
        // do something on invoke event
        return nil
    }
    client.CallbackShutdown = func (ctx context.Context, event *extensions.ShutdownEvent) error {
        log.Printf("shutdown event: %v", event)
        // do something on shutdown event
        cancel()
        return nil
    }
    if err := client.Register(ctx); err != nil {
        log.Fatal(err)
    }
    if err := client.Run(ctx); err != nil {
        log.Fatal(err)
    }
}
Subscribe to Telemetry API
package main

import (
    "context"
    "log"

    extensions "github.com/fujiwara/lambda-extensions"
)

func main() {
    ctx, cancel := context.WithCancel(context.Background())
    defer cancel()

    go func () {
       // run http server listening on 9999 for receiving telemetry data
    }()

    client, _ := extensions.NewClient()
    client.CallbackShutdown = func (ctx context.Context, event *extensions.ShutdownEvent) error {
        log.Printf("shutdown event: %v", event)
        // do something
        stopHttpServer() // stop your http server
        return nil
    }
    if err := client.Register(ctx); err != nil {
        log.Fatal(err)
    }
    sub := &extensions.TelemetrySubscription{
		SchemaVersion: "2022-12-13",
		Types:         []string{"function", "platform"},
		Buffering: extensions.TelemetryBuffering{
			MaxItems:  500,
			MaxBytes:  1024 * 1024,
			TimeoutMs: 1000,
		},
		Destination: extensions.TelemetryDestination{
			Protocol: "HTTP",
			URI:      "http://sandbox.localdomain:9999",
		},
	}
    if err := client.SubscribeTelemetry(ctx, sub); err != nil {
        log.Fatal(err)
    }
    if err := client.Run(ctx); err != nil {
        log.Fatal(err)
    }
}

LICENSE

MIT

Author

fujiwara

Documentation

Index

Constants

View Source
const (
	Invoke               = EventType("INVOKE")
	Shutdown             = EventType("SHUTDOWN")
	DefaultTelemetryPort = 8080
)

Variables

View Source
var (
	DefaultTelemetrySubscription = TelemetrySubscription{
		SchemaVersion: "2022-12-13",
		Types:         []string{"function", "platform"},
		Buffering: TelemetryBuffering{
			MaxItems:  500,
			MaxBytes:  1024 * 1024,
			TimeoutMs: 1000,
		},
		Destination: TelemetryDestination{
			Protocol: "HTTP",
			URI:      fmt.Sprintf("http://sandbox.localdomain:%d", DefaultTelemetryPort),
		},
	}
)

Functions

func MockExtensionAPIHandler added in v0.0.4

func MockExtensionAPIHandler() http.Handler

Types

type Client

type Client struct {
	Name             string // Name is the name of the extension
	CallbackInvoke   func(context.Context, *InvokeEvent) error
	CallbackShutdown func(context.Context, *ShutdownEvent) error
	// contains filtered or unexported fields
}

Client is a client for Lambda Extensions API

func NewClient

func NewClient() (*Client, error)

NewClient creates a new client for Lambda Extensions API

func (*Client) Register

func (c *Client) Register(ctx context.Context) error

Register registers the extension to the Lambda extension API

func (*Client) Run

func (c *Client) Run(ctx context.Context) error

Run runs the extension client

func (*Client) SubscribeTelemetry

func (c *Client) SubscribeTelemetry(ctx context.Context, subscription *TelemetrySubscription) error

SubscribeTelemetry subscribes to the telemetry API

type Event

type Event struct {
	Invoke   *InvokeEvent
	Shutdown *ShutdownEvent
}

func (*Event) UnmarshalJSON

func (r *Event) UnmarshalJSON(data []byte) error

type EventType

type EventType string

type InvokeEvent

type InvokeEvent struct {
	EventType          EventType `json:"eventType"`
	DeadlineMs         int       `json:"deadlineMs"`
	RequestID          string    `json:"requestId"`
	InvokedFunctionArn string    `json:"invokedFunctionArn"`
	Tracing            struct {
		Type  string `json:"type"`
		Value string `json:"value"`
	} `json:"tracing"`
}

type ShutdownEvent

type ShutdownEvent struct {
	EventType      EventType `json:"eventType"`
	DeadlineMs     int       `json:"deadlineMs"`
	ShutdownReason string    `json:"shutdownReason"`
}

type TelemetryBuffering

type TelemetryBuffering struct {
	MaxItems  int `json:"maxItems"`
	MaxBytes  int `json:"maxBytes"`
	TimeoutMs int `json:"timeoutMs"`
}

type TelemetryDestination

type TelemetryDestination struct {
	Protocol string `json:"protocol"`
	URI      string `json:"URI"`
}

type TelemetrySubscription

type TelemetrySubscription struct {
	SchemaVersion string               `json:"schemaVersion"`
	Types         []string             `json:"types"`
	Buffering     TelemetryBuffering   `json:"buffering"`
	Destination   TelemetryDestination `json:"destination"`
}

func NewDefaultTelemetrySubscription

func NewDefaultTelemetrySubscription() *TelemetrySubscription

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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