Changes needed before gokit/log to slog transition. (#39527)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #38889 PLEASE READ BELOW before looking at file changes Before converting individual files/packages to slog, we generally need to make these 2 changes to make the conversion easier: - Replace uses of `kitlog.With` since they are not fully compatible with our kitlog adapter - Directly use the kitlog adapter logger type instead of the kitlog interface, which will let us have direct access to the underlying slog logger: `*logging.Logger` Note: that I did not replace absolutely all uses of `kitlog.Logger`, but I did remove all uses of `kitlog.With` except for these due to complexity: - server/logging/filesystem.go and the other log writers (webhook, firehose, kinesis, lambda, pubsub, nats) - server/datastore/mysql/nanomdm_storage.go (adapter pattern) - server/vulnerabilities/nvd/* (cascades to CLI tools) - server/service/osquery_utils/queries.go (callback type signatures cascade broadly) - cmd/maintained-apps/ (standalone, so can be transitioned later all at once) Most of the changes in this PR follow these patterns: - `kitlog.Logger` type → `*logging.Logger` - `kitlog.With(logger, ...)` → `logger.With(...)` - `kitlog.NewNopLogger() → logging.NewNopLogger()`, including similar variations such as `logging.NewLogfmtLogger(w)` and `logging.NewJSONLogger(w)` - removed many now-unused kitlog imports Unique changes that the PR review should focus on: - server/platform/logging/kitlog_adapter.go: Core adapter changes - server/platform/logging/logging.go: New convenience functions - server/service/integration_logger_test.go: Test changes for slog # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Was added in previous PR ## Testing - [x] Added/updated automated tests - [x] QA'd all new/changed functionality manually <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Refactor** * Migrated the codebase to a unified internal structured logging system for more consistent, reliable logs and observability. * No user-facing functionality changed; runtime behavior and APIs remain compatible. * **Tests** * Updated tests to use the new logging helpers to ensure consistent test logging and validation. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/fleetdm/fleet/v4/server/mdm/assets"
|
||||
depclient "github.com/fleetdm/fleet/v4/server/mdm/nanodep/client"
|
||||
"github.com/fleetdm/fleet/v4/server/mdm/nanodep/storage"
|
||||
kitlog "github.com/go-kit/log"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
)
|
||||
|
||||
// SetABMTokenMetadata uses the provided ABM token to fetch the associated
|
||||
@@ -23,7 +23,7 @@ func SetABMTokenMetadata(
|
||||
abmToken *fleet.ABMToken,
|
||||
depStorage storage.AllDEPStorage,
|
||||
ds fleet.Datastore,
|
||||
logger kitlog.Logger,
|
||||
logger *logging.Logger,
|
||||
renewal bool,
|
||||
) error {
|
||||
decryptedToken, err := assets.ABMToken(ctx, ds, abmToken.OrganizationName)
|
||||
@@ -42,7 +42,7 @@ func SetDecryptedABMTokenMetadata(
|
||||
decryptedToken *depclient.OAuth1Tokens,
|
||||
depStorage storage.AllDEPStorage,
|
||||
ds fleet.Datastore,
|
||||
logger kitlog.Logger,
|
||||
logger *logging.Logger,
|
||||
renewal bool,
|
||||
) error {
|
||||
depClient := NewDEPClient(depStorage, ds, logger)
|
||||
|
||||
@@ -28,7 +28,7 @@ import (
|
||||
depclient "github.com/fleetdm/fleet/v4/server/mdm/nanodep/client"
|
||||
nanodep_storage "github.com/fleetdm/fleet/v4/server/mdm/nanodep/storage"
|
||||
depsync "github.com/fleetdm/fleet/v4/server/mdm/nanodep/sync"
|
||||
kitlog "github.com/go-kit/log"
|
||||
platformlogging "github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -92,7 +92,7 @@ type DEPService struct {
|
||||
ds fleet.Datastore
|
||||
depStorage nanodep_storage.AllDEPStorage
|
||||
depClient *godep.Client
|
||||
logger kitlog.Logger
|
||||
logger *platformlogging.Logger
|
||||
}
|
||||
|
||||
// getDefaultProfile returns a godep.Profile with default values set.
|
||||
@@ -443,7 +443,7 @@ func (d *DEPService) EnsureCustomSetupAssistantIfExists(ctx context.Context, tea
|
||||
}
|
||||
|
||||
func (d *DEPService) RunAssigner(ctx context.Context) error {
|
||||
syncerLogger := logging.NewNanoDEPLogger(kitlog.With(d.logger, "component", "nanodep-syncer"))
|
||||
syncerLogger := logging.NewNanoDEPLogger(d.logger.With("component", "nanodep-syncer"))
|
||||
teams, err := d.ds.ListTeams(
|
||||
ctx, fleet.TeamFilter{
|
||||
User: &fleet.User{
|
||||
@@ -572,7 +572,7 @@ func (d *DEPService) AssignMDMAppleServiceDiscoveryURL(ctx context.Context, toke
|
||||
func NewDEPService(
|
||||
ds fleet.Datastore,
|
||||
depStorage nanodep_storage.AllDEPStorage,
|
||||
logger kitlog.Logger,
|
||||
logger *platformlogging.Logger,
|
||||
) *DEPService {
|
||||
depSvc := &DEPService{
|
||||
depStorage: depStorage,
|
||||
@@ -747,15 +747,15 @@ func (d *DEPService) processDeviceResponse(
|
||||
n, err := d.ds.IngestMDMAppleDevicesFromDEPSync(ctx, addedDevicesSlice, abmTokenID, macOSTeam, iosTeam, ipadTeam)
|
||||
switch {
|
||||
case err != nil:
|
||||
level.Error(kitlog.With(d.logger)).Log("err", err)
|
||||
level.Error(d.logger).Log("err", err)
|
||||
ctxerr.Handle(ctx, err)
|
||||
case n > 0:
|
||||
level.Info(kitlog.With(d.logger)).Log("msg", fmt.Sprintf("added %d new mdm device(s) to pending hosts", n))
|
||||
level.Info(d.logger).Log("msg", fmt.Sprintf("added %d new mdm device(s) to pending hosts", n))
|
||||
case n == 0:
|
||||
level.Debug(kitlog.With(d.logger)).Log("msg", "no DEP hosts to add")
|
||||
level.Debug(d.logger).Log("msg", "no DEP hosts to add")
|
||||
}
|
||||
|
||||
level.Info(kitlog.With(d.logger)).Log("msg", "devices to assign DEP profiles",
|
||||
level.Info(d.logger).Log("msg", "devices to assign DEP profiles",
|
||||
"to_add", strings.Join(addedSerials, ", "),
|
||||
"to_remove", strings.Join(deletedSerials, ", "),
|
||||
"to_modify", strings.Join(modifiedSerials, ", "),
|
||||
@@ -801,7 +801,7 @@ func (d *DEPService) processDeviceResponse(
|
||||
level.Info(d.logger).Log("msg", "preparing to upsert DEP assignment for existing host", "serial", existingHost.HardwareSerial, "host_id", existingHost.ID)
|
||||
md, ok := modifiedDevices[existingHost.HardwareSerial]
|
||||
if !ok {
|
||||
level.Error(kitlog.With(d.logger)).Log("msg",
|
||||
level.Error(d.logger).Log("msg",
|
||||
"serial coming from ABM is in the database, but it's not in the list of modified devices", "serial",
|
||||
existingHost.HardwareSerial)
|
||||
continue
|
||||
@@ -851,7 +851,7 @@ func (d *DEPService) processDeviceResponse(
|
||||
continue
|
||||
}
|
||||
|
||||
logger := kitlog.With(d.logger, "profile_uuid", profUUID)
|
||||
logger := d.logger.With("profile_uuid", profUUID)
|
||||
|
||||
skipSerials, assignSerials, err := d.ds.ScreenDEPAssignProfileSerialsForCooldown(ctx, serials)
|
||||
if err != nil {
|
||||
@@ -916,7 +916,7 @@ func (d *DEPService) processDeviceResponse(
|
||||
}
|
||||
|
||||
if len(skippedSerials) > 0 {
|
||||
level.Info(kitlog.With(d.logger)).Log("msg", "found devices that already have the right profile, skipping assignment", "serials",
|
||||
level.Info(d.logger).Log("msg", "found devices that already have the right profile, skipping assignment", "serials",
|
||||
fmt.Sprintf("%s", skippedSerials))
|
||||
}
|
||||
|
||||
@@ -970,7 +970,7 @@ func logCountsForResults(deviceResults map[string]string) (out []interface{}) {
|
||||
// storage that will flag the ABM token's terms expired field and the
|
||||
// AppConfig's AppleBMTermsExpired field whenever the status of the terms
|
||||
// changes.
|
||||
func NewDEPClient(storage godep.ClientStorage, updater fleet.ABMTermsUpdater, logger kitlog.Logger) *godep.Client {
|
||||
func NewDEPClient(storage godep.ClientStorage, updater fleet.ABMTermsUpdater, logger *platformlogging.Logger) *godep.Client {
|
||||
return godep.NewClient(storage, fleethttp.NewClient(), godep.WithAfterHook(func(ctx context.Context, reqErr error) error {
|
||||
// to check for ABM terms expired, we must have an ABM token organization
|
||||
// name and NOT a raw ABM token in the context (as the presence of a raw
|
||||
@@ -1400,7 +1400,8 @@ func (pb *ProfileBimap) add(wantedProfile, currentProfile *fleet.MDMAppleProfile
|
||||
// NewActivityFunc is the function signature for creating a new activity.
|
||||
type NewActivityFunc func(ctx context.Context, user *fleet.User, activity fleet.ActivityDetails) error
|
||||
|
||||
func IOSiPadOSRefetch(ctx context.Context, ds fleet.Datastore, commander *MDMAppleCommander, logger kitlog.Logger, newActivityFn NewActivityFunc) error {
|
||||
func IOSiPadOSRefetch(ctx context.Context, ds fleet.Datastore, commander *MDMAppleCommander, logger *platformlogging.Logger,
|
||||
newActivityFn NewActivityFunc) error {
|
||||
appCfg, err := ds.AppConfig(ctx)
|
||||
if err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "fetching app config")
|
||||
@@ -1513,7 +1514,8 @@ func IOSiPadOSRefetch(ctx context.Context, ds fleet.Datastore, commander *MDMApp
|
||||
|
||||
// turnOffMDMIfAPNSFailed checks if the error is an APNSDeliveryError and turns off MDM for the failed devices.
|
||||
// Returns a boolean value to indicate whether or not MDM was turned off.
|
||||
func turnOffMDMIfAPNSFailed(ctx context.Context, ds fleet.Datastore, err error, logger kitlog.Logger, newActivityFn NewActivityFunc) (bool, error) {
|
||||
func turnOffMDMIfAPNSFailed(ctx context.Context, ds fleet.Datastore, err error, logger *platformlogging.Logger, newActivityFn NewActivityFunc) (bool,
|
||||
error) {
|
||||
var e *APNSDeliveryError
|
||||
if !errors.As(err, &e) {
|
||||
return false, nil
|
||||
@@ -1578,7 +1580,7 @@ func GenerateOTAEnrollmentProfileMobileconfig(orgName, fleetURL, enrollSecret, i
|
||||
return profileBuf.Bytes(), nil
|
||||
}
|
||||
|
||||
func IOSiPadOSRevive(ctx context.Context, ds fleet.Datastore, commander *MDMAppleCommander, logger kitlog.Logger) error {
|
||||
func IOSiPadOSRevive(ctx context.Context, ds fleet.Datastore, commander *MDMAppleCommander, logger *platformlogging.Logger) error {
|
||||
appCfg, err := ds.AppConfig(ctx)
|
||||
if err != nil {
|
||||
return ctxerr.Wrap(ctx, err, "fetching app config")
|
||||
|
||||
@@ -16,8 +16,8 @@ import (
|
||||
apple_mdm "github.com/fleetdm/fleet/v4/server/mdm/apple"
|
||||
nanodep_client "github.com/fleetdm/fleet/v4/server/mdm/nanodep/client"
|
||||
"github.com/fleetdm/fleet/v4/server/mdm/nanodep/godep"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/fleetdm/fleet/v4/server/test"
|
||||
"github.com/go-kit/log"
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
@@ -41,7 +41,7 @@ func TestDEPService_RunAssigner(t *testing.T) {
|
||||
|
||||
mysql.SetTestABMAssets(t, ds, abmTokenOrgName)
|
||||
|
||||
logger := log.NewNopLogger()
|
||||
logger := logging.NewNopLogger()
|
||||
return apple_mdm.NewDEPService(ds, depStorage, logger)
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ import (
|
||||
"github.com/fleetdm/fleet/v4/server/mdm/nanodep/godep"
|
||||
"github.com/fleetdm/fleet/v4/server/mock"
|
||||
nanodep_mock "github.com/fleetdm/fleet/v4/server/mock/nanodep"
|
||||
"github.com/go-kit/log"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/micromdm/plist"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
@@ -25,7 +25,7 @@ func TestDEPService(t *testing.T) {
|
||||
t.Run("EnsureDefaultSetupAssistant", func(t *testing.T) {
|
||||
ds := new(mock.Store)
|
||||
ctx := context.Background()
|
||||
logger := log.NewNopLogger()
|
||||
logger := logging.NewNopLogger()
|
||||
depStorage := new(nanodep_mock.Storage)
|
||||
depSvc := NewDEPService(ds, depStorage, logger)
|
||||
defaultProfile := depSvc.getDefaultProfile()
|
||||
|
||||
Reference in New Issue
Block a user