diff --git a/configurations/beelzebub.yaml b/configurations/beelzebub.yaml index 9bfe9eb..c479509 100644 --- a/configurations/beelzebub.yaml +++ b/configurations/beelzebub.yaml @@ -3,4 +3,7 @@ core: debug: false debugReportCaller: false logDisableTimestamp: true - logsPath: ./logs \ No newline at end of file + logsPath: ./logs + tracing: + mongoEnabled: true + mongoURI: "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 45d9f75..12a8935 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -23,7 +23,7 @@ services: restart: always container_name: mongo-express ports: - - 8081:8081 + - "8081:8081" environment: ME_CONFIG_MONGODB_ADMINUSERNAME: root ME_CONFIG_MONGODB_ADMINPASSWORD: example diff --git a/main.go b/main.go index 4506207..5e4235d 100644 --- a/main.go +++ b/main.go @@ -12,12 +12,11 @@ import ( "go.mongodb.org/mongo-driver/mongo/readpref" "io" "os" + "time" ) var quit = make(chan struct{}) -const mongoURI = "mongodb://root:example@mongo:27017/?maxPoolSize=20&w=majority" - var mongoClient *mongo.Client func main() { @@ -43,8 +42,15 @@ func main() { // Init protocol manager, with simple log on stout trace strategy and default protocol HTTP protocolManager := protocols.InitProtocolManager(traceStrategyStdout, hypertextTransferProtocolStrategy) - mongoClient = buildMongoClient(mongoURI) - defer mongoClient.Disconnect(context.TODO()) + if coreConfigurations.Core.Tracing.MongoEnabled { + mongoClient = buildMongoClient(coreConfigurations.Core.Tracing.MongoURI) + defer func(mongoClient *mongo.Client, ctx context.Context) { + err := mongoClient.Disconnect(ctx) + if err != nil { + log.Error(err) + } + }(mongoClient, context.TODO()) + } for _, beelzebubServiceConfiguration := range beelzebubServicesConfiguration { switch beelzebubServiceConfiguration.Protocol { @@ -68,19 +74,21 @@ func main() { } func traceStrategyStdout(event tracer.Event) { log.WithFields(log.Fields{ - "status": event.Status.String(), + "status": event.Status, "event": event, }).Info("New Event") - coll := mongoClient.Database("beelzebub").Collection("event") - data, err := bson.Marshal(event) - if err != nil { - log.Fatal(err) - } + if mongoClient != nil { + coll := mongoClient.Database("beelzebub").Collection("event") + data, err := bson.Marshal(event) + if err != nil { + log.Fatal(err) + } - _, err = coll.InsertOne(context.TODO(), data) - if err != nil { - log.Fatal(err) + _, err = coll.InsertOne(context.TODO(), data) + if err != nil { + log.Fatal(err) + } } } @@ -106,13 +114,13 @@ func configureLoggingByConfigurations(configurations parser.Logging) *os.File { func buildMongoClient(uri string) *mongo.Client { // Create a new client and connect to the server - client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri)) + client, err := mongo.Connect(context.TODO(), options.Client().ApplyURI(uri).SetServerSelectionTimeout(time.Second*2)) if err != nil { log.Fatal(err) } // Ping the primary if err := client.Ping(context.TODO(), readpref.Primary()); err != nil { - panic(err) + log.Fatal(err) } log.Println("Successfully connected and pinged.") return client diff --git a/parser/configurationsParser.go b/parser/configurationsParser.go index ff59d1a..43e46f0 100644 --- a/parser/configurationsParser.go +++ b/parser/configurationsParser.go @@ -12,6 +12,7 @@ import ( type BeelzebubCoreConfigurations struct { Core struct { Logging Logging `yaml:"logging"` + Tracing Tracing `yaml:"tracing"` } } @@ -22,6 +23,11 @@ type Logging struct { LogsPath string `yaml:"logsPath,omitempty"` } +type Tracing struct { + MongoEnabled bool `yaml:"mongoEnabled,omitempty"` + MongoURI string `yaml:"mongoURI,omitempty"` +} + type BeelzebubServiceConfiguration struct { ApiVersion string `yaml:"apiVersion"` Protocol string `yaml:"protocol"`