mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
feat: Migrate the service call tracing to use otlphttp or otlpgrpc for reporting (#3001)
This commit is contained in:
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/registry/etcd/v2"
|
||||
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlpgrpc/v2"
|
||||
"github.com/gogf/gf/example/trace/grpc_with_db/protobuf/user"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
@ -11,15 +11,16 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "grpc-client-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-grpc-client"
|
||||
endpoint = "tracing-analysis-dc-bj.aliyuncs.com:8090"
|
||||
traceToken = "******_******"
|
||||
)
|
||||
|
||||
func main() {
|
||||
grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
|
||||
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlpgrpc.Init(serviceName, endpoint, traceToken)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
@ -28,6 +29,7 @@ func main() {
|
||||
StartRequests()
|
||||
}
|
||||
|
||||
// StartRequests is a demo for tracing.
|
||||
func StartRequests() {
|
||||
ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
|
||||
defer span.End()
|
||||
@ -57,20 +59,18 @@ func StartRequests() {
|
||||
g.Log().Info(ctx, "query result:", queryRes)
|
||||
|
||||
// Delete.
|
||||
_, err = client.Delete(ctx, &user.DeleteReq{
|
||||
if _, err = client.Delete(ctx, &user.DeleteReq{
|
||||
Id: insertRes.Id,
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
g.Log().Errorf(ctx, `%+v`, err)
|
||||
return
|
||||
}
|
||||
g.Log().Info(ctx, "delete id:", insertRes.Id)
|
||||
|
||||
// Delete with error.
|
||||
_, err = client.Delete(ctx, &user.DeleteReq{
|
||||
if _, err = client.Delete(ctx, &user.DeleteReq{
|
||||
Id: -1,
|
||||
})
|
||||
if err != nil {
|
||||
}); err != nil {
|
||||
g.Log().Errorf(ctx, `%+v`, err)
|
||||
return
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import (
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
_ "github.com/gogf/gf/contrib/nosql/redis/v2"
|
||||
"github.com/gogf/gf/contrib/registry/etcd/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlpgrpc/v2"
|
||||
"github.com/gogf/gf/example/trace/grpc_with_db/protobuf/user"
|
||||
|
||||
"context"
|
||||
@ -11,27 +12,28 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/contrib/rpc/grpcx/v2"
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
// Controller is the gRPC controller for user management.
|
||||
type Controller struct {
|
||||
user.UnimplementedUserServer
|
||||
}
|
||||
|
||||
const (
|
||||
ServiceName = "grpc-server-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-grpc-server"
|
||||
endpoint = "tracing-analysis-dc-bj.aliyuncs.com:8090"
|
||||
traceToken = "******_******"
|
||||
)
|
||||
|
||||
func main() {
|
||||
grpcx.Resolver.Register(etcd.New("127.0.0.1:2379"))
|
||||
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlpgrpc.Init(serviceName, endpoint, traceToken)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
@ -63,12 +65,11 @@ func (s *Controller) Insert(ctx context.Context, req *user.InsertReq) (res *user
|
||||
// Query is a route handler for querying user info. It firstly retrieves the info from redis,
|
||||
// if there's nothing in the redis, it then does db select.
|
||||
func (s *Controller) Query(ctx context.Context, req *user.QueryReq) (res *user.QueryRes, err error) {
|
||||
err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
|
||||
if err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
|
||||
Duration: 5 * time.Second,
|
||||
Name: s.userCacheKey(req.Id),
|
||||
Force: false,
|
||||
}).WherePri(req.Id).Scan(&res)
|
||||
if err != nil {
|
||||
}).WherePri(req.Id).Scan(&res); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
|
||||
@ -1,20 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "http-client"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-http-client"
|
||||
endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
|
||||
path = "adapt_******_******/api/otlp/traces"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlphttp.Init(serviceName, endpoint, path)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
@ -23,6 +24,7 @@ func main() {
|
||||
StartRequests()
|
||||
}
|
||||
|
||||
// StartRequests is a demo for tracing.
|
||||
func StartRequests() {
|
||||
ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
|
||||
defer span.End()
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
@ -9,13 +9,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "http-server"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-http-server"
|
||||
endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
|
||||
path = "adapt_******_******/api/otlp/traces"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlphttp.Init(serviceName, endpoint, path)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
@ -29,6 +30,7 @@ func main() {
|
||||
s.Run()
|
||||
}
|
||||
|
||||
// HelloHandler is a demo handler for tracing.
|
||||
func HelloHandler(r *ghttp.Request) {
|
||||
ctx, span := gtrace.NewSpan(r.Context(), "HelloHandler")
|
||||
defer span.End()
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
@ -10,13 +10,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "http-client-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-http-client-with-db"
|
||||
endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
|
||||
path = "adapt_******_******/api/otlp/traces"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlphttp.Init(serviceName, endpoint, path)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
|
||||
@ -6,7 +6,7 @@ import (
|
||||
"time"
|
||||
|
||||
_ "github.com/gogf/gf/contrib/drivers/mysql/v2"
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
@ -17,13 +17,14 @@ import (
|
||||
type cTrace struct{}
|
||||
|
||||
const (
|
||||
ServiceName = "http-server-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "otlp-http-server-with-db"
|
||||
endpoint = "tracing-analysis-dc-hz.aliyuncs.com"
|
||||
path = "adapt_******_******/api/otlp/traces"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlphttp.Init(serviceName, endpoint, path)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
@ -42,11 +43,14 @@ func main() {
|
||||
s.Run()
|
||||
}
|
||||
|
||||
// InsertReq is the input parameter for inserting user info.
|
||||
type InsertReq struct {
|
||||
Name string `v:"required#Please input user name."`
|
||||
}
|
||||
|
||||
// InsertRes is the output parameter for inserting user info.
|
||||
type InsertRes struct {
|
||||
Id int64
|
||||
ID int64
|
||||
}
|
||||
|
||||
// Insert is a route handler for inserting user info into database.
|
||||
@ -57,14 +61,17 @@ func (c *cTrace) Insert(ctx context.Context, req *InsertReq) (res *InsertRes, er
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
res = &InsertRes{
|
||||
Id: id,
|
||||
ID: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// QueryReq is the input parameter for querying user info.
|
||||
type QueryReq struct {
|
||||
Id int `v:"min:1#User id is required for querying"`
|
||||
ID int `v:"min:1#User id is required for querying"`
|
||||
}
|
||||
|
||||
// QueryRes is the output parameter for querying user info.
|
||||
type QueryRes struct {
|
||||
User gdb.Record
|
||||
}
|
||||
@ -74,9 +81,9 @@ type QueryRes struct {
|
||||
func (c *cTrace) Query(ctx context.Context, req *QueryReq) (res *QueryRes, err error) {
|
||||
one, err := g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
|
||||
Duration: 5 * time.Second,
|
||||
Name: c.userCacheKey(req.Id),
|
||||
Name: c.userCacheKey(req.ID),
|
||||
Force: false,
|
||||
}).WherePri(req.Id).One()
|
||||
}).WherePri(req.ID).One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -86,9 +93,12 @@ func (c *cTrace) Query(ctx context.Context, req *QueryReq) (res *QueryRes, err e
|
||||
return
|
||||
}
|
||||
|
||||
// DeleteReq is the input parameter for deleting user info.
|
||||
type DeleteReq struct {
|
||||
Id int `v:"min:1#User id is required for deleting."`
|
||||
}
|
||||
|
||||
// DeleteRes is the output parameter for deleting user info.
|
||||
type DeleteRes struct{}
|
||||
|
||||
// Delete is a route handler for deleting specified user info.
|
||||
|
||||
@ -3,7 +3,7 @@ package main
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/contrib/trace/otlphttp/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
@ -11,13 +11,14 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "inprocess"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
serviceName = "inprocess"
|
||||
endpoint = "localhost:6831"
|
||||
path = "adapt_******_******/api/otlp/traces"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
tp, err := otlphttp.Init(serviceName, endpoint, path)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user