ppfasthttp

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2024 License: Apache-2.0 Imports: 7 Imported by: 2

README

ppfasthttp

This package instruments the valyala/fasthttp package.

Installation

$ go get github.com/pinpoint-apm/pinpoint-go-agent/plugin/fasthttp
import "github.com/pinpoint-apm/pinpoint-go-agent/plugin/fasthttp"

Usage

PkgGoDev

server

This package instruments inbound requests handled by a fasthttp instance. Use WrapHandler to select the handlers you want to track:

fasthttp.ListenAndServe(":9000", ppfasthttp.WrapHandler(requestHandler))

WrapHandler sets the pinpoint.Tracer as a user value of fasthttp handler's context. By using the ppfasthttp.CtxKey, this tracer can be obtained in your handler. Alternatively, the context of the handler may be propagated where the context that contains the pinpoint.Tracer is required.

package main

import (
    "github.com/pinpoint-apm/pinpoint-go-agent"
    "github.com/pinpoint-apm/pinpoint-go-agent/plugin/fasthttp"
    "github.com/valyala/fasthttp"
)

func requestHandler(ctx *fasthttp.RequestCtx) {
    tracer := pinpoint.FromContext(ctx.UserValue(ppfasthttp.CtxKey).(context.Context))
    defer tracer.NewSpanEvent("f1").EndSpanEvent()

    fmt.Fprintf(ctx, "Hello, world!\n\n")
}

func main() {
    ... //setup agent
	
    fasthttp.ListenAndServe(":9000", ppfasthttp.WrapHandler(requestHandler))
}

Full Example Source

This package supports URL Statistics feature. It aggregates response times, successes and failures for each pattern given as parameter of a WrapHandler function.

Config Options
client

This package instruments outbound requests and add distributed tracing headers. Use DoClient.

err := ppfasthttp.DoClient(func() error {
    return hc.Do(req, resp)
}, ctx, req, resp)

It is necessary to pass the context containing the pinpoint.Tracer to DoClient.

func client(ctx *fasthttp.RequestCtx) {
    url := fasthttp.AcquireURI()
    url.Parse(nil, []byte("http://localhost:8080/"))

    hc := &fasthttp.HostClient{Addr: "localhost:8080"}
    req := fasthttp.AcquireRequest()
    req.SetURI(url)
    resp := fasthttp.AcquireResponse()

    ctxWithTracer := ctx.UserValue(ppfasthttp.CtxKey).(context.Context)
    err := ppfasthttp.DoClient(func() error {
        return hc.Do(req, resp)
    }, ctxWithTracer, req, resp)

    ...
}

Full Example Source

Config Options

Documentation

Overview

Package ppfasthttp instruments the valyala/fasthttp package (https://github.com/valyala/fasthttp).

This package instruments inbound requests handled by a fasthttp instance. Use WrapHandler to select the handlers you want to track:

fasthttp.ListenAndServe(":9000", func(ctx *fasthttp.RequestCtx) {
  path := string(ctx.Path())
  if strings.HasPrefix(path, "/foo") {
    ppfasthttp.WrapHandler(fooHandler, "/foo")(ctx)
  } else if strings.HasPrefix(path, "/bar") {
    ppfasthttp.WrapHandler(barHandler, "/bar")(ctx)
  }
})

WrapHandler sets the pinpoint.Tracer as a user value of fasthttp handler's context. By using the ppfasthttp.CtxKey, this tracer can be obtained.

func requestHandler(ctx *fasthttp.RequestCtx) {
    tracer := pinpoint.FromContext(ctx.UserValue(ppfasthttp.CtxKey).(context.Context))

This package instruments outbound requests and add distributed tracing headers. Use DoClient.

err := ppfasthttp.DoClient(func() error {
	return hc.Do(req, resp)
}, ctx, req, resp)

It is necessary to pass the context containing the pinpoint.Tracer to DoClient.

Index

Constants

View Source
const CtxKey = "pinpoint"

Variables

This section is empty.

Functions

func DoClient

func DoClient(doFunc func() error, ctx context.Context, req *fasthttp.Request, res *fasthttp.Response) error

DoClient instruments outbound requests and add distributed tracing headers.

func WrapHandler

func WrapHandler(handler fasthttp.RequestHandler, pattern ...string) fasthttp.RequestHandler

WrapHandler wraps the given http request handler.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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