github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/profile/tcprtt/tracer/gadget.go (about)

     1  // Copyright 2023 The Inspektor Gadget authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tracer
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strings"
    21  
    22  	gadgetregistry "github.com/inspektor-gadget/inspektor-gadget/pkg/gadget-registry"
    23  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    24  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets/profile/tcprtt/types"
    25  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/parser"
    27  )
    28  
    29  const (
    30  	ParamMilliseconds          = "milliseconds"
    31  	ParamByLocalAddress        = "byladdr"
    32  	ParamByRemoteAddress       = "byraddr"
    33  	ParamFilterLocalPort       = "lport"
    34  	ParamFilterRemotePort      = "rport"
    35  	ParamFilterLocalAddress    = "laddr"
    36  	ParamFilterRemoteAddress   = "raddr"
    37  	ParamFilterLocalAddressV6  = "laddrv6"
    38  	ParamFilterRemoteAddressV6 = "raddrv6"
    39  )
    40  
    41  type GadgetDesc struct{}
    42  
    43  func (g *GadgetDesc) Name() string {
    44  	return "tcprtt"
    45  }
    46  
    47  func (g *GadgetDesc) Category() string {
    48  	return gadgets.CategoryProfile
    49  }
    50  
    51  func (g *GadgetDesc) Type() gadgets.GadgetType {
    52  	return gadgets.TypeProfile
    53  }
    54  
    55  func (g *GadgetDesc) Description() string {
    56  	return "Analyze TCP connections through an Round-Trip Time (RTT) distribution"
    57  }
    58  
    59  func (g *GadgetDesc) ParamDescs() params.ParamDescs {
    60  	return params.ParamDescs{
    61  		{
    62  			Key:          ParamMilliseconds,
    63  			Alias:        "m",
    64  			DefaultValue: "false",
    65  			Description:  "Show histogram in milliseconds instead of microseconds",
    66  			TypeHint:     params.TypeBool,
    67  		},
    68  		{
    69  			Key:          ParamByLocalAddress,
    70  			Alias:        "b",
    71  			DefaultValue: "false",
    72  			Description:  "Show histogram by local address",
    73  			TypeHint:     params.TypeBool,
    74  		},
    75  		{
    76  			Key:          ParamByRemoteAddress,
    77  			Alias:        "B",
    78  			DefaultValue: "false",
    79  			Description:  "Show histogram by remote address",
    80  			TypeHint:     params.TypeBool,
    81  		},
    82  		{
    83  			Key:          ParamFilterLocalPort,
    84  			Alias:        "",
    85  			DefaultValue: "0",
    86  			Description:  "Filter for local port",
    87  			TypeHint:     params.TypeUint16,
    88  		},
    89  		{
    90  			Key:          ParamFilterRemotePort,
    91  			Alias:        "",
    92  			DefaultValue: "0",
    93  			Description:  "Filter for remote port",
    94  			TypeHint:     params.TypeUint16,
    95  		},
    96  		{
    97  			Key:          ParamFilterLocalAddress,
    98  			Alias:        "", // It was "a" in BCC but ParamFilterRemoteAddress had a conflict
    99  			DefaultValue: "",
   100  			Description:  "Filter for local address",
   101  			TypeHint:     params.TypeIP,
   102  		},
   103  		{
   104  			Key:          ParamFilterRemoteAddress,
   105  			Alias:        "", // It was "A" in BCC but it collides with the alias of ParamAllNamespaces
   106  			DefaultValue: "",
   107  			Description:  "Filter for remote address",
   108  			TypeHint:     params.TypeIP,
   109  		},
   110  		{
   111  			Key:          ParamFilterLocalAddressV6,
   112  			Alias:        "",
   113  			DefaultValue: "",
   114  			Description:  "Filter for local address using IPv6",
   115  			TypeHint:     params.TypeIP,
   116  		},
   117  		{
   118  			Key:          ParamFilterRemoteAddressV6,
   119  			Alias:        "",
   120  			DefaultValue: "",
   121  			Description:  "Filter for remote address using IPv6",
   122  			TypeHint:     params.TypeIP,
   123  		},
   124  	}
   125  }
   126  
   127  func (g *GadgetDesc) Parser() parser.Parser {
   128  	return nil
   129  }
   130  
   131  func (g *GadgetDesc) EventPrototype() any {
   132  	return &types.Report{}
   133  }
   134  
   135  func (g *GadgetDesc) OutputFormats() (gadgets.OutputFormats, string) {
   136  	return gadgets.OutputFormats{
   137  		"report": gadgets.OutputFormat{
   138  			Name:        "Report",
   139  			Description: "A histogram showing the TCP RTT distribution",
   140  			Transform: func(data any) ([]byte, error) {
   141  				var report types.Report
   142  				b, ok := data.([]byte)
   143  				if !ok {
   144  					return nil, fmt.Errorf("type must be []byte and is: %T", data)
   145  				}
   146  				err := json.Unmarshal(b, &report)
   147  				if err != nil {
   148  					return nil, err
   149  				}
   150  				var sb strings.Builder
   151  				for _, h := range report.Histograms {
   152  					sb.WriteString(fmt.Sprintf("%s = %s", h.AddressType, h.Address))
   153  
   154  					if h.LocalPort > 0 {
   155  						sb.WriteString(fmt.Sprintf(" Local port = %d", h.LocalPort))
   156  					}
   157  
   158  					if h.RemotePort > 0 {
   159  						sb.WriteString(fmt.Sprintf(" Remote port = %d", h.RemotePort))
   160  					}
   161  
   162  					if h.Average > 0 {
   163  						sb.WriteString(fmt.Sprintf(" [AVG %f]", h.Average))
   164  					}
   165  					sb.WriteString(fmt.Sprintf("\n%s\n", h.Histogram.String()))
   166  				}
   167  				return []byte(sb.String()), nil
   168  			},
   169  		},
   170  	}, "report"
   171  }
   172  
   173  func init() {
   174  	gadgetregistry.Register(&GadgetDesc{})
   175  }