-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
84 lines (73 loc) · 2.13 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// It is modified the source code from kratos example
// https://github.com/go-kratos/examples/tree/main/helloworld
package main
import (
"context"
"fmt"
"log"
"os"
"github.com/go-kratos/kratos/v2"
"github.com/go-kratos/kratos/v2/errors"
"github.com/go-kratos/kratos/v2/transport"
"github.com/go-kratos/kratos/v2/transport/grpc"
"github.com/go-kratos/kratos/v2/transport/http"
"github.com/pinpoint-apm/pinpoint-go-agent"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/kratos"
"github.com/pinpoint-apm/pinpoint-go-agent/plugin/kratos/example/helloworld"
)
// server is used to implement helloworld.GreeterServer.
type server struct {
helloworld.UnimplementedGreeterServer
}
// SayHello implements helloworld.GreeterServer
func (s *server) SayHello(ctx context.Context, in *helloworld.HelloRequest) (*helloworld.HelloReply, error) {
tracer := pinpoint.FromContext(ctx)
defer tracer.NewSpanEvent("f1").EndSpanEvent()
defer tracer.NewSpanEvent("f2").EndSpanEvent()
tr, _ := transport.FromServerContext(ctx)
log.Printf("[server] Endpoint %s\n", tr.Endpoint())
if in.Name == "error" {
return nil, errors.BadRequest("custom_error", fmt.Sprintf("invalid argument %s", in.Name))
}
if in.Name == "panic" {
panic("server panic")
}
return &helloworld.HelloReply{Message: fmt.Sprintf("Hello %+v", in.Name)}, nil
}
func main() {
opts := []pinpoint.ConfigOption{
pinpoint.WithAppName("TestKratosServer"),
pinpoint.WithConfigFile(os.Getenv("HOME") + "/tmp/pinpoint-config.yaml"),
}
cfg, _ := pinpoint.NewConfig(opts...)
agent, err := pinpoint.NewAgent(cfg)
if err != nil {
log.Fatalf("pinpoint agent start fail: %v", err)
}
defer agent.Shutdown()
s := &server{}
httpSrv := http.NewServer(
http.Address(":8000"),
http.Middleware(
ppkratos.ServerMiddleware(),
),
)
grpcSrv := grpc.NewServer(
grpc.Address(":9000"),
grpc.Middleware(
ppkratos.ServerMiddleware(),
),
)
helloworld.RegisterGreeterServer(grpcSrv, s)
helloworld.RegisterGreeterHTTPServer(httpSrv, s)
app := kratos.New(
kratos.Name("helloworld"),
kratos.Server(
httpSrv,
grpcSrv,
),
)
if err := app.Run(); err != nil {
log.Fatal(err)
}
}