kratos

package module
v0.0.0-...-1db7dc6 Latest Latest
Warning

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

Go to latest
Published: Jun 14, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

Go2sky with go-kratos v2

Installation

go get -u github.com/SkyAPM/go2sky-plugins/kratos

Usage

Server:

    //Use gRPC reporter for production
	r, err := reporter.NewLogReporter()
	if err != nil {
		panic(err)
	}
	defer r.Close()

	logger := log.NewStdLogger(os.Stdout)
	logger = log.With(logger, "segment_id", SegmentID())
	logger = log.With(logger, "trace_id", TraceID())
	logger = log.With(logger, "span_id", SpanID())

	serverName := "kratos-server"
	tracer, err := go2sky.NewTracer(serverName, go2sky.WithReporter(r))
	if err != nil {
		panic(err)
	}
	httpSvr := http.NewServer(
		http.Address(":8000"),
		http.Middleware(
			recovery.Recovery(),
			Server(tracer),
			logging.Server(logger),
		),
	)

	grpcSvr := grpc.NewServer(
		grpc.Address(":9000"),
		grpc.Middleware(
			middleware.Chain(
				recovery.Recovery(),
				Server(tracer),
				logging.Server(logger),
			),
		))

	s := &server{}
	helloworld.RegisterGreeterServer(grpcSvr, s)
	helloworld.RegisterGreeterHTTPServer(httpSvr, s)

	app := kratos.New(
		kratos.Name(serverName),
		kratos.Server(
			httpSvr,
			grpcSvr,
		),
	)

	if err := app.Run(); err != nil {
		panic(err)
	}

Client:

    //Use gRPC reporter for production
	r, err := reporter.NewLogReporter()
	if err != nil {
		panic(err)
	}
	defer r.Close()

	logger := log.NewStdLogger(os.Stdout)
	logger = log.With(logger, "segment_id", SegmentID())
	logger = log.With(logger, "trace_id", TraceID())
	logger = log.With(logger, "span_id", SpanID())

	clientName := "kratos-client"
	tracer, err := go2sky.NewTracer(clientName, go2sky.WithReporter(r))
	if err != nil {
		panic(err)
	}

	httpCli, err := http.NewClient(
		context.Background(),
		http.WithMiddleware(
			Client(tracer),
			logging.Client(logger),
		),
		http.WithEndpoint("127.0.0.1:8000"),
	)
	if err != nil {
		panic(err)
	}
	defer httpCli.Close()

	grpcCli, err := grpc.DialInsecure(
		context.Background(),
		grpc.WithMiddleware(
			Client(tracer),
			logging.Client(logger),
		),
		grpc.WithEndpoint("127.0.0.1:9000"),
	)
	if err != nil {
		panic(err)
	}
	defer grpcCli.Close()

	httpClient := helloworld.NewGreeterHTTPClient(httpCli)
	grpcClient := helloworld.NewGreeterClient(grpcCli)

	reply, err := httpClient.SayHello(context.Background(), &helloworld.HelloRequest{Name: "http-kratos"})
	if err != nil {
		panic(err)
	}
	fmt.Printf("[http] Say hello: %s\n", reply)

	reply, err = grpcClient.SayHello(context.Background(), &helloworld.HelloRequest{Name: "grpc-kratos"})
	if err != nil {
		panic(err)
	}
	fmt.Printf("[grpc] Say hello: %s\n", reply)

Documentation

Overview

Package kratos is a plugin that can be used to trace Go-kratos framework.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client(tracer *go2sky.Tracer, opts ...Option) middleware.Middleware

Client go2sky middleware for kratos client

Example
//Use gRPC reporter for production
r, err := reporter.NewLogReporter()
if err != nil {
	panic(err)
}
defer r.Close()

logger := log.NewStdLogger(os.Stdout)
logger = log.With(logger, "segment_id", SegmentID())
logger = log.With(logger, "trace_id", TraceID())
logger = log.With(logger, "span_id", SpanID())

clientName := "kratos-client"
tracer, err := go2sky.NewTracer(clientName, go2sky.WithReporter(r))
if err != nil {
	panic(err)
}

httpCli, err := http.NewClient(
	context.Background(),
	http.WithMiddleware(
		Client(tracer),
		logging.Client(logger),
	),
	http.WithEndpoint("localhost:8000"),
)
if err != nil {
	panic(err)
}
defer func() {
	_ = httpCli.Close()
}()

grpcCli, err := grpc.DialInsecure(
	context.Background(),
	grpc.WithMiddleware(
		Client(tracer),
		logging.Client(logger),
	),
	grpc.WithEndpoint("localhost:9000"),
)
if err != nil {
	panic(err)
}
defer func() {
	_ = grpcCli.Close()
}()

httpClient := helloworld.NewGreeterHTTPClient(httpCli)
grpcClient := helloworld.NewGreeterClient(grpcCli)

reply, err := httpClient.SayHello(context.Background(), &helloworld.HelloRequest{Name: "http-kratos"})
if err != nil {
	panic(err)
}
fmt.Printf("[http] Say hello: %s\n", reply)

reply, err = grpcClient.SayHello(context.Background(), &helloworld.HelloRequest{Name: "grpc-kratos"})
if err != nil {
	panic(err)
}
fmt.Printf("[grpc] Say hello: %s\n", reply)
Output:

func SegmentID

func SegmentID() log.Valuer

SegmentID inject the current segmentId into the kratos log

func Server

func Server(tracer *go2sky.Tracer, opts ...Option) middleware.Middleware

Server go2sky middleware for kratos server

Example
//Use gRPC reporter for production
r, err := reporter.NewLogReporter()
if err != nil {
	panic(err)
}
defer r.Close()

logger := log.NewStdLogger(os.Stdout)
logger = log.With(logger, "segment_id", SegmentID())
logger = log.With(logger, "trace_id", TraceID())
logger = log.With(logger, "span_id", SpanID())

serverName := "kratos-server"
tracer, err := go2sky.NewTracer(serverName, go2sky.WithReporter(r))
if err != nil {
	panic(err)
}
httpSvr := http.NewServer(
	http.Address(":8000"),
	http.Middleware(
		recovery.Recovery(),
		Server(tracer),
		logging.Server(logger),
	),
)

grpcSvr := grpc.NewServer(
	grpc.Address(":9000"),
	grpc.Middleware(
		middleware.Chain(
			recovery.Recovery(),
			Server(tracer),
			logging.Server(logger),
		),
	))

s := &server{}
helloworld.RegisterGreeterServer(grpcSvr, s)
helloworld.RegisterGreeterHTTPServer(httpSvr, s)

app := kratos.New(
	kratos.Name(serverName),
	kratos.Server(
		httpSvr,
		grpcSvr,
	),
)

if err := app.Run(); err != nil {
	panic(err)
}
Output:

func SpanID

func SpanID() log.Valuer

SpanID inject the current spanId into the kratos log

func TraceID

func TraceID() log.Valuer

TraceID inject the current traceId into the kratos log

Types

type Option

type Option func(*options)

Option allows for functional options to adjust behaviour

func WithReportTags

func WithReportTags(tags ...string) Option

WithReportTags will set tags that need to report in metadata

Directories

Path Synopsis
test

Jump to

Keyboard shortcuts

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