Esempio n. 1
0
func main() {
	var dbid = flag.String("database", "", "database identifier")
	var path = flag.String("path", "", "log file path")
	var fluentHost = flag.String("fluent-host", "", "fluentd hostname")
	var fluentPort = flag.Int("fluent-port", 24224, "fluentd forward port (default 24224)")
	var fluentTag = flag.String("fluent-tag", "mysql.slowquery", "fluentd tag")
	var raw = flag.Bool("raw", false, "output raw data (stdout only)")
	flag.Parse()

	if *dbid == "" || *path == "" {
		fmt.Println("Usage: rds-throwlog --database=[database identifier] --path=[log file path]")
		return
	}

	stream, err := Fetch(dbid, path)
	if err != nil {
		log.Println(err)
		return
	}
	defer stream.Close()
	log.Println("download completed")

	// output raw
	if *raw {
		io.Copy(os.Stdout, stream)
		return
	}

	// prepare fluent-logger
	var logger *fluent.Fluent
	if *fluentHost != "" {
		logger, err = fluent.New(fluent.Config{
			FluentPort: *fluentPort,
			FluentHost: *fluentHost,
		})
		if err != nil {
			log.Println("fluent.New returned error:", err)
			return
		}
	}

	records := mysqlslow.Parse(stream)
	for _, r := range records {
		if logger != nil {
			t, msg := r.ToFluentLog()
			log.Println(t)
			logger.PostWithTime(*fluentTag, t, msg)
		} else {
			data, err := json.Marshal(r)
			if err != nil {
				fmt.Println(err)
			}
			fmt.Println(string(data))
		}
	}
}
Esempio n. 2
0
// Fire is invoked by logrus and sends log to fluentd logger.
func (hook *FluentHook) Fire(entry *logrus.Entry) error {
	var logger *fluent.Fluent
	var err error

	switch {
	case hook.Fluent != nil:
		logger = hook.Fluent
	default:
		logger, err = fluent.New(fluent.Config{
			FluentHost: hook.host,
			FluentPort: hook.port,
		})
		if err != nil {
			return err
		}
		defer logger.Close()
	}

	// Create a map for passing to FluentD
	data := make(logrus.Fields)
	for k, v := range entry.Data {
		if _, ok := hook.ignoreFields[k]; ok {
			continue
		}
		if fn, ok := hook.filters[k]; ok {
			v = fn(v)
		}
		data[k] = v
	}

	setLevelString(entry, data)
	tag := hook.getTagAndDel(entry, data)
	if tag != entry.Message {
		setMessage(entry, data)
	}

	fluentData := ConvertToValue(data, TagName)
	err = logger.PostWithTime(tag, entry.Time, fluentData)
	return err
}