Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logging): only log GET requests at debug level #3237

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 1 addition & 9 deletions app/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,196 +97,188 @@
}
}

func startManager(c *cli.Context) error {
var (
err error
)

engineImage := c.String(FlagEngineImage)
if engineImage == "" {
return fmt.Errorf("require %v", FlagEngineImage)
}
instanceManagerImage := c.String(FlagInstanceManagerImage)
if instanceManagerImage == "" {
return fmt.Errorf("require %v", FlagInstanceManagerImage)
}
shareManagerImage := c.String(FlagShareManagerImage)
if shareManagerImage == "" {
return fmt.Errorf("require %v", FlagShareManagerImage)
}
backingImageManagerImage := c.String(FlagBackingImageManagerImage)
if backingImageManagerImage == "" {
return fmt.Errorf("require %v", FlagBackingImageManagerImage)
}
supportBundleManagerImage := c.String(FlagSupportBundleManagerImage)
if supportBundleManagerImage == "" {
return fmt.Errorf("require %v", FlagSupportBundleManagerImage)
}
managerImage := c.String(FlagManagerImage)
if managerImage == "" {
return fmt.Errorf("require %v", FlagManagerImage)
}
serviceAccount := c.String(FlagServiceAccount)
if serviceAccount == "" {
return fmt.Errorf("require %v", FlagServiceAccount)
}
kubeconfigPath := c.String(FlagKubeConfig)

if err := environmentCheck(); err != nil {
return errors.Wrap(err, "failed to check environment, please make sure you have iscsiadm/open-iscsi installed on the host")
}

currentNodeID, err := util.GetRequiredEnv(types.EnvNodeName)
if err != nil {
return fmt.Errorf("failed to detect the node name")
}

currentIP, err := util.GetRequiredEnv(types.EnvPodIP)
if err != nil {
return fmt.Errorf("failed to detect the node IP")
}

podName, err := util.GetRequiredEnv(types.EnvPodName)
if err != nil {
return fmt.Errorf("failed to detect the manager pod name")
}

podNamespace, err := util.GetRequiredEnv(types.EnvPodNamespace)
if err != nil {
return fmt.Errorf("failed to detect the manager pod namespace")
}

ctx := signals.SetupSignalContext()

logger := logrus.StandardLogger().WithField("node", currentNodeID)

// Conversion webhook needs to be started first since we use its port 9501 as readiness port.
// longhorn-manager pod becomes ready only when conversion webhook is running.
// The services in the longhorn-manager can then start to receive the requests.
// Conversion webhook does not use datastore, since it is a prerequisite for
// datastore operation.
clientsWithoutDatastore, err := client.NewClients(kubeconfigPath, false, ctx.Done())
if err != nil {
return err
}
if err := webhook.StartWebhook(ctx, types.WebhookTypeConversion, clientsWithoutDatastore); err != nil {
return err
}

// This adds the label for the conversion webhook's selector. We do it the hard way without datastore to avoid chicken-and-egg.
pod, err := clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Get(context.Background(), podName, v1.GetOptions{})
if err != nil {
return err
}
labels := types.GetConversionWebhookLabel()
for key, value := range labels {
pod.Labels[key] = value
}
_, err = clientsWithoutDatastore.Clients.K8s.CoreV1().Pods(podNamespace).Update(context.Background(), pod, v1.UpdateOptions{})
if err != nil {
return err
}
if err := webhook.CheckWebhookServiceAvailability(types.WebhookTypeConversion); err != nil {
return err
}

clients, err := client.NewClients(kubeconfigPath, true, ctx.Done())
if err != nil {
return err
}

if err := webhook.StartWebhook(ctx, types.WebhookTypeAdmission, clients); err != nil {
return err
}
if err := clients.Datastore.AddLabelToManagerPod(currentNodeID, types.GetAdmissionWebhookLabel()); err != nil {
return err
}
if err := webhook.CheckWebhookServiceAvailability(types.WebhookTypeAdmission); err != nil {
return err
}

if err := recoverybackend.StartRecoveryBackend(clients); err != nil {
return err
}

if err := clients.Datastore.AddLabelToManagerPod(currentNodeID, types.GetRecoveryBackendLabel()); err != nil {
return err
}

if err := upgrade.Upgrade(kubeconfigPath, currentNodeID, managerImage, c.Bool(FlagUpgradeVersionCheck)); err != nil {
return err
}

proxyConnCounter := util.NewAtomicCounter()

wsc, err := controller.StartControllers(logger, clients,
currentNodeID, serviceAccount, managerImage, backingImageManagerImage, shareManagerImage,
kubeconfigPath, meta.Version, proxyConnCounter)
if err != nil {
return err
}

m := manager.NewVolumeManager(currentNodeID, clients.Datastore, proxyConnCounter)

metricscollector.InitMetricsCollectorSystem(logger, currentNodeID, clients.Datastore, kubeconfigPath, proxyConnCounter)

defaultImageSettings := map[types.SettingName]string{
types.SettingNameDefaultEngineImage: engineImage,
types.SettingNameDefaultInstanceManagerImage: instanceManagerImage,
types.SettingNameDefaultBackingImageManagerImage: backingImageManagerImage,
types.SettingNameSupportBundleManagerImage: supportBundleManagerImage,
}
if err := clients.Datastore.UpdateCustomizedSettings(defaultImageSettings); err != nil {
return err
}

if err := updateRegistrySecretName(m); err != nil {
return err
}

if err := initDaemonNode(clients.Datastore, currentNodeID); err != nil {
return err
}

if err := m.DeployEngineImage(engineImage); err != nil {
return err
}

server := api.NewServer(m, wsc)
router := http.Handler(api.NewRouter(server))
router = util.FilteredLoggingHandler(map[string]struct{}{
"/v1/apiversions": {},
"/v1/schemas": {},
"/v1/settings": {},
"/v1/volumes": {},
"/v1/nodes": {},
"/v1/engineimages": {},
"/v1/events": {},
}, os.Stdout, router)
router = util.FilteredLoggingHandler(os.Stdout, router)
router = handlers.ProxyHeaders(router)

listen := types.GetAPIServerAddressFromIP(currentIP)
logger.Infof("Listening on %s", listen)

go func() {
if err := http.ListenAndServe(listen, router); err != nil {
logrus.Fatalf("Error longhorn backend server failed: %v", err)
}
}()

go func() {
debugAddress := "127.0.0.1:6060"
debugHandler := http.DefaultServeMux
logger.Infof("Debug Server listening on %s", debugAddress)
if err := http.ListenAndServe(debugAddress, debugHandler); err != nil && !errors.Is(err, http.ErrServerClosed) {
logger.WithError(err).Error("Failed to start debug server")
}
}()

<-ctx.Done()
return nil
}

