-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
/loki/api/v1/push
to use the v1 json format (#1145)
Signed-off-by: Joe Elliott <number101010@gmail.com>
- Loading branch information
1 parent
81f9786
commit bb2b925
Showing
10 changed files
with
269 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package unmarshal | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
) | ||
|
||
// DecodePushRequest directly decodes json to a logproto.PushRequest | ||
func DecodePushRequest(b io.Reader, r *logproto.PushRequest) error { | ||
return json.NewDecoder(b).Decode(r) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package unmarshal | ||
|
||
import ( | ||
"io/ioutil" | ||
"log" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// covers requests to /api/prom/push | ||
var pushTests = []struct { | ||
expected []*logproto.Stream | ||
actual string | ||
}{ | ||
{ | ||
[]*logproto.Stream{ | ||
{ | ||
Entries: []logproto.Entry{ | ||
{ | ||
Timestamp: mustParse(time.RFC3339Nano, "2019-09-13T18:32:22.380001319Z"), | ||
Line: "super line", | ||
}, | ||
}, | ||
Labels: `{test="test"}`, | ||
}, | ||
}, | ||
`{ | ||
"streams":[ | ||
{ | ||
"labels":"{test=\"test\"}", | ||
"entries":[ | ||
{ | ||
"ts": "2019-09-13T18:32:22.380001319Z", | ||
"line": "super line" | ||
} | ||
] | ||
} | ||
] | ||
}`, | ||
}, | ||
} | ||
|
||
func Test_DecodePushRequest(t *testing.T) { | ||
|
||
for i, pushTest := range pushTests { | ||
var actual logproto.PushRequest | ||
closer := ioutil.NopCloser(strings.NewReader(pushTest.actual)) | ||
|
||
err := DecodePushRequest(closer, &actual) | ||
require.NoError(t, err) | ||
|
||
require.Equalf(t, pushTest.expected, actual.Streams, "Push Test %d failed", i) | ||
} | ||
} | ||
|
||
func mustParse(l string, t string) time.Time { | ||
ret, err := time.Parse(l, t) | ||
if err != nil { | ||
log.Fatalf("Failed to parse %s", t) | ||
} | ||
|
||
return ret | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package unmarshal | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
|
||
"github.com/grafana/loki/pkg/loghttp" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
) | ||
|
||
// DecodePushRequest directly decodes json to a logproto.PushRequest | ||
func DecodePushRequest(b io.Reader, r *logproto.PushRequest) error { | ||
var request loghttp.PushRequest | ||
|
||
err := json.NewDecoder(b).Decode(&request) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
*r = NewPushRequest(request) | ||
|
||
return nil | ||
} | ||
|
||
// NewPushRequest constructs a logproto.PushRequest from a PushRequest | ||
func NewPushRequest(r loghttp.PushRequest) logproto.PushRequest { | ||
ret := logproto.PushRequest{ | ||
Streams: make([]*logproto.Stream, len(r.Streams)), | ||
} | ||
|
||
for i, s := range r.Streams { | ||
ret.Streams[i] = NewStream(s) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// NewStream constructs a logproto.Stream from a Stream | ||
func NewStream(s *loghttp.Stream) *logproto.Stream { | ||
ret := &logproto.Stream{ | ||
Entries: make([]logproto.Entry, len(s.Entries)), | ||
Labels: s.Labels.String(), | ||
} | ||
|
||
for i, e := range s.Entries { | ||
ret.Entries[i] = NewEntry(e) | ||
} | ||
|
||
return ret | ||
} | ||
|
||
// NewEntry constructs a logproto.Entry from a Entry | ||
func NewEntry(e loghttp.Entry) logproto.Entry { | ||
return logproto.Entry{ | ||
Timestamp: e.Timestamp, | ||
Line: e.Line, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package unmarshal | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/grafana/loki/pkg/logproto" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// covers requests to /loki/api/v1/push | ||
var pushTests = []struct { | ||
expected []*logproto.Stream | ||
actual string | ||
}{ | ||
{ | ||
[]*logproto.Stream{ | ||
{ | ||
Entries: []logproto.Entry{ | ||
{ | ||
Timestamp: time.Unix(0, 123456789012345), | ||
Line: "super line", | ||
}, | ||
}, | ||
Labels: `{test="test"}`, | ||
}, | ||
}, | ||
`{ | ||
"streams": [ | ||
{ | ||
"stream": { | ||
"test": "test" | ||
}, | ||
"values":[ | ||
[ "123456789012345", "super line" ] | ||
] | ||
} | ||
] | ||
}`, | ||
}, | ||
} | ||
|
||
func Test_DecodePushRequest(t *testing.T) { | ||
|
||
for i, pushTest := range pushTests { | ||
var actual logproto.PushRequest | ||
closer := ioutil.NopCloser(strings.NewReader(pushTest.actual)) | ||
|
||
err := DecodePushRequest(closer, &actual) | ||
require.NoError(t, err) | ||
|
||
require.Equalf(t, pushTest.expected, actual.Streams, "Push Test %d failed", i) | ||
} | ||
} |