@@ -41,19 +41,24 @@ const timeout = 5 * time.Second
4141
4242type fakeEmitter struct {
4343 mock.Mock
44+ reorderJSON bool
4445}
4546
4647func (f * fakeEmitter ) Handle (labels model.LabelSet , timestamp time.Time , record string ) error {
47- // sort alphabetically records just for simplifying testing verification with JSON strings
48- recordMap := map [string ]interface {}{}
49- if err := json .Unmarshal ([]byte (record ), & recordMap ); err != nil {
50- panic ("expected JSON: " + err .Error ())
51- }
52- recordBytes , err := json .Marshal (recordMap )
53- if err != nil {
54- panic ("error unmarshaling: " + err .Error ())
48+ if f .reorderJSON {
49+ // sort alphabetically records just for simplifying testing verification with JSON strings
50+ recordMap := map [string ]interface {}{}
51+ if err := json .Unmarshal ([]byte (record ), & recordMap ); err != nil {
52+ panic ("expected JSON: " + err .Error ())
53+ }
54+ recordBytes , err := json .Marshal (recordMap )
55+ if err != nil {
56+ panic ("error unmarshaling: " + err .Error ())
57+ }
58+ a := f .Mock .Called (labels , timestamp , string (recordBytes ))
59+ return a .Error (0 )
5560 }
56- a := f .Mock .Called (labels , timestamp , string ( recordBytes ) )
61+ a := f .Mock .Called (labels , timestamp , record )
5762 return a .Error (0 )
5863}
5964
@@ -122,32 +127,17 @@ parameters:
122127}
123128
124129func TestLoki_ProcessRecord (t * testing.T ) {
125- var yamlConfig = `
126- log-level: debug
127- pipeline:
128- - name: write1
129- parameters:
130- - name: write1
131- write:
132- type: loki
133- loki:
134- url: http://loki:3100/
135- timestampLabel: ts
136- ignoreList:
137- - ignored
138- staticLabels:
139- static: label
140- labels:
141- - foo
142- - bar
143- `
144- v , cfg := test .InitConfig (t , yamlConfig )
145- require .NotNil (t , v )
146-
147- loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), cfg .Parameters [0 ])
130+ params := api.WriteLoki {
131+ URL : "http://loki:3100/" ,
132+ TimestampLabel : "ts" ,
133+ IgnoreList : []string {"ignored" },
134+ StaticLabels : model.LabelSet {"static" : "label" },
135+ Labels : []string {"foo" , "bar" },
136+ }
137+ loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), config.StageParam {Write : & config.Write {Loki : & params }})
148138 require .NoError (t , err )
149139
150- fe := fakeEmitter {}
140+ fe := fakeEmitter {reorderJSON : true }
151141 fe .On ("Handle" , mock .Anything , mock .Anything , mock .Anything ).Return (nil )
152142 loki .client = & fe
153143
@@ -171,6 +161,49 @@ parameters:
171161 }, time .Unix (124567 , 0 ), `{"other":"val","ts":124567,"value":5678}` )
172162}
173163
164+ func TestLoki_ProcessRecordOrdered (t * testing.T ) {
165+ params := api.WriteLoki {
166+ URL : "http://loki:3100/" ,
167+ TimestampLabel : "ts" ,
168+ Reorder : true ,
169+ }
170+ loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), config.StageParam {Write : & config.Write {Loki : & params }})
171+ require .NoError (t , err )
172+
173+ fe := fakeEmitter {reorderJSON : false }
174+ fe .On ("Handle" , mock .Anything , mock .Anything , mock .Anything ).Return (nil )
175+ loki .client = & fe
176+
177+ require .NoError (t , loki .ProcessRecord (map [string ]interface {}{"ts" : 123456 , "c" : "c" , "e" : "e" , "a" : "a" , "d" : "d" , "b" : "b" }))
178+
179+ fe .AssertCalled (t , "Handle" , model.LabelSet {}, time .Unix (123456 , 0 ), `{"a":"a","b":"b","c":"c","d":"d","e":"e","ts":123456}` )
180+ }
181+
182+ func TestLoki_ProcessRecordGoFormat (t * testing.T ) {
183+ params := api.WriteLoki {
184+ URL : "http://loki:3100/" ,
185+ TimestampLabel : "ts" ,
186+ Labels : []string {"foo" },
187+ Format : "printf" ,
188+ }
189+ loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), config.StageParam {Write : & config.Write {Loki : & params }})
190+ require .NoError (t , err )
191+
192+ fe := fakeEmitter {}
193+ fe .On ("Handle" , mock .Anything , mock .Anything , mock .Anything ).Return (nil )
194+ loki .client = & fe
195+
196+ // WHEN it processes input records
197+ require .NoError (t , loki .ProcessRecord (map [string ]interface {}{
198+ "ts" : 123456 , "foo" : "fooLabel" , "bar" : "barLabel" , "value" : 1234 }))
199+ require .NoError (t , loki .ProcessRecord (map [string ]interface {}{
200+ "ts" : 124567 , "foo" : "fooLabel2" , "bar" : "barLabel2" , "value" : 5678 , "other" : "val" }))
201+
202+ // THEN it forwards the records extracting the timestamp and labels from the configuration
203+ fe .AssertCalled (t , "Handle" , model.LabelSet {"foo" : "fooLabel" }, time .Unix (123456 , 0 ), `map[bar:barLabel ts:123456 value:1234]` )
204+ fe .AssertCalled (t , "Handle" , model.LabelSet {"foo" : "fooLabel2" }, time .Unix (124567 , 0 ), `map[bar:barLabel2 other:val ts:124567 value:5678]` )
205+ }
206+
174207func TestTimestampScale (t * testing.T ) {
175208 // verifies that the unix residual time (below 1-second precision) is properly
176209 // incorporated into the timestamp whichever scale it is
@@ -262,26 +295,12 @@ func TestTimestampExtraction_LocalTime(t *testing.T) {
262295// Tests that labels are sanitized before being sent to loki.
263296// Labels that are invalid even if sanitized are ignored
264297func TestSanitizedLabels (t * testing.T ) {
265- var yamlConfig = `
266- log-level: debug
267- pipeline:
268- - name: write1
269- parameters:
270- - name: write1
271- write:
272- type: loki
273- loki:
274- url: http://loki:3100/
275- labels:
276- - "fo.o"
277- - "ba-r"
278- - "ba/z"
279- - "ignored?"
280- `
281- v , cfg := test .InitConfig (t , yamlConfig )
282- require .NotNil (t , v )
283-
284- loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), cfg .Parameters [0 ])
298+ params := api.WriteLoki {
299+ URL : "http://loki:3100/" ,
300+ TimestampLabel : "ts" ,
301+ Labels : []string {"fo.o" , "ba-r" , "ba/z" , "ignored?" },
302+ }
303+ loki , err := NewWriteLoki (operational .NewMetrics (& config.MetricsSettings {}), config.StageParam {Write : & config.Write {Loki : & params }})
285304 require .NoError (t , err )
286305
287306 fe := fakeEmitter {}
0 commit comments