Skip to content

Commit

Permalink
api support for scalars (#1704)
Browse files Browse the repository at this point in the history
* adds conversions to loghttp scalar types

* logcli prints scalars, lothttp scalars use prometheus json marshaling
  • Loading branch information
owen-d authored Feb 14, 2020
1 parent 414f95f commit db6fd7e
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
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

0 comments on commit db6fd7e

Please sign in to comment.