// 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) }
// 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 }
// 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 }
// 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() }
// 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...) }
// 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 }
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) }
// 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) }) } }
// 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() }
// 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 }
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...) }
// 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 }
// 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 }
// 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 }
// 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 }
// traceInfo returns a traceInfo and associates it with stream, if tracing is enabled. // If tracing is not enabled, it returns nil. func (s *Server) traceInfo(st transport.ServerTransport, stream *transport.Stream) (trInfo *traceInfo) { tr, ok := trace.FromContext(stream.Context()) if !ok { return nil } trInfo = &traceInfo{ tr: tr, } trInfo.firstLine.client = false trInfo.firstLine.remoteAddr = st.RemoteAddr() if dl, ok := stream.Context().Deadline(); ok { trInfo.firstLine.deadline = dl.Sub(time.Now()) } return trInfo }
// VerifyToken finds a customer by authentication token. func (s *authServer) VerifyToken(ctx context.Context, req *auth.Request) (*auth.Result, error) { md, _ := metadata.FromContext(ctx) traceID := strings.Join(md["traceID"], ",") if tr, ok := trace.FromContext(ctx); ok { tr.LazyPrintf("traceID %s", traceID) } customer := s.customers[req.AuthToken] if customer == nil { return &auth.Result{}, errors.New("Invalid Token") } reply := new(auth.Result) reply.Customer = customer return reply, nil }
// BoundedBox returns all hotels contained within a given rectangle. func (s *geoServer) BoundedBox(ctx context.Context, req *geo.Request) (*geo.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(geo.Result) for _, loc := range s.locations { if inRange(loc.Point, req) { res.HotelIds = append(res.HotelIds, loc.HotelID) } } return res, nil }
// GetRates gets rates for hotels for specific date range. func (s *rateServer) GetRates(ctx context.Context, req *rate.Request) (*rate.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(rate.Result) for _, hotelID := range req.HotelIds { k := stay{hotelID, req.InDate, req.OutDate} if s.rates[k] == nil { continue } res.RatePlans = append(res.RatePlans, s.rates[k]) } return res, nil }
// Unlink removes entry from specified parent directory. // // Required for fuse.FileSystem. func (k *KodingNetworkFS) Unlink(ctx context.Context, op *fuseops.UnlinkOp) error { dir, err := k.getDir(ctx, op.Parent) if err != nil { return err } entry, err := dir.RemoveEntry(op.Name) if err != nil { return err } if r, ok := trace.FromContext(ctx); ok { r.LazyPrintf("removed %s", entry.ToString()) } k.deleteEntry(entry.GetID()) return nil }
// GetRates gets rates for hotels for specific date range. func (s *Rate) GetRates(ctx context.Context, req *rate.Request, rsp *rate.Result) error { md, _ := metadata.FromContext(ctx) traceID := md["traceID"] if tr, ok := trace.FromContext(ctx); ok { tr.LazyPrintf("traceID %s", traceID) } for _, hotelID := range req.HotelIds { stay := stay{ HotelID: hotelID, InDate: req.InDate, OutDate: req.OutDate, } if s.rateTable[stay] != nil { rsp.RatePlans = append(rsp.RatePlans, s.rateTable[stay]) } } return nil }
func Tracef(ctx context.Context, s string, i ...interface{}) { if tr, ok := trace.FromContext(ctx); ok { tr.LazyPrintf(s, i...) } }
func logSleep(ctx context.Context, d time.Duration) { if tr, ok := trace.FromContext(ctx); ok { // HL tr.LazyPrintf("sleeping for %s", d) // HL } }