代码拉取完成,页面将自动刷新
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
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。