metric.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. package protocol
  2. import (
  3. "fmt"
  4. "hash/fnv"
  5. "sort"
  6. "time"
  7. )
  8. // Tag holds the keys and values for a bunch of Tag k/v pairs.
  9. type Tag struct {
  10. Key string
  11. Value string
  12. }
  13. // Field holds the keys and values for a bunch of Metric Field k/v pairs where Value can be a uint64, int64, int, float32, float64, string, or bool.
  14. type Field struct {
  15. Key string
  16. Value interface{}
  17. }
  18. // Metric is the interface for marshaling, if you implement this interface you can be marshalled into the line protocol. Woot!
  19. type Metric interface {
  20. Time() time.Time
  21. Name() string
  22. TagList() []*Tag
  23. FieldList() []*Field
  24. }
  25. // MutableMetric represents a metric that can be be modified.
  26. type MutableMetric interface {
  27. Metric
  28. SetTime(time.Time)
  29. AddTag(key, value string)
  30. AddField(key string, value interface{})
  31. }
  32. // FieldSortOrder is a type for controlling if Fields are sorted
  33. type FieldSortOrder int
  34. const (
  35. // NoSortFields tells the Decoder to not sort the fields.
  36. NoSortFields FieldSortOrder = iota
  37. // SortFields tells the Decoder to sort the fields.
  38. SortFields
  39. )
  40. // FieldTypeSupport is a type for the parser to understand its type support.
  41. type FieldTypeSupport int
  42. const (
  43. // UintSupport means the parser understands uint64s and can store them without having to convert to int64.
  44. UintSupport FieldTypeSupport = 1 << iota
  45. )
  46. // MetricError is an error causing a metric to be unserializable.
  47. type MetricError struct {
  48. s string
  49. }
  50. func (e MetricError) Error() string {
  51. return e.s
  52. }
  53. // FieldError is an error causing a field to be unserializable.
  54. type FieldError struct {
  55. s string
  56. }
  57. func (e FieldError) Error() string {
  58. return e.s
  59. }
  60. var (
  61. // ErrNeedMoreSpace tells us that the Decoder's io.Reader is full.
  62. ErrNeedMoreSpace = &MetricError{"need more space"}
  63. // ErrInvalidName tells us that the chosen name is invalid.
  64. ErrInvalidName = &MetricError{"invalid name"}
  65. // ErrNoFields tells us that there were no serializable fields in the line/metric.
  66. ErrNoFields = &MetricError{"no serializable fields"}
  67. )
  68. type metric struct {
  69. name string
  70. tags []*Tag
  71. fields []*Field
  72. tm time.Time
  73. }
  74. // New creates a new metric via maps.
  75. func New(
  76. name string,
  77. tags map[string]string,
  78. fields map[string]interface{},
  79. tm time.Time,
  80. ) (MutableMetric, error) {
  81. m := &metric{
  82. name: name,
  83. tags: nil,
  84. fields: nil,
  85. tm: tm,
  86. }
  87. if len(tags) > 0 {
  88. m.tags = make([]*Tag, 0, len(tags))
  89. for k, v := range tags {
  90. m.tags = append(m.tags,
  91. &Tag{Key: k, Value: v})
  92. }
  93. sort.Slice(m.tags, func(i, j int) bool { return m.tags[i].Key < m.tags[j].Key })
  94. }
  95. if len(fields) > 0 {
  96. m.fields = make([]*Field, 0, len(fields))
  97. for k, v := range fields {
  98. v := convertField(v)
  99. if v == nil {
  100. continue
  101. }
  102. m.AddField(k, v)
  103. }
  104. }
  105. return m, nil
  106. }
  107. // FromMetric returns a deep copy of the metric with any tracking information
  108. // removed.
  109. func FromMetric(other Metric) Metric {
  110. m := &metric{
  111. name: other.Name(),
  112. tags: make([]*Tag, len(other.TagList())),
  113. fields: make([]*Field, len(other.FieldList())),
  114. tm: other.Time(),
  115. }
  116. for i, tag := range other.TagList() {
  117. m.tags[i] = &Tag{Key: tag.Key, Value: tag.Value}
  118. }
  119. for i, field := range other.FieldList() {
  120. m.fields[i] = &Field{Key: field.Key, Value: field.Value}
  121. }
  122. return m
  123. }
  124. func (m *metric) String() string {
  125. return fmt.Sprintf("%s %v %v %d", m.name, m.Tags(), m.Fields(), m.tm.UnixNano())
  126. }
  127. func (m *metric) Name() string {
  128. return m.name
  129. }
  130. func (m *metric) Tags() map[string]string {
  131. tags := make(map[string]string, len(m.tags))
  132. for _, tag := range m.tags {
  133. tags[tag.Key] = tag.Value
  134. }
  135. return tags
  136. }
  137. func (m *metric) TagList() []*Tag {
  138. return m.tags
  139. }
  140. func (m *metric) Fields() map[string]interface{} {
  141. fields := make(map[string]interface{}, len(m.fields))
  142. for _, field := range m.fields {
  143. fields[field.Key] = field.Value
  144. }
  145. return fields
  146. }
  147. func (m *metric) FieldList() []*Field {
  148. return m.fields
  149. }
  150. func (m *metric) Time() time.Time {
  151. return m.tm
  152. }
  153. func (m *metric) SetName(name string) {
  154. m.name = name
  155. }
  156. func (m *metric) AddPrefix(prefix string) {
  157. m.name = prefix + m.name
  158. }
  159. func (m *metric) AddSuffix(suffix string) {
  160. m.name = m.name + suffix
  161. }
  162. func (m *metric) AddTag(key, value string) {
  163. for i, tag := range m.tags {
  164. if key > tag.Key {
  165. continue
  166. }
  167. if key == tag.Key {
  168. tag.Value = value
  169. return
  170. }
  171. m.tags = append(m.tags, nil)
  172. copy(m.tags[i+1:], m.tags[i:])
  173. m.tags[i] = &Tag{Key: key, Value: value}
  174. return
  175. }
  176. m.tags = append(m.tags, &Tag{Key: key, Value: value})
  177. }
  178. func (m *metric) HasTag(key string) bool {
  179. for _, tag := range m.tags {
  180. if tag.Key == key {
  181. return true
  182. }
  183. }
  184. return false
  185. }
  186. func (m *metric) GetTag(key string) (string, bool) {
  187. for _, tag := range m.tags {
  188. if tag.Key == key {
  189. return tag.Value, true
  190. }
  191. }
  192. return "", false
  193. }
  194. func (m *metric) RemoveTag(key string) {
  195. for i, tag := range m.tags {
  196. if tag.Key == key {
  197. copy(m.tags[i:], m.tags[i+1:])
  198. m.tags[len(m.tags)-1] = nil
  199. m.tags = m.tags[:len(m.tags)-1]
  200. return
  201. }
  202. }
  203. }
  204. func (m *metric) AddField(key string, value interface{}) {
  205. for i, field := range m.fields {
  206. if key == field.Key {
  207. m.fields[i] = &Field{Key: key, Value: convertField(value)}
  208. return
  209. }
  210. }
  211. m.fields = append(m.fields, &Field{Key: key, Value: convertField(value)})
  212. }
  213. func (m *metric) HasField(key string) bool {
  214. for _, field := range m.fields {
  215. if field.Key == key {
  216. return true
  217. }
  218. }
  219. return false
  220. }
  221. func (m *metric) GetField(key string) (interface{}, bool) {
  222. for _, field := range m.fields {
  223. if field.Key == key {
  224. return field.Value, true
  225. }
  226. }
  227. return nil, false
  228. }
  229. func (m *metric) RemoveField(key string) {
  230. for i, field := range m.fields {
  231. if field.Key == key {
  232. copy(m.fields[i:], m.fields[i+1:])
  233. m.fields[len(m.fields)-1] = nil
  234. m.fields = m.fields[:len(m.fields)-1]
  235. return
  236. }
  237. }
  238. }
  239. func (m *metric) SetTime(t time.Time) {
  240. m.tm = t
  241. }
  242. func (m *metric) Copy() Metric {
  243. m2 := &metric{
  244. name: m.name,
  245. tags: make([]*Tag, len(m.tags)),
  246. fields: make([]*Field, len(m.fields)),
  247. tm: m.tm,
  248. }
  249. for i, tag := range m.tags {
  250. m2.tags[i] = &Tag{Key: tag.Key, Value: tag.Value}
  251. }
  252. for i, field := range m.fields {
  253. m2.fields[i] = &Field{Key: field.Key, Value: field.Value}
  254. }
  255. return m2
  256. }
  257. func (m *metric) HashID() uint64 {
  258. h := fnv.New64a()
  259. h.Write([]byte(m.name))
  260. h.Write([]byte("\n"))
  261. for _, tag := range m.tags {
  262. h.Write([]byte(tag.Key))
  263. h.Write([]byte("\n"))
  264. h.Write([]byte(tag.Value))
  265. h.Write([]byte("\n"))
  266. }
  267. return h.Sum64()
  268. }
  269. func (m *metric) Accept() {
  270. }
  271. func (m *metric) Reject() {
  272. }
  273. func (m *metric) Drop() {
  274. }
  275. // Convert field to a supported type or nil if unconvertible
  276. func convertField(v interface{}) interface{} {
  277. switch v := v.(type) {
  278. case float64:
  279. return v
  280. case int64:
  281. return v
  282. case string:
  283. return v
  284. case bool:
  285. return v
  286. case int:
  287. return int64(v)
  288. case uint:
  289. return uint64(v)
  290. case uint64:
  291. return uint64(v)
  292. case []byte:
  293. return string(v)
  294. case int32:
  295. return int64(v)
  296. case int16:
  297. return int64(v)
  298. case int8:
  299. return int64(v)
  300. case uint32:
  301. return uint64(v)
  302. case uint16:
  303. return uint64(v)
  304. case uint8:
  305. return uint64(v)
  306. case float32:
  307. return float64(v)
  308. case *float64:
  309. if v != nil {
  310. return *v
  311. }
  312. case *int64:
  313. if v != nil {
  314. return *v
  315. }
  316. case *string:
  317. if v != nil {
  318. return *v
  319. }
  320. case *bool:
  321. if v != nil {
  322. return *v
  323. }
  324. case *int:
  325. if v != nil {
  326. return int64(*v)
  327. }
  328. case *uint:
  329. if v != nil {
  330. return uint64(*v)
  331. }
  332. case *uint64:
  333. if v != nil {
  334. return uint64(*v)
  335. }
  336. case *[]byte:
  337. if v != nil {
  338. return string(*v)
  339. }
  340. case *int32:
  341. if v != nil {
  342. return int64(*v)
  343. }
  344. case *int16:
  345. if v != nil {
  346. return int64(*v)
  347. }
  348. case *int8:
  349. if v != nil {
  350. return int64(*v)
  351. }
  352. case *uint32:
  353. if v != nil {
  354. return uint64(*v)
  355. }
  356. case *uint16:
  357. if v != nil {
  358. return uint64(*v)
  359. }
  360. case *uint8:
  361. if v != nil {
  362. return uint64(*v)
  363. }
  364. case *float32:
  365. if v != nil {
  366. return float64(*v)
  367. }
  368. default:
  369. return nil
  370. }
  371. return nil
  372. }