Exemple #1
0
func NewRouter(cfg *config.Config, p proxy.Proxy, mbusClient yagnats.NATSConn, r *registry.RouteRegistry,
	v varz.Varz, logCounter *vcap.LogCounter, errChan chan error) (*Router, error) {

	var host string
	if cfg.Status.Port != 0 {
		host = fmt.Sprintf("%s:%d", cfg.Ip, cfg.Status.Port)
	}

	varz := &vcap.Varz{
		UniqueVarz: v,
		GenericVarz: vcap.GenericVarz{
			Type:        "Router",
			Index:       cfg.Index,
			Host:        host,
			Credentials: []string{cfg.Status.User, cfg.Status.Pass},
			LogCounts:   logCounter,
		},
	}

	healthz := &vcap.Healthz{}

	component := &vcap.VcapComponent{
		Config:  cfg,
		Varz:    varz,
		Healthz: healthz,
		InfoRoutes: map[string]json.Marshaler{
			"/routes": r,
		},
		Logger: steno.NewLogger("common.logger"),
	}

	routerErrChan := errChan
	if routerErrChan == nil {
		routerErrChan = make(chan error, 2)
	}

	router := &Router{
		config:       cfg,
		proxy:        p,
		mbusClient:   mbusClient,
		registry:     r,
		varz:         v,
		component:    component,
		serveDone:    make(chan struct{}),
		tlsServeDone: make(chan struct{}),
		idleConns:    make(map[net.Conn]struct{}),
		activeConns:  make(map[net.Conn]struct{}),
		logger:       steno.NewLogger("router"),
		errChan:      routerErrChan,
		stopping:     false,
	}

	if err := router.component.Start(); err != nil {
		return nil, err
	}

	return router, nil
}
Exemple #2
0
func TestDontSendEmptyData(t *testing.T) {
	bufferSize := 4096
	loggregatorClient := l.NewLoggregatorClient("localhost:9876", gosteno.NewLogger("TestLogger"), bufferSize)

	udpAddr, err := net.ResolveUDPAddr("udp", "localhost:9876")
	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	udpListener, err := net.ListenUDP("udp", udpAddr)
	defer udpListener.Close()
	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	firstMessage := []byte("")
	secondMessage := []byte("hi")
	loggregatorClient.Send(firstMessage)
	loggregatorClient.Send(secondMessage)

	buffer := make([]byte, bufferSize)
	readCount, _, err := udpListener.ReadFromUDP(buffer)

	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	received := string(buffer[:readCount])
	if string(secondMessage) != received {
		t.Errorf("Expected %s to equal %s", string(secondMessage), received)
	}
}
func NewAtkListService(cloudController CloudController, SpaceSummaryHelper SpaceSummaryHelper) *AtkListService {
	return &AtkListService{
		cloudController:    cloudController,
		SpaceSummaryHelper: SpaceSummaryHelper,
		logger:             gosteno.NewLogger("atk_list_service"),
	}
}
Exemple #4
0
func main() {
	flag.Parse()

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

	level := gosteno.LOG_INFO

	if *logLevel {
		level = gosteno.LOG_DEBUG
	}

	loggingConfig := &gosteno.Config{
		Sinks:     make([]gosteno.Sink, 1),
		Level:     level,
		Codec:     gosteno.NewJsonCodec(),
		EnableLOC: true}
	if strings.TrimSpace(*logFilePath) == "" {
		loggingConfig.Sinks[0] = gosteno.NewIOSink(os.Stdout)
	} else {
		loggingConfig.Sinks[0] = gosteno.NewFileSink(*logFilePath)
	}
	gosteno.Init(loggingConfig)
	logger := gosteno.NewLogger("deaagent")

	loggregatorClient := loggregatorclient.NewLoggregatorClient(*loggregatorAddress, logger, 4096)

	agent := deaagent.NewAgent(*instancesJsonFilePath, logger)
	agent.Start(loggregatorClient)
}
Exemple #5
0
func NewProxy(args ProxyArgs) Proxy {
	return &proxy{
		accessLogger: args.AccessLogger,
		traceKey:     args.TraceKey,
		ip:           args.Ip,
		logger:       steno.NewLogger("router.proxy"),
		registry:     args.Registry,
		reporter:     args.Reporter,
		transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				conn, err := net.DialTimeout(network, addr, 5*time.Second)
				if err != nil {
					return conn, err
				}
				if args.EndpointTimeout > 0 {
					err = conn.SetDeadline(time.Now().Add(args.EndpointTimeout))
				}
				return conn, err
			},
			DisableKeepAlives:     true,
			ResponseHeaderTimeout: args.EndpointTimeout,
		},
		waitgroup: &sync.WaitGroup{},
	}
}
Exemple #6
0
func NewProxy(args ProxyArgs) Proxy {
	p := &proxy{
		accessLogger: args.AccessLogger,
		traceKey:     args.TraceKey,
		ip:           args.Ip,
		logger:       steno.NewLogger("router.proxy"),
		registry:     args.Registry,
		reporter:     args.Reporter,
		transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				conn, err := net.DialTimeout(network, addr, 5*time.Second)
				if err != nil {
					return conn, err
				}
				if args.EndpointTimeout > 0 {
					err = conn.SetDeadline(time.Now().Add(args.EndpointTimeout))
				}
				return conn, err
			},
			DisableKeepAlives:  true,
			DisableCompression: true,
		},
		secureCookies: args.SecureCookies,
	}
	return p
}
func CreateRunningAccessLogger(config *config.Config) (AccessLogger, error) {
	loggregatorUrl := config.LoggregatorConfig.Url

	if config.AccessLog == "" && loggregatorUrl == "" {
		return &NullAccessLogger{}, nil
	}

	logger := steno.NewLogger("access_log")

	var err error
	var file *os.File
	if config.AccessLog != "" {
		file, err = os.OpenFile(config.AccessLog, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
		if err != nil {
			logger.Errorf("Error creating accesslog file, %s: (%s)", config.AccessLog, err.Error())
			return nil, err
		}
	}

	var e emitter.Emitter
	if loggregatorUrl != "" {
		loggregatorSharedSecret := config.LoggregatorConfig.SharedSecret
		e, err = NewEmitter(loggregatorUrl, loggregatorSharedSecret, config.Index)
		if err != nil {
			logger.Errorf("Error creating loggregator emitter: (%s)", err.Error())
			return nil, err
		}
	}

	accessLogger := NewFileAndLoggregatorAccessLogger(file, e)
	go accessLogger.Run()
	return accessLogger, nil
}
func CreateRunningAccessLogger(config *config.Config) (AccessLogger, error) {

	if config.AccessLog == "" && !config.Logging.LoggregatorEnabled {
		return &NullAccessLogger{}, nil
	}

	logger := steno.NewLogger("access_log")

	var err error
	var file *os.File
	if config.AccessLog != "" {
		file, err = os.OpenFile(config.AccessLog, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
		if err != nil {
			logger.Errorf("Error creating accesslog file, %s: (%s)", config.AccessLog, err.Error())
			return nil, err
		}
	}

	var dropsondeSourceInstance string
	if config.Logging.LoggregatorEnabled {
		dropsondeSourceInstance = strconv.FormatUint(uint64(config.Index), 10)
	}

	accessLogger := NewFileAndLoggregatorAccessLogger(file, dropsondeSourceInstance)
	go accessLogger.Run()
	return accessLogger, nil
}
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"))
}
Exemple #10
0
func main() {
	flag.Parse()

	level, err := gosteno.GetLogLevel(*logLevel)
	if err != nil {
		level = gosteno.LOG_INFO
	}
	loggingConfig := &gosteno.Config{
		Sinks: []gosteno.Sink{
			gosteno.NewIOSink(os.Stdout),
		},
		Level:     level,
		Codec:     gosteno.NewJsonCodec(),
		EnableLOC: true,
	}

	gosteno.Init(loggingConfig)
	logger := gosteno.NewLogger("statsdinjector")

	statsdMessageListener := statsdlistener.New(*statsdPort, logger)
	statsdEmitter := statsdemitter.New(*metronPort, logger)

	inputChan := make(chan *events.Envelope)

	go statsdMessageListener.Run(inputChan)
	statsdEmitter.Run(inputChan)
}
Exemple #11
0
func (c *Config) Process() {
	var err error

	c.PruneStaleDropletsInterval = time.Duration(c.PruneStaleDropletsIntervalInSeconds) * time.Second
	c.DropletStaleThreshold = time.Duration(c.DropletStaleThresholdInSeconds) * time.Second
	c.PublishActiveAppsInterval = time.Duration(c.PublishActiveAppsIntervalInSeconds) * time.Second
	c.StartResponseDelayInterval = time.Duration(c.StartResponseDelayIntervalInSeconds) * time.Second
	c.EndpointTimeout = time.Duration(c.EndpointTimeoutInSeconds) * time.Second

	if c.StartResponseDelayInterval > c.DropletStaleThreshold {
		c.DropletStaleThreshold = c.StartResponseDelayInterval
		log := steno.NewLogger("config.logger")
		log.Warnf("DropletStaleThreshold (%s) cannot be less than StartResponseDelayInterval (%s); setting both equal to StartResponseDelayInterval and continuing", c.DropletStaleThreshold, c.StartResponseDelayInterval)
	}

	drain := c.DrainTimeoutInSeconds
	if drain == 0 {
		drain = c.EndpointTimeoutInSeconds
	}
	c.DrainTimeout = time.Duration(drain) * time.Second

	c.Ip, err = vcap.LocalIP()
	if err != nil {
		panic(err)
	}
}
Exemple #12
0
func main() {
	flag.Parse()

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

	level := gosteno.LOG_INFO

	if *logLevel {
		level = gosteno.LOG_DEBUG
	}

	loggingConfig := &gosteno.Config{
		Sinks:     make([]gosteno.Sink, 1),
		Level:     level,
		Codec:     gosteno.NewJsonCodec(),
		EnableLOC: true}

	if strings.TrimSpace(*logFilePath) == "" {
		loggingConfig.Sinks[0] = gosteno.NewIOSink(os.Stdout)
	} else {
		loggingConfig.Sinks[0] = gosteno.NewFileSink(*logFilePath)
	}
	gosteno.Init(loggingConfig)
	logger := gosteno.NewLogger("loggregator")

	listener := loggregator.NewAgentListener(*host, logger)
	incomingData := listener.Start()

	cfSink := loggregator.NewCfSink(incomingData, logger)
	cfSink.Start()
}
Exemple #13
0
func NewLogger(verbose bool, logFilePath, name string, syslogNamespace string) *gosteno.Logger {
	level := gosteno.LOG_INFO

	if verbose {
		level = gosteno.LOG_DEBUG
	}

	loggingConfig := &gosteno.Config{
		Sinks:     make([]gosteno.Sink, 1),
		Level:     level,
		Codec:     gosteno.NewJsonCodec(),
		EnableLOC: true}

	if strings.TrimSpace(logFilePath) == "" {
		loggingConfig.Sinks[0] = gosteno.NewIOSink(os.Stdout)
	} else {
		loggingConfig.Sinks[0] = gosteno.NewFileSink(logFilePath)
	}

	if syslogNamespace != "" {
		loggingConfig.Sinks = append(loggingConfig.Sinks, GetNewSyslogSink(syslogNamespace))
	}

	gosteno.Init(loggingConfig)
	logger := gosteno.NewLogger(name)
	logger.Debugf("Component %s in debug mode!", name)

	return logger
}
Exemple #14
0
func TestSend(t *testing.T) {
	bufferSize := 4096
	loggregatorClient := l.NewLoggregatorClient("localhost:9876", gosteno.NewLogger("TestLogger"), bufferSize)

	udpAddr, err := net.ResolveUDPAddr("udp", "localhost:9876")
	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	udpListener, err := net.ListenUDP("udp", udpAddr)
	defer udpListener.Close()
	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	expectedOutput := []byte("Important Testmessage")
	loggregatorClient.Send(expectedOutput)

	buffer := make([]byte, bufferSize)
	readCount, _, err := udpListener.ReadFromUDP(buffer)
	if err != nil {
		t.Errorf("Expected no error, but got %v", err)
	}

	received := string(buffer[:readCount])
	if string(expectedOutput) != received {
		t.Errorf("Expected %s to equal %s", string(expectedOutput), received)
	}
}
Exemple #15
0
func SetupLoggerFromConfig(c *config.Config) {
	l, err := steno.GetLogLevel(c.Logging.Level)
	if err != nil {
		panic(err)
	}

	s := make([]steno.Sink, 0)
	if c.Logging.File != "" {
		s = append(s, steno.NewFileSink(c.Logging.File))
	} else {
		s = append(s, steno.NewIOSink(os.Stdout))
	}

	if c.Logging.Syslog != "" {
		s = append(s, steno.NewSyslogSink(c.Logging.Syslog))
	}

	stenoConfig := &steno.Config{
		Sinks: s,
		Codec: steno.NewJsonCodec(),
		Level: l,
	}

	steno.Init(stenoConfig)
	log = steno.NewLogger("router")
}
Exemple #16
0
func NewReporter() *Reporter {
	rep := &Reporter{}

	rep.logger = steno.NewLogger("ila.reporter")

	config := configuration.NewRepositoryFromFilepath(configuration.DefaultFilePath(), func(err error) {
		if err != nil {
			rep.logger.Info("error reading api configuration")
		}
	})

	gateways := map[string]net.Gateway{
		"auth":             net.NewUAAGateway(config),
		"cloud-controller": net.NewCloudControllerGateway(config, time.Now),
		"uaa":              net.NewUAAGateway(config),
	}

	rep.apiAuth = api.NewUAAAuthenticationRepository(gateways["auth"], config)
	rep.credentials = make(map[string]string)
	rep.credentials["username"] = "******"
	rep.credentials["password"] = "******"

	apiErr := rep.apiAuth.Authenticate(rep.credentials)

	rep.AppRepo = api.NewCloudControllerApplicationRepository(config, gateways["cloud-controller"])
	if apiErr != nil {
		//cmd.ui.Failed(apiErr.Error())
		rep.logger.Info("error authenticating")
	} else {
		rep.logger.Info("successfully authenticated")
	}

	return rep
}
Exemple #17
0
func NewProxy(args ProxyArgs) Proxy {
	routeServiceConfig := route_service.NewRouteServiceConfig(args.RouteServiceEnabled, args.RouteServiceTimeout, args.Crypto, args.CryptoPrev)

	p := &proxy{
		accessLogger: args.AccessLogger,
		traceKey:     args.TraceKey,
		ip:           args.Ip,
		logger:       steno.NewLogger("router.proxy"),
		registry:     args.Registry,
		reporter:     args.Reporter,
		transport: &http.Transport{
			Dial: func(network, addr string) (net.Conn, error) {
				conn, err := net.DialTimeout(network, addr, 5*time.Second)
				if err != nil {
					return conn, err
				}
				if args.EndpointTimeout > 0 {
					err = conn.SetDeadline(time.Now().Add(args.EndpointTimeout))
				}
				return conn, err
			},
			DisableKeepAlives:  true,
			DisableCompression: true,
			TLSClientConfig:    args.TLSConfig,
		},
		fakedomain:         args.FakeDomain,
		secureCookies:      args.SecureCookies,
		routeServiceConfig: routeServiceConfig,
		ExtraHeadersToLog:  args.ExtraHeadersToLog,
	}

	return p
}
func loadLoggerAndConfig(c *cli.Context, component string) (logger.Logger, *gosteno.Logger, *config.Config) {
	configPath := c.String("config")
	if configPath == "" {
		fmt.Printf("Config path required")
		os.Exit(1)
	}

	conf, err := config.FromFile(configPath)
	if err != nil {
		fmt.Printf("Failed to load config: %s", err.Error())
		os.Exit(1)
	}

	stenoConf := &gosteno.Config{
		Sinks: []gosteno.Sink{
			gosteno.NewIOSink(os.Stdout),
			gosteno.NewSyslogSink("vcap.hm9000." + component),
		},
		Level: conf.LogLevel(),
		Codec: gosteno.NewJsonCodec(),
	}
	gosteno.Init(stenoConf)
	steno := gosteno.NewLogger("vcap.hm9000." + component)
	hmLogger := logger.NewRealLogger(steno)

	return hmLogger, steno, conf
}
Exemple #19
0
func main() {
	var configPath string
	flag.StringVar(&configPath,
		"conf",
		"", "Path of the YAML configuration of the co-located DEA.")
	flag.Parse()

	config, err := common.ConfigFromFile(configPath)
	if err != nil {
		panic(err.Error())
	}

	common.SetupSteno(&config.Server.Logging)
	log := steno.NewLogger("runner")

	var localIp *string
	localIp, err = getLocalIp()

	if err != nil {
		log.Fatal(err.Error())
	}

	if err := directoryserver.Start(*localIp, config); err != nil {
		log.Fatal(err.Error())
	}
}
Exemple #20
0
func NewLogger(name string) *gosteno.Logger {
	if !initialized {
		initLogging()
		initialized = true
	}

	return gosteno.NewLogger(name)
}
Exemple #21
0
func initialize() {
	sender := metric_sender.NewMetricSender(AutowiredEmitter())
	batcher := metricbatcher.New(sender, defaultBatchInterval)
	metrics.Initialize(sender, batcher)
	logs.Initialize(log_sender.NewLogSender(AutowiredEmitter(), statsInterval, gosteno.NewLogger("dropsonde/logs")))
	go runtime_stats.NewRuntimeStats(autowiredEmitter, statsInterval).Run(nil)
	http.DefaultTransport = InstrumentedRoundTripper(http.DefaultTransport)
}
Exemple #22
0
func NewProxy(c *config.Config, r *Registry, v Varz) *Proxy {
	return &Proxy{
		Config:   c,
		Logger:   steno.NewLogger("router.proxy"),
		Registry: r,
		Varz:     v,
	}
}
func NewRestCloudController(apiUrl string, accessToken string) service.CloudController {
	return &RestController{
		client:      &http.Client{},
		apiUrl:      apiUrl,
		accessToken: accessToken,
		logger:      gosteno.NewLogger("cc_client"),
	}
}
func NewRestServiceCatalog(apiUrl string, accessToken string) service.ServiceCatalog {
	return &RestController{
		client:      &http.Client{},
		apiUrl:      apiUrl,
		accessToken: accessToken,
		logger:      gosteno.NewLogger("service_catalog"),
	}
}
Exemple #25
0
func main() {
	c := config.DefaultConfig()
	logCounter := vcap.NewLogCounter()

	if configFile != "" {
		c = config.InitConfigFromFile(configFile)
	}

	InitLoggerFromConfig(c, logCounter)
	logger := steno.NewLogger("router.main")

	err := dropsonde.Initialize(c.Logging.MetronAddress, c.Logging.JobName)
	if err != nil {
		logger.Errorf("Dropsonde failed to initialize: %s", err.Error())
		os.Exit(1)
	}

	// setup number of procs
	if c.GoMaxProcs != 0 {
		runtime.GOMAXPROCS(c.GoMaxProcs)
	}

	if c.DebugAddr != "" {
		cf_debug_server.Run(c.DebugAddr)
	}

	logger.Info("Setting up NATs connection")
	natsClient := connectToNatsServer(c, logger)

	registry := rregistry.NewRouteRegistry(c, natsClient)

	logger.Info("Setting up routing_api route fetcher")
	setupRouteFetcher(c, registry)

	varz := rvarz.NewVarz(registry)

	accessLogger, err := access_log.CreateRunningAccessLogger(c)
	if err != nil {
		logger.Fatalf("Error creating access logger: %s\n", err)
	}

	proxy := buildProxy(c, registry, accessLogger, varz)

	router, err := router.NewRouter(c, proxy, natsClient, registry, varz, logCounter)
	if err != nil {
		logger.Errorf("An error occurred: %s", err.Error())
		os.Exit(1)
	}

	errChan := router.Run()

	logger.Info("gorouter.started")

	waitOnErrOrSignal(c, logger, errChan, router)

	os.Exit(0)
}
Exemple #26
0
func main() {

	stenoConfig := &steno.Config{
		Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)},
		Codec: steno.NewJsonCodec(),
		Level: steno.LOG_ALL,
	}

	steno.Init(stenoConfig)
	log = steno.NewLogger("router.test")
	s := true
	log.Infof("test %s ", s)

	p := &Proxy{}
	p.Logger = steno.NewLogger("router.registry")
	p.Infof("hello %s", "ss\n")
	fmt.Printf("fmt world\n")
}
Exemple #27
0
func init() {
	steno.Init(&steno.Config{
		Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)},
		Codec: steno.NewJsonCodec(),
		Level: steno.LOG_ALL,
	})

	log = steno.NewLogger("directory_server_test")
}
func NewRouteServiceConfig(enabled bool, timeout time.Duration, crypto secure.Crypto, cryptoPrev secure.Crypto) *RouteServiceConfig {
	return &RouteServiceConfig{
		routeServiceEnabled: enabled,
		routeServiceTimeout: timeout,
		crypto:              crypto,
		cryptoPrev:          cryptoPrev,
		logger:              steno.NewLogger("router.proxy.route-service"),
	}
}
Exemple #29
0
func main() {
	c := &gosteno.Config{
		Sinks: []gosteno.Sink{
			gosteno.NewIOSink(os.Stdout),
		},
		Level:     gosteno.LOG_DEBUG,
		Codec:     gosteno.NewJsonPrettifier(0),
		EnableLOC: true,
	}
	gosteno.Init(c)

	logger := gosteno.NewLogger("atk_instances")

	conf := config.NewConfig()

	m := martini.Classic()

	key, err := atkoauth2.TokenKey(conf.TokenKeyUrl)
	if err != nil {
		logger.Error(err.Error())
		return
	}

	m.Handlers(
		atkoauth2.ResourceServer(key),
		martini.Logger(),
		render.Renderer(render.Options{IndentJSON: true}),
	)

	m.Get("/rest/orgs/:id/atkinstances", func(params martini.Params, t *jwt.Token, r render.Render) {

		cloudController := cc.NewRestCloudController(conf.ApiUrl, t.Raw)

		spaceSummaryHelper := service.NewSpaceSummaryHelper()

		srv := service.NewAtkListService(cloudController, spaceSummaryHelper)

		if len(conf.CommonService) == 0 {
			conf.CommonService = "postgres"
		}

		//TODO: check id param

		instances, err := srv.GetAllInstances(conf.ServiceLabel,
			conf.ScoringEngineLabel,
			conf.CommonService,
			params["id"])
		if err != nil {
			r.JSON(500, err.Error())
		}

		r.JSON(200, instances)
	})

	m.Run()
}
Exemple #30
0
func setupRouteFetcher(c *config.Config, registry rregistry.RegistryInterface) {
	if c.RoutingApiEnabled() {
		tokenFetcher := token_fetcher.NewTokenFetcher(&c.OAuth)
		routingApiUri := fmt.Sprintf("%s:%d", c.RoutingApi.Uri, c.RoutingApi.Port)
		routingApiClient := routing_api.NewClient(routingApiUri)
		routeFetcher := route_fetcher.NewRouteFetcher(steno.NewLogger("router.route_fetcher"), tokenFetcher, registry, c, routingApiClient, 1)
		routeFetcher.StartFetchCycle()
		routeFetcher.StartEventCycle()
	}
}