mirror of
https://github.com/netbirdio/netbird.git
synced 2026-07-16 19:49:56 +00:00
When the daemon is set to debug/trace, the GUI now automatically writes a rotated gui-client.log in the user's config dir and the daemon's debug bundle collects it. The UI learns the level both at startup (daemon already in debug) and live, by piggybacking the existing SubscribeEvents stream: the daemon publishes a marked log-level-changed SystemEvent (and a per-subscription snapshot), which DaemonFeed routes to guilog.DebugLog instead of an OS toast. The UI registers its log path via a new RegisterUILog RPC so the root daemon, which can't resolve the user's config dir, knows where to find the file. Manual --log-file (any value) disables the daemon-driven file logging. Fix: client/ui SetLogLevel looked up proto.LogLevel_value with the lowercase logrus name, which never matched the uppercase enum keys and silently fell back to INFO — so trace/debug requests from the bundle flow had no effect.
213 lines
6.2 KiB
Go
213 lines
6.2 KiB
Go
//go:build !android && !ios
|
|
|
|
package server
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"runtime/pprof"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/netbirdio/netbird/client/internal/debug"
|
|
"github.com/netbirdio/netbird/client/proto"
|
|
mgmProto "github.com/netbirdio/netbird/shared/management/proto"
|
|
"github.com/netbirdio/netbird/version"
|
|
)
|
|
|
|
// DebugBundle creates a debug bundle and returns the location.
|
|
func (s *Server) DebugBundle(_ context.Context, req *proto.DebugBundleRequest) (resp *proto.DebugBundleResponse, err error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
syncResponse, err := s.getLatestSyncResponse()
|
|
if err != nil {
|
|
log.Warnf("failed to get latest sync response: %v", err)
|
|
}
|
|
|
|
var clientMetrics debug.MetricsExporter
|
|
if s.connectClient != nil {
|
|
if engine := s.connectClient.Engine(); engine != nil {
|
|
if cm := engine.GetClientMetrics(); cm != nil {
|
|
clientMetrics = cm
|
|
}
|
|
}
|
|
}
|
|
|
|
var cpuProfileData []byte
|
|
if s.cpuProfileBuf != nil && !s.cpuProfiling {
|
|
cpuProfileData = s.cpuProfileBuf.Bytes()
|
|
defer func() {
|
|
s.cpuProfileBuf = nil
|
|
}()
|
|
}
|
|
|
|
capturePath := s.bundleCapturePath()
|
|
defer s.cleanupBundleCapture()
|
|
|
|
var refreshStatus func()
|
|
if s.connectClient != nil {
|
|
engine := s.connectClient.Engine()
|
|
if engine != nil {
|
|
refreshStatus = func() {
|
|
log.Debug("refreshing system health status for debug bundle")
|
|
// Background ctx: the bundle wants a full, fresh probe regardless
|
|
// of the DebugBundle RPC client's lifetime. The engine's own ctx
|
|
// still aborts it on shutdown.
|
|
engine.RunHealthProbes(context.Background(), true)
|
|
}
|
|
}
|
|
}
|
|
|
|
bundleGenerator := debug.NewBundleGenerator(
|
|
debug.GeneratorDependencies{
|
|
InternalConfig: s.config,
|
|
StatusRecorder: s.statusRecorder,
|
|
SyncResponse: syncResponse,
|
|
LogPath: s.logFile,
|
|
UILogPath: s.uiLogPath,
|
|
CPUProfile: cpuProfileData,
|
|
CapturePath: capturePath,
|
|
RefreshStatus: refreshStatus,
|
|
ClientMetrics: clientMetrics,
|
|
DaemonVersion: version.NetbirdVersion(),
|
|
CliVersion: req.CliVersion,
|
|
},
|
|
debug.BundleConfig{
|
|
Anonymize: req.GetAnonymize(),
|
|
IncludeSystemInfo: req.GetSystemInfo(),
|
|
LogFileCount: req.GetLogFileCount(),
|
|
},
|
|
)
|
|
|
|
path, err := bundleGenerator.Generate()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("generate debug bundle: %w", err)
|
|
}
|
|
|
|
if req.GetUploadURL() == "" {
|
|
return &proto.DebugBundleResponse{Path: path}, nil
|
|
}
|
|
key, err := debug.UploadDebugBundle(context.Background(), req.GetUploadURL(), s.config.ManagementURL.String(), path)
|
|
if err != nil {
|
|
log.Errorf("failed to upload debug bundle to %s: %v", req.GetUploadURL(), err)
|
|
return &proto.DebugBundleResponse{Path: path, UploadFailureReason: err.Error()}, nil
|
|
}
|
|
|
|
log.Infof("debug bundle uploaded to %s with key %s", req.GetUploadURL(), key)
|
|
|
|
return &proto.DebugBundleResponse{Path: path, UploadedKey: key}, nil
|
|
}
|
|
|
|
// GetLogLevel gets the current logging level for the server.
|
|
func (s *Server) GetLogLevel(_ context.Context, _ *proto.GetLogLevelRequest) (*proto.GetLogLevelResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
level := ParseLogLevel(log.GetLevel().String())
|
|
return &proto.GetLogLevelResponse{Level: level}, nil
|
|
}
|
|
|
|
// SetLogLevel sets the logging level for the server.
|
|
func (s *Server) SetLogLevel(_ context.Context, req *proto.SetLogLevelRequest) (*proto.SetLogLevelResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
level, err := log.ParseLevel(req.Level.String())
|
|
if err != nil {
|
|
return nil, fmt.Errorf("invalid log level: %w", err)
|
|
}
|
|
|
|
log.SetLevel(level)
|
|
|
|
if s.connectClient != nil {
|
|
s.connectClient.SetLogLevel(level)
|
|
}
|
|
|
|
log.Infof("Log level set to %s", level.String())
|
|
|
|
// Signal the desktop UI so it can attach/detach its gui-client.log. Rides
|
|
// the SubscribeEvents stream as a marked event (see publishLogLevelChanged).
|
|
s.publishLogLevelChanged(level.String())
|
|
|
|
return &proto.SetLogLevelResponse{}, nil
|
|
}
|
|
|
|
// RegisterUILog records the desktop UI's absolute log path so DebugBundle can
|
|
// collect the GUI log. The daemon runs as root and can't resolve the user's
|
|
// config dir, so the UI reports it. Last-writer-wins (one UI per socket).
|
|
func (s *Server) RegisterUILog(_ context.Context, req *proto.RegisterUILogRequest) (*proto.RegisterUILogResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
s.uiLogPath = req.GetPath()
|
|
log.Infof("registered UI log path: %s", s.uiLogPath)
|
|
|
|
return &proto.RegisterUILogResponse{}, nil
|
|
}
|
|
|
|
// SetSyncResponsePersistence sets the sync response persistence for the server.
|
|
func (s *Server) SetSyncResponsePersistence(_ context.Context, req *proto.SetSyncResponsePersistenceRequest) (*proto.SetSyncResponsePersistenceResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
enabled := req.GetEnabled()
|
|
s.persistSyncResponse = enabled
|
|
if s.connectClient != nil {
|
|
s.connectClient.SetSyncResponsePersistence(enabled)
|
|
}
|
|
|
|
return &proto.SetSyncResponsePersistenceResponse{}, nil
|
|
}
|
|
|
|
func (s *Server) getLatestSyncResponse() (*mgmProto.SyncResponse, error) {
|
|
cClient := s.connectClient
|
|
if cClient == nil {
|
|
return nil, errors.New("connect client is not initialized")
|
|
}
|
|
|
|
return cClient.GetLatestSyncResponse()
|
|
}
|
|
|
|
// StartCPUProfile starts CPU profiling in the daemon.
|
|
func (s *Server) StartCPUProfile(_ context.Context, _ *proto.StartCPUProfileRequest) (*proto.StartCPUProfileResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
if s.cpuProfiling {
|
|
return nil, fmt.Errorf("CPU profiling already in progress")
|
|
}
|
|
|
|
s.cpuProfileBuf = &bytes.Buffer{}
|
|
s.cpuProfiling = true
|
|
if err := pprof.StartCPUProfile(s.cpuProfileBuf); err != nil {
|
|
s.cpuProfileBuf = nil
|
|
s.cpuProfiling = false
|
|
return nil, fmt.Errorf("start CPU profile: %w", err)
|
|
}
|
|
|
|
log.Info("CPU profiling started")
|
|
return &proto.StartCPUProfileResponse{}, nil
|
|
}
|
|
|
|
// StopCPUProfile stops CPU profiling in the daemon.
|
|
func (s *Server) StopCPUProfile(_ context.Context, _ *proto.StopCPUProfileRequest) (*proto.StopCPUProfileResponse, error) {
|
|
s.mutex.Lock()
|
|
defer s.mutex.Unlock()
|
|
|
|
if !s.cpuProfiling {
|
|
return nil, fmt.Errorf("CPU profiling not in progress")
|
|
}
|
|
|
|
pprof.StopCPUProfile()
|
|
s.cpuProfiling = false
|
|
|
|
if s.cpuProfileBuf != nil {
|
|
log.Infof("CPU profiling stopped, captured %d bytes", s.cpuProfileBuf.Len())
|
|
}
|
|
|
|
return &proto.StopCPUProfileResponse{}, nil
|
|
}
|