Optimize dataobj module (#79)
This commit is contained in:
parent
08f4f1788f
commit
eedab9d8c2
|
@ -14,6 +14,7 @@ const (
|
|||
GAUGE = "GAUGE"
|
||||
COUNTER = "COUNTER"
|
||||
DERIVE = "DERIVE"
|
||||
SPLIT = "/"
|
||||
)
|
||||
|
||||
type MetricValue struct {
|
||||
|
@ -28,9 +29,11 @@ type MetricValue struct {
|
|||
TagsMap map[string]string `json:"tagsMap"` //保留2种格式,方便后端组件使用
|
||||
}
|
||||
|
||||
const SPLIT = "/"
|
||||
|
||||
var bufferPool = sync.Pool{New: func() interface{} { return new(bytes.Buffer) }}
|
||||
var bufferPool = sync.Pool{
|
||||
New: func() interface{} {
|
||||
return new(bytes.Buffer)
|
||||
},
|
||||
}
|
||||
|
||||
func (m *MetricValue) String() string {
|
||||
return fmt.Sprintf("<MetaData Endpoint:%s, Metric:%s, CounterType:%s Timestamp:%d, Step:%d, Value:%v, Tags:%v(%v)>",
|
||||
|
@ -63,38 +66,37 @@ func (m *MetricValue) CheckValidity() (err error) {
|
|||
return
|
||||
}
|
||||
|
||||
//检测保留字
|
||||
if HasReservedWords(m.Metric) {
|
||||
err = fmt.Errorf("metric:%s contains reserved words:[\\t] [\\r] [\\n] [,] [ ] [=]", m.Metric)
|
||||
return
|
||||
}
|
||||
|
||||
if HasReservedWords(m.Endpoint) {
|
||||
err = fmt.Errorf("endpoint:%s contains reserved words:[\\t] [\\r] [\\n] [,] [ ] [=]", m.Endpoint)
|
||||
return
|
||||
}
|
||||
|
||||
if m.Metric == "" || m.Endpoint == "" {
|
||||
err = fmt.Errorf("Metric|Endpoint is nil")
|
||||
err = fmt.Errorf("metric or endpoint should not be empty")
|
||||
return
|
||||
}
|
||||
|
||||
// 检测保留字
|
||||
reservedWords := "[\\t] [\\r] [\\n] [,] [ ] [=]"
|
||||
if HasReservedWords(m.Metric) {
|
||||
err = fmt.Errorf("metric:%s contains reserved words: %s", m.Metric, reservedWords)
|
||||
return
|
||||
}
|
||||
if HasReservedWords(m.Endpoint) {
|
||||
err = fmt.Errorf("endpoint:%s contains reserved words: %s", m.Endpoint, reservedWords)
|
||||
return
|
||||
}
|
||||
|
||||
if m.CounterType == "" {
|
||||
m.CounterType = GAUGE
|
||||
}
|
||||
|
||||
if m.CounterType != COUNTER && m.CounterType != GAUGE && m.CounterType != DERIVE {
|
||||
err = fmt.Errorf("CounterType error")
|
||||
err = fmt.Errorf("wrong counter type")
|
||||
return
|
||||
}
|
||||
|
||||
if m.ValueUntyped == "" {
|
||||
err = fmt.Errorf("Value is nil")
|
||||
err = fmt.Errorf("value is nil")
|
||||
return
|
||||
}
|
||||
|
||||
if m.Step <= 0 {
|
||||
err = fmt.Errorf("step < 0")
|
||||
err = fmt.Errorf("step sholud larger than 0")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -106,9 +108,9 @@ func (m *MetricValue) CheckValidity() (err error) {
|
|||
}
|
||||
|
||||
m.Tags = SortedTags(m.TagsMap)
|
||||
|
||||
// TODO(): why 510?
|
||||
if len(m.Metric)+len(m.Tags) > 510 {
|
||||
err = fmt.Errorf("metrc+tag > 510 is not illegal:")
|
||||
err = fmt.Errorf("len(m.Metric)+len(m.Tags) is too large")
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -140,7 +142,7 @@ func (m *MetricValue) CheckValidity() (err error) {
|
|||
}
|
||||
|
||||
if !valid {
|
||||
err = fmt.Errorf("value is not illegal:%v", m)
|
||||
err = fmt.Errorf("value [%v] is illegal", m.Value)
|
||||
return
|
||||
}
|
||||
|
||||
|
@ -149,20 +151,15 @@ func (m *MetricValue) CheckValidity() (err error) {
|
|||
}
|
||||
|
||||
func HasReservedWords(str string) bool {
|
||||
if -1 == strings.IndexFunc(str,
|
||||
func(r rune) bool {
|
||||
return r == '\t' ||
|
||||
r == '\r' ||
|
||||
r == '\n' ||
|
||||
r == ',' ||
|
||||
r == ' ' ||
|
||||
r == '='
|
||||
}) {
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
idx := strings.IndexFunc(str, func(r rune) bool {
|
||||
return r == '\t' ||
|
||||
r == '\r' ||
|
||||
r == '\n' ||
|
||||
r == ',' ||
|
||||
r == ' ' ||
|
||||
r == '='
|
||||
})
|
||||
return idx != -1
|
||||
}
|
||||
|
||||
func SortedTags(tags map[string]string) string {
|
||||
|
@ -171,7 +168,6 @@ func SortedTags(tags map[string]string) string {
|
|||
}
|
||||
|
||||
size := len(tags)
|
||||
|
||||
if size == 0 {
|
||||
return ""
|
||||
}
|
||||
|
@ -195,7 +191,6 @@ func SortedTags(tags map[string]string) string {
|
|||
keys[i] = k
|
||||
i++
|
||||
}
|
||||
|
||||
sort.Strings(keys)
|
||||
|
||||
for j, key := range keys {
|
||||
|
@ -211,7 +206,6 @@ func SortedTags(tags map[string]string) string {
|
|||
}
|
||||
|
||||
func SplitTagsString(s string) (tags map[string]string, err error) {
|
||||
err = nil
|
||||
tags = make(map[string]string)
|
||||
|
||||
s = strings.Replace(s, " ", "", -1)
|
||||
|
@ -239,15 +233,16 @@ func DictedTagstring(s string) map[string]string {
|
|||
}
|
||||
s = strings.Replace(s, " ", "", -1)
|
||||
|
||||
tag_dict := make(map[string]string)
|
||||
result := make(map[string]string)
|
||||
tags := strings.Split(s, ",")
|
||||
for _, tag := range tags {
|
||||
tag_pair := strings.SplitN(tag, "=", 2)
|
||||
if len(tag_pair) == 2 {
|
||||
tag_dict[tag_pair[0]] = tag_pair[1]
|
||||
pair := strings.SplitN(tag, "=", 2)
|
||||
if len(pair) == 2 {
|
||||
result[pair[0]] = pair[1]
|
||||
}
|
||||
}
|
||||
return tag_dict
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
func PKWithCounter(endpoint, counter string) string {
|
||||
|
@ -303,12 +298,8 @@ type BuiltinMetric struct {
|
|||
Tags string
|
||||
}
|
||||
|
||||
func (this *BuiltinMetric) String() string {
|
||||
return fmt.Sprintf(
|
||||
"%s/%s",
|
||||
this.Metric,
|
||||
this.Tags,
|
||||
)
|
||||
func (bm *BuiltinMetric) String() string {
|
||||
return fmt.Sprintf("%s/%s", bm.Metric, bm.Tags)
|
||||
}
|
||||
|
||||
type BuiltinMetricRequest struct {
|
||||
|
@ -324,23 +315,23 @@ type BuiltinMetricResponse struct {
|
|||
ErrCode int
|
||||
}
|
||||
|
||||
func (this *BuiltinMetricResponse) String() string {
|
||||
func (br *BuiltinMetricResponse) String() string {
|
||||
return fmt.Sprintf(
|
||||
"<Metrics:%v, Checksum:%s, Timestamp:%v>",
|
||||
this.Metrics,
|
||||
this.Checksum,
|
||||
this.Timestamp,
|
||||
br.Metrics,
|
||||
br.Checksum,
|
||||
br.Timestamp,
|
||||
)
|
||||
}
|
||||
|
||||
type BuiltinMetricSlice []*BuiltinMetric
|
||||
|
||||
func (this BuiltinMetricSlice) Len() int {
|
||||
return len(this)
|
||||
func (bm BuiltinMetricSlice) Len() int {
|
||||
return len(bm)
|
||||
}
|
||||
func (this BuiltinMetricSlice) Swap(i, j int) {
|
||||
this[i], this[j] = this[j], this[i]
|
||||
func (bm BuiltinMetricSlice) Swap(i, j int) {
|
||||
bm[i], bm[j] = bm[j], bm[i]
|
||||
}
|
||||
func (this BuiltinMetricSlice) Less(i, j int) bool {
|
||||
return this[i].String() < this[j].String()
|
||||
func (bm BuiltinMetricSlice) Less(i, j int) bool {
|
||||
return bm[i].String() < bm[j].String()
|
||||
}
|
||||
|
|
|
@ -34,12 +34,12 @@ func NewRRDData(ts int64, val float64) *RRDData {
|
|||
return &RRDData{Timestamp: ts, Value: JsonFloat(val)}
|
||||
}
|
||||
|
||||
func (this *RRDData) String() string {
|
||||
func (rrd *RRDData) String() string {
|
||||
return fmt.Sprintf(
|
||||
"<RRDData:Value:%v TS:%d %v>",
|
||||
this.Value,
|
||||
this.Timestamp,
|
||||
time.Unix(this.Timestamp, 0).Format("2006-01-02 15:04:05"),
|
||||
rrd.Value,
|
||||
rrd.Timestamp,
|
||||
time.Unix(rrd.Timestamp, 0).Format("2006-01-02 15:04:05"),
|
||||
)
|
||||
}
|
||||
|
||||
|
@ -70,34 +70,34 @@ type TsdbItem struct {
|
|||
|
||||
const GRAPH = 1
|
||||
|
||||
func (this *TsdbItem) String() string {
|
||||
func (t *TsdbItem) String() string {
|
||||
return fmt.Sprintf(
|
||||
"<Endpoint:%s, Metric:%s, Tags:%v, TagsMap:%v, Value:%v, TS:%d %v DsType:%s, Step:%d, Heartbeat:%d, Min:%s, Max:%s>",
|
||||
this.Endpoint,
|
||||
this.Metric,
|
||||
this.Tags,
|
||||
this.TagsMap,
|
||||
this.Value,
|
||||
this.Timestamp,
|
||||
str.UnixTsFormat(this.Timestamp),
|
||||
this.DsType,
|
||||
this.Step,
|
||||
this.Heartbeat,
|
||||
this.Min,
|
||||
this.Max,
|
||||
t.Endpoint,
|
||||
t.Metric,
|
||||
t.Tags,
|
||||
t.TagsMap,
|
||||
t.Value,
|
||||
t.Timestamp,
|
||||
str.UnixTsFormat(t.Timestamp),
|
||||
t.DsType,
|
||||
t.Step,
|
||||
t.Heartbeat,
|
||||
t.Min,
|
||||
t.Max,
|
||||
)
|
||||
}
|
||||
|
||||
func (g *TsdbItem) PrimaryKey() string {
|
||||
return str.PK(g.Endpoint, g.Metric, g.Tags)
|
||||
func (t *TsdbItem) PrimaryKey() string {
|
||||
return str.PK(t.Endpoint, t.Metric, t.Tags)
|
||||
}
|
||||
|
||||
func (g *TsdbItem) MD5() string {
|
||||
return str.MD5(g.Endpoint, g.Metric, str.SortedTags(g.TagsMap))
|
||||
func (t *TsdbItem) MD5() string {
|
||||
return str.MD5(t.Endpoint, t.Metric, str.SortedTags(t.TagsMap))
|
||||
}
|
||||
|
||||
func (this *TsdbItem) UUID() string {
|
||||
return str.UUID(this.Endpoint, this.Metric, this.Tags, this.DsType, this.Step)
|
||||
func (t *TsdbItem) UUID() string {
|
||||
return str.UUID(t.Endpoint, t.Metric, t.Tags, t.DsType, t.Step)
|
||||
}
|
||||
|
||||
// ConsolFunc 是RRD中的概念,比如:MIN|MAX|AVERAGE
|
||||
|
|
Loading…
Reference in New Issue