mirror of
https://github.com/coder/coder.git
synced 2026-06-04 05:28:20 +00:00
bddb808b25
Fixes all our Go file imports to match the preferred spec that we've _mostly_ been using. For example: ``` import ( "context" "time" "github.com/prometheus/client_golang/prometheus" "golang.org/x/xerrors" "gopkg.in/natefinch/lumberjack.v2" "cdr.dev/slog/v3" "github.com/coder/coder/v2/codersdk/agentsdk" "github.com/coder/serpent" ) ``` 3 groups: standard library, 3rd partly libs, Coder libs. This PR makes the change across the codebase. The PR in the stack above modifies our formatting to maintain this state of affairs, and is a separate PR so it's possible to review that one in detail.
67 lines
1.6 KiB
Go
67 lines
1.6 KiB
Go
package connectionlog
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/go-multierror"
|
|
|
|
"cdr.dev/slog/v3"
|
|
agpl "github.com/coder/coder/v2/coderd/connectionlog"
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
"github.com/coder/coder/v2/coderd/database/dbauthz"
|
|
auditbackends "github.com/coder/coder/v2/enterprise/audit/backends"
|
|
)
|
|
|
|
type Backend interface {
|
|
Upsert(ctx context.Context, clog database.UpsertConnectionLogParams) error
|
|
}
|
|
|
|
func NewConnectionLogger(backends ...Backend) agpl.ConnectionLogger {
|
|
return &connectionLogger{
|
|
backends: backends,
|
|
}
|
|
}
|
|
|
|
type connectionLogger struct {
|
|
backends []Backend
|
|
}
|
|
|
|
func (c *connectionLogger) Upsert(ctx context.Context, clog database.UpsertConnectionLogParams) error {
|
|
var errs error
|
|
for _, backend := range c.backends {
|
|
err := backend.Upsert(ctx, clog)
|
|
if err != nil {
|
|
errs = multierror.Append(errs, err)
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
type dbBackend struct {
|
|
db database.Store
|
|
}
|
|
|
|
func NewDBBackend(db database.Store) Backend {
|
|
return &dbBackend{db: db}
|
|
}
|
|
|
|
func (b *dbBackend) Upsert(ctx context.Context, clog database.UpsertConnectionLogParams) error {
|
|
//nolint:gocritic // This is the Connection Logger
|
|
_, err := b.db.UpsertConnectionLog(dbauthz.AsConnectionLogger(ctx), clog)
|
|
return err
|
|
}
|
|
|
|
type connectionSlogBackend struct {
|
|
exporter *auditbackends.SlogExporter
|
|
}
|
|
|
|
func NewSlogBackend(logger slog.Logger) Backend {
|
|
return &connectionSlogBackend{
|
|
exporter: auditbackends.NewSlogExporter(logger),
|
|
}
|
|
}
|
|
|
|
func (b *connectionSlogBackend) Upsert(ctx context.Context, clog database.UpsertConnectionLogParams) error {
|
|
return b.exporter.ExportStruct(ctx, clog, "connection_log")
|
|
}
|