mirror of
https://gitee.com/johng/gf
synced 2026-07-07 14:25:17 +08:00
improve trace feature for package gdb/gredis; add trace example cases
This commit is contained in:
6
example/trace/grpc_with_db/client/config.yaml
Normal file
6
example/trace/grpc_with_db/client/config.yaml
Normal file
@ -0,0 +1,6 @@
|
||||
|
||||
registry:
|
||||
endpoints: ["127.0.0.1:2379"]
|
||||
|
||||
|
||||
|
||||
78
example/trace/grpc_with_db/client/main.go
Normal file
78
example/trace/grpc_with_db/client/main.go
Normal file
@ -0,0 +1,78 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/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"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "grpc-client-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
StartRequests()
|
||||
}
|
||||
|
||||
func StartRequests() {
|
||||
ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
|
||||
defer span.End()
|
||||
|
||||
var client, err = user.NewClient()
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, `%+v`, err)
|
||||
}
|
||||
|
||||
// Baggage.
|
||||
ctx = gtrace.SetBaggageValue(ctx, "uid", 100)
|
||||
|
||||
// Insert.
|
||||
insertRes, err := client.User().Insert(ctx, &user.InsertReq{
|
||||
Name: "john",
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Fatalf(ctx, `%+v`, err)
|
||||
}
|
||||
g.Log().Info(ctx, "insert id:", insertRes.Id)
|
||||
|
||||
// Query.
|
||||
queryRes, err := client.User().Query(ctx, &user.QueryReq{
|
||||
Id: insertRes.Id,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, `%+v`, err)
|
||||
return
|
||||
}
|
||||
g.Log().Info(ctx, "query result:", queryRes)
|
||||
|
||||
// Delete.
|
||||
_, err = client.User().Delete(ctx, &user.DeleteReq{
|
||||
Id: insertRes.Id,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, `%+v`, err)
|
||||
return
|
||||
}
|
||||
g.Log().Info(ctx, "delete id:", insertRes.Id)
|
||||
|
||||
// Delete with error.
|
||||
_, err = client.User().Delete(ctx, &user.DeleteReq{
|
||||
Id: -1,
|
||||
})
|
||||
if err != nil {
|
||||
g.Log().Errorf(ctx, `%+v`, err)
|
||||
return
|
||||
}
|
||||
g.Log().Info(ctx, "delete id:", -1)
|
||||
|
||||
}
|
||||
27
example/trace/grpc_with_db/protobuf/user/client.go
Normal file
27
example/trace/grpc_with_db/protobuf/user/client.go
Normal file
@ -0,0 +1,27 @@
|
||||
package user
|
||||
|
||||
import (
|
||||
"google.golang.org/grpc"
|
||||
|
||||
"github.com/gogf/katyusha/krpc"
|
||||
)
|
||||
|
||||
const (
|
||||
AppID = "demo"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
conn *grpc.ClientConn
|
||||
}
|
||||
|
||||
func NewClient(options ...grpc.DialOption) (*Client, error) {
|
||||
conn, err := krpc.Client.NewGrpcClientConn(AppID, options...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &Client{conn: conn}, nil
|
||||
}
|
||||
|
||||
func (c *Client) User() UserClient {
|
||||
return NewUserClient(c.conn)
|
||||
}
|
||||
1335
example/trace/grpc_with_db/protobuf/user/user.pb.go
Normal file
1335
example/trace/grpc_with_db/protobuf/user/user.pb.go
Normal file
File diff suppressed because it is too large
Load Diff
38
example/trace/grpc_with_db/protocol/user/user.proto
Normal file
38
example/trace/grpc_with_db/protocol/user/user.proto
Normal file
@ -0,0 +1,38 @@
|
||||
// protoc --gofast_out=plugins=grpc:. protocol/user/*.proto -I/Users/john/Workspace/Go/GOPATH/src -I/Users/john/Workspace/Go/GOPATH/src/gitee.com/johng/katyusha/.examples/tracing
|
||||
syntax = "proto3";
|
||||
|
||||
package user;
|
||||
|
||||
option go_package = "protobuf/user";
|
||||
|
||||
import "github.com/gogo/protobuf/gogoproto/gogo.proto";
|
||||
|
||||
// User service for tracing demo.
|
||||
service User {
|
||||
rpc Insert(InsertReq) returns (InsertRes) {}
|
||||
rpc Query(QueryReq) returns (QueryRes) {}
|
||||
rpc Delete(DeleteReq) returns (DeleteRes) {}
|
||||
}
|
||||
|
||||
message InsertReq {
|
||||
string Name = 1 [(gogoproto.moretags) = 'v:"required#Please input user name."'];
|
||||
}
|
||||
|
||||
message InsertRes {
|
||||
int32 Id = 1;
|
||||
}
|
||||
|
||||
message QueryReq {
|
||||
int32 Id = 1 [(gogoproto.moretags) = 'v:"min:1#User id is required for querying."'];
|
||||
}
|
||||
|
||||
message QueryRes {
|
||||
int32 Id = 1;
|
||||
string Name = 2;
|
||||
}
|
||||
|
||||
message DeleteReq {
|
||||
int32 Id = 1 [(gogoproto.moretags) = 'v:"min:1#User id is required for deleting."'];
|
||||
}
|
||||
|
||||
message DeleteRes {}
|
||||
33
example/trace/grpc_with_db/server/config.yaml
Normal file
33
example/trace/grpc_with_db/server/config.yaml
Normal file
@ -0,0 +1,33 @@
|
||||
|
||||
registry:
|
||||
endpoints: ["127.0.0.1:2379"]
|
||||
|
||||
grpcserver:
|
||||
name: "demo"
|
||||
logStdout: true
|
||||
errorLogEnabled: true
|
||||
accessLogEnabled: true
|
||||
errorStack: true
|
||||
|
||||
# MySQL.
|
||||
database:
|
||||
logger:
|
||||
level: "all"
|
||||
stdout: true
|
||||
default:
|
||||
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
debug: true
|
||||
|
||||
# Redis.
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 0
|
||||
cache:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
81
example/trace/grpc_with_db/server/main.go
Normal file
81
example/trace/grpc_with_db/server/main.go
Normal file
@ -0,0 +1,81 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/example/trace/grpc_with_db/protobuf/user"
|
||||
"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"
|
||||
"github.com/gogf/katyusha/krpc"
|
||||
)
|
||||
|
||||
type server struct{}
|
||||
|
||||
const (
|
||||
ServiceName = "grpc-server-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
// Set ORM cache adapter with redis.
|
||||
g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(g.Redis()))
|
||||
|
||||
s := krpc.Server.NewGrpcServer()
|
||||
user.RegisterUserServer(s.Server, &server{})
|
||||
s.Run()
|
||||
}
|
||||
|
||||
// Insert is a route handler for inserting user info into database.
|
||||
func (s *server) Insert(ctx context.Context, req *user.InsertReq) (res *user.InsertRes, err error) {
|
||||
result, err := g.Model("user").Ctx(ctx).Insert(g.Map{
|
||||
"name": req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
res = &user.InsertRes{
|
||||
Id: int32(id),
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// 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 *server) Query(ctx context.Context, req *user.QueryReq) (res *user.QueryRes, err error) {
|
||||
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 {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// Delete is a route handler for deleting specified user info.
|
||||
func (s *server) Delete(ctx context.Context, req *user.DeleteReq) (res *user.DeleteRes, err error) {
|
||||
err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: s.userCacheKey(req.Id),
|
||||
Force: false,
|
||||
}).WherePri(req.Id).Scan(&res)
|
||||
return
|
||||
}
|
||||
|
||||
func (s *server) userCacheKey(id int32) string {
|
||||
return fmt.Sprintf(`userInfo:%d`, id)
|
||||
}
|
||||
6
example/trace/grpc_with_db/sql.sql
Normal file
6
example/trace/grpc_with_db/sql.sql
Normal file
@ -0,0 +1,6 @@
|
||||
DROP TABLE IF EXISTS `user`;
|
||||
CREATE TABLE `user` (
|
||||
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(45) NOT NULL,
|
||||
PRIMARY KEY (`uid`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
34
example/trace/http/client/main.go
Normal file
34
example/trace/http/client/main.go
Normal file
@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/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"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
StartRequests()
|
||||
}
|
||||
|
||||
func StartRequests() {
|
||||
ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
|
||||
defer span.End()
|
||||
|
||||
ctx = gtrace.SetBaggageValue(ctx, "name", "john")
|
||||
|
||||
content := g.Client().GetContent(ctx, "http://127.0.0.1:8199/hello")
|
||||
g.Log().Print(ctx, content)
|
||||
}
|
||||
39
example/trace/http/server/main.go
Normal file
39
example/trace/http/server/main.go
Normal file
@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/ghttp"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "http-server"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
s := g.Server()
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.GET("/hello", HelloHandler)
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
func HelloHandler(r *ghttp.Request) {
|
||||
ctx, span := gtrace.NewSpan(r.Context(), "HelloHandler")
|
||||
defer span.End()
|
||||
|
||||
value := gtrace.GetBaggageVar(ctx, "name").String()
|
||||
|
||||
r.Response.Write("hello:", value)
|
||||
}
|
||||
77
example/trace/http_with_db/client/main.go
Normal file
77
example/trace/http_with_db/client/main.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"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/net/ghttp"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "http-client-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
StartRequests()
|
||||
}
|
||||
|
||||
func StartRequests() {
|
||||
ctx, span := gtrace.NewSpan(gctx.New(), "StartRequests")
|
||||
defer span.End()
|
||||
|
||||
var (
|
||||
err error
|
||||
client = g.Client()
|
||||
)
|
||||
// Add user info.
|
||||
var insertRes = struct {
|
||||
ghttp.DefaultHandlerResponse
|
||||
Data struct{ Id int64 } `json:"data"`
|
||||
}{}
|
||||
err = client.PostVar(ctx, "http://127.0.0.1:8199/user/insert", g.Map{
|
||||
"name": "john",
|
||||
}).Scan(&insertRes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Log().Info(ctx, "insert result:", insertRes)
|
||||
if insertRes.Data.Id == 0 {
|
||||
g.Log().Error(ctx, "retrieve empty id string")
|
||||
return
|
||||
}
|
||||
|
||||
// Query user info.
|
||||
var queryRes = struct {
|
||||
ghttp.DefaultHandlerResponse
|
||||
Data struct{ User gdb.Record } `json:"data"`
|
||||
}{}
|
||||
err = client.GetVar(ctx, "http://127.0.0.1:8199/user/query", g.Map{
|
||||
"id": insertRes.Data.Id,
|
||||
}).Scan(&queryRes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Log().Info(ctx, "query result:", queryRes)
|
||||
|
||||
// Delete user info.
|
||||
var deleteRes = struct {
|
||||
ghttp.DefaultHandlerResponse
|
||||
}{}
|
||||
err = client.PostVar(ctx, "http://127.0.0.1:8199/user/delete", g.Map{
|
||||
"id": insertRes.Data.Id,
|
||||
}).Scan(&deleteRes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
g.Log().Info(ctx, "delete result:", deleteRes)
|
||||
}
|
||||
23
example/trace/http_with_db/server/config.yaml
Normal file
23
example/trace/http_with_db/server/config.yaml
Normal file
@ -0,0 +1,23 @@
|
||||
|
||||
# MySQL.
|
||||
database:
|
||||
logger:
|
||||
level: "all"
|
||||
stdout: true
|
||||
default:
|
||||
link: "mysql:root:12345678@tcp(127.0.0.1:3306)/test"
|
||||
debug: true
|
||||
|
||||
# Redis.
|
||||
redis:
|
||||
default:
|
||||
address: 127.0.0.1:6379
|
||||
db: 0
|
||||
cache:
|
||||
address: 127.0.0.1:6379
|
||||
db: 1
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
108
example/trace/http_with_db/server/main.go
Normal file
108
example/trace/http_with_db/server/main.go
Normal file
@ -0,0 +1,108 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"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/net/ghttp"
|
||||
"github.com/gogf/gf/v2/os/gcache"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
)
|
||||
|
||||
type cTrace struct{}
|
||||
|
||||
const (
|
||||
ServiceName = "http-server-with-db"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
// Set ORM cache adapter with redis.
|
||||
g.DB().GetCache().SetAdapter(gcache.NewAdapterRedis(g.Redis()))
|
||||
|
||||
// Start HTTP server.
|
||||
s := g.Server()
|
||||
s.Use(ghttp.MiddlewareHandlerResponse)
|
||||
s.Group("/", func(group *ghttp.RouterGroup) {
|
||||
group.ALL("/user", new(cTrace))
|
||||
})
|
||||
s.SetPort(8199)
|
||||
s.Run()
|
||||
}
|
||||
|
||||
type InsertReq struct {
|
||||
Name string `v:"required#Please input user name."`
|
||||
}
|
||||
type InsertRes struct {
|
||||
Id int64
|
||||
}
|
||||
|
||||
// Insert is a route handler for inserting user info into database.
|
||||
func (c *cTrace) Insert(ctx context.Context, req *InsertReq) (res *InsertRes, err error) {
|
||||
result, err := g.Model("user").Ctx(ctx).Insert(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
id, _ := result.LastInsertId()
|
||||
res = &InsertRes{
|
||||
Id: id,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type QueryReq struct {
|
||||
Id int `v:"min:1#User id is required for querying"`
|
||||
}
|
||||
type QueryRes struct {
|
||||
User gdb.Record
|
||||
}
|
||||
|
||||
// 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 (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),
|
||||
Force: false,
|
||||
}).WherePri(req.Id).One()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
res = &QueryRes{
|
||||
User: one,
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
type DeleteReq struct {
|
||||
Id int `v:"min:1#User id is required for deleting."`
|
||||
}
|
||||
type DeleteRes struct{}
|
||||
|
||||
// Delete is a route handler for deleting specified user info.
|
||||
func (c *cTrace) Delete(ctx context.Context, req *DeleteReq) (res *DeleteRes, err error) {
|
||||
_, err = g.Model("user").Ctx(ctx).Cache(gdb.CacheOption{
|
||||
Duration: -1,
|
||||
Name: c.userCacheKey(req.Id),
|
||||
Force: false,
|
||||
}).WherePri(req.Id).Delete()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (c *cTrace) userCacheKey(id int) string {
|
||||
return fmt.Sprintf(`userInfo:%d`, id)
|
||||
}
|
||||
6
example/trace/http_with_db/sql.sql
Normal file
6
example/trace/http_with_db/sql.sql
Normal file
@ -0,0 +1,6 @@
|
||||
DROP TABLE IF EXISTS `user`;
|
||||
CREATE TABLE `user` (
|
||||
`uid` int(10) unsigned NOT NULL AUTO_INCREMENT,
|
||||
`name` varchar(45) NOT NULL,
|
||||
PRIMARY KEY (`uid`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
||||
91
example/trace/inprocess/main.go
Normal file
91
example/trace/inprocess/main.go
Normal file
@ -0,0 +1,91 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/gogf/gf/contrib/trace/jaeger/v2"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/net/gtrace"
|
||||
"github.com/gogf/gf/v2/os/gctx"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
)
|
||||
|
||||
const (
|
||||
ServiceName = "inprocess"
|
||||
JaegerUdpEndpoint = "localhost:6831"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var ctx = gctx.New()
|
||||
tp, err := jaeger.Init(ServiceName, JaegerUdpEndpoint)
|
||||
if err != nil {
|
||||
g.Log().Fatal(ctx, err)
|
||||
}
|
||||
defer tp.Shutdown(ctx)
|
||||
|
||||
ctx, span := gtrace.NewSpan(ctx, "main")
|
||||
defer span.End()
|
||||
|
||||
// Trace 1.
|
||||
user1 := GetUser(ctx, 1)
|
||||
g.Dump(user1)
|
||||
|
||||
// Trace 2.
|
||||
user100 := GetUser(ctx, 100)
|
||||
g.Dump(user100)
|
||||
}
|
||||
|
||||
// GetUser retrieves and returns hard coded user data for demonstration.
|
||||
func GetUser(ctx context.Context, id int) g.Map {
|
||||
ctx, span := gtrace.NewSpan(ctx, "GetUser")
|
||||
defer span.End()
|
||||
m := g.Map{}
|
||||
gutil.MapMerge(
|
||||
m,
|
||||
GetInfo(ctx, id),
|
||||
GetDetail(ctx, id),
|
||||
GetScores(ctx, id),
|
||||
)
|
||||
return m
|
||||
}
|
||||
|
||||
// GetInfo retrieves and returns hard coded user info for demonstration.
|
||||
func GetInfo(ctx context.Context, id int) g.Map {
|
||||
ctx, span := gtrace.NewSpan(ctx, "GetInfo")
|
||||
defer span.End()
|
||||
if id == 100 {
|
||||
return g.Map{
|
||||
"id": 100,
|
||||
"name": "john",
|
||||
"gender": 1,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetDetail retrieves and returns hard coded user detail for demonstration.
|
||||
func GetDetail(ctx context.Context, id int) g.Map {
|
||||
ctx, span := gtrace.NewSpan(ctx, "GetDetail")
|
||||
defer span.End()
|
||||
if id == 100 {
|
||||
return g.Map{
|
||||
"site": "https://goframe.org",
|
||||
"email": "john@goframe.org",
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetScores retrieves and returns hard coded user scores for demonstration.
|
||||
func GetScores(ctx context.Context, id int) g.Map {
|
||||
ctx, span := gtrace.NewSpan(ctx, "GetScores")
|
||||
defer span.End()
|
||||
if id == 100 {
|
||||
return g.Map{
|
||||
"math": 100,
|
||||
"english": 60,
|
||||
"chinese": 50,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user