// Returns byte array of a line protocol representation of the point func (p Point) Bytes(precision string) []byte { key := imodels.MakeKey([]byte(p.Name), imodels.Tags(p.Tags)) fields := imodels.Fields(p.Fields).MarshalBinary() kl := len(key) fl := len(fields) var bytes []byte if p.Time.IsZero() { bytes = make([]byte, fl+kl+1) copy(bytes, key) bytes[kl] = ' ' copy(bytes[kl+1:], fields) } else { timeStr := strconv.FormatInt(p.Time.UnixNano()/imodels.GetPrecisionMultiplier(precision), 10) tl := len(timeStr) bytes = make([]byte, fl+kl+tl+2) copy(bytes, key) bytes[kl] = ' ' copy(bytes[kl+1:], fields) bytes[kl+fl+1] = ' ' copy(bytes[kl+fl+2:], []byte(timeStr)) } return bytes }
func TestMakeKeyEscaped(t *testing.T) { if exp, got := `cpu\ load`, models.MakeKey([]byte(`cpu\ load`), models.Tags{}); string(got) != exp { t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp) } if exp, got := `cpu\ load`, models.MakeKey([]byte(`cpu load`), models.Tags{}); string(got) != exp { t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp) } if exp, got := `cpu\,load`, models.MakeKey([]byte(`cpu\,load`), models.Tags{}); string(got) != exp { t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp) } if exp, got := `cpu\,load`, models.MakeKey([]byte(`cpu,load`), models.Tags{}); string(got) != exp { t.Errorf("MakeKey() mismatch.\ngot %v\nexp %v", got, exp) } }
func TestWriteTimeField(t *testing.T) { tmpDir, _ := ioutil.TempDir("", "shard_test") defer os.RemoveAll(tmpDir) tmpShard := path.Join(tmpDir, "shard") tmpWal := path.Join(tmpDir, "wal") index := tsdb.NewDatabaseIndex("db") opts := tsdb.NewEngineOptions() opts.Config.WALDir = filepath.Join(tmpDir, "wal") sh := tsdb.NewShard(1, index, tmpShard, tmpWal, opts) if err := sh.Open(); err != nil { t.Fatalf("error opening shard: %s", err.Error()) } defer sh.Close() pt := models.MustNewPoint( "cpu", models.NewTags(map[string]string{"time": "now"}), map[string]interface{}{"value": 1.0}, time.Unix(1, 2), ) buf := bytes.NewBuffer(nil) sh.SetLogOutput(buf) if err := sh.WritePoints([]models.Point{pt}); err != nil { t.Fatalf("unexpected error: %v", err) } else if got, exp := buf.String(), "dropping tag 'time'"; !strings.Contains(got, exp) { t.Fatalf("unexpected log message: %s", strings.TrimSpace(got)) } key := models.MakeKey([]byte("cpu"), nil) series := index.Series(string(key)) if series == nil { t.Fatal("expected series") } else if len(series.Tags) != 0 { t.Fatalf("unexpected number of tags: got=%v exp=%v", len(series.Tags), 0) } }