@@ -10,13 +10,13 @@ import (
1010
1111 "github.com/grafana/metrictank/idx"
1212 "github.com/grafana/metrictank/mdata"
13- "github.com/grafana/metrictank/msg"
1413 "github.com/grafana/metrictank/stats"
1514 "github.com/raintank/worldping-api/pkg/log"
1615)
1716
1817type Handler interface {
19- Process (pointMsg msg.Point , partition int32 )
18+ ProcessMetricData (md * schema.MetricData , partition int32 )
19+ ProcessMetricPoint (point schema.MetricPointId2 , partition int32 )
2020}
2121
2222// TODO: clever way to document all metrics for all different inputs
@@ -46,55 +46,63 @@ func NewDefaultHandler(metrics mdata.Metrics, metricIndex idx.MetricIndex, input
4646 }
4747}
4848
49- // process makes sure the data is stored and the metadata is in the index
49+ // ProcessMetricPoint updates the index if possible, and stores the data if we have an index entry
5050// concurrency-safe.
51- func (in DefaultHandler ) Process ( pointMsg msg. Point , partition int32 ) {
51+ func (in DefaultHandler ) ProcessMetricPoint ( point schema. MetricPointId2 , partition int32 ) {
5252 in .metricsReceived .Inc ()
53- var mkey schema.MKey
54- var timestamp uint32
55- var value float64
56-
57- if pointMsg .Val == 0 {
58- err := pointMsg .Md .Validate ()
59- if err != nil {
60- in .MetricInvalid .Inc ()
61- log .Debug ("in: Invalid metric %v: %s" , pointMsg .Md , err )
62- return
63- }
64- if pointMsg .Md .Time == 0 {
65- in .MetricInvalid .Inc ()
66- log .Warn ("in: invalid metric. metric.Time is 0. %s" , pointMsg .Md .Id )
67- return
68- }
69-
70- mkey , err = schema .MKeyFromString (pointMsg .Md .Id )
71- if err != nil {
72- log .Debug ("in: Invalid metric %v: %s" , pointMsg .Md , err )
73- }
74-
75- timestamp = uint32 (pointMsg .Md .Time )
76- value = pointMsg .Md .Value
77- } else {
78- if ! pointMsg .Point .Valid () {
79- in .MetricInvalid .Inc ()
80- log .Debug ("in: Invalid metric %v" , pointMsg .Point )
81- return
82- }
83- mkey = schema.MKey {
84- Key : pointMsg .Point .MetricPointId1 .Id ,
85- Org : pointMsg .Point .Org ,
86- }
87- timestamp = pointMsg .Point .MetricPointId1 .Time
88- value = pointMsg .Point .MetricPointId1 .Value
53+ if ! point .Valid () {
54+ in .MetricInvalid .Inc ()
55+ log .Debug ("in: Invalid metric %v" , point )
56+ return
57+ }
58+ mkey := schema.MKey {
59+ Key : point .MetricPointId1 .Id ,
60+ Org : point .Org ,
61+ }
62+
63+ pre := time .Now ()
64+ archive , ok := in .metricIndex .UpdateMaybe (point , partition )
65+ in .pressureIdx .Add (int (time .Since (pre ).Nanoseconds ()))
66+
67+ if ! ok {
68+ return
69+ }
70+
71+ pre = time .Now ()
72+ m := in .metrics .GetOrCreate (mkey , archive .SchemaId , archive .AggId )
73+ m .Add (point .MetricPointId1 .Time , point .MetricPointId1 .Value )
74+ in .pressureTank .Add (int (time .Since (pre ).Nanoseconds ()))
75+
76+ }
77+
78+ // ProcessMetricData assures the data is stored and the metadata is in the index
79+ // concurrency-safe.
80+ func (in DefaultHandler ) ProcessMetricData (md * schema.MetricData , partition int32 ) {
81+ in .metricsReceived .Inc ()
82+ err := md .Validate ()
83+ if err != nil {
84+ in .MetricInvalid .Inc ()
85+ log .Debug ("in: Invalid metric %v: %s" , md , err )
86+ return
87+ }
88+ if md .Time == 0 {
89+ in .MetricInvalid .Inc ()
90+ log .Warn ("in: invalid metric. metric.Time is 0. %s" , md .Id )
91+ return
92+ }
8993
94+ mkey , err := schema .MKeyFromString (md .Id )
95+ if err != nil {
96+ log .Error (3 , "in: Invalid metric %v: could not parse ID: %s" , md , err )
97+ return
9098 }
9199
92100 pre := time .Now ()
93- archive := in .metricIndex .AddOrUpdate (pointMsg , partition )
101+ archive := in .metricIndex .AddOrUpdate (mkey , md , partition )
94102 in .pressureIdx .Add (int (time .Since (pre ).Nanoseconds ()))
95103
96104 pre = time .Now ()
97105 m := in .metrics .GetOrCreate (mkey , archive .SchemaId , archive .AggId )
98- m .Add (timestamp , value )
106+ m .Add (uint32 ( md . Time ), md . Value )
99107 in .pressureTank .Add (int (time .Since (pre ).Nanoseconds ()))
100108}
0 commit comments