-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: lost trace content in occasion when shutting down trace (gogf#3418)
- Loading branch information
Showing
3 changed files
with
106 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// 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/contrib/trace/otlpgrpc/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-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, 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 | ||
} |