Esempio n. 1
0
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
}
Esempio n. 2
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.global")
}
Esempio n. 3
0
func InitLoggerFromConfig(c *config.Config, logCounter *vcap.LogCounter) {
	l, err := steno.GetLogLevel(c.Logging.Level)
	if err != nil {
		panic(err)
	}

	s := make([]steno.Sink, 0, 3)
	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))
	}

	s = append(s, logCounter)

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

	steno.Init(stenoConfig)
}
Esempio n. 4
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
}
Esempio n. 5
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)
}
Esempio n. 6
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()
}
Esempio n. 7
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)
}
func setup_steno(conf *proxy.ProxyConfig) {
	level, err := steno.GetLogLevel(conf.LOGGING.LEVEL)
	if err != nil {
		panic(err)
	}

	log_path := conf.LOGGING.PATH
	if log_path != "" {
		os.MkdirAll(filepath.Dir(log_path), 0755)
	}

	sinks := make([]steno.Sink, 0)
	if log_path != "" {
		sinks = append(sinks, steno.NewFileSink(log_path))
	} else {
		sinks = append(sinks, steno.NewIOSink(os.Stdout))
	}

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

	steno.Init(stenoConfig)
}
Esempio n. 9
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")
}
Esempio n. 10
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()
}
Esempio n. 11
0
func init() {
	stenoConfig := &steno.Config{
		Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)},
		Codec: steno.NewJsonCodec(),
		Level: steno.LOG_ALL,
	}

	steno.Init(stenoConfig)
	log = steno.NewLogger("common.logger")
}
Esempio n. 12
0
func InitLog(logFile string) {
	c := &steno.Config{
		Level:     steno.LOG_INFO,
		Codec:     steno.NewJsonCodec(),
		EnableLOC: true,
	}
	if logFile == "" {
		c.Sinks = []steno.Sink{steno.NewIOSink(os.Stdout)}
	} else {
		c.Sinks = []steno.Sink{steno.NewFileSink(logFile)}
	}
	steno.Init(c)
	obj = steno.NewLogger("")
}
Esempio n. 13
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")
}
Esempio n. 14
0
func getLogger(debug bool) *gosteno.Logger {
	if debug {
		level := gosteno.LOG_DEBUG

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

		loggingConfig.Sinks[0] = gosteno.NewIOSink(os.Stdout)

		gosteno.Init(loggingConfig)
	}

	return gosteno.NewLogger("TestLogger")
}
Esempio n. 15
0
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)
	}

	sinks := make([]gosteno.Sink, 0, 3)
	if conf.LogDirectory != "" {
		logName := fmt.Sprintf("hm9000_%s.log", component)
		logFile := path.Join(conf.LogDirectory, logName)

		sinks = append(sinks, gosteno.NewFileSink(logFile))
	} else {
		sinks = append(sinks, gosteno.NewIOSink(os.Stdout))
	}
	sinks = append(sinks, gosteno.NewSyslogSink("vcap.hm9000."+component))

	stenoConf := &gosteno.Config{
		Sinks: sinks,
		Level: conf.LogLevel(),
		Codec: gosteno.NewJsonCodec(),
	}
	gosteno.Init(stenoConf)
	steno := gosteno.NewLogger("vcap.hm9000." + component)
	hmLogger := logger.NewRealLogger(steno)

	debugAddr := c.String("debugAddr")
	if debugAddr != "" {
		_, err = cf_debug_server.Run(debugAddr, nil)
		if err != nil {
			hmLogger.Error("Failed to start debug server", err)
			os.Exit(1)
		}
	}

	return hmLogger, steno, conf
}
Esempio n. 16
0
func setupStdoutLogger() steno.Logger {
	level, err := steno.GetLogLevel("debug")
	if err != nil {
		panic(err)
	}

	sinks := make([]steno.Sink, 0)
	sinks = append(sinks, steno.NewIOSink(os.Stdout))

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

	steno.Init(stenoConfig)

	return steno.NewLogger("mongodb_proxy")
}
Esempio n. 17
0
func NewLogger(file *os.File, level string) (Logger, error) {
	if len(level) == 0 {
		level = "debug"
	}

	logLevel, err := steno.GetLogLevel(level)
	if err != nil {
		return nil, err
	}

	out := steno.NewIOSink(file)
	out.SetCodec(steno.NewJsonPrettifier(steno.EXCLUDE_DATA))

	cnf := &steno.Config{
		Sinks:     []steno.Sink{out},
		Level:     logLevel,
		Port:      8080, // TODO: parameterize?
		EnableLOC: false,
	}
	steno.Init(cnf)

	return steno.NewLogger("gonatsd"), nil
}
Esempio n. 18
0
func SetupSteno(logConfig *LogConfig) {
	level, err := steno.GetLogLevel(logConfig.Level)
	if err != nil {
		panic(err)
	}

	sinks := make([]steno.Sink, 0)
	if logConfig.File != "" {
		sinks = append(sinks, steno.NewFileSink(logConfig.File))
	} else {
		sinks = append(sinks, steno.NewIOSink(os.Stdout))
	}
	if logConfig.Syslog != "" {
		sinks = append(sinks, steno.NewSyslogSink(logConfig.Syslog))
	}

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

	steno.Init(stenoConfig)
}
Esempio n. 19
0
		panic(err)
	}

	sinks := []gosteno.Sink{}
	if params.path != "" {
		sinks = append(sinks, NewFileSink(params.path))
	} else {
		sinks = append(sinks, NewIOSink(os.Stdout))
	}

	c := &gosteno.Config{
		Sinks:     sinks,
		Level:     level,
		EnableLOC: true,
	}

	InitGoSteno(c)
}

