github.com/stackdocker/rkt@v0.10.1-0.20151109095037-1aa827478248/Godeps/_workspace/src/google.golang.org/grpc/trace.go (about)

     1  /*
     2   *
     3   * Copyright 2015, Google Inc.
     4   * All rights reserved.
     5   *
     6   * Redistribution and use in source and binary forms, with or without
     7   * modification, are permitted provided that the following conditions are
     8   * met:
     9   *
    10   *     * Redistributions of source code must retain the above copyright
    11   * notice, this list of conditions and the following disclaimer.
    12   *     * Redistributions in binary form must reproduce the above
    13   * copyright notice, this list of conditions and the following disclaimer
    14   * in the documentation and/or other materials provided with the
    15   * distribution.
    16   *     * Neither the name of Google Inc. nor the names of its
    17   * contributors may be used to endorse or promote products derived from
    18   * this software without specific prior written permission.
    19   *
    20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    21   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    22   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    23   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
    24   * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
    25   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
    26   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
    27   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
    28   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    29   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
    30   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    31   *
    32   */
    33  
    34  package grpc
    35  
    36  import (
    37  	"bytes"
    38  	"fmt"
    39  	"io"
    40  	"net"
    41  	"strings"
    42  	"time"
    43  
    44  	"github.com/coreos/rkt/Godeps/_workspace/src/golang.org/x/net/trace"
    45  )
    46  
    47  // EnableTracing controls whether to trace RPCs using the golang.org/x/net/trace package.
    48  // This should only be set before any RPCs are sent or received by this program.
    49  var EnableTracing = true
    50  
    51  // methodFamily returns the trace family for the given method.
    52  // It turns "/pkg.Service/GetFoo" into "pkg.Service".
    53  func methodFamily(m string) string {
    54  	m = strings.TrimPrefix(m, "/") // remove leading slash
    55  	if i := strings.Index(m, "/"); i >= 0 {
    56  		m = m[:i] // remove everything from second slash
    57  	}
    58  	if i := strings.LastIndex(m, "."); i >= 0 {
    59  		m = m[i+1:] // cut down to last dotted component
    60  	}
    61  	return m
    62  }
    63  
    64  // traceInfo contains tracing information for an RPC.
    65  type traceInfo struct {
    66  	tr        trace.Trace
    67  	firstLine firstLine
    68  }
    69  
    70  // firstLine is the first line of an RPC trace.
    71  type firstLine struct {
    72  	client     bool // whether this is a client (outgoing) RPC
    73  	remoteAddr net.Addr
    74  	deadline   time.Duration // may be zero
    75  }
    76  
    77  func (f *firstLine) String() string {
    78  	var line bytes.Buffer
    79  	io.WriteString(&line, "RPC: ")
    80  	if f.client {
    81  		io.WriteString(&line, "to")
    82  	} else {
    83  		io.WriteString(&line, "from")
    84  	}
    85  	fmt.Fprintf(&line, " %v deadline:", f.remoteAddr)
    86  	if f.deadline != 0 {
    87  		fmt.Fprint(&line, f.deadline)
    88  	} else {
    89  		io.WriteString(&line, "none")
    90  	}
    91  	return line.String()
    92  }
    93  
    94  // payload represents an RPC request or response payload.
    95  type payload struct {
    96  	sent bool        // whether this is an outgoing payload
    97  	msg  interface{} // e.g. a proto.Message
    98  	// TODO(dsymonds): add stringifying info to codec, and limit how much we hold here?
    99  }
   100  
   101  func (p payload) String() string {
   102  	if p.sent {
   103  		return fmt.Sprintf("sent: %v", p.msg)
   104  	} else {
   105  		return fmt.Sprintf("recv: %v", p.msg)
   106  	}
   107  }
   108  
   109  type fmtStringer struct {
   110  	format string
   111  	a      []interface{}
   112  }
   113  
   114  func (f *fmtStringer) String() string {
   115  	return fmt.Sprintf(f.format, f.a...)
   116  }