Skip to content

Commit 0883266

Browse files
authored
Support sampling config in network profiling (#171)
1 parent 602ebe5 commit 0883266

6 files changed

Lines changed: 147 additions & 4 deletions

File tree

CHANGES.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Release Notes.
3333
* Add compatibility documentation by @mrproliu in https://github.com/apache/skywalking-cli/pull/164
3434
* Add the sub-command `records list` for adapt the new record query API by @mrproliu in https://github.com/apache/skywalking-cli/pull/167
3535
* Add the attached events fields into the `trace` sub-command by @mrproliu in https://github.com/apache/skywalking-cli/pull/169
36+
* Add the sampling config file into the `profiling ebpf create network` sub-command by @mrproliu in https://github.com/apache/skywalking-cli/pull/171
3637

3738
0.10.0
3839
------------------

dist/LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ The text of each license is also included at licenses/license-[project].txt.
213213
k8s.io/utils v0.0.0-20210802155522-efc7438f0176 Apache-2.0
214214
sigs.k8s.io/controller-runtime v0.10.0 Apache-2.0
215215
sigs.k8s.io/structured-merge-diff/v4 v4.1.2 Apache-2.0
216-
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187 Apache-2.0
216+
skywalking.apache.org/repo/goapi v0.0.0-20221123034834-51b3101f6c9f Apache-2.0
217217

218218
========================================================================
219219
BSD-2-Clause licenses

examples/network-sampling.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# The command `dashboard global` supports displaying three kinds of data:
18+
# `global metrics`, `global response latency`, `Global heat map`.
19+
# If you don't want to display an item, you can just delete or comment its whole configuration below.
20+
# Generally, there is no need to modify properties unless there is a explanatory comment.
21+
22+
# Sampling config
23+
# samplings: the sampling config list
24+
# - uri_pattern: <optional> string. The match pattern for HTTP request. This is HTTP URI-oriented. matches all requests if not set
25+
# min_duration: <optional> int(ms). The minimal request duration to activate the network data(HTTP request/response raw data) sampling.
26+
# when_4xx: <required> bool. Collecting requests when the response code is 400-499.
27+
# when_5xx: <required> bool. Collecting requests when the response code is 500-599.
28+
# setting: define how to collect sampled data
29+
# require_request: <required> bool. Require to collect the complete request.
30+
# max_request_size: <optional> int. The max size of request context. The unit is byte.
31+
# require_response: <required> bool. Require to collect the complete response.
32+
# max_response_size: <optional> int. The max size of response context. The unit is byte.
33+
34+
samplings:
35+
# define collecting full request data when the response code is 400-499 and 500-599 .
36+
- when_4xx: true
37+
when_5xx: true
38+
setting:
39+
require_request: true
40+
require_response: false
41+
# define collecting first 1KB response data when the response code is 500-599 and the duration bigger than or equals 10ms.
42+
- uri_pattern: /index.html
43+
when_4xx: false
44+
when_5xx: true
45+
min_duration: 10
46+
setting:
47+
require_request: false
48+
require_response: true
49+
max_response_size: 1024

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ require (
2020
gopkg.in/yaml.v2 v2.4.0
2121
k8s.io/apimachinery v0.22.1
2222
sigs.k8s.io/controller-runtime v0.10.0
23-
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187
23+
skywalking.apache.org/repo/goapi v0.0.0-20221123034834-51b3101f6c9f
2424
)
2525

2626
require (

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -857,5 +857,5 @@ sigs.k8s.io/structured-merge-diff/v4 v4.1.2 h1:Hr/htKFmJEbtMgS/UD0N+gtgctAqz81t3
857857
sigs.k8s.io/structured-merge-diff/v4 v4.1.2/go.mod h1:j/nl6xW8vLS49O8YvXW1ocPhZawJtm+Yrr7PPRQ0Vg4=
858858
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
859859
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
860-
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187 h1:6JgAg9aohcHd72VplZUGycZgCNo6iQrz735nmtOTCnE=
861-
skywalking.apache.org/repo/goapi v0.0.0-20221019074310-53ebda305187/go.mod h1:lxmYWY1uAP5SLVKNymAyDzn7KG6dhPWN+pYHmyt+0vo=
860+
skywalking.apache.org/repo/goapi v0.0.0-20221123034834-51b3101f6c9f h1:iRQHKYsca0gbSxGWFjlkQ/WIuLyKReFUk2PscgzBqAw=
861+
skywalking.apache.org/repo/goapi v0.0.0-20221123034834-51b3101f6c9f/go.mod h1:lxmYWY1uAP5SLVKNymAyDzn7KG6dhPWN+pYHmyt+0vo=

internal/commands/profiling/ebpf/create/network.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,41 @@
1818
package create
1919

2020
import (
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+
3256
var 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

Comments
 (0)