var InitGoSteno = func(c *gosteno.Config) {
	gosteno.Init(c)
}

var NewFileSink = func(path string) gosteno.Sink {
	return gosteno.NewFileSink(path)
}

var NewIOSink = func(file *os.File) gosteno.Sink {
	return gosteno.NewIOSink(file)
}
Esempio n. 20
0
// the HTTP request is from dea push cmd, from 10.0.2.15:5678
func (p *Proxy) ServeHTTP(hrw http.ResponseWriter, req *http.Request) {
	rw := responseWriter{
		ResponseWriter: hrw,
		Logger:         p.Logger.Copy(),
	}
	// log for zjlog!
	stenoConfig := &steno.Config{
		Sinks: []steno.Sink{steno.NewIOSink(os.Stderr)},
		Codec: steno.NewJsonCodec(),
		Level: steno.LOG_ALL,
	}

	steno.Init(stenoConfig)
	zlog = steno.NewLogger("router.test")

	rw.Set("RemoteAddr", req.RemoteAddr)
	rw.Set("Host", req.Host)
	rw.Set("X-Forwarded-For", req.Header["X-Forwarded-For"]) //
	rw.Set("X-Forwarded-Proto", req.Header["X-Forwarded-Proto"])
	a := AccessLogRecord{
		Request:   req,
		StartedAt: time.Now(),
	}

	if req.ProtoMajor != 1 && (req.ProtoMinor != 0 || req.ProtoMinor != 1) {
		c, brw, err := rw.Hijack()
		if err != nil {
			panic(err)
		}

		fmt.Fprintf(brw, "HTTP/1.0 400 Bad Request\r\n\r\n")
		brw.Flush()
		c.Close()
		return
	}

	start := time.Now()

	// Return 200 OK for heartbeats from LB
	if req.UserAgent() == "HTTP-Monitor/1.1" {
		rw.WriteHeader(http.StatusOK)
		fmt.Fprintln(rw, "ok")
		return
	}

	rw.Infof("zhang:req= %s", req)
	bytes, _ := ioutil.ReadAll(req.Body)

	parentTx := ""
	rootTx := ""
	parentName := ""
	parent_port := ""
	subName := ""
	invocationCtx := ""
	cat := string(bytes)
	var rm reqMessage
	u_err := json.Unmarshal(bytes, &rm)
	if u_err != nil {
		rw.Infof("cat= %s", cat)
		rw.Infof("!umarshal error %s ", u_err)
	}
	if len(rm.Params) > 0 {
		str := string(rm.Params[0]) //居然index out of range
		rw.Info(str)

		arr := strings.Split(str, "|")
		//		     rw.Infof("0=%s", arr[0])
		//		     rw.Infof("1=%s",arr[1])
		//
		rootTx = arr[1]
		parentTx = arr[2]

		if len(arr) > 6 {
			parentName = arr[3] // actually , this is current component
			parent_port = arr[4]
			subName = arr[5]
			invocationCtx = arr[6]
		}
		rm.Params[0] = Param(arr[0]) //什么意思???
		y, err := json.Marshal(rm)
		if err != nil {
			rw.Infof("marshal error %s ", err)
		}
		req.Body = ioutil.NopCloser(strings.NewReader(string(y))) //

	} else {
		rw.Infof("rm.Params == nil %s", rm.Params)
	}

	//
	rw.Infof("mybody = %s", cat) // bufio.Reader
	//所以,或许parentTx要在lookup前

	//首先时获取backend,如果此处因为状态,返回nil,那么,就不会再有请求了。
	x, ok := p.Lookup(req, rootTx) //   get backend,  if there is JSESSIONID , router to the same backend
	if !ok {
		p.Varz.CaptureBadRequest(req)
		rw.WriteStatus(http.StatusNotFound)
		return
	}
	fmt.Printf("check whether use cachedTxBackends \n")
	if rootTx != "" {

		h := hostWithoutPort(req)
		fmt.Printf("h = %s \n", h)
		find_key := h + ":" + rootTx
		if p.CachedTxBackends[find_key] != nil {
			x = p.CachedTxBackends[find_key]
			fmt.Printf("%s get key: app_id = %s from map ,tx = %s\n", x.CollectPort, x.ApplicationId, rootTx)
		} else {
			key := h + ":" + rootTx
			p.CachedTxBackends[key] = x
			fmt.Printf("%s save key:%s to map ,tx = %s  \n", x.CollectPort, x.ApplicationId, rootTx)
		}

		fmt.Printf("%v ,%d, %s, blocked false\n   ", x.BackendId, x.ApplicationId, x.Port)
		sub_port := x.CollectPort

		remote := &remoteMessage{
			RootTx:        rootTx,
			ParentTx:      parentTx,
			ParentPort:    parent_port,
			ParentName:    parentName,
			SubPort:       sub_port,
			SubName:       subName,
			InvocationCtx: invocationCtx,
		}

		cachedTxes := p.CachedBackendTxes[x.PrivateInstanceId]
		p.CachedBackendTxes[x.PrivateInstanceId] = append(cachedTxes, remote)

		//		fmt.Printf("after add \n")
		//
		//
		//		for  x,y := range p.CachedBackendTxes {
		//			for j := 0 ; j < len (y); j++ {
		//				fmt.Printf("i:%s,j:%d,remote:%s.\n",x,j,y[j].RootTx)
		//			}
		//		}
	} else {

		fmt.Printf("rootTx == whitespace \n")
	}

	rw.Set("Backend", x.ToLogData())

	a.Backend = x

	p.Registry.CaptureBackendRequest(x, start) //record request
	p.Varz.CaptureBackendRequest(x, req)

	req.URL.Scheme = "http"
	req.URL.Host = x.CanonicalAddr()

	// Add X-Forwarded-For
	if host, _, err := net.SplitHostPort(req.RemoteAddr); err == nil {
		// We assume there is a trusted upstream (L7 LB) that properly
		// strips client's XFF header

		// This is sloppy but fine since we don't share this request or
		// headers. Otherwise we should copy the underlying header and
		// append
		xff := append(req.Header["X-Forwarded-For"], host)
		req.Header.Set("X-Forwarded-For", strings.Join(xff, ", "))
	}

	// Check if the connection is going to be upgraded to a WebSocket connection
	if p.CheckWebSocket(rw, req) {
		p.ServeWebSocket(rw, req)
		return
	}

	// Use a new connection for every request
	// Keep-alive can be bolted on later, if we want to
	req.Close = true
	req.Header.Del("Connection")

	// this is used for send request and get resposne

	res, err := http.DefaultTransport.RoundTrip(req)

	latency := time.Since(start)

	a.FirstByteAt = time.Now()
	a.Response = res

	if err != nil {
		p.Varz.CaptureBackendResponse(x, res, latency)
		rw.Warnf("Error reading from upstream: %s", err)
		rw.WriteStatus(http.StatusBadGateway)
		return
	}

	rw.Infof("zhang:res= %s", res)
	p.Varz.CaptureBackendResponse(x, res, latency)

	for k, vv := range res.Header {
		for _, v := range vv {
			rw.Header().Add(k, v)
		}
	}

	if p.Config.TraceKey != "" && req.Header.Get(VcapTraceHeader) == p.Config.TraceKey {
		rw.Header().Set(VcapRouterHeader, p.Config.Ip)
		rw.Header().Set(VcapBackendHeader, x.CanonicalAddr())
	}

	needSticky := false

	//这里是判断response中,cookie中是否有jsessionid
	for _, v := range res.Cookies() { // here we get res.Cookies(), if exists v[i].name = StickyCookieKey
		zlog.Info(v.Name) //基于Cookie的web应用,会将数据存成<key,value>
		//但是基于Http Session的应用,为什么在这里只看到了jsessionid?

		if v.Name == StickyCookieKey { //JSESSIONID
			needSticky = true // here flag = needSticky
			break
		}
	}

	if needSticky && x.PrivateInstanceId != "" {
		cookie := &http.Cookie{ // here we got http cookie which has sessionid
			Name:  VcapCookieId,
			Value: x.PrivateInstanceId,
			Path:  "/",
		} //

		http.SetCookie(rw, cookie) // set httpCookie 只在发现有sticky session时,才set Cookie
		// http session,第一次生成jsessionid时,会在返回头部加上cookie
	}

	rw.WriteHeader(res.StatusCode)
	n, _ := rw.CopyFrom(res.Body)

	a.FinishedAt = time.Now()
	a.BodyBytesSent = n

	if p.AccessLogger != nil {
		p.AccessLogger.Log(a)
	}
}