当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
util.go 3.78 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2016-12-08 20:19 . name option not needed
package awsxray
import (
"crypto/rand"
"fmt"
"strings"
"time"
"github.com/micro/go-awsxray"
"github.com/micro/go-micro/errors"
"github.com/micro/go-micro/metadata"
"golang.org/x/net/context"
)
// getHTTP returns a http struct
func getHTTP(url, method string, err error) *awsxray.HTTP {
return &awsxray.HTTP{
Request: &awsxray.Request{
Method: method,
URL: url,
},
Response: &awsxray.Response{
Status: getStatus(err),
},
}
}
// getRandom generates a random byte slice
func getRandom(i int) []byte {
b := make([]byte, i)
for {
// keep trying till we get it
if _, err := rand.Read(b); err != nil {
continue
}
return b
}
}
// getSegment creates a new segment based on whether we're part of an existing flow
func getSegment(name string, ctx context.Context) *awsxray.Segment {
md, _ := metadata.FromContext(ctx)
parentId := getParentId(md)
traceId := getTraceId(md)
// try get existing segment for parent Id
if s, ok := awsxray.FromContext(ctx); ok {
// only set existing segment as parent if its not a subsegment itself
if len(parentId) == 0 && len(s.Type) == 0 {
parentId = s.Id
}
if len(traceId) == 0 {
traceId = s.TraceId
}
}
// create segment
s := &awsxray.Segment{
Id: fmt.Sprintf("%x", getRandom(8)),
Name: name,
TraceId: traceId,
StartTime: float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9,
}
// we have a parent so subsegment
if len(parentId) > 0 {
s.ParentId = parentId
s.Type = "subsegment"
}
return s
}
// getStatus returns a status code from the error
func getStatus(err error) int {
// no error
if err == nil {
return 200
}
// try get errors.Error
if e, ok := err.(*errors.Error); ok {
return int(e.Code)
}
// try parse marshalled error
if e := errors.Parse(err.Error()); e.Code > 0 {
return int(e.Code)
}
// could not parse, 500
return 500
}
// getTraceId returns trace header or generates a new one
func getTraceId(md metadata.Metadata) string {
// try as is
if h, ok := md[awsxray.TraceHeader]; ok {
return awsxray.GetTraceId(h)
}
// try lower case
if h, ok := md[strings.ToLower(awsxray.TraceHeader)]; ok {
return awsxray.GetTraceId(h)
}
// generate new one, probably a bad idea...
return fmt.Sprintf("%d-%x-%x", 1, time.Now().Unix(), getRandom(12))
}
// getParentId returns parent header or blank
func getParentId(md metadata.Metadata) string {
// try as is
if h, ok := md[awsxray.TraceHeader]; ok {
return awsxray.GetParentId(h)
}
// try lower case
if h, ok := md[strings.ToLower(awsxray.TraceHeader)]; ok {
return awsxray.GetParentId(h)
}
return ""
}
func newXRay(opts Options) *awsxray.AWSXRay {
return awsxray.New(
awsxray.WithClient(opts.Client),
awsxray.WithDaemon(opts.Daemon),
)
}
func record(x *awsxray.AWSXRay, s *awsxray.Segment) error {
// set end time
s.EndTime = float64(time.Now().Truncate(time.Millisecond).UnixNano()) / 1e9
return x.Record(s)
}
// setCallStatus sets the http section and related status
func setCallStatus(s *awsxray.Segment, url, method string, err error) {
s.HTTP = getHTTP(url, method, err)
status := getStatus(err)
switch {
case status >= 500:
s.Fault = true
case status >= 400:
s.Error = true
case err != nil:
s.Fault = true
}
}
func newContext(ctx context.Context, s *awsxray.Segment) context.Context {
md, _ := metadata.FromContext(ctx)
// make copy to avoid races
newMd := metadata.Metadata{}
for k, v := range md {
newMd[k] = v
}
// set trace id in header
newMd[awsxray.TraceHeader] = awsxray.SetTraceId(newMd[awsxray.TraceHeader], s.TraceId)
// set parent id in header
newMd[awsxray.TraceHeader] = awsxray.SetParentId(newMd[awsxray.TraceHeader], s.ParentId)
// store segment in context
ctx = awsxray.NewContext(ctx, s)
// store metadata in context
ctx = metadata.NewContext(ctx, newMd)
return ctx
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.2.1

搜索帮助

0d507c66 1850385 C8b1a773 1850385