Skip to content

Commit 02eaa3f

Browse files
committed
Implement profiling service
- Generate gRPC/Protobuf code with buf - Unify import aliases - Add pprof subcommand to collect profiles via socket
1 parent 13d1f4b commit 02eaa3f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1522
-333
lines changed

.golangci.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ linters-settings:
3838
- return
3939
govet:
4040
check-shadowing: true
41+
importas:
42+
no-unaliased: true
43+
alias:
44+
- pkg: gitlab.com/inetmock/inetmock/pkg/(audit|rpc)/(v[\w\d]+)
45+
alias: $1$2
46+
- pkg: github.com/miekg/dns
47+
alias: mdns
4148
lll:
4249
line-length: 140
4350
maligned:
@@ -73,6 +80,7 @@ linters:
7380
- gosec
7481
- gosimple
7582
- govet
83+
- importas
7684
- ineffassign
7785
- lll
7886
- misspell
@@ -105,6 +113,8 @@ issues:
105113
- funlen
106114

107115
run:
116+
build-tags:
117+
- sudo
108118
skip-dirs:
109119
- internal/mock
110120
skip-files:

Taskfile.yml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ vars:
44
OUT_DIR: ./out
55
INETMOCK_PKG: gitlab.com/inetmock/inetmock/cmd/inetmock
66
IMCTL_PKG: gitlab.com/inetmock/inetmock/cmd/imctl
7-
PROTO_FILES:
8-
sh: find ./api/ -type f -name "*.proto" -printf "%p "
97
DOCKER_ENDPOINT:
108
sh: echo "${DOCKER_ENDPOINT:-localhost}"
119

@@ -24,7 +22,7 @@ tasks:
2422
format:
2523
desc: format all source files
2624
cmds:
27-
- gofumpt -l -s -w ./
25+
- gofumpt -l -w ./
2826
- goimports -local gitlab.com/inetmock/inetmock -w ./
2927

3028
deps:
@@ -55,9 +53,8 @@ tasks:
5553

5654
protobuf-lint:
5755
desc: run protobuf linter
58-
dir: api/
5956
sources:
60-
- "proto/**/*.proto"
57+
- "api/proto/**/*.proto"
6158
cmds:
6259
- buf lint
6360

@@ -76,7 +73,7 @@ tasks:
7673
generates:
7774
- "**/*.pb.go"
7875
cmds:
79-
- buf protoc --proto_path ./api/proto/ --go_out=./pkg/ --go_opt=paths=source_relative --go-grpc_out=./pkg/ --go-grpc_opt=paths=source_relative {{ .PROTO_FILES }}
76+
- buf generate
8077

8178
go-generate:
8279
desc: run all go:generate directives

buf.gen.yaml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
version: v1
2+
managed:
3+
enabled: true
4+
java_multiple_files: true
5+
optimize_for: CODE_SIZE
6+
go_package_prefix:
7+
default: gitlab.com/inetmock/inetmock/pkg
8+
except:
9+
- buf.build/googleapis/googleapis
10+
plugins:
11+
- name: go
12+
out: ./pkg/
13+
opt: paths=source_relative
14+
- name: go-grpc
15+
out: ./pkg/
16+
opt: paths=source_relative

buf.work.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
version: v1
2+
directories:
3+
- api/proto

cmd/imctl/audit.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515

1616
"gitlab.com/inetmock/inetmock/internal/format"
1717
"gitlab.com/inetmock/inetmock/pkg/audit"
18-
rpcV1 "gitlab.com/inetmock/inetmock/pkg/rpc/v1"
18+
rpcv1 "gitlab.com/inetmock/inetmock/pkg/rpc/v1"
1919
)
2020

