ppkratos

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: 9 Imported by: 0

README

ppkratos

This package instruments the go-kratos/kratos/v2 package.

Installation

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

Usage

PkgGoDev

This package instruments kratos servers and clients.

server

To instrument a kratos server, use ServerMiddleware.

httpSrv := http.NewServer(
    http.Address(":8000"),
    http.Middleware(ppkratos.ServerMiddleware()),
)

The server middleware adds the pinpoint.Tracer to the kratos server handler's context. By using the pinpoint.FromContext function, this tracer can be obtained. Alternatively, the context of the handler may be propagated where the context that contains the pinpoint.Tracer is required.

func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
    tracer := pinpoint.FromContext(ctx)
    defer tracer.NewSpanEvent("f1").EndSpanEvent()
    ...
}
package main

import (
    "github.com/go-kratos/kratos/v2"
    "github.com/go-kratos/kratos/v2/transport"
    "github.com/go-kratos/kratos/v2/transport/http"
    "github.com/pinpoint-apm/pinpoint-go-agent"
    "github.com/pinpoint-apm/pinpoint-go-agent/plugin/kratos"
)

type server struct {
    helloworld.UnimplementedGreeterServer
}

func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
    tracer := pinpoint.FromContext(ctx)
    defer tracer.NewSpanEvent("f1").EndSpanEvent()

    return &helloworld.HelloReply{Message: fmt.Sprintf("Hello %+v", in.Name)}, nil
}

func main() {
    ... //setup agent
	
    s := &server{}
    httpSrv := http.NewServer(
        http.Address(":8000"),
        http.Middleware(
            ppkratos.ServerMiddleware(),
        ),
    )
    ...
}

Full Example Source

client

To instrument a kratos client, use ClientMiddleware.

conn, err := transhttp.NewClient(
    context.Background(),
    transhttp.WithMiddleware(ppkratos.ClientMiddleware()),
    transhttp.WithEndpoint("127.0.0.1:8000"),
)

It is necessary to pass the context containing the pinpoint.Tracer to kratos client.

client := pb.NewGreeterHTTPClient(conn)
reply, err := client.SayHello(pinpoint.NewContext(context.Background(), tracer), &pb.HelloRequest{Name: "kratos"})
import (
    transhttp "github.com/go-kratos/kratos/v2/transport/http"
    "github.com/pinpoint-apm/pinpoint-go-agent"
    "github.com/pinpoint-apm/pinpoint-go-agent/plugin/kratos"
)

func callHTTP(w http.ResponseWriter, r *http.Request) {
    conn, err := transhttp.NewClient(
        context.Background(),
        transhttp.WithMiddleware(ppkratos.ClientMiddleware()),
        transhttp.WithEndpoint("127.0.0.1:8000"),
    )

    client := pb.NewGreeterHTTPClient(conn)
    reply, err := client.SayHello(r.Context(), &pb.HelloRequest{Name: "kratos"})
    ...
}

Full Example Source

Documentation

Overview

Package ppkratos instruments the go-kratos/kratos/v2 package (https://github.com/go-kratos/kratos).

This package instruments kratos servers and clients. To instrument a kratos server, use ServerMiddleware.

httpSrv := http.NewServer(
	http.Address(":8000"),
	http.Middleware(ppkratos.ServerMiddleware()),
)

The server middleware adds the pinpoint.Tracer to the kratos server handler's context. By using the pinpoint.FromContext function, this tracer can be obtained.

func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
	tracer := pinpoint.FromContext(ctx)
	defer tracer.NewSpanEvent("f1").EndSpanEvent()
	...
}

To instrument a kratos client, use ClientMiddleware.

conn, err := transhttp.NewClient(
	context.Background(),
	transhttp.WithMiddleware(ppkratos.ClientMiddleware()),
	transhttp.WithEndpoint("127.0.0.1:8000"),
)

It is necessary to pass the context containing the pinpoint.Tracer to kratos client.

client := pb.NewGreeterHTTPClient(conn)
reply, err := client.SayHello(pinpoint.NewContext(context.Background(), tracer), &pb.HelloRequest{Name: "kratos"})

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClientMiddleware

func ClientMiddleware() middleware.Middleware

ClientMiddleware returns a client side middleware ready to instrument.

func ServerMiddleware

func ServerMiddleware() middleware.Middleware

ServerMiddleware returns a server side middleware ready to instrument and adds the pinpoint.Tracer to the handler's context. By using the pinpoint.FromContext function, this tracer can be obtained.

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