Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api support for scalars #1704

Merged
merged 2 commits into from
Feb 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pkg/logcli/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func (q *Query) DoQuery(c *client.Client, out output.LogOutput, statistics bool)
case logql.ValueTypeStreams:
streams := resp.Data.Result.(loghttp.Streams)
q.printStream(streams, out)
case promql.ValueTypeScalar:
q.printScalar(resp.Data.Result.(loghttp.Scalar))
case promql.ValueTypeMatrix:
matrix := resp.Data.Result.(loghttp.Matrix)
q.printMatrix(matrix)
Expand Down Expand Up @@ -170,6 +172,16 @@ func (q *Query) printVector(vector loghttp.Vector) {
fmt.Print(string(bytes))
}

func (q *Query) printScalar(scalar loghttp.Scalar) {
bytes, err := json.MarshalIndent(scalar, "", " ")

if err != nil {
log.Fatalf("Error marshalling scalar: %v", err)
}

fmt.Print(string(bytes))
}

type kvLogger struct {
*tabwriter.Writer
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/loghttp/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ func (e *Entry) UnmarshalJSON(data []byte) error {
// Scalar is a single timestamp/float with no labels
type Scalar model.Scalar

func (s Scalar) MarshalJSON() ([]byte, error) {
return model.Scalar(s).MarshalJSON()
}

func (s *Scalar) UnmarshalJSON(b []byte) error {
var v model.Scalar
if err := v.UnmarshalJSON(b); err != nil {
return err
}
*s = Scalar(v)
return nil
}

// Vector is a slice of Samples
type Vector []model.Sample

Expand Down
18 changes: 18 additions & 0 deletions pkg/logql/marshal/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ func NewResultValue(v promql.Value) (loghttp.ResultValue, error) {
if err != nil {
return nil, err
}

case loghttp.ResultTypeScalar:
scalar, ok := v.(promql.Scalar)

if !ok {
return nil, fmt.Errorf("unexpected type %T for scalar", scalar)
}

value = NewScalar(scalar)

case loghttp.ResultTypeVector:
vector, ok := v.(promql.Vector)

Expand Down Expand Up @@ -95,6 +105,14 @@ func NewEntry(e logproto.Entry) loghttp.Entry {
}
}

func NewScalar(s promql.Scalar) loghttp.Scalar {
return loghttp.Scalar{
Timestamp: model.Time(s.T),
Value: model.SampleValue(s.V),
}

}

// NewVector constructs a Vector from a promql.Vector
func NewVector(v promql.Vector) loghttp.Vector {
ret := make([]model.Sample, len(v))
Expand Down