mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
<!-- 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 -->
105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package logging
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log/slog"
|
|
"time"
|
|
|
|
"cloud.google.com/go/pubsub"
|
|
"github.com/fleetdm/fleet/v4/server/contexts/ctxerr"
|
|
)
|
|
|
|
type pubSubLogWriter struct {
|
|
topic *pubsub.Topic
|
|
logger *slog.Logger
|
|
addAttributes bool
|
|
}
|
|
|
|
type PubSubAttributes struct {
|
|
Name string `json:"name"`
|
|
UnixTime int64 `json:"unixTime"`
|
|
Decorations map[string]string `json:"decorations"`
|
|
}
|
|
|
|
func NewPubSubLogWriter(ctx context.Context, projectId string, topicName string, addAttributes bool, logger *slog.Logger) (*pubSubLogWriter, error) {
|
|
client, err := pubsub.NewClient(ctx, projectId)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create pubsub client: %w", err)
|
|
}
|
|
|
|
topic := client.Topic(topicName)
|
|
|
|
logger.InfoContext(ctx, "GCP PubSub writer configured",
|
|
"project", projectId,
|
|
"topic", topicName,
|
|
"add_attributes", addAttributes,
|
|
)
|
|
|
|
return &pubSubLogWriter{
|
|
topic: topic,
|
|
logger: logger,
|
|
addAttributes: addAttributes,
|
|
}, nil
|
|
}
|
|
|
|
func estimateAttributeSize(attributes map[string]string) int {
|
|
var sz int
|
|
for k, v := range attributes {
|
|
sz += len(k) + len(v) + 2
|
|
}
|
|
return sz
|
|
}
|
|
|
|
func (w *pubSubLogWriter) Write(ctx context.Context, logs []json.RawMessage) error {
|
|
results := make([]*pubsub.PublishResult, len(logs))
|
|
|
|
// Add all of the messages to the global pubsub queue
|
|
for i, log := range logs {
|
|
data, err := log.MarshalJSON()
|
|
if err != nil {
|
|
return ctxerr.Wrap(ctx, err, "marshal message into JSON")
|
|
}
|
|
|
|
attributes := make(map[string]string)
|
|
|
|
if w.addAttributes {
|
|
var unmarshaled PubSubAttributes
|
|
|
|
if err := json.Unmarshal(log, &unmarshaled); err != nil {
|
|
return ctxerr.Wrap(ctx, err, "unmarshalling log message JSON")
|
|
}
|
|
attributes["name"] = unmarshaled.Name
|
|
attributes["timestamp"] = time.Unix(unmarshaled.UnixTime, 0).Format(time.RFC3339)
|
|
for k, v := range unmarshaled.Decorations {
|
|
attributes[k] = v
|
|
}
|
|
}
|
|
|
|
if len(data)+estimateAttributeSize(attributes) > pubsub.MaxPublishRequestBytes {
|
|
w.logger.InfoContext(ctx, "dropping log over 10MB PubSub limit",
|
|
"size", len(data),
|
|
"log", string(log[:100])+"...",
|
|
)
|
|
continue
|
|
}
|
|
|
|
message := &pubsub.Message{
|
|
Data: data,
|
|
Attributes: attributes,
|
|
}
|
|
|
|
results[i] = w.topic.Publish(ctx, message)
|
|
}
|
|
|
|
// Wait for each message to be pushed to the server
|
|
for _, result := range results {
|
|
_, err := result.Get(ctx)
|
|
if err != nil {
|
|
return ctxerr.Wrap(ctx, err, "pubsub publish")
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|