diff --git a/builder/director.go b/builder/director.go index bbacf28..31d2c9a 100644 --- a/builder/director.go +++ b/builder/director.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "github.com/mariocandela/beelzebub/v3/parser" + "github.com/mariocandela/beelzebub/v3/plugins" "github.com/mariocandela/beelzebub/v3/tracer" amqp "github.com/rabbitmq/amqp091-go" @@ -37,6 +38,10 @@ func (d *Director) BuildBeelzebub(beelzebubCoreConfigurations *parser.BeelzebubC } } + if beelzebubCoreConfigurations.Core.Tracings.BeelzebubCloud.Enabled { + d.builder.setTraceStrategy(d.beelzebubCloudStrategy) + } + return d.builder.build(), nil } @@ -47,6 +52,27 @@ func (d *Director) standardOutStrategy(event tracer.Event) { }).Info("New Event") } +func (d *Director) beelzebubCloudStrategy(event tracer.Event) { + log.WithFields(log.Fields{ + "status": event.Status, + "event": event, + }).Info("New Event") + + conf := d.builder.beelzebubCoreConfigurations.Core.Tracings.BeelzebubCloud + + beelzebubCloud := plugins.InitBeelzebubCloud(conf.URI, conf.AuthToken) + + result, err := beelzebubCloud.SendEvent(event) + if err != nil { + log.Error(err.Error()) + } else { + log.WithFields(log.Fields{ + "status": result, + "event": event, + }).Debug("Event published on beelzebub cloud") + } +} + func (d *Director) rabbitMQTraceStrategy(event tracer.Event) { log.WithFields(log.Fields{ "status": event.Status, diff --git a/configurations/beelzebub.yaml b/configurations/beelzebub.yaml index c30883f..bc944ef 100644 --- a/configurations/beelzebub.yaml +++ b/configurations/beelzebub.yaml @@ -8,6 +8,10 @@ core: rabbit-mq: enabled: false uri: "" + beelzebub-cloud: + enabled: false + uri: "" + auth-token: "" prometheus: path: "/metrics" port: ":2112" diff --git a/parser/configurations_parser.go b/parser/configurations_parser.go index 67c044b..6b28272 100644 --- a/parser/configurations_parser.go +++ b/parser/configurations_parser.go @@ -30,9 +30,15 @@ type Logging struct { // Tracings is the struct that contains the configurations of the tracings type Tracings struct { - RabbitMQ `yaml:"rabbit-mq"` + RabbitMQ `yaml:"rabbit-mq"` + BeelzebubCloud `yaml:"beelzebub-cloud"` } +type BeelzebubCloud struct { + Enabled bool `yaml:"enabled"` + URI string `yaml:"uri"` + AuthToken string `yaml:"auth-token"` +} type RabbitMQ struct { Enabled bool `yaml:"enabled"` URI string `yaml:"uri"` diff --git a/parser/configurations_parser_test.go b/parser/configurations_parser_test.go index 3e150f4..fb00eb0 100644 --- a/parser/configurations_parser_test.go +++ b/parser/configurations_parser_test.go @@ -19,7 +19,11 @@ core: tracings: rabbit-mq: enabled: true - uri: "amqp://user:password@localhost/"`) + uri: "amqp://user:password@localhost/" + beelzebub-cloud: + enabled: true + uri: "amqp://user:password@localhost/" + auth-token: "iejfdjsl-aosdajosoidaj-dunfkjnfkjsdnkn"`) return configurationsCoreBytes, nil } @@ -85,6 +89,9 @@ func TestReadConfigurationsCoreValid(t *testing.T) { assert.Equal(t, coreConfigurations.Core.Logging.LogsPath, "./logs") assert.Equal(t, coreConfigurations.Core.Tracings.RabbitMQ.Enabled, true) assert.Equal(t, coreConfigurations.Core.Tracings.RabbitMQ.URI, "amqp://user:password@localhost/") + assert.Equal(t, coreConfigurations.Core.Tracings.BeelzebubCloud.Enabled, true) + assert.Equal(t, coreConfigurations.Core.Tracings.BeelzebubCloud.URI, "amqp://user:password@localhost/") + assert.Equal(t, coreConfigurations.Core.Tracings.BeelzebubCloud.AuthToken, "iejfdjsl-aosdajosoidaj-dunfkjnfkjsdnkn") } func TestReadConfigurationsServicesFail(t *testing.T) { diff --git a/plugins/beelzebub-cloud.go b/plugins/beelzebub-cloud.go new file mode 100644 index 0000000..18e9ac8 --- /dev/null +++ b/plugins/beelzebub-cloud.go @@ -0,0 +1,49 @@ +package plugins + +import ( + "encoding/json" + "errors" + "github.com/go-resty/resty/v2" + "github.com/mariocandela/beelzebub/v3/tracer" + log "github.com/sirupsen/logrus" +) + +type beelzebubCloud struct { + URI string + AuthToken string + client *resty.Client +} + +func InitBeelzebubCloud(uri, authToken string) *beelzebubCloud { + return &beelzebubCloud{ + URI: uri, + AuthToken: authToken, + client: resty.New(), + } +} + +func (beelzebubCloud *beelzebubCloud) SendEvent(event tracer.Event) (bool, error) { + requestJson, err := json.Marshal(event) + if err != nil { + return false, err + } + + if beelzebubCloud.AuthToken == "" { + return false, errors.New("authToken is empty") + } + + response, err := beelzebubCloud.client.R(). + SetHeader("Content-Type", "application/json"). + SetBody(requestJson). + SetHeader("Authorization", beelzebubCloud.AuthToken). + SetResult(&gptResponse{}). + Post(beelzebubCloud.URI) + + log.Debug(response) + + if err != nil { + return false, err + } + + return response.StatusCode() == 200, nil +} diff --git a/plugins/beelzebub-cloud_test.go b/plugins/beelzebub-cloud_test.go new file mode 100644 index 0000000..d7e5974 --- /dev/null +++ b/plugins/beelzebub-cloud_test.go @@ -0,0 +1,71 @@ +package plugins + +import ( + "github.com/go-resty/resty/v2" + "github.com/jarcoal/httpmock" + "github.com/mariocandela/beelzebub/v3/tracer" + "github.com/stretchr/testify/assert" + "net/http" + "testing" +) + +func TestBuildSendEventFailValidation(t *testing.T) { + beelzebubCloud := InitBeelzebubCloud("", "") + + _, err := beelzebubCloud.SendEvent(tracer.Event{}) + + assert.Equal(t, "authToken is empty", err.Error()) +} + +func TestBuildSendEventWithResults(t *testing.T) { + client := resty.New() + httpmock.ActivateNonDefault(client.GetClient()) + defer httpmock.DeactivateAndReset() + + uri := "localhost:8081/events" + + // Given + httpmock.RegisterResponder("POST", uri, + func(req *http.Request) (*http.Response, error) { + resp, err := httpmock.NewJsonResponse(200, &tracer.Event{}) + if err != nil { + return httpmock.NewStringResponse(500, ""), nil + } + return resp, nil + }, + ) + + beelzebubCloud := InitBeelzebubCloud(uri, "sdjdnklfjndslkjanfk") + beelzebubCloud.client = client + + //When + result, err := beelzebubCloud.SendEvent(tracer.Event{}) + + //Then + assert.Equal(t, true, result) + assert.Nil(t, err) +} + +func TestBuildSendEventErro(t *testing.T) { + client := resty.New() + httpmock.ActivateNonDefault(client.GetClient()) + defer httpmock.DeactivateAndReset() + + uri := "localhost:8081/events" + + // Given + httpmock.RegisterResponder("POST", uri, + func(req *http.Request) (*http.Response, error) { + return httpmock.NewStringResponse(500, ""), nil + }, + ) + + beelzebubCloud := InitBeelzebubCloud(uri, "sdjdnklfjndslkjanfk") + beelzebubCloud.client = client + + //When + result, _ := beelzebubCloud.SendEvent(tracer.Event{}) + + //Then + assert.Equal(t, false, result) +}