Check notice on line 281 in app/daemon.go

View check run for this annotation

codefactor.io / CodeFactor

app/daemon.go#L100-L281

Complex Method
func environmentCheck() error {
// Here we only check if the necessary tool the iscsiadm is installed when Longhorn starts up.
// Others tools and settings like kernel versions, multipathd, nfs client, etc. are checked in the node controller (every 30 sec).
Expand Down
6 changes: 2 additions & 4 deletions util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,13 @@ func RunAsync(wg *sync.WaitGroup, f func()) {
}

type filteredLoggingHandler struct {
filteredPaths map[string]struct{}
handler http.Handler
loggingHandler http.Handler
}

func FilteredLoggingHandler(filteredPaths map[string]struct{}, writer io.Writer, router http.Handler) http.Handler {
func FilteredLoggingHandler(writer io.Writer, router http.Handler) http.Handler {

return filteredLoggingHandler{
filteredPaths: filteredPaths,
handler: router,
loggingHandler: handlers.CombinedLoggingHandler(writer, router),
}
Expand All @@ -477,7 +475,7 @@ func FilteredLoggingHandler(filteredPaths map[string]struct{}, writer io.Writer,
func (h filteredLoggingHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.Method {
case "GET":
if _, exists := h.filteredPaths[req.URL.Path]; exists {
if logrus.GetLevel() < logrus.DebugLevel {
h.handler.ServeHTTP(w, req)
return
}
Expand Down