func TestThatItIgnoresBadMessages(t *testing.T) {
	listener, dataChannel := agentlistener.NewAgentListener("localhost:9996", logger)
	go listener.Start()

	loggregatorServers := []string{"localhost:9996"}
	hasher := hasher.NewHasher(loggregatorServers)
	r, err := NewRouter("localhost:3455", hasher, newCfConfig(), logger)
	assert.NoError(t, err)

	go r.Start(logger)
	time.Sleep(50 * time.Millisecond)

	lc := loggregatorclient.NewLoggregatorClient("localhost:3455", logger, loggregatorclient.DefaultBufferSize)
	lc.Send([]byte("This is poorly formatted"))

	logEmitter, _ := emitter.NewEmitter("localhost:3455", "ROUTER", "42", "secret", logger)
	logEmitter.Emit("my_awesome_app", "Hello World")

	received := <-dataChannel
	receivedEnvelope := &logmessage.LogEnvelope{}
	proto.Unmarshal(received, receivedEnvelope)

	assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "my_awesome_app")
	assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "Hello World")
}
Beispiel #2
0
func NewRouter(host string, h *hasher, config cfcomponent.Config, logger *gosteno.Logger) (r *router, err error) {
	var instrumentables []instrumentation.Instrumentable
	loggregatorClients := make(map[string]loggregatorclient.LoggregatorClient, len(h.loggregatorServers()))

	for _, server := range h.loggregatorServers() {
		client := loggregatorclient.NewLoggregatorClient(server, logger, loggregatorclient.DefaultBufferSize)
		loggregatorClients[server] = client
		instrumentables = append(instrumentables, client)
	}

	al := agentlistener.NewAgentListener(host, logger)
	instrumentables = append(instrumentables, al)

	cfc, err := cfcomponent.NewComponent(
		0,
		"LoggregatorRouter",
		0,
		&LoggregatorRouterMonitor{},
		config.VarzPort,
		[]string{config.VarzUser, config.VarzPass},
		instrumentables,
	)

	if err != nil {
		return
	}

	r = &router{Component: cfc, h: h, lcs: loggregatorClients, agentListener: al, host: host}

	return
}
Beispiel #3
0
func NewRouter(host string, hasher *hasher.Hasher, config cfcomponent.Config, logger *gosteno.Logger) (r *Router, err error) {
	var instrumentables []instrumentation.Instrumentable
	servers := hasher.LoggregatorServers()
	loggregatorClients := make(map[string]loggregatorclient.LoggregatorClient, len(servers))

	for _, server := range servers {
		client := loggregatorclient.NewLoggregatorClient(server, logger, loggregatorclient.DefaultBufferSize)
		loggregatorClients[server] = client
		instrumentables = append(instrumentables, client)
	}

	agentListener, dataChan := agentlistener.NewAgentListener(host, logger)
	instrumentables = append(instrumentables, agentListener)

	cfc, err := cfcomponent.NewComponent(
		logger,
		"LoggregatorTrafficcontroller",
		0,
		&TrafficControllerMonitor{},
		config.VarzPort,
		[]string{config.VarzUser, config.VarzPass},
		instrumentables,
	)

	if err != nil {
		return
	}

	r = &Router{Component: cfc, hasher: hasher, loggregatorClients: loggregatorClients, agentListener: agentListener, dataChan: dataChan, host: host}

	return
}
Beispiel #4
0
func NewEmitter(loggregatorServer, sourceName, sourceId, sharedSecret string, logger *gosteno.Logger) (*LoggregatorEmitter, error) {
	if logger == nil {
		logger = gosteno.NewLogger("loggregatorlib.emitter")
	}

	e := &LoggregatorEmitter{sharedSecret: sharedSecret}

	e.sn = sourceName
	e.logger = logger
	e.LoggregatorClient = loggregatorclient.NewLoggregatorClient(loggregatorServer, logger, loggregatorclient.DefaultBufferSize)
	e.sId = sourceId

	e.logger.Debugf("Created new loggregator emitter: %#v", e)
	return e, nil
}
Beispiel #5
0
func NewEmitter(loggregatorServer, sourceName, sourceId string, debug bool) (*LoggregatorEmitter, error) {
	// TODO: delete when "legacy" format goes away
	sharedSecret := os.Getenv("LOGGREGATOR_SHARED_SECRET")
	if sharedSecret == "" {
		return nil, ERR_SHARED_SECRET_NOT_SET
	}

	e := &LoggregatorEmitter{sharedSecret: sharedSecret}

	e.sn = sourceName
	e.logger = generic_logger.NewDefaultGenericLogger(debug)
	e.LoggregatorClient = loggregatorclient.NewLoggregatorClient(loggregatorServer, e.logger, loggregatorclient.DefaultBufferSize)
	e.sId = sourceId

	e.logger.Debugf("Created new loggregator emitter: %#v", e)
	return e, nil
}
	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
)

var _ = Describe("loggregatorclient", func() {
	var (
		fakeMetricSender  *fake.FakeMetricSender
		loggregatorClient loggregatorclient.LoggregatorClient
		udpListener       *net.UDPConn
	)

	BeforeEach(func() {
		fakeMetricSender = fake.NewFakeMetricSender()
		metrics.Initialize(fakeMetricSender)

		loggregatorClient = loggregatorclient.NewLoggregatorClient("localhost:9875", gosteno.NewLogger("TestLogger"), 0)

		udpAddr, _ := net.ResolveUDPAddr("udp", "localhost:9875")
		udpListener, _ = net.ListenUDP("udp", udpAddr)
	})

	AfterEach(func() {
		loggregatorClient.Stop()
		udpListener.Close()
	})

	It("sends log messages to loggregator", func() {
		expectedOutput := []byte("Important Testmessage")

		loggregatorClient.Send(expectedOutput)
Beispiel #7
0
func main() {
	flag.Parse()

	if *version {
		fmt.Printf("\n\nversion: %s\ngitSha: %s\n\n", versionNumber, gitSha)
		return
	}

	logger := cfcomponent.NewLogger(*logLevel, *logFilePath, "deaagent")

	// ** Config Setup
	config := &Config{}
	err := cfcomponent.ReadConfigInto(config, *configFile)
	if err != nil {
		panic(err)
	}

	err = config.validate(logger)
	if err != nil {
		panic(err)
	}

	// ** END Config Setup

	loggregatorClient := loggregatorclient.NewLoggregatorClient(config.LoggregatorAddress, logger, 4096)

	agent := deaagent.NewAgent(*instancesJsonFilePath, logger)

	cfc, err := cfcomponent.NewComponent(
		0,
		"LoggregatorDeaAgent",
		config.Index,
		&DeaAgentHealthMonitor{},
		config.VarzPort,
		[]string{config.VarzUser, config.VarzPass},
		[]instrumentation.Instrumentable{loggregatorClient},
	)

	if err != nil {
		panic(err)
	}

	cr := collectorregistrar.NewCollectorRegistrar(config.MbusClient, logger)
	err = cr.RegisterWithCollector(cfc)
	if err != nil {
		panic(err)
	}

	go func() {
		err := cfc.StartMonitoringEndpoints()
		if err != nil {
			panic(err)
		}
	}()
	go agent.Start(loggregatorClient)

	for {
		select {
		case <-cfcomponent.RegisterGoRoutineDumpSignalChannel():
			cfcomponent.DumpGoRoutine()
		}
	}
}