mrproliu commented on code in PR #232: URL: https://github.com/apache/skywalking-go/pull/232#discussion_r2370950631
########## plugins/core/reporter/pprof_manager.go: ########## @@ -0,0 +1,333 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package reporter + +import ( + "context" + "io" + "strconv" + "time" + + "github.com/apache/skywalking-go/plugins/core/operator" + commonv3 "github.com/apache/skywalking-go/protocols/collect/common/v3" + pprofv10 "github.com/apache/skywalking-go/protocols/collect/pprof/v10" +) + +const ( + // max chunk size for pprof data + maxChunkSize = 1 * 1024 * 1024 + // max send queue size for pprof data + maxPprofSendQueueSize = 30000 +) + +type PprofTaskCommand interface { + GetTaskID() string + GetCreateTime() int64 + GetDuration() time.Duration + StartTask() (io.Writer, error) + StopTask(io.Writer) + IsDirectSamplingType() bool +} +type PprofReporter interface { + ReportPprof(taskID string, content []byte) +} + +var NewPprofTaskCommand func(taskID, events string, duration time.Duration, + createTime int64, dumpPeriod int, pprofFilePath string, + logger operator.LogOperator, manager PprofReporter) PprofTaskCommand + +type PprofTaskManager struct { + logger operator.LogOperator + serverAddr string + pprofInterval time.Duration + PprofClient pprofv10.PprofTaskClient // for grpc + connManager *ConnectionManager + entity *Entity + pprofFilePath string + LastUpdateTime int64 + commands PprofTaskCommand + pprofSendCh chan *pprofv10.PprofData +} + +func NewPprofTaskManager(logger operator.LogOperator, serverAddr string, + pprofInterval time.Duration, connManager *ConnectionManager, + pprofFilePath string) (*PprofTaskManager, error) { + if pprofInterval <= 0 { + var err error + logger.Errorf("pprof interval less than zero, pprof profiling is disabled") + return nil, err + } + pprofManager := &PprofTaskManager{ + logger: logger, + serverAddr: serverAddr, + pprofInterval: pprofInterval, + connManager: connManager, + pprofFilePath: pprofFilePath, + pprofSendCh: make(chan *pprofv10.PprofData, maxPprofSendQueueSize), + } + conn, err := connManager.GetConnection(serverAddr) + if err != nil { + return nil, err + } + pprofManager.PprofClient = pprofv10.NewPprofTaskClient(conn) + pprofManager.commands = nil + return pprofManager, nil +} + +func (r *PprofTaskManager) InitPprofTask(entity *Entity) { + if r.PprofClient == nil { + return + } + r.entity = entity + r.initPprofSendPipeline() + go func() { + for { + switch r.connManager.GetConnectionStatus(r.serverAddr) { + case ConnectionStatusShutdown: + return + case ConnectionStatusDisconnect: + time.Sleep(r.pprofInterval) + continue + } + pprofCommand, err := r.PprofClient.GetPprofTaskCommands(context.Background(), &pprofv10.PprofTaskCommandQuery{ + Service: r.entity.ServiceName, + ServiceInstance: r.entity.ServiceInstanceName, + LastCommandTime: r.LastUpdateTime, + }) + if err != nil { + r.logger.Errorf("fetch pprof task commands error %v", err) + time.Sleep(r.pprofInterval) + continue + } + + if len(pprofCommand.GetCommands()) > 0 && pprofCommand.GetCommands()[0].Command == "PprofTaskQuery" { + rawCommand := pprofCommand.GetCommands()[0] + r.HandleCommand(rawCommand) + } + + time.Sleep(r.pprofInterval) + } + }() +} + +func (r *PprofTaskManager) HandleCommand(rawCommand *commonv3.Command) { + command := r.deserializePprofTaskCommand(rawCommand) Review Comment: Please add a verification for the command. The OAP could send incorrect data, which will cause the agent to execute something unexpected. ########## plugins/core/reporter/pprof_manager.go: ########## @@ -0,0 +1,333 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package reporter + +import ( + "context" + "io" + "strconv" + "time" + + "github.com/apache/skywalking-go/plugins/core/operator" + commonv3 "github.com/apache/skywalking-go/protocols/collect/common/v3" + pprofv10 "github.com/apache/skywalking-go/protocols/collect/pprof/v10" +) + +const ( + // max chunk size for pprof data + maxChunkSize = 1 * 1024 * 1024 + // max send queue size for pprof data + maxPprofSendQueueSize = 30000 +) + +type PprofTaskCommand interface { + GetTaskID() string + GetCreateTime() int64 + GetDuration() time.Duration + StartTask() (io.Writer, error) + StopTask(io.Writer) + IsDirectSamplingType() bool +} +type PprofReporter interface { + ReportPprof(taskID string, content []byte) +} + +var NewPprofTaskCommand func(taskID, events string, duration time.Duration, + createTime int64, dumpPeriod int, pprofFilePath string, + logger operator.LogOperator, manager PprofReporter) PprofTaskCommand + +type PprofTaskManager struct { + logger operator.LogOperator + serverAddr string + pprofInterval time.Duration + PprofClient pprofv10.PprofTaskClient // for grpc + connManager *ConnectionManager + entity *Entity + pprofFilePath string + LastUpdateTime int64 + commands PprofTaskCommand + pprofSendCh chan *pprofv10.PprofData +} + +func NewPprofTaskManager(logger operator.LogOperator, serverAddr string, + pprofInterval time.Duration, connManager *ConnectionManager, + pprofFilePath string) (*PprofTaskManager, error) { + if pprofInterval <= 0 { + var err error + logger.Errorf("pprof interval less than zero, pprof profiling is disabled") + return nil, err Review Comment: The error is always `nil`? Is it correct? ########## plugins/core/reporter/pprof_manager.go: ########## @@ -0,0 +1,333 @@ +// Licensed to Apache Software Foundation (ASF) under one or more contributor +// license agreements. See the NOTICE file distributed with +// this work for additional information regarding copyright +// ownership. Apache Software Foundation (ASF) licenses this file to you under +// the Apache License, Version 2.0 (the "License"); you may +// not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +package reporter + +import ( + "context" + "io" + "strconv" + "time" + + "github.com/apache/skywalking-go/plugins/core/operator" + commonv3 "github.com/apache/skywalking-go/protocols/collect/common/v3" + pprofv10 "github.com/apache/skywalking-go/protocols/collect/pprof/v10" +) + +const ( + // max chunk size for pprof data + maxChunkSize = 1 * 1024 * 1024 + // max send queue size for pprof data + maxPprofSendQueueSize = 30000 +) + +type PprofTaskCommand interface { + GetTaskID() string + GetCreateTime() int64 + GetDuration() time.Duration + StartTask() (io.Writer, error) + StopTask(io.Writer) + IsDirectSamplingType() bool +} +type PprofReporter interface { + ReportPprof(taskID string, content []byte) +} + +var NewPprofTaskCommand func(taskID, events string, duration time.Duration, + createTime int64, dumpPeriod int, pprofFilePath string, + logger operator.LogOperator, manager PprofReporter) PprofTaskCommand + +type PprofTaskManager struct { + logger operator.LogOperator + serverAddr string + pprofInterval time.Duration + PprofClient pprofv10.PprofTaskClient // for grpc + connManager *ConnectionManager + entity *Entity + pprofFilePath string + LastUpdateTime int64 + commands PprofTaskCommand + pprofSendCh chan *pprofv10.PprofData +} + +func NewPprofTaskManager(logger operator.LogOperator, serverAddr string, + pprofInterval time.Duration, connManager *ConnectionManager, + pprofFilePath string) (*PprofTaskManager, error) { + if pprofInterval <= 0 { + var err error + logger.Errorf("pprof interval less than zero, pprof profiling is disabled") + return nil, err + } + pprofManager := &PprofTaskManager{ + logger: logger, + serverAddr: serverAddr, + pprofInterval: pprofInterval, + connManager: connManager, + pprofFilePath: pprofFilePath, + pprofSendCh: make(chan *pprofv10.PprofData, maxPprofSendQueueSize), + } + conn, err := connManager.GetConnection(serverAddr) + if err != nil { + return nil, err + } + pprofManager.PprofClient = pprofv10.NewPprofTaskClient(conn) + pprofManager.commands = nil + return pprofManager, nil +} + +func (r *PprofTaskManager) InitPprofTask(entity *Entity) { + if r.PprofClient == nil { Review Comment: Why could the client be `nil`? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@skywalking.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org