今天学在 Hertz 里面用 Jaeger
使用 Jaeger 进行链接追踪的示例
调用关系:
flowchart LR
n1[Hertz Client] -->|HTTP Call| n2[Hertz Server / Kitex Client] -->|RPC Call| n3[Kitex Server]
n3 --> n2 --> n1
首先来看 Hertz 的客户端代码:
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"time"
"github.com/cloudwego/hertz/pkg/app/client"
"github.com/cloudwego/hertz/pkg/common/hlog"
hertztracer "github.com/hertz-contrib/tracer/hertz"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
func main() {
closer := InitJaeger("hertz-client")
defer closer.Close()
c, _ := client.NewClient()
// Register and use client tracer middleware.
// This middleware is simple demo.
// You can refer to the example to implement a tracer middleware yourself to get the metrics you want.
c.Use(hertztracer.ClientTraceMW, hertztracer.ClientCtx)
for {
_, b, err := c.Get(context.Background(), nil, "http://localhost:8888/ping?name=hertz")
if err != nil {
hlog.Errorf(err.Error())
}
hlog.Infof(string(b))
time.Sleep(time.Second)
}
}
// InitJaeger ...
func InitJaeger(service string) io.Closer {
cfg, _ := jaegercfg.FromEnv()
cfg.ServiceName = service
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
opentracing.InitGlobalTracer(tracer)
return closer
}
这个例子里面追踪的是 ping 的结果,这里可以自己重新写逻辑追踪其他的信息。
然后来看 Hertz 服务端的代码:
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"github.com/cloudwego/hertz-examples/tracer/kitex/kitex_gen/api"
"github.com/cloudwego/hertz-examples/tracer/kitex/kitex_gen/api/echo"
"github.com/cloudwego/hertz/pkg/app"
"github.com/cloudwego/hertz/pkg/app/server"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/protocol/consts"
kclient "github.com/cloudwego/kitex/client"
"github.com/cloudwego/kitex/pkg/rpcinfo"
hertztracer "github.com/hertz-contrib/tracer/hertz"
kopentracing "github.com/kitex-contrib/tracer-opentracing"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
/*
export JAEGER_DISABLED=false
export JAEGER_SAMPLER_TYPE="const"
export JAEGER_SAMPLER_PARAM=1
export JAEGER_REPORTER_LOG_SPANS=true
export JAEGER_AGENT_HOST="127.0.0.1"
export JAEGER_AGENT_PORT=6831
*/
// InitTracer Initialize and create tracer
func InitTracer(serviceName string) (opentracing.Tracer, io.Closer) {
cfg, _ := jaegercfg.FromEnv()
cfg.ServiceName = serviceName
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
// opentracing.InitGlobalTracer(tracer)
return tracer, closer
}
func main() {
ht, hc := InitTracer("hertz-server")
kt, kc := InitTracer("kitex-client")
defer hc.Close()
defer kc.Close()
// kitex-client configure tracer
client, err := echo.NewClient("echo",
kclient.WithHostPorts("0.0.0.0:5555"),
kclient.WithSuite(kopentracing.NewClientSuite(kt, func(c context.Context) string {
endpoint := rpcinfo.GetRPCInfo(c).From()
return endpoint.ServiceName() + "::" + endpoint.Method()
})))
if err != nil {
panic(err)
}
// hertz-server configure tracer
h := server.Default(server.WithTracer(hertztracer.NewTracer(ht, func(c *app.RequestContext) string {
return "test.hertz.server" + "::" + c.FullPath()
})))
// Register and use tracer middleware.
// This middleware is simple demo.
// You can refer to the example to implement a tracer middleware yourself to get the metrics you want.
h.Use(hertztracer.ServerCtx())
h.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
type PingReq struct {
Name string `query:"name"`
}
var hertzReq PingReq
err := ctx.BindAndValidate(&hertzReq)
if err != nil {
hlog.Errorf(err.Error())
return
}
KitexReq := &api.Request{Message: hertzReq.Name}
resp, err := client.Echo(c, KitexReq)
if err != nil {
hlog.Errorf(err.Error())
}
ctx.JSON(consts.StatusOK, resp)
})
h.Spin()
}
这个代码和之前客户端的代码对比就能知道每个部分的代码在初始化的时候就要说明自己的角色, Hertz 的服务端同时也是 Kitex 的客户端。然后主函数里面 Kitex 的 tracer 和 Hertz 的 tracer 是分开配置的,后者的 tracer 还是一个中间件,例子里面追踪的是 Hertz 请求可能发生的错误以及 Kitex 响应可能发生的错误和响应本身。
到这里还不能跑起来,还有 Kitex 的服务端代码:
/*
* Copyright 2022 CloudWeGo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"context"
"fmt"
"io"
"log"
"net"
"github.com/cloudwego/hertz-examples/tracer/kitex/kitex_gen/api"
"github.com/cloudwego/hertz-examples/tracer/kitex/kitex_gen/api/echo"
"github.com/cloudwego/kitex/pkg/klog"
"github.com/cloudwego/kitex/server"
internal_opentracing "github.com/kitex-contrib/tracer-opentracing"
"github.com/opentracing/opentracing-go"
"github.com/uber/jaeger-client-go"
jaegercfg "github.com/uber/jaeger-client-go/config"
)
var _ api.Echo = &EchoImpl{}
// EchoImpl implements the last service interface defined in the IDL.
type EchoImpl struct{}
// Echo implements the Echo interface.
func (s *EchoImpl) Echo(ctx context.Context, req *api.Request) (resp *api.Response, err error) {
klog.Debug("echo called")
return &api.Response{Message: req.Message}, nil
}
/*
export JAEGER_DISABLED=false
export JAEGER_SAMPLER_TYPE="const"
export JAEGER_SAMPLER_PARAM=1
export JAEGER_REPORTER_LOG_SPANS=true
export JAEGER_AGENT_HOST="127.0.0.1"
export JAEGER_AGENT_PORT=6831
*/
// InitJaeger ...
func InitJaeger(service string) (server.Suite, io.Closer) {
cfg, _ := jaegercfg.FromEnv()
cfg.ServiceName = service
tracer, closer, err := cfg.NewTracer(jaegercfg.Logger(jaeger.StdLogger))
if err != nil {
panic(fmt.Sprintf("ERROR: cannot init Jaeger: %v\n", err))
}
opentracing.InitGlobalTracer(tracer)
return internal_opentracing.NewDefaultServerSuite(), closer
}
func main() {
tracerSuite, closer := InitJaeger("kitex-server")
defer closer.Close()
n, _ := net.ResolveTCPAddr("tcp", ":5555")
svr := echo.NewServer(new(EchoImpl), server.WithSuite(tracerSuite), server.WithServiceAddr(n))
if err := svr.Run(); err != nil {
log.Println("server stopped with error:", err)
} else {
log.Println("server stopped")
}
}
同样在初始化的时候说明自己的角色,然后和客户端一样, RPC 走 5555 端口。Kitex 还有一些生成的 API 代码,代码结构如下(与 echo 有关):
kitex_gen/api/
├── echo
│ ├── client.go
│ ├── echo.go
│ ├── invoker.go
│ └── server.go
├── echo.go
└── k-echo.go
这些全是生成的代码,所以不贴到这里凑字数了。
现在介绍这个项目到底怎么跑起来。