mirror of
https://github.com/mariocandela/beelzebub.git
synced 2025-07-01 18:47:26 -04:00
feat: Beelzebub cloud tracer plugin (#92)
This commit is contained in:
@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"github.com/mariocandela/beelzebub/v3/parser"
|
"github.com/mariocandela/beelzebub/v3/parser"
|
||||||
|
"github.com/mariocandela/beelzebub/v3/plugins"
|
||||||
"github.com/mariocandela/beelzebub/v3/tracer"
|
"github.com/mariocandela/beelzebub/v3/tracer"
|
||||||
|
|
||||||
amqp "github.com/rabbitmq/amqp091-go"
|
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
|
return d.builder.build(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -47,6 +52,27 @@ func (d *Director) standardOutStrategy(event tracer.Event) {
|
|||||||
}).Info("New 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) {
|
func (d *Director) rabbitMQTraceStrategy(event tracer.Event) {
|
||||||
log.WithFields(log.Fields{
|
log.WithFields(log.Fields{
|
||||||
"status": event.Status,
|
"status": event.Status,
|
||||||
|
@ -8,6 +8,10 @@ core:
|
|||||||
rabbit-mq:
|
rabbit-mq:
|
||||||
enabled: false
|
enabled: false
|
||||||
uri: ""
|
uri: ""
|
||||||
|
beelzebub-cloud:
|
||||||
|
enabled: false
|
||||||
|
uri: ""
|
||||||
|
auth-token: ""
|
||||||
prometheus:
|
prometheus:
|
||||||
path: "/metrics"
|
path: "/metrics"
|
||||||
port: ":2112"
|
port: ":2112"
|
||||||
|
@ -30,9 +30,15 @@ type Logging struct {
|
|||||||
|
|
||||||
// Tracings is the struct that contains the configurations of the tracings
|
// Tracings is the struct that contains the configurations of the tracings
|
||||||
type Tracings struct {
|
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 {
|
type RabbitMQ struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
URI string `yaml:"uri"`
|
URI string `yaml:"uri"`
|
||||||
|
@ -19,7 +19,11 @@ core:
|
|||||||
tracings:
|
tracings:
|
||||||
rabbit-mq:
|
rabbit-mq:
|
||||||
enabled: true
|
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
|
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.Logging.LogsPath, "./logs")
|
||||||
assert.Equal(t, coreConfigurations.Core.Tracings.RabbitMQ.Enabled, true)
|
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.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) {
|
func TestReadConfigurationsServicesFail(t *testing.T) {
|
||||||
|
49
plugins/beelzebub-cloud.go
Normal file
49
plugins/beelzebub-cloud.go
Normal file
@ -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
|
||||||
|
}
|
71
plugins/beelzebub-cloud_test.go
Normal file
71
plugins/beelzebub-cloud_test.go
Normal file
@ -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)
|
||||||
|
}
|
Reference in New Issue
Block a user