forked from grafana/loki
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated stream json objects to be more parse friendly (grafana#1010)
* Added custom marshaller for Entry Signed-off-by: Joe Elliott <[email protected]> * Added comment and test Signed-off-by: Joe Elliott <[email protected]> * Fixed spelling error * Added a custom marshaller to map entries => values * Added support for labels Signed-off-by: Joe Elliott <[email protected]> * Changed test to only check to the microsecond level Signed-off-by: Joe Elliott <[email protected]> * Extended Entry tests Signed-off-by: Joe Elliott <[email protected]> * Swapped to nanosecs encoded as strings * Cleaned up docs with new loki paths. Removed references to deprecated endpoints * Added support for /loki/api/v1/push Signed-off-by: Joe Elliott <[email protected]> * Removed obsolete comment Signed-off-by: Joe Elliott <[email protected]> * Updated docs for new push endpoint Signed-off-by: Joe Elliott <[email protected]> * Ran gofmt on test file Signed-off-by: Joe Elliott <[email protected]> * Set GOGC=20 on test to avoid out of memory issue in circle ci Signed-off-by: Joe Elliott <[email protected]>
- Loading branch information
1 parent
4763785
commit 6185d54
Showing
5 changed files
with
167 additions
and
81 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -214,7 +214,7 @@ lint: | |
######## | ||
|
||
test: all | ||
go test -p=6 ./... | ||
GOGC=20 go test -p=6 ./... | ||
|
||
######### | ||
# Clean # | ||
|
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,35 @@ | ||
package logproto | ||
|
||
import ( | ||
"encoding/json" | ||
fmt "fmt" | ||
|
||
"github.com/prometheus/prometheus/promql" | ||
) | ||
|
||
// MarshalJSON converts an Entry object to be prom compatible for http queries | ||
func (e *Entry) MarshalJSON() ([]byte, error) { | ||
l, err := json.Marshal(e.Line) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return []byte(fmt.Sprintf("[\"%d\",%s]", e.Timestamp.UnixNano(), l)), nil | ||
} | ||
|
||
// MarshalJSON converts a Stream object to be prom compatible for http queries | ||
func (s *Stream) MarshalJSON() ([]byte, error) { | ||
parsedLabels, err := promql.ParseMetric(s.Labels) | ||
if err != nil { | ||
return nil, err | ||
} | ||
l, err := json.Marshal(parsedLabels) | ||
if err != nil { | ||
return nil, err | ||
} | ||
e, err := json.Marshal(s.Entries) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []byte(fmt.Sprintf("{\"stream\":%s,\"values\":%s}", l, e)), nil | ||
} |
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,85 @@ | ||
package logproto | ||
|
||
import ( | ||
"encoding/json" | ||
fmt "fmt" | ||
reflect "reflect" | ||
"testing" | ||
time "time" | ||
|
||
"github.com/prometheus/prometheus/promql" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var ( | ||
entries = []Entry{ | ||
{ | ||
Timestamp: time.Now(), | ||
Line: "testline", | ||
}, | ||
{ | ||
Timestamp: time.Date(2019, 9, 10, 1, 1, 1, 1, time.UTC), | ||
Line: "{}\"'!@$%&*^(_)(", | ||
}, | ||
} | ||
streams = []Stream{ | ||
{ | ||
Labels: "{}", | ||
Entries: []Entry{}, | ||
}, | ||
{ | ||
Labels: "{name=\"value\",name1=\"value1\"}", | ||
Entries: []Entry{}, | ||
}, | ||
} | ||
) | ||
|
||
func Test_EntryMarshalJSON(t *testing.T) { | ||
var array []interface{} | ||
|
||
for _, entry := range entries { | ||
|
||
bytes, err := entry.MarshalJSON() | ||
require.NoError(t, err) | ||
|
||
err = json.Unmarshal(bytes, &array) | ||
require.NoError(t, err) | ||
|
||
timestamp, ok := array[0].(string) | ||
require.True(t, ok) | ||
|
||
line, ok := array[1].(string) | ||
require.True(t, ok) | ||
|
||
require.Equal(t, fmt.Sprint(entry.Timestamp.UnixNano()), timestamp, "Timestamps not equal ", array[0]) | ||
require.Equal(t, entry.Line, line, "Lines are not equal ", array[1]) | ||
} | ||
} | ||
|
||
func Test_StreamMarshalJSON(t *testing.T) { | ||
actual := struct { | ||
Labels map[string]string `json:"stream"` | ||
Entries []Entry `json:"values"` | ||
}{} | ||
|
||
for _, expected := range streams { | ||
|
||
bytes, err := expected.MarshalJSON() | ||
require.NoError(t, err) | ||
|
||
err = json.Unmarshal(bytes, &actual) | ||
require.NoError(t, err) | ||
|
||
// check labels | ||
expectedLabels, err := promql.ParseMetric(expected.Labels) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, len(actual.Labels), len(expectedLabels)) | ||
for _, l := range expectedLabels { | ||
require.Equal(t, l.Value, actual.Labels[l.Name]) | ||
} | ||
|
||
// check entries | ||
require.True(t, reflect.DeepEqual(actual.Entries, expected.Entries)) | ||
} | ||
} |
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