本文整理匯總了Golang中golang.org/x/net/trace.FromContext函數的典型用法代碼示例。如果您正苦於以下問題:Golang FromContext函數的具體用法?Golang FromContext怎麽用?Golang FromContext使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了FromContext函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: LazyLog
// LazyLog is a light wrapper around golang.org/x/net/trace/Trace.LazyLog which extracts the
// Trace instance from ctx and calls LazyLog on it. If no Trace instance is attached to the
// context then this method returns immediately.
func LazyLog(ctx context.Context, x fmt.Stringer, sensitive bool) {
tr, ok := trace.FromContext(ctx)
if !ok {
return
}
tr.LazyLog(x, sensitive)
}
示例2: Rename
// Rename changes a file or directory from old name and parent to new name and
// parent.
//
// Note if a new name already exists, we still go ahead and rename it. While
// the old and new entries are the same, we throw out the old one and create
// new entry for it.
//
// Required for fuse.FileSystem.
func (k *KodingNetworkFS) Rename(ctx context.Context, op *fuseops.RenameOp) error {
dir, err := k.getDir(ctx, op.OldParent)
if err != nil {
return err
}
oldEntry, err := dir.FindEntry(op.OldName)
if err != nil {
return err
}
newDir, err := k.getDir(ctx, op.NewParent)
if err != nil {
return err
}
newEntry, err := dir.MoveEntry(op.OldName, op.NewName, newDir)
if err != nil {
return err
}
// delete old entry from live nodes
k.deleteEntry(oldEntry.GetID())
// save new entry to live nodes
k.setEntry(newEntry.GetID(), newEntry)
if r, ok := trace.FromContext(ctx); ok {
r.LazyPrintf("moved from %s", oldEntry.ToString())
r.LazyPrintf("moved to %s", newEntry.ToString())
}
return nil
}
示例3: Nearby
// Nearby returns all hotels within a given distance.
func (s *Geo) Nearby(ctx context.Context, req *geo.Request, rsp *geo.Result) error {
md, _ := metadata.FromContext(ctx)
traceID := md["traceID"]
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("traceID %s", traceID)
}
// create center point for query
center := &geoindex.GeoPoint{
Pid: "",
Plat: float64(req.Lat),
Plon: float64(req.Lon),
}
// find points around center point
points := s.index.KNearest(center, maxSearchResults, geoindex.Km(maxSearchRadius), func(p geoindex.Point) bool {
return true
})
for _, p := range points {
rsp.HotelIds = append(rsp.HotelIds, p.Id())
}
return nil
}
示例4: SetError
// SetError is a light wrapper around golang.org/x/net/trace/Trace.SetError which extracts
// a Trace instance from ctx and calls SetError on it. If no Trace instance is attached to the
// context then this method returns immediately.
func SetError(ctx context.Context) {
tr, ok := trace.FromContext(ctx)
if !ok {
return
}
tr.SetError()
}
示例5: LazyPrintf
// LazyPrintf is a light wrapper around golang.org/x/net/trace/Trace.LazyPrintf which extracts
// a Trace instance from ctx and calls LazyPrintf on it. If no Trace instance is attached to the
// context then this method returns immediately.
func LazyPrintf(ctx context.Context, format string, a ...interface{}) {
tr, ok := trace.FromContext(ctx)
if !ok {
return
}
tr.LazyPrintf(format, a...)
}
示例6: Get
// Get implements Client.
func (c *client) Get(ctx context.Context, path string) (f *File, err error) {
tr, ok := trace.FromContext(ctx)
if ok {
tr.LazyPrintf("(%v, %#v) get '%v'", c.addr, c.label, path)
defer func() {
if err != nil {
tr.LazyPrintf("(%v, %#v) error: %v", c.addr, c.label, err)
return
}
tr.LazyPrintf("(%v, %#v) returned: %v", c.addr, c.label, f.Name)
}()
}
conn, err := net.Dial("tcp", c.addr)
if err != nil {
return nil, err
}
enc := json.NewEncoder(conn)
err = enc.Encode(Request{
Path: path,
Label: c.label,
})
if err != nil {
return nil, err
}
// Decode the Response
dec := json.NewDecoder(conn)
var resp Response
err = dec.Decode(&resp)
if err != nil {
return nil, err
}
if resp.Status != StatusOK {
return nil, fmt.Errorf("error from '%v' (%v): %v", c.addr, c.label, resp.Status)
}
r := readCloser{io.MultiReader(dec.Buffered(), conn), conn}
// Remove the extra \n character added when the JSON is encoded
b := make([]byte, 1)
_, err = r.Read(b)
if err != nil {
return nil, fmt.Errorf("error reading '\n' after Response: %v", err)
}
if b[0] != '\n' {
return nil, fmt.Errorf("expected to read '\n' after Response")
}
return &File{
ReadCloser: r,
Name: resp.Name,
ModTime: resp.ModTime,
Size: resp.Size,
}, nil
}
示例7: logRequest
func logRequest(ctx context.Context) {
md, _ := metadata.FromContext(ctx)
traceID := strings.Join(md["traceID"], ",")
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("traceID %s", traceID)
}
log.Printf("traceID %s", traceID)
}
示例8: tracingMiddleware
// tracingMiddleware - Tracing for middlewares.
func tracingMiddleware(md *middlewares.Middleware, handler middlewares.Handler) middlewares.Handler {
return func(next xhandler.HandlerC) xhandler.HandlerC {
h := handler(next)
return xhandler.HandlerFuncC(func(ctx context.Context, w http.ResponseWriter, r *http.Request) {
tr, _ := trace.FromContext(ctx)
tr.LazyPrintf("%s", md.Name)
h.ServeHTTPC(ctx, w, r)
})
}
}
示例9: SyncFile
// SyncFile sends file contents from local to remote.
//
// Required for fuse.FileSystem.
func (k *KodingNetworkFS) SyncFile(ctx context.Context, op *fuseops.SyncFileOp) error {
file, err := k.getFile(ctx, op.Inode)
if err != nil {
return err
}
if r, ok := trace.FromContext(ctx); ok {
r.LazyPrintf("syncing file=%s sized=%d", file.Name, len(file.GetContent()))
}
return file.Sync()
}
示例10: GetProfiles
// GetProfiles returns hotel profiles for requested IDs
func (s *Profile) GetProfiles(ctx context.Context, req *profile.Request, rsp *profile.Result) error {
md, _ := metadata.FromContext(ctx)
traceID := md["traceID"]
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("traceID %s", traceID)
}
for _, i := range req.HotelIds {
rsp.Hotels = append(rsp.Hotels, s.hotels[i])
}
return nil
}
示例11: Trace
func Trace(ctx context.Context, format string, args ...interface{}) {
if *debugMode {
fmt.Printf(format+"\n", args...)
return
}
tr, ok := trace.FromContext(ctx)
if !ok {
return
}
tr.LazyPrintf(format, args...)
}
示例12: getEntry
// getEntry gets an entry with specified id from list of live nodes.
func (k *KodingNetworkFS) getEntry(ctx context.Context, id fuseops.InodeID) (Node, error) {
k.RLock()
defer k.RUnlock()
entry, ok := k.liveNodes[id]
if !ok {
return nil, fuse.ENOENT
}
if r, ok := trace.FromContext(ctx); ok {
r.LazyPrintf(entry.ToString())
}
return entry, nil
}
示例13: GetProfiles
// VerifyToken finds a customer by authentication token.
func (s *profileServer) GetProfiles(ctx context.Context, req *profile.Request) (*profile.Result, error) {
md, _ := metadata.FromContext(ctx)
traceID := strings.Join(md["traceID"], ",")
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("traceID %s", traceID)
}
res := new(profile.Result)
for _, i := range req.HotelIds {
res.Hotels = append(res.Hotels, s.hotels[i])
}
return res, nil
}
示例14: Get
// Open implements Client.
func (tc traceClient) Get(ctx context.Context, path string) (*File, error) {
tr, ok := trace.FromContext(ctx)
if !ok {
return tc.Client.Get(ctx, path)
}
tr.LazyPrintf("%v: Get: %v", tc.name, path)
f, err := tc.Client.Get(ctx, path)
if err != nil {
tr.LazyPrintf("%v: error opening: %v", tc.name, path)
return nil, err
}
tr.LazyPrintf("%v: got file: %v", tc.name, f.Name)
return f, err
}
示例15: VerifyToken
// VerifyToken returns a customer from authentication token.
func (s *Auth) VerifyToken(ctx context.Context, req *auth.Request, rsp *auth.Result) error {
md, _ := metadata.FromContext(ctx)
traceID := md["traceID"]
if tr, ok := trace.FromContext(ctx); ok {
tr.LazyPrintf("traceID %s", traceID)
}
customer := s.customers[req.AuthToken]
if customer == nil {
return errors.New("Invalid Token")
}
rsp.Customer = customer
return nil
}