mirror of
https://gitee.com/johng/gf
synced 2026-06-07 02:12:11 +08:00
103 lines
2.1 KiB
Go
103 lines
2.1 KiB
Go
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
|
|
//
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
// If a copy of the MIT was not distributed with this file,
|
|
// You can obtain one at https://github.com/gogf/gf.
|
|
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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"
|
|
|
|
"github.com/gogf/gf/contrib/trace/otlpgrpc/v2"
|
|
)
|
|
|
|
const (
|
|
serviceName = "inprocess-grpc"
|
|
endpoint = "tracing-analysis-dc-bj.aliyuncs.com:8090"
|
|
traceToken = "******_******"
|
|
)
|
|
|
|
func main() {
|
|
var (
|
|
ctx = gctx.New()
|
|
shutdown, err = otlpgrpc.Init(serviceName, endpoint, traceToken)
|
|
)
|
|
|
|
if err != nil {
|
|
g.Log().Fatal(ctx, err)
|
|
}
|
|
defer 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
|
|
}
|