Beispiel #1
0
func BenchmarkLTSVEncode(b *testing.B) {
	buf := &bytes.Buffer{}
	encoder := ltsv.NewEncoder(buf)
	for i := 0; i < b.N; i++ {
		encoder.Encode(testMap)
	}
}
Beispiel #2
0
func TestLTSVEncode(t *testing.T) {
	buf := &bytes.Buffer{}
	encoder := ltsv.NewEncoder(buf)
	encoder.Encode(testMap)
	if string(buf.Bytes()) != compareLTSV {
		t.Errorf("unexpected encoded", string(buf.Bytes()))
	}
}
Beispiel #3
0
func runDumper(dir string, format string, option OutputOption) {
	var filename string
	var fh io.WriteCloser
	var err error
	for {
		rs := <-DumpCh
		now := time.Now().Format("2006-01-02-15")
		_filename := filepath.Join(dir, rs.Tag+"."+now)
		if filename != _filename {
			if fh != nil {
				fh.Close()
			}
			filename = _filename
			fh, err = os.OpenFile(filename, os.O_CREATE|os.O_APPEND|os.O_RDWR, 0644)
			defer fh.Close()
			if err != nil {
				log.Println("[error]", err)
				continue
			}
		}
		if fh == nil {
			continue
		}
		var encoder Encoder
		switch format {
		case "ltsv":
			encoder = ltsv.NewEncoder(fh)
		default:
			encoder = json.NewEncoder(fh)
		}
		for _, record := range rs.Records {
			record, _ := record.(*fluent.TinyFluentRecord)
			if option.IncludeTime {
				fmt.Fprint(fh, time.Unix(record.Timestamp, 0).Format(time.RFC3339), "\t")
			}
			if option.IncludeTag {
				fmt.Fprint(fh, rs.Tag, "\t")
			}
			err = encoder.Encode(record.GetAllData())
			if err != nil {
				continue
			}
		}
	}
}
Beispiel #4
0
func httpHandler(w http.ResponseWriter, r *http.Request) {
	tag := strings.Trim(r.URL.Path, "/")
	if tag == "" {
		http.NotFound(w, r)
		return
	}

	option := OutputOption{false, false}
	option.IncludeTime, _ = strconv.ParseBool(r.FormValue("time"))
	option.IncludeTag, _ = strconv.ParseBool(r.FormValue("tag"))
	var encoder Encoder
	switch t, _ := strconv.ParseBool(r.FormValue("ltsv")); t {
	case true:
		encoder = ltsv.NewEncoder(w)
	default:
		encoder = json.NewEncoder(w)
	}

	id := atomic.AddInt64(&ConnectionId, 1)
	ch := subscribe(tag, id)
	defer unsubscribe(tag, id)

	log.Printf(
		"[info] client %s tag:%s",
		r.RemoteAddr,
		tag,
	)

	w.WriteHeader(http.StatusOK)
	if f, ok := w.(http.Flusher); ok {
		f.Flush()
	}
	for {
		recordSet := <-ch
		err := writeResponse(encoder, w, recordSet, option)
		if err != nil {
			return
		}
		if f, ok := w.(http.Flusher); ok {
			f.Flush()
		}
	}
	return
}