Migrated logging and google calendar files to use slog (#40541)
<!-- Add the related story/sub-task/bug number, like Resolves #123, or remove if NA --> **Related issue:** Resolves #40540 # Checklist for submitter - [ ] Changes file added for user-visible changes in `changes/`, `orbit/changes/` or `ee/fleetd-chrome/changes`. - Changes present 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** * Switched the application logging to Go's standard slog with context-aware logging, improving structured logs and observability across services (status, audit, result, integrations). * Replaced legacy logging implementations and updated runtime wiring to propagate contextual loggers for more consistent, searchable log output. * **Tests** * Updated test suites to use the new slog discard/logger setup. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
@@ -15,7 +16,6 @@ import (
|
||||
"github.com/cenkalti/backoff/v4"
|
||||
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
||||
"github.com/fleetdm/fleet/v4/server/fleet"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/oauth2/google"
|
||||
"golang.org/x/oauth2/jwt"
|
||||
@@ -52,7 +52,7 @@ var (
|
||||
type GoogleCalendarConfig struct {
|
||||
Context context.Context
|
||||
IntegrationConfig *fleet.GoogleCalendarIntegration
|
||||
Logger *logging.Logger
|
||||
Logger *slog.Logger
|
||||
ServerURL string
|
||||
// Should be nil for production
|
||||
API GoogleCalendarAPI
|
||||
@@ -107,7 +107,7 @@ type eventDetails struct {
|
||||
|
||||
type GoogleCalendarLowLevelAPI struct {
|
||||
service *calendar.Service
|
||||
logger *logging.Logger
|
||||
logger *slog.Logger
|
||||
serverURL string
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package calendar
|
||||
|
||||
import (
|
||||
"context"
|
||||
"log/slog"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
@@ -9,7 +10,6 @@ import (
|
||||
|
||||
"github.com/fleetdm/fleet/v4/ee/server/calendar/load_test"
|
||||
"github.com/fleetdm/fleet/v4/server/fleet"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/stretchr/testify/suite"
|
||||
@@ -62,7 +62,7 @@ func (s *googleCalendarIntegrationTestSuite) TestCreateGetDeleteEvent() {
|
||||
"private_key": s.server.URL,
|
||||
}},
|
||||
},
|
||||
Logger: logging.NewLogfmtLogger(os.Stdout),
|
||||
Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
|
||||
}
|
||||
gCal := NewGoogleCalendar(config)
|
||||
err := gCal.Configure(userEmail)
|
||||
@@ -129,7 +129,7 @@ func (s *googleCalendarIntegrationTestSuite) TestFillUpCalendar() {
|
||||
"private_key": s.server.URL,
|
||||
}},
|
||||
},
|
||||
Logger: logging.NewLogfmtLogger(os.Stdout),
|
||||
Logger: slog.New(slog.NewTextHandler(os.Stdout, nil)),
|
||||
}
|
||||
gCal := NewGoogleCalendar(config)
|
||||
err := gCal.Configure(userEmail)
|
||||
|
||||
@@ -6,19 +6,20 @@ import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"google.golang.org/api/calendar/v3"
|
||||
"google.golang.org/api/googleapi"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
|
||||
"github.com/fleetdm/fleet/v4/pkg/fleethttp"
|
||||
"google.golang.org/api/calendar/v3"
|
||||
"google.golang.org/api/googleapi"
|
||||
)
|
||||
|
||||
// GoogleCalendarLoadAPI is used for load testing.
|
||||
type GoogleCalendarLoadAPI struct {
|
||||
Logger *logging.Logger
|
||||
Logger *slog.Logger
|
||||
baseUrl string
|
||||
userToImpersonate string
|
||||
ctx context.Context
|
||||
@@ -30,7 +31,7 @@ type GoogleCalendarLoadAPI struct {
|
||||
func (lowLevelAPI *GoogleCalendarLoadAPI) Configure(ctx context.Context, _ string, privateKey string, userToImpersonate string,
|
||||
serverURL string) error {
|
||||
if lowLevelAPI.Logger == nil {
|
||||
lowLevelAPI.Logger = logging.NewLogfmtLogger(os.Stderr).With("mock", "GoogleCalendarLoadAPI", "user", userToImpersonate)
|
||||
lowLevelAPI.Logger = slog.New(slog.NewTextHandler(os.Stderr, nil)).With("mock", "GoogleCalendarLoadAPI", "user", userToImpersonate)
|
||||
}
|
||||
lowLevelAPI.baseUrl = privateKey
|
||||
lowLevelAPI.userToImpersonate = userToImpersonate
|
||||
|
||||
@@ -3,20 +3,20 @@ package calendar
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"github.com/google/uuid"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/google/uuid"
|
||||
"google.golang.org/api/calendar/v3"
|
||||
"google.golang.org/api/googleapi"
|
||||
)
|
||||
|
||||
type GoogleCalendarMockAPI struct {
|
||||
logger *logging.Logger
|
||||
logger *slog.Logger
|
||||
}
|
||||
|
||||
type channel struct {
|
||||
@@ -36,14 +36,14 @@ const latency = 200 * time.Millisecond
|
||||
// Configure creates a new Google Calendar service using the provided credentials.
|
||||
func (lowLevelAPI *GoogleCalendarMockAPI) Configure(_ context.Context, _ string, _ string, userToImpersonate string, _ string) error {
|
||||
if lowLevelAPI.logger == nil {
|
||||
lowLevelAPI.logger = logging.NewLogfmtLogger(os.Stderr).With("mock", "GoogleCalendarMockAPI", "user", userToImpersonate)
|
||||
lowLevelAPI.logger = slog.New(slog.NewTextHandler(os.Stderr, nil)).With("mock", "GoogleCalendarMockAPI", "user", userToImpersonate)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (lowLevelAPI *GoogleCalendarMockAPI) GetSetting(name string) (*calendar.Setting, error) {
|
||||
time.Sleep(latency)
|
||||
lowLevelAPI.logger.Log("msg", "GetSetting", "name", name)
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "GetSetting", "name", name)
|
||||
if name == "timezone" {
|
||||
return &calendar.Setting{
|
||||
Id: "timezone",
|
||||
@@ -59,7 +59,7 @@ func (lowLevelAPI *GoogleCalendarMockAPI) CreateEvent(event *calendar.Event) (*c
|
||||
defer mu.Unlock()
|
||||
id += 1
|
||||
event.Id = strconv.FormatUint(id, 10)
|
||||
lowLevelAPI.logger.Log("msg", "CreateEvent", "id", event.Id, "start", event.Start.DateTime)
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "CreateEvent", "id", event.Id, "start", event.Start.DateTime)
|
||||
mockEvents[event.Id] = event
|
||||
return event, nil
|
||||
}
|
||||
@@ -68,7 +68,7 @@ func (lowLevelAPI *GoogleCalendarMockAPI) UpdateEvent(event *calendar.Event) (*c
|
||||
time.Sleep(latency)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
lowLevelAPI.logger.Log("msg", "UpdateEvent", "id", event.Id, "start", event.Start.DateTime)
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "UpdateEvent", "id", event.Id, "start", event.Start.DateTime)
|
||||
mockEvents[event.Id] = event
|
||||
return event, nil
|
||||
}
|
||||
@@ -81,13 +81,13 @@ func (lowLevelAPI *GoogleCalendarMockAPI) GetEvent(id, _ string) (*calendar.Even
|
||||
if !ok {
|
||||
return nil, &googleapi.Error{Code: http.StatusNotFound}
|
||||
}
|
||||
lowLevelAPI.logger.Log("msg", "GetEvent", "id", id, "start", event.Start.DateTime)
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "GetEvent", "id", id, "start", event.Start.DateTime)
|
||||
return event, nil
|
||||
}
|
||||
|
||||
func (lowLevelAPI *GoogleCalendarMockAPI) ListEvents(string, string) (*calendar.Events, error) {
|
||||
time.Sleep(latency)
|
||||
lowLevelAPI.logger.Log("msg", "ListEvents")
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "ListEvents")
|
||||
return &calendar.Events{}, nil
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func (lowLevelAPI *GoogleCalendarMockAPI) DeleteEvent(id string) error {
|
||||
time.Sleep(latency)
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
lowLevelAPI.logger.Log("msg", "DeleteEvent", "id", id)
|
||||
lowLevelAPI.logger.InfoContext(context.TODO(), "DeleteEvent", "id", id)
|
||||
delete(mockEvents, id)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package calendar
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
@@ -10,7 +11,6 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/fleetdm/fleet/v4/server/fleet"
|
||||
"github.com/fleetdm/fleet/v4/server/platform/logging"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/api/calendar/v3"
|
||||
@@ -26,7 +26,7 @@ const (
|
||||
|
||||
var (
|
||||
baseCtx = context.Background()
|
||||
logger = logging.NewLogfmtLogger(os.Stdout)
|
||||
logger = slog.New(slog.NewTextHandler(os.Stdout, nil))
|
||||
)
|
||||
|
||||
type MockGoogleCalendarLowLevelAPI struct {
|
||||
|
||||
@@ -95,7 +95,7 @@ func (svc *Service) CalendarWebhook(ctx context.Context, eventUUID string, chann
|
||||
GoogleCalendarIntegration: *googleCalendarIntegrationConfig,
|
||||
ServerURL: appConfig.ServerSettings.ServerURL,
|
||||
}
|
||||
userCalendar := calendar.CreateUserCalendarFromConfig(ctx, localConfig, svc.logger)
|
||||
userCalendar := calendar.CreateUserCalendarFromConfig(ctx, localConfig, svc.logger.SlogLogger())
|
||||
|
||||
// Authenticate request. We will use the channel ID for authentication.
|
||||
svc.authz.SkipAuthorization(ctx)
|
||||
@@ -231,7 +231,7 @@ func (svc *Service) processCalendarEvent(ctx context.Context, eventDetails *flee
|
||||
return "", false, err
|
||||
}
|
||||
|
||||
body, generatedTag = calendar.GenerateCalendarEventBody(ctx, svc.ds, team.Name, host, &sync.Map{}, conflict, svc.logger)
|
||||
body, generatedTag = calendar.GenerateCalendarEventBody(ctx, svc.ds, team.Name, host, &sync.Map{}, conflict, svc.logger.SlogLogger())
|
||||
return body, true, nil
|
||||
}
|
||||
|
||||
@@ -443,7 +443,7 @@ func (svc *Service) processCalendarEventAsync(ctx context.Context, eventUUID str
|
||||
GoogleCalendarIntegration: *googleCalendarIntegrationConfig,
|
||||
ServerURL: appConfig.ServerSettings.ServerURL,
|
||||
}
|
||||
userCalendar := calendar.CreateUserCalendarFromConfig(ctx, localConfig, svc.logger)
|
||||
userCalendar := calendar.CreateUserCalendarFromConfig(ctx, localConfig, svc.logger.SlogLogger())
|
||||
|
||||
err = svc.processCalendarEvent(ctx, eventDetails, googleCalendarIntegrationConfig, userCalendar)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user