func BenchmarkLogEnvelopeEmit(b *testing.B) { received := make(chan *[]byte, 1) e, _ := emitter.NewEmitter("localhost:3457", "ROUTER", "42", "secret", nil) e.LoggregatorClient = &fake.FakeLoggregatorClient{Received: received} testEmitHelper(b, e, received, true) }
func TestThatItWorksWithTwoLoggregators(t *testing.T) { listener1, dataChan1 := agentlistener.NewAgentListener("localhost:9998", logger) go listener1.Start() listener2, dataChan2 := agentlistener.NewAgentListener("localhost:9997", logger) go listener2.Start() loggregatorServers := []string{"localhost:9998", "localhost:9997"} hasher := hasher.NewHasher(loggregatorServers) rt, err := NewRouter("localhost:3457", hasher, newCfConfig(), logger) assert.NoError(t, err) go rt.Start(logger) time.Sleep(50 * time.Millisecond) logEmitter, _ := emitter.NewEmitter("localhost:3457", "ROUTER", "42", "secret", logger) logEmitter.Emit("2", "My message") receivedData := <-dataChan1 receivedEnvelope := &logmessage.LogEnvelope{} proto.Unmarshal(receivedData, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "2") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "My message") logEmitter.Emit("1", "Another message") receivedData = <-dataChan2 receivedEnvelope = &logmessage.LogEnvelope{} proto.Unmarshal(receivedData, receivedEnvelope) assert.Equal(t, receivedEnvelope.GetLogMessage().GetAppId(), "1") assert.Equal(t, string(receivedEnvelope.GetLogMessage().GetMessage()), "Another message") }
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") }
func NewEmitter(loggregatorUrl, loggregatorSharedSecret string, index uint) (emitter.Emitter, error) { if !isValidUrl(loggregatorUrl) { return nil, fmt.Errorf("Invalid loggregator url %s", loggregatorUrl) } return emitter.NewEmitter(loggregatorUrl, "RTR", strconv.FormatUint(uint64(index), 10), loggregatorSharedSecret, steno.NewLogger("router.loggregator")) }
func NewFileAndLoggregatorAccessLogger(f io.Writer, loggregatorUrl, loggregatorSharedSecret string, index uint) *FileAndLoggregatorAccessLogger { a := &FileAndLoggregatorAccessLogger{ writer: f, channel: make(chan AccessLogRecord, 128), index: index, } if isValidUrl(loggregatorUrl) { a.emitter, _ = emitter.NewEmitter(loggregatorUrl, "RTR", strconv.FormatUint(uint64(index), 10), loggregatorSharedSecret, steno.NewLogger("router.loggregator")) } else { log.Errorf("Invalid loggregator url %s", loggregatorUrl) } return a }
func TestThatItWorksWithOneLoggregator(t *testing.T) { listener, dataChannel := agentlistener.NewAgentListener("localhost:9999", logger) go listener.Start() loggregatorServers := []string{"localhost:9999"} hasher := hasher.NewHasher(loggregatorServers) r, err := NewRouter("localhost:3456", hasher, newCfConfig(), logger) assert.NoError(t, err) go r.Start(logger) time.Sleep(50 * time.Millisecond) logEmitter, _ := emitter.NewEmitter("localhost:3456", "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") }
func main() { flag.Parse() if *version { fmt.Printf("version: %s\ngitSha: %s\nsourceUrl: https://github.com/cloudfoundry/loggregator/tree/%s\n\n", versionNumber, gitSha, gitSha) return } // ** Config Setup config := &Config{} err := cfcomponent.ReadConfigInto(config, *configFile) if err != nil { panic(err) } logger := cfcomponent.NewLogger(*logLevel, *logFilePath, "deaagent", config.Config) logger.Info("Startup: Setting up the loggregator dea logging agent") err = config.validate(logger) if err != nil { panic(err) } // ** END Config Setup loggregatorEmitter, err := emitter.NewEmitter(config.LoggregatorAddress, "APP", "NA", config.SharedSecret, logger) if err != nil { panic(err) } agent := deaagent.NewAgent(*instancesJsonFilePath, logger) cfc, err := cfcomponent.NewComponent( logger, "LoggregatorDeaAgent", config.Index, &DeaAgentHealthMonitor{}, config.VarzPort, []string{config.VarzUser, config.VarzPass}, []instrumentation.Instrumentable{loggregatorEmitter.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(loggregatorEmitter) for { select { case <-cfcomponent.RegisterGoRoutineDumpSignalChannel(): cfcomponent.DumpGoRoutine() } } }
func main() { flag.Parse() if *version { fmt.Printf("version: %s\ngitSha: %s\nsourceUrl: https://github.com/cloudfoundry/loggregator/tree/%s\n\n", versionNumber, gitSha, gitSha) return } // ** Config Setup config := &Config{} err := cfcomponent.ReadConfigInto(config, *configFile) if err != nil { panic(err) } if *cpuprofile != "" { f, err := os.Create(*cpuprofile) if err != nil { panic(err) } pprof.StartCPUProfile(f) defer func() { pprof.StopCPUProfile() f.Close() }() } if *memprofile != "" { f, err := os.Create(*memprofile) if err != nil { panic(err) } go func() { defer f.Close() ticker := time.NewTicker(time.Second * 1) defer ticker.Stop() for { <-ticker.C pprof.WriteHeapProfile(f) } }() } logger := cfcomponent.NewLogger(*logLevel, *logFilePath, "deaagent", config.Config) logger.Info("Startup: Setting up the loggregator dea logging agent") err = config.validate(logger) if err != nil { panic(err) } // ** END Config Setup loggregatorEmitter, err := emitter.NewEmitter(config.LoggregatorAddress, "APP", "NA", config.SharedSecret, logger) if err != nil { panic(err) } agent := deaagent.NewAgent(*instancesJsonFilePath, logger) cfc, err := cfcomponent.NewComponent( logger, "LoggregatorDeaAgent", config.Index, &DeaAgentHealthMonitor{}, config.VarzPort, []string{config.VarzUser, config.VarzPass}, []instrumentation.Instrumentable{loggregatorEmitter.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(loggregatorEmitter) killChan := make(chan os.Signal) signal.Notify(killChan, os.Kill, os.Interrupt) for { select { case <-cfcomponent.RegisterGoRoutineDumpSignalChannel(): cfcomponent.DumpGoRoutine() case <-killChan: logger.Info("Shutting down") return } } }