go.uber.org/yarpc@v1.72.1/dispatcher_introspection.go (about)

     1  // Copyright (c) 2022 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package yarpc
    22  
    23  import (
    24  	"fmt"
    25  	"runtime"
    26  	"sort"
    27  
    28  	tchannel "github.com/uber/tchannel-go"
    29  	thriftrw "go.uber.org/thriftrw/version"
    30  	xintrospection "go.uber.org/yarpc/api/x/introspection"
    31  	"go.uber.org/yarpc/internal/introspection"
    32  	"google.golang.org/grpc"
    33  )
    34  
    35  // Introspect returns detailed information about the dispatcher. This function
    36  // acquires a lots of locks throughout and should only be called with some
    37  // reserve.
    38  func (d *Dispatcher) Introspect() introspection.DispatcherStatus {
    39  	var inbounds []xintrospection.InboundStatus
    40  	for _, i := range d.inbounds {
    41  		var status xintrospection.InboundStatus
    42  		if i, ok := i.(xintrospection.IntrospectableInbound); ok {
    43  			status = i.Introspect()
    44  		} else {
    45  			status = xintrospection.InboundStatus{
    46  				Transport: "Introspection not supported",
    47  			}
    48  		}
    49  		inbounds = append(inbounds, status)
    50  	}
    51  	var outbounds []xintrospection.OutboundStatus
    52  	for outboundKey, o := range d.outbounds {
    53  		if o.Unary != nil {
    54  			var status xintrospection.OutboundStatus
    55  			if o, ok := o.Unary.(xintrospection.IntrospectableOutbound); ok {
    56  				status = o.Introspect()
    57  			} else {
    58  				status.Transport = "Introspection not supported"
    59  			}
    60  			status.RPCType = "unary"
    61  			status.Service = o.ServiceName
    62  			status.OutboundKey = outboundKey
    63  			outbounds = append(outbounds, status)
    64  		}
    65  		if o.Oneway != nil {
    66  			var status xintrospection.OutboundStatus
    67  			if o, ok := o.Oneway.(xintrospection.IntrospectableOutbound); ok {
    68  				status = o.Introspect()
    69  			} else {
    70  				status.Transport = "Introspection not supported"
    71  			}
    72  			status.RPCType = "oneway"
    73  			status.Service = o.ServiceName
    74  			status.OutboundKey = outboundKey
    75  			outbounds = append(outbounds, status)
    76  		}
    77  	}
    78  
    79  	sort.Sort(outboundStatuses(outbounds)) // keep debug pages deterministic
    80  
    81  	procedures := introspection.IntrospectProcedures(d.table.Procedures())
    82  	return introspection.DispatcherStatus{
    83  		Name:            d.name,
    84  		ID:              fmt.Sprintf("%p", d),
    85  		Procedures:      procedures,
    86  		Inbounds:        inbounds,
    87  		Outbounds:       outbounds,
    88  		PackageVersions: PackageVersions,
    89  	}
    90  }
    91  
    92  // PackageVersions is a list of packages with corresponding versions.
    93  var PackageVersions = []introspection.PackageVersion{
    94  	{Name: "yarpc", Version: Version},
    95  	{Name: "tchannel", Version: tchannel.VersionInfo},
    96  	{Name: "thriftrw", Version: thriftrw.Version},
    97  	{Name: "grpc-go", Version: grpc.Version},
    98  	{Name: "go", Version: runtime.Version()},
    99  }
   100  
   101  type outboundStatuses []xintrospection.OutboundStatus
   102  
   103  func (o outboundStatuses) Len() int {
   104  	return len(o)
   105  }
   106  func (o outboundStatuses) Less(i, j int) bool {
   107  	if o[i].OutboundKey == o[j].OutboundKey {
   108  		return o[i].RPCType < o[j].RPCType
   109  	}
   110  
   111  	return o[i].OutboundKey < o[j].OutboundKey
   112  }
   113  func (o outboundStatuses) Swap(i, j int) {
   114  	o[i], o[j] = o[j], o[i]
   115  }