@@ -113,34 +113,54 @@ type MetricDataArray []*MetricData
113
113
114
114
// for ES
115
115
type MetricDefinition struct {
116
- Id string `json:"id"`
117
- OrgId int `json:"org_id"`
118
- Name string `json:"name" elastic:"type:string,index:not_analyzed"` // graphite format
119
- Metric string `json:"metric"` // kairosdb format (like graphite, but not including some tags)
120
- Interval int `json:"interval"` // minimum 10
121
- Unit string `json:"unit"`
122
- Mtype string `json:"mtype"`
123
- Tags []string `json:"tags" elastic:"type:string,index:not_analyzed"`
124
- LastUpdate int64 `json:"lastUpdate"` // unix timestamp
125
- Partition int32 `json:"partition"`
116
+ Id string `json:"id"`
117
+ OrgId int `json:"org_id"`
118
+ Name string `json:"name" elastic:"type:string,index:not_analyzed"` // graphite format
119
+ Metric string `json:"metric"` // kairosdb format (like graphite, but not including some tags)
120
+ Interval int `json:"interval"` // minimum 10
121
+ Unit string `json:"unit"`
122
+ Mtype string `json:"mtype"`
123
+ Tags []string `json:"tags" elastic:"type:string,index:not_analyzed"`
124
+ LastUpdate int64 `json:"lastUpdate"` // unix timestamp
125
+ Partition int32 `json:"partition"`
126
+ NameWithTags string `json:"nameWithTags"`
126
127
}
127
128
128
129
func (m * MetricDefinition ) SetId () {
129
130
sort .Strings (m .Tags )
130
131
131
- buffer := bytes .NewBufferString (m .Metric )
132
- buffer .WriteByte (0 )
133
- buffer .WriteString (m .Unit )
134
- buffer .WriteByte (0 )
135
- buffer .WriteString (m .Mtype )
136
- buffer .WriteByte (0 )
137
- fmt .Fprintf (buffer , "%d" , m .Interval )
132
+ nameWithTagsBuffer := bytes .NewBufferString (m .Name )
138
133
139
- for _ , k := range m .Tags {
140
- buffer .WriteByte (0 )
141
- buffer .WriteString (k )
134
+ idBuffer := bytes .NewBufferString (m .Metric )
135
+ idBuffer .WriteByte (0 )
136
+ idBuffer .WriteString (m .Unit )
137
+ idBuffer .WriteByte (0 )
138
+ idBuffer .WriteString (m .Mtype )
139
+ idBuffer .WriteByte (0 )
140
+ fmt .Fprintf (idBuffer , "%d" , m .Interval )
141
+
142
+ tagPositions := make ([]int , 0 , len (m .Tags )* 2 )
143
+ for _ , t := range m .Tags {
144
+ if len (t ) >= 5 && t [:5 ] == "name=" {
145
+ continue
146
+ }
147
+
148
+ nameWithTagsBuffer .WriteString (";" )
149
+ tagPositions = append (tagPositions , nameWithTagsBuffer .Len ())
150
+ nameWithTagsBuffer .WriteString (t )
151
+ tagPositions = append (tagPositions , nameWithTagsBuffer .Len ())
152
+
153
+ idBuffer .WriteByte (0 )
154
+ idBuffer .WriteString (t )
142
155
}
143
- m .Id = fmt .Sprintf ("%d.%x" , m .OrgId , md5 .Sum (buffer .Bytes ()))
156
+
157
+ m .NameWithTags = fmt .Sprintf ("%s" , nameWithTagsBuffer )
158
+ m .Tags = make ([]string , len (tagPositions )/ 2 )
159
+ for i := 0 ; i < len (m .Tags ); i ++ {
160
+ m .Tags [i ] = m .NameWithTags [tagPositions [i * 2 ]:tagPositions [i * 2 + 1 ]]
161
+ }
162
+ m .Name = m .NameWithTags [:len (m .Name )]
163
+ m .Id = fmt .Sprintf ("%d.%x" , m .OrgId , md5 .Sum (idBuffer .Bytes ()))
144
164
}
145
165
146
166
func (m * MetricDefinition ) Validate () error {
@@ -184,40 +204,6 @@ func (m *MetricDefinition) KeyBySeries(b []byte) []byte {
184
204
return b
185
205
}
186
206
187
- // NameWithTags returns the full metric name, including tags
188
- // the special tag "name" is ignored
189
- //
190
- // it is assumed that SetId() is called before this method, this ensures
191
- // that the tags are sorted
192
- //
193
- // example:
194
- // name: a.b.c
195
- // tags: c=c, b=b, a=a, name=a.b.c
196
- // becomes: a.b.c;a=a;b=b;c=c
197
- //
198
- func (m * MetricDefinition ) NameWithTags () string {
199
- nameLen := len (m .Name )
200
- count := 0
201
- for _ , tag := range m .Tags {
202
- if len (tag ) >= 5 && tag [:5 ] == "name=" {
203
- continue
204
- }
205
- count ++
206
- nameLen += len (tag )
207
- }
208
- nameLen += count // accounting for all the ";" between tags
209
- b := make ([]byte , nameLen )
210
- pos := copy (b , m .Name )
211
- for _ , tag := range m .Tags {
212
- if len (tag ) >= 5 && tag [:5 ] == "name=" {
213
- continue
214
- }
215
- pos += copy (b [pos :], ";" )
216
- pos += copy (b [pos :], tag )
217
- }
218
- return string (b )
219
- }
220
-
221
207
func MetricDefinitionFromJSON (b []byte ) (* MetricDefinition , error ) {
222
208
def := new (MetricDefinition )
223
209
if err := json .Unmarshal (b , & def ); err != nil {
0 commit comments