2121
var (
@@ -89,16 +89,15 @@ func init() {
8989
}
9090

9191
func watchAuditEvents(_ *cobra.Command, _ []string) (err error) {
92-
auditClient := rpcV1.NewAuditServiceClient(conn)
92+
auditClient := rpcv1.NewAuditServiceClient(conn)
9393

94-
var watchClient rpcV1.AuditService_WatchEventsClient
95-
if watchClient, err = auditClient.WatchEvents(cliApp.Context(), &rpcV1.WatchEventsRequest{WatcherName: listenerName}); err != nil {
96-
fmt.Println(err.Error())
97-
os.Exit(1)
94+
var watchClient rpcv1.AuditService_WatchEventsClient
95+
if watchClient, err = auditClient.WatchEvents(cliApp.Context(), &rpcv1.WatchEventsRequest{WatcherName: listenerName}); err != nil {
96+
return err
9897
}
9998

10099
go func() {
101-
var resp *rpcV1.WatchEventsResponse
100+
var resp *rpcv1.WatchEventsResponse
102101
for resp, err = watchClient.Recv(); err == nil; resp, err = watchClient.Recv() {
103102
ev := audit.NewEventFromProto(resp.Entity)
104103
var out []byte
@@ -117,12 +116,12 @@ func watchAuditEvents(_ *cobra.Command, _ []string) (err error) {
117116
}
118117

119118
func runListSinks(*cobra.Command, []string) (err error) {
120-
auditClient := rpcV1.NewAuditServiceClient(conn)
119+
auditClient := rpcv1.NewAuditServiceClient(conn)
121120
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
122121
defer cancel()
123122

124-
var resp *rpcV1.ListSinksResponse
125-
if resp, err = auditClient.ListSinks(ctx, new(rpcV1.ListSinksRequest)); err != nil {
123+
var resp *rpcv1.ListSinksResponse
124+
if resp, err = auditClient.ListSinks(ctx, new(rpcv1.ListSinksRequest)); err != nil {
126125
return
127126
}
128127

@@ -141,12 +140,12 @@ func runListSinks(*cobra.Command, []string) (err error) {
141140
}
142141

143142
func runAddFile(_ *cobra.Command, args []string) (err error) {
144-
auditClient := rpcV1.NewAuditServiceClient(conn)
143+
auditClient := rpcv1.NewAuditServiceClient(conn)
145144
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
146145
defer cancel()
147146

148-
var resp *rpcV1.RegisterFileSinkResponse
149-
resp, err = auditClient.RegisterFileSink(ctx, &rpcV1.RegisterFileSinkRequest{TargetPath: args[0]})
147+
var resp *rpcv1.RegisterFileSinkResponse
148+
resp, err = auditClient.RegisterFileSink(ctx, &rpcv1.RegisterFileSinkRequest{TargetPath: args[0]})
150149

151150
if err != nil {
152151
return
@@ -158,11 +157,11 @@ func runAddFile(_ *cobra.Command, args []string) (err error) {
158157
}
159158

160159
func runRemoveFile(_ *cobra.Command, args []string) (err error) {
161-
auditClient := rpcV1.NewAuditServiceClient(conn)
160+
auditClient := rpcv1.NewAuditServiceClient(conn)
162161
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
163162
defer cancel()
164163

165-
_, err = auditClient.RemoveFileSink(ctx, &rpcV1.RemoveFileSinkRequest{TargetPath: args[0]})
164+
_, err = auditClient.RemoveFileSink(ctx, &rpcv1.RemoveFileSinkRequest{TargetPath: args[0]})
166165
return
167166
}
168167

cmd/imctl/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func main() {
3535
Short: "IMCTL is the CLI app to interact with an INetMock server",
3636
LogEncoding: "console",
3737
Config: &cfg,
38-
SubCommands: []*cobra.Command{healthCmd, auditCmd, pcapCmd, checkCmd},
38+
SubCommands: []*cobra.Command{healthCmd, auditCmd, pcapCmd, checkCmd, pprofCmd},
3939
LateInitTasks: []func(cmd *cobra.Command, args []string) (err error){
4040
initGRPCConnection,
4141
},

cmd/imctl/pcap.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
"go.uber.org/zap"
1414

1515
"gitlab.com/inetmock/inetmock/internal/format"
16-
rpcV1 "gitlab.com/inetmock/inetmock/pkg/rpc/v1"
16+
rpcv1 "gitlab.com/inetmock/inetmock/pkg/rpc/v1"
1717
)
1818

1919
const (
@@ -57,11 +57,11 @@ var (
5757
}
5858

5959
var err error
60-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
60+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
6161
ctx, cancel := context.WithTimeout(context.Background(), cfg.GRPCTimeout)
6262
defer cancel()
63-
var resp *rpcV1.ListAvailableDevicesResponse
64-
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcV1.ListAvailableDevicesRequest)); err == nil {
63+
var resp *rpcv1.ListAvailableDevicesResponse
64+
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcv1.ListAvailableDevicesRequest)); err == nil {
6565
var completions []string
6666

6767
for _, d := range resp.AvailableDevices {
@@ -93,11 +93,11 @@ var (
9393
Args: cobra.ExactArgs(1),
9494
ValidArgsFunction: func(*cobra.Command, []string, string) ([]string, cobra.ShellCompDirective) {
9595
var err error
96-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
96+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
9797
ctx, cancel := context.WithTimeout(context.Background(), cfg.GRPCTimeout)
9898
defer cancel()
99-
var resp *rpcV1.ListActiveRecordingsResponse
100-
if resp, err = pcapClient.ListActiveRecordings(ctx, new(rpcV1.ListActiveRecordingsRequest)); err == nil {
99+
var resp *rpcv1.ListActiveRecordingsResponse
100+
if resp, err = pcapClient.ListActiveRecordings(ctx, new(rpcv1.ListActiveRecordingsRequest)); err == nil {
101101
return resp.Subscriptions, cobra.ShellCompDirectiveNoFileComp
102102
}
103103
return nil, cobra.ShellCompDirectiveError
@@ -130,12 +130,12 @@ func init() {
130130
}
131131

132132
func runListAvailableDevices(*cobra.Command, []string) (err error) {
133-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
133+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
134134
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
135135
defer cancel()
136136

137-
var resp *rpcV1.ListAvailableDevicesResponse
138-
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcV1.ListAvailableDevicesRequest)); err != nil {
137+
var resp *rpcv1.ListAvailableDevicesResponse
138+
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcv1.ListAvailableDevicesRequest)); err != nil {
139139
return
140140
}
141141

@@ -165,14 +165,14 @@ func runListActiveRecordings(*cobra.Command, []string) error {
165165
ConsumerKey string
166166
}
167167

168-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
168+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
169169

170170
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
171171
defer cancel()
172172

173173
var err error
174-
var resp *rpcV1.ListActiveRecordingsResponse
175-
if resp, err = pcapClient.ListActiveRecordings(ctx, new(rpcV1.ListActiveRecordingsRequest)); err != nil {
174+
var resp *rpcv1.ListActiveRecordingsResponse
175+
if resp, err = pcapClient.ListActiveRecordings(ctx, new(rpcv1.ListActiveRecordingsRequest)); err != nil {
176176
return err
177177
}
178178

@@ -197,7 +197,7 @@ func runListActiveRecordings(*cobra.Command, []string) error {
197197
}
198198

199199
func runAddRecording(_ *cobra.Command, args []string) (err error) {
200-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
200+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
201201

202202
if err = isValidRecordDevice(args[0], pcapClient); err != nil {
203203
return
@@ -206,8 +206,8 @@ func runAddRecording(_ *cobra.Command, args []string) (err error) {
206206
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
207207
defer cancel()
208208

209-
var resp *rpcV1.StartPCAPFileRecordingResponse
210-
resp, err = pcapClient.StartPCAPFileRecording(ctx, &rpcV1.StartPCAPFileRecordingRequest{
209+
var resp *rpcv1.StartPCAPFileRecordingResponse
210+
resp, err = pcapClient.StartPCAPFileRecording(ctx, &rpcv1.StartPCAPFileRecordingRequest{
211211
Device: args[0],
212212
TargetPath: args[1],
213213
})
@@ -222,14 +222,14 @@ func runAddRecording(_ *cobra.Command, args []string) (err error) {
222222
}
223223

224224
func runRemoveCurrentlyRunningRecording(_ *cobra.Command, args []string) error {
225-
pcapClient := rpcV1.NewPCAPServiceClient(conn)
225+
pcapClient := rpcv1.NewPCAPServiceClient(conn)
226226

227227
listRecsCtx, listRecsCancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
228228
defer listRecsCancel()
229229

230230
var err error
231-
var listRecsResp *rpcV1.ListActiveRecordingsResponse
232-
if listRecsResp, err = pcapClient.ListActiveRecordings(listRecsCtx, new(rpcV1.ListActiveRecordingsRequest)); err != nil {
231+
var listRecsResp *rpcv1.ListActiveRecordingsResponse
232+
if listRecsResp, err = pcapClient.ListActiveRecordings(listRecsCtx, new(rpcv1.ListActiveRecordingsRequest)); err != nil {
233233
return err
234234
}
235235

@@ -248,8 +248,8 @@ func runRemoveCurrentlyRunningRecording(_ *cobra.Command, args []string) error {
248248
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
249249
defer cancel()
250250

251-
var stopRecResp *rpcV1.StopPCAPFileRecordingResponse
252-
stopRecResp, err = pcapClient.StopPCAPFileRecording(ctx, &rpcV1.StopPCAPFileRecordingRequest{
251+
var stopRecResp *rpcv1.StopPCAPFileRecordingResponse
252+
stopRecResp, err = pcapClient.StopPCAPFileRecording(ctx, &rpcv1.StopPCAPFileRecordingRequest{
253253
ConsumerKey: args[0],
254254
})
255255

@@ -263,11 +263,11 @@ func runRemoveCurrentlyRunningRecording(_ *cobra.Command, args []string) error {
263263
return nil
264264
}
265265

266-
func isValidRecordDevice(device string, pcapClient rpcV1.PCAPServiceClient) (err error) {
266+
func isValidRecordDevice(device string, pcapClient rpcv1.PCAPServiceClient) (err error) {
267267
ctx, cancel := context.WithTimeout(cliApp.Context(), cfg.GRPCTimeout)
268268
defer cancel()
269-
var resp *rpcV1.ListAvailableDevicesResponse
270-
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcV1.ListAvailableDevicesRequest)); err != nil {
269+
var resp *rpcv1.ListAvailableDevicesResponse
270+
if resp, err = pcapClient.ListAvailableDevices(ctx, new(rpcv1.ListAvailableDevicesRequest)); err != nil {
271271
return
272272
}
273273

0 commit comments

Comments
 (0)