1818package create
1919
2020import (
21+ "fmt"
22+ "os"
23+
2124 "github.com/apache/skywalking-cli/internal/commands/interceptor"
2225 "github.com/apache/skywalking-cli/internal/flags"
2326 "github.com/apache/skywalking-cli/pkg/display"
2427 "github.com/apache/skywalking-cli/pkg/display/displayable"
2528 "github.com/apache/skywalking-cli/pkg/graphql/profiling"
2629
30+ "gopkg.in/yaml.v2"
31+
2732 "github.com/urfave/cli/v2"
2833
2934 api "skywalking.apache.org/repo/goapi/query"
3035)
3136
37+ type SamplingConfig struct {
38+ Samplings []* SamplingRule `yaml:"samplings"`
39+ }
40+
41+ type SamplingRule struct {
42+ URIPattern * string `yaml:"uri_pattern"`
43+ MinDuration * int `yaml:"min_duration"`
44+ When4xx * bool `yaml:"when_4xx"`
45+ When5xx * bool `yaml:"when_5xx"`
46+ Setting * SamplingSetting `yaml:"setting"`
47+ }
48+
49+ type SamplingSetting struct {
50+ RequireRequest * bool `yaml:"require_request"`
51+ MaxRequestSize * int `yaml:"max_request_size"`
52+ RequireResponse * bool `yaml:"require_response"`
53+ MaxResponseSize * int `yaml:"max_response_size"`
54+ }
55+
3256var NetworkCreateCommand = & cli.Command {
3357 Name : "network" ,
3458 Aliases : []string {"net" },
@@ -41,15 +65,43 @@ $ swctl profiling ebpf create network --service-instance-id=abc`,
4165 Flags : flags .Flags (
4266 flags .ServiceFlags ,
4367 flags .InstanceFlags ,
68+ []cli.Flag {
69+ & cli.StringFlag {
70+ Name : "sampling-config" ,
71+ Usage : "the `sampling-config` file define how to sampling the network data, if not then then ignore data sampling" ,
72+ Required : false ,
73+ },
74+ },
4475 ),
4576 Before : interceptor .BeforeChain (
4677 interceptor .ParseInstance (true ),
4778 ),
4879 Action : func (ctx * cli.Context ) error {
4980 instanceID := ctx .String ("instance-id" )
5081
82+ samplingConfigFile := ctx .String ("sampling-config" )
83+
84+ // convert the sampling rule
85+ var samplings = make ([]* api.EBPFNetworkSamplingRule , 0 )
86+ if samplingConfigFile != "" {
87+ config , err := os .ReadFile (samplingConfigFile )
88+ if err != nil {
89+ return err
90+ }
91+ r := & SamplingConfig {}
92+ if e := yaml .Unmarshal (config , r ); e != nil {
93+ return e
94+ }
95+
96+ samplings , err = parsingNetworkSampling (r )
97+ if err != nil {
98+ return err
99+ }
100+ }
101+
51102 request := & api.EBPFProfilingNetworkTaskRequest {
52103 InstanceID : instanceID ,
104+ Samplings : samplings ,
53105 }
54106
55107 task , err := profiling .CreateEBPFNetworkProfilingTask (ctx , request )
@@ -61,3 +113,44 @@ $ swctl profiling ebpf create network --service-instance-id=abc`,
61113 return display .Display (ctx , & displayable.Displayable {Data : task , Condition : request })
62114 },
63115}
116+
117+ func parsingNetworkSampling (config * SamplingConfig ) ([]* api.EBPFNetworkSamplingRule , error ) {
118+ rules := make ([]* api.EBPFNetworkSamplingRule , 0 )
119+ if config == nil || len (config .Samplings ) == 0 {
120+ return rules , nil
121+ }
122+ for _ , conf := range config .Samplings {
123+ rule := & api.EBPFNetworkSamplingRule {}
124+ rule .URIRegex = conf .URIPattern
125+ rule .MinDuration = conf .MinDuration
126+ if conf .When4xx == nil {
127+ return nil , fmt .Errorf ("the when_4xx is required" )
128+ }
129+ rule .When4xx = * conf .When4xx
130+ if conf .When5xx == nil {
131+ return nil , fmt .Errorf ("the when_5xx is required" )
132+ }
133+ rule .When5xx = * conf .When5xx
134+
135+ confSetting := conf .Setting
136+ if confSetting == nil {
137+ return nil , fmt .Errorf ("the sampling settings is required" )
138+ }
139+ setting := & api.EBPFNetworkDataCollectingSettings {}
140+ rule .Settings = setting
141+ if confSetting .RequireRequest == nil {
142+ return nil , fmt .Errorf ("the sampling request is required" )
143+ }
144+ setting .RequireCompleteRequest = * confSetting .RequireRequest
145+ setting .MaxRequestSize = confSetting .MaxRequestSize
146+ if confSetting .RequireResponse == nil {
147+ return nil , fmt .Errorf ("the sampling response is required" )
148+ }
149+ setting .RequireCompleteResponse = * confSetting .RequireResponse
150+ setting .MaxResponseSize = confSetting .MaxResponseSize
151+
152+ rules = append (rules , rule )
153+ }
154+
155+ return rules , nil
156+ }
0 commit comments