2022-12-23 22:04:13 +00:00
|
|
|
// Package logging provides logger "plugins" for various destinations.
|
2019-04-08 18:47:15 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
2021-11-22 14:13:26 +00:00
|
|
|
"fmt"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2021-02-24 18:02:26 +00:00
|
|
|
"github.com/go-kit/kit/log"
|
|
|
|
"github.com/go-kit/kit/log/level"
|
2019-04-08 18:47:15 +00:00
|
|
|
)
|
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
type FilesystemConfig struct {
|
|
|
|
LogFile string
|
|
|
|
|
|
|
|
EnableLogRotation bool
|
|
|
|
EnableLogCompression bool
|
2019-04-08 18:47:15 +00:00
|
|
|
}
|
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
type FirehoseConfig struct {
|
|
|
|
StreamName string
|
2019-04-08 18:47:15 +00:00
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
Region string
|
|
|
|
EndpointURL string
|
|
|
|
AccessKeyID string
|
|
|
|
SecretAccessKey string
|
|
|
|
StsAssumeRoleArn string
|
|
|
|
}
|
|
|
|
|
|
|
|
type KinesisConfig struct {
|
|
|
|
StreamName string
|
|
|
|
|
|
|
|
Region string
|
|
|
|
EndpointURL string
|
|
|
|
AccessKeyID string
|
|
|
|
SecretAccessKey string
|
|
|
|
StsAssumeRoleArn string
|
|
|
|
}
|
|
|
|
|
|
|
|
type LambdaConfig struct {
|
|
|
|
Function string
|
|
|
|
|
|
|
|
Region string
|
|
|
|
AccessKeyID string
|
|
|
|
SecretAccessKey string
|
|
|
|
StsAssumeRoleArn string
|
|
|
|
}
|
|
|
|
|
|
|
|
type PubSubConfig struct {
|
|
|
|
Topic string
|
2019-04-08 18:47:15 +00:00
|
|
|
|
2022-12-23 22:04:13 +00:00
|
|
|
Project string
|
|
|
|
AddAttributes bool
|
|
|
|
}
|
|
|
|
|
|
|
|
type KafkaRESTConfig struct {
|
|
|
|
Topic string
|
|
|
|
|
|
|
|
ProxyHost string
|
|
|
|
ContentTypeValue string
|
|
|
|
Timeout int
|
|
|
|
}
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
Plugin string
|
|
|
|
|
|
|
|
Filesystem FilesystemConfig
|
|
|
|
Firehose FirehoseConfig
|
|
|
|
Kinesis KinesisConfig
|
|
|
|
Lambda LambdaConfig
|
|
|
|
PubSub PubSubConfig
|
|
|
|
KafkaREST KafkaRESTConfig
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewJSONLogger(name string, config Config, logger log.Logger) (fleet.JSONLogger, error) {
|
|
|
|
switch config.Plugin {
|
2019-04-08 18:47:15 +00:00
|
|
|
case "":
|
|
|
|
// Allow "" to mean filesystem for backwards compatibility
|
2022-12-23 22:04:13 +00:00
|
|
|
level.Info(logger).Log(
|
|
|
|
"msg",
|
|
|
|
fmt.Sprintf("plugin for %s not explicitly specified. Assuming 'filesystem'", name),
|
|
|
|
)
|
2019-04-08 18:47:15 +00:00
|
|
|
fallthrough
|
|
|
|
case "filesystem":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewFilesystemLogWriter(
|
|
|
|
config.Filesystem.LogFile,
|
2019-04-08 18:47:15 +00:00
|
|
|
logger,
|
|
|
|
config.Filesystem.EnableLogRotation,
|
2020-09-09 20:33:32 +00:00
|
|
|
config.Filesystem.EnableLogCompression,
|
2019-04-08 18:47:15 +00:00
|
|
|
)
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create filesystem %s logger: %w", name, err)
|
2019-04-08 18:47:15 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2019-04-08 18:47:15 +00:00
|
|
|
case "firehose":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewFirehoseLogWriter(
|
2019-04-08 18:47:15 +00:00
|
|
|
config.Firehose.Region,
|
2021-07-30 15:45:49 +00:00
|
|
|
config.Firehose.EndpointURL,
|
2019-04-08 18:47:15 +00:00
|
|
|
config.Firehose.AccessKeyID,
|
|
|
|
config.Firehose.SecretAccessKey,
|
2022-05-10 15:27:32 +00:00
|
|
|
config.Firehose.StsAssumeRoleArn,
|
2022-12-23 22:04:13 +00:00
|
|
|
config.Firehose.StreamName,
|
2019-04-08 18:47:15 +00:00
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create firehose %s logger: %w", name, err)
|
2019-04-08 18:47:15 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2020-08-19 21:56:44 +00:00
|
|
|
case "kinesis":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewKinesisLogWriter(
|
2020-08-19 21:56:44 +00:00
|
|
|
config.Kinesis.Region,
|
2021-07-30 15:45:49 +00:00
|
|
|
config.Kinesis.EndpointURL,
|
2020-08-19 21:56:44 +00:00
|
|
|
config.Kinesis.AccessKeyID,
|
|
|
|
config.Kinesis.SecretAccessKey,
|
|
|
|
config.Kinesis.StsAssumeRoleArn,
|
2022-12-23 22:04:13 +00:00
|
|
|
config.Kinesis.StreamName,
|
2020-08-19 21:56:44 +00:00
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create kinesis %s logger: %w", name, err)
|
2020-08-19 21:56:44 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2021-02-24 18:02:26 +00:00
|
|
|
case "lambda":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewLambdaLogWriter(
|
2021-02-24 18:02:26 +00:00
|
|
|
config.Lambda.Region,
|
|
|
|
config.Lambda.AccessKeyID,
|
|
|
|
config.Lambda.SecretAccessKey,
|
|
|
|
config.Lambda.StsAssumeRoleArn,
|
2022-12-23 22:04:13 +00:00
|
|
|
config.Lambda.Function,
|
2021-02-24 18:02:26 +00:00
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create lambda %s logger: %w", name, err)
|
2021-02-24 18:02:26 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2019-07-16 22:41:50 +00:00
|
|
|
case "pubsub":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewPubSubLogWriter(
|
2019-07-16 22:41:50 +00:00
|
|
|
config.PubSub.Project,
|
2022-12-23 22:04:13 +00:00
|
|
|
config.PubSub.Topic,
|
2021-05-08 19:29:52 +00:00
|
|
|
config.PubSub.AddAttributes,
|
2019-07-16 22:41:50 +00:00
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create pubsub %s logger: %w", name, err)
|
2019-07-16 22:41:50 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2020-08-19 21:56:44 +00:00
|
|
|
case "stdout":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewStdoutLogWriter()
|
2020-08-19 21:56:44 +00:00
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create stdout %s logger: %w", name, err)
|
2020-08-19 21:56:44 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2021-10-28 04:51:17 +00:00
|
|
|
case "kafkarest":
|
2022-12-23 22:04:13 +00:00
|
|
|
writer, err := NewKafkaRESTWriter(&KafkaRESTParams{
|
2022-03-09 22:22:29 +00:00
|
|
|
KafkaProxyHost: config.KafkaREST.ProxyHost,
|
2022-12-23 22:04:13 +00:00
|
|
|
KafkaTopic: config.KafkaREST.Topic,
|
2022-03-09 22:22:29 +00:00
|
|
|
KafkaContentTypeValue: config.KafkaREST.ContentTypeValue,
|
|
|
|
KafkaTimeout: config.KafkaREST.Timeout,
|
2021-10-28 04:51:17 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
2022-12-23 22:04:13 +00:00
|
|
|
return nil, fmt.Errorf("create kafka rest %s logger: %w", name, err)
|
2021-10-28 04:51:17 +00:00
|
|
|
}
|
2022-12-23 22:04:13 +00:00
|
|
|
return fleet.JSONLogger(writer), nil
|
2019-04-08 18:47:15 +00:00
|
|
|
default:
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf(
|
2022-12-23 22:04:13 +00:00
|
|
|
"unknown %s log plugin: %s", name, config.Plugin,
|
2019-04-08 18:47:15